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 fc79381 commit b7cabb3
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 3 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
3 changes: 3 additions & 0 deletions modules/sdk-coin-xrp/src/lib/transaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,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
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
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -120,5 +120,6 @@
},
"dependencies": {
"terser": "^5.14.2"
}
},
"packageManager": "[email protected]+sha512.a6b2f7906b721bba3d67d4aff083df04dad64c399707841b7acf00f6b133b7ac24255f2652fa22ae3534329dc6180534e98d17432037ff6fd140556e2bb3137e"
}

0 comments on commit b7cabb3

Please sign in to comment.