-
Notifications
You must be signed in to change notification settings - Fork 274
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(sdk-coin-xrp): add trustsetBuilder
TICKET: WIN-3611
- Loading branch information
1 parent
8d4a245
commit 5032929
Showing
6 changed files
with
97 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters