-
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.
Merge pull request #5022 from BitGo/WIN-3610-transfer-builder
feat(sdk-coin-xrp): add token transaction builders
- Loading branch information
Showing
12 changed files
with
353 additions
and
16 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,14 @@ | ||
import Utils from './utils'; | ||
|
||
export { AccountSetBuilder } from './accountSetBuilder'; | ||
export * from './constants'; | ||
export * from './iface'; | ||
export { AccountSetBuilder } from './accountSetBuilder'; | ||
export { KeyPair } from './keyPair'; | ||
export { TokenTransferBuilder } from './tokenTransferBuilder'; | ||
export { Transaction } from './transaction'; | ||
export { TransactionBuilder } from './transactionBuilder'; | ||
export { TransactionBuilderFactory } from './transactionBuilderFactory'; | ||
export { TransferBuilder } from './transferBuilder'; | ||
export { TrustSetBuilder } from './trustsetBuilder'; | ||
export { WalletInitializationBuilder } from './walletInitializationBuilder'; | ||
export { Utils }; |
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,103 @@ | ||
import { BuildTransactionError, TransactionType } from '@bitgo/sdk-core'; | ||
import { BaseCoin as CoinConfig } from '@bitgo/statics'; | ||
import BigNumber from 'bignumber.js'; | ||
import { Amount, Payment } from 'xrpl'; | ||
import { XrpTransactionType } from './iface'; | ||
import { Transaction } from './transaction'; | ||
import { TransactionBuilder } from './transactionBuilder'; | ||
import utils from './utils'; | ||
|
||
export class TokenTransferBuilder extends TransactionBuilder { | ||
private _amount: Amount; | ||
private _destination: string; | ||
private _destinationTag?: number; | ||
|
||
constructor(_coinConfig: Readonly<CoinConfig>) { | ||
super(_coinConfig); | ||
} | ||
|
||
protected get transactionType(): TransactionType { | ||
return TransactionType.Send; | ||
} | ||
|
||
protected get xrpTransactionType(): XrpTransactionType.Payment { | ||
return XrpTransactionType.Payment; | ||
} | ||
|
||
initBuilder(tx: Transaction): void { | ||
super.initBuilder(tx); | ||
|
||
const { destination, amount, destinationTag } = tx.toJson(); | ||
if (!destination) { | ||
throw new BuildTransactionError('Missing destination'); | ||
} | ||
if (!amount) { | ||
throw new BuildTransactionError('Missing amount'); | ||
} | ||
|
||
const normalizeAddress = utils.normalizeAddress({ address: destination, destinationTag }); | ||
this.to(normalizeAddress); | ||
if (!utils.isIssuedCurrencyAmount(amount)) { | ||
throw new BuildTransactionError('Invalid Amount'); | ||
} | ||
const amountBigNum = BigNumber(amount.value).shiftedBy(this._coinConfig.decimalPlaces); | ||
this.amount(amountBigNum.toFixed()); | ||
} | ||
|
||
/** | ||
* Set the receiver address | ||
* @param {string} address - the address with optional destination tag | ||
* @returns {TransactionBuilder} This transaction builder | ||
*/ | ||
to(address: string): TransactionBuilder { | ||
const { address: xrpAddress, destinationTag } = utils.getAddressDetails(address); | ||
this._destination = xrpAddress; | ||
this._destinationTag = destinationTag; | ||
return this; | ||
} | ||
|
||
/** | ||
* Set the amount to send | ||
* @param {string} amount - the amount sent | ||
* @returns {TransactionBuilder} This transaction builder | ||
*/ | ||
amount(amount: string): TransactionBuilder { | ||
if (typeof amount !== 'string') { | ||
throw new Error(`amount type ${typeof amount} must be a string`); | ||
} | ||
const amountBigNum = BigNumber(amount); | ||
if (amountBigNum.lt(0)) { | ||
throw new Error(`amount ${amount} is not valid`); | ||
} | ||
const currency = utils.getXrpCurrencyFromTokenName(this._coinConfig.name); | ||
// Unlike most coins, XRP Token amounts are represented in decimal notation | ||
const value = amountBigNum.shiftedBy(-1 * this._coinConfig.decimalPlaces).toFixed(); | ||
this._amount = { | ||
value: value, | ||
...currency, | ||
}; | ||
return this; | ||
} | ||
|
||
/** @inheritdoc */ | ||
protected async buildImplementation(): Promise<Transaction> { | ||
if (!this._sender) { | ||
throw new BuildTransactionError('Sender must be set before building the transaction'); | ||
} | ||
|
||
const transferFields: Payment = { | ||
TransactionType: this.xrpTransactionType, | ||
Account: this._sender, | ||
Destination: this._destination, | ||
Amount: this._amount, | ||
}; | ||
|
||
if (typeof this._destinationTag === 'number') { | ||
transferFields.DestinationTag = this._destinationTag; | ||
} | ||
|
||
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
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,79 @@ | ||
import { BuildTransactionError, TransactionType } from '@bitgo/sdk-core'; | ||
import { BaseCoin as CoinConfig } from '@bitgo/statics'; | ||
import BigNumber from 'bignumber.js'; | ||
import { IssuedCurrencyAmount, TrustSet } from 'xrpl'; | ||
import { XrpTransactionType } from './iface'; | ||
import { Transaction } from './transaction'; | ||
import { TransactionBuilder } from './transactionBuilder'; | ||
import utils from './utils'; | ||
|
||
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 { amount } = tx.toJson(); | ||
if (!amount) { | ||
throw new BuildTransactionError('Missing amount'); | ||
} | ||
if (!utils.isIssuedCurrencyAmount(amount)) { | ||
throw new BuildTransactionError('Invalid Limit Amount'); | ||
} | ||
// The amount is represented in decimal notation, so we need to multiply it by the decimal places | ||
const amountBigNum = BigNumber(amount.value).shiftedBy(this._coinConfig.decimalPlaces); | ||
this.amount(amountBigNum.toFixed()); | ||
} | ||
|
||
/** | ||
* Set the amount to send | ||
* @param {string} amount - the amount sent | ||
* @returns {TransactionBuilder} This transaction builder | ||
*/ | ||
amount(amount: string): TransactionBuilder { | ||
if (typeof amount !== 'string') { | ||
throw new Error(`amount type ${typeof amount} must be a string`); | ||
} | ||
const amountBigNum = BigNumber(amount); | ||
if (amountBigNum.lt(0)) { | ||
throw new Error(`amount ${amount} is not valid`); | ||
} | ||
const currency = utils.getXrpCurrencyFromTokenName(this._coinConfig.name); | ||
// Unlike most coins, XRP Token amounts are represented in decimal notation | ||
const value = amountBigNum.shiftedBy(-1 * this._coinConfig.decimalPlaces).toFixed(); | ||
this._amount = { | ||
value: value, | ||
...currency, | ||
}; | ||
return this; | ||
} | ||
|
||
/** @inheritdoc */ | ||
protected async buildImplementation(): Promise<Transaction> { | ||
if (!this._sender) { | ||
throw new BuildTransactionError('Sender must be set before building the transaction'); | ||
} | ||
|
||
const trustSetFields: TrustSet = { | ||
TransactionType: this.xrpTransactionType, | ||
Account: this._sender, | ||
LimitAmount: this._amount, | ||
}; | ||
|
||
this._specificFields = trustSetFields; | ||
|
||
return await super.buildImplementation(); | ||
} | ||
} |
Oops, something went wrong.