-
Notifications
You must be signed in to change notification settings - Fork 272
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
fc79381
commit b7cabb3
Showing
5 changed files
with
85 additions
and
3 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
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
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 |
---|---|---|
|
@@ -120,5 +120,6 @@ | |
}, | ||
"dependencies": { | ||
"terser": "^5.14.2" | ||
} | ||
}, | ||
"packageManager": "[email protected]+sha512.a6b2f7906b721bba3d67d4aff083df04dad64c399707841b7acf00f6b133b7ac24255f2652fa22ae3534329dc6180534e98d17432037ff6fd140556e2bb3137e" | ||
} |