Skip to content

Commit

Permalink
feat(sdk-coin-xrp): add trustsetBuilder
Browse files Browse the repository at this point in the history
TICKET: WIN-3611
  • Loading branch information
nvrakesh06 committed Oct 17, 2024
1 parent 8d4a245 commit 5032929
Show file tree
Hide file tree
Showing 6 changed files with 97 additions and 2 deletions.
4 changes: 2 additions & 2 deletions modules/sdk-coin-xrp/src/lib/iface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {
VerifyAddressOptions as BaseVerifyAddressOptions,
TransactionPrebuild,
} from '@bitgo/sdk-core';
import { AccountSet, Payment, Signer, SignerEntry, SignerListSet, Amount } from 'xrpl';
import { AccountSet, Payment, Signer, SignerEntry, SignerListSet, Amount, TrustSet } from 'xrpl';

export enum XrpTransactionType {
AccountSet = 'AccountSet',
Expand All @@ -15,7 +15,7 @@ export enum XrpTransactionType {
TokenPayment = 'TokenPayment',
}

export type XrpTransaction = Payment | AccountSet | SignerListSet;
export type XrpTransaction = Payment | AccountSet | SignerListSet | TrustSet;

export interface Address {
address: string;
Expand Down
1 change: 1 addition & 0 deletions modules/sdk-coin-xrp/src/lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,6 @@ export { TransactionBuilder } from './transactionBuilder';
export { TransactionBuilderFactory } from './transactionBuilderFactory';
export { TransferBuilder } from './transferBuilder';
export { TokenTransferBuilder } from './tokenTransferBuilder';
export { TrustSetBuilder } from './trustsetBuilder';
export { WalletInitializationBuilder } from './walletInitializationBuilder';
export { Utils };
8 changes: 8 additions & 0 deletions modules/sdk-coin-xrp/src/lib/transaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,9 @@ export class Transaction extends BaseTransaction {
txData.signerEntries = this._xrpTransaction.SignerEntries;
return txData;

case XrpTransactionType.TrustSet:
return txData;

default:
throw new InvalidTransactionError('Invalid transaction type');
}
Expand Down Expand Up @@ -299,6 +302,8 @@ export class Transaction extends BaseTransaction {
return XrpTransactionType.AccountSet;
case XrpTransactionType.SignerListSet:
return XrpTransactionType.SignerListSet;
case XrpTransactionType.TrustSet:
return XrpTransactionType.TrustSet;
default:
throw new InvalidTransactionError(`Invalid TransactionType: ${xrpTransaction.TransactionType}`);
}
Expand Down Expand Up @@ -351,6 +356,9 @@ export class Transaction extends BaseTransaction {
case XrpTransactionType.TokenPayment:
this.setTransactionType(TransactionType.SendToken);
break;
case XrpTransactionType.TrustSet:
this.setTransactionType(TransactionType.TrustLine);
break;
}
this.loadInputsAndOutputs();
}
Expand Down
8 changes: 8 additions & 0 deletions modules/sdk-coin-xrp/src/lib/transactionBuilderFactory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { TransferBuilder } from './transferBuilder';
import utils from './utils';
import { WalletInitializationBuilder } from './walletInitializationBuilder';
import { TokenTransferBuilder } from './tokenTransferBuilder';
import { TrustSetBuilder } from './trustsetBuilder';

export class TransactionBuilderFactory extends BaseTransactionBuilderFactory {
constructor(_coinConfig: Readonly<CoinConfig>) {
Expand Down Expand Up @@ -35,6 +36,8 @@ export class TransactionBuilderFactory extends BaseTransactionBuilderFactory {
return this.getWalletInitializationBuilder(tx);
case TransactionType.SendToken:
return this.getTokenTransferBuilder(tx);
case TransactionType.TrustLine:
return this.getTrustSetBuilder(tx);
default:
throw new InvalidTransactionError('Invalid transaction');
}
Expand Down Expand Up @@ -63,6 +66,11 @@ export class TransactionBuilderFactory extends BaseTransactionBuilderFactory {
return this.initializeBuilder(tx, new TokenTransferBuilder(this._coinConfig));
}

/** @inheritdoc */
public getTrustSetBuilder(tx?: Transaction): TrustSetBuilder {
return this.initializeBuilder(tx, new TrustSetBuilder(this._coinConfig));
}

/**
* Initialize the builder with the given transaction
*
Expand Down
77 changes: 77 additions & 0 deletions modules/sdk-coin-xrp/src/lib/trustsetBuilder.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
import { TransactionBuilder } from './transactionBuilder';
import { BaseCoin as CoinConfig } from '@bitgo/statics';
import { BuildTransactionError, TransactionType } from '@bitgo/sdk-core';
import { XrpTransactionType } from './iface';
import { Amount, IssuedCurrencyAmount, TrustSet } from 'xrpl';
import { Transaction } from './transaction';
import _ from 'lodash';

export class TrustSetBuilder extends TransactionBuilder {
private _amount: IssuedCurrencyAmount;

constructor(_coinConfig: Readonly<CoinConfig>) {
super(_coinConfig);
}

protected get transactionType(): TransactionType {
return TransactionType.TrustLine;
}

protected get xrpTransactionType(): XrpTransactionType.TrustSet {
return XrpTransactionType.TrustSet;
}

initBuilder(tx: Transaction): void {
super.initBuilder(tx);

const { destination, amount } = tx.toJson();
if (!destination) {
throw new BuildTransactionError('Missing destination');
}
if (!amount) {
throw new BuildTransactionError('Missing amount');
}

this.amount(amount);
}

/**
* Set the amount to send
* @param {string} amount - the amount sent
* @returns {TransactionBuilder} This transaction builder
*/
amount(amount: Amount): TransactionBuilder {
function isIssuedCurrencyAmount(amount: Amount): amount is IssuedCurrencyAmount {
return (
!_.isString(amount) &&
_.isObjectLike(amount) &&
_.isString(amount.currency) &&
_.isString(amount.issuer) &&
_.isString(amount.value)
);
}

if (!isIssuedCurrencyAmount(amount)) {
throw new Error(`amount type ${typeof amount} must be a IssuedCurrencyAmount type`);
}
this._amount = amount;
return this;
}

/** @inheritdoc */
protected async buildImplementation(): Promise<Transaction> {
if (!this._sender) {
throw new BuildTransactionError('Sender must be set before building the transaction');
}

const transferFields: TrustSet = {
TransactionType: this.xrpTransactionType,
Account: this._sender,
LimitAmount: this._amount,
};

this._specificFields = transferFields;

return await super.buildImplementation();
}
}
1 change: 1 addition & 0 deletions modules/sdk-core/src/account-lib/baseCoin/enum.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ export enum TransactionType {
CloseAssociatedTokenAccount,
SingleNominatorWithdraw,
SendToken,
TrustLine,
}

/**
Expand Down

0 comments on commit 5032929

Please sign in to comment.