-
Notifications
You must be signed in to change notification settings - Fork 137
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add helpers to create CAP23 operations. (#368)
* Add Operation.createClaimableBalance helper. * Add claimClaimableBalance helper. * Update Changelog. * Add TS definitions. * Implement balanceID to match current Horizon implementation. * Document source attribute. * Fix return value on cap23 operation helpers. * Update code based on review.
- Loading branch information
Showing
8 changed files
with
318 additions
and
7 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,33 @@ | ||
import xdr from '../generated/stellar-xdr_generated'; | ||
|
||
/** | ||
* Create a new claim claimable balance operation. | ||
* @function | ||
* @alias Operation.claimClaimableBalance | ||
* @param {object} opts Options object | ||
* @param {string} opts.balanceId - The claimable balance id to be claimed. | ||
* @param {string} [opts.source] - The source account for the operation. Defaults to the transaction's source account. | ||
* @returns {xdr.Operation} Claim claimable balance operation | ||
* | ||
* @example | ||
* const op = Operation.claimClaimableBalance({ | ||
* balanceId: '00000000da0d57da7d4850e7fc10d2a9d0ebc731f7afb40574c03395b17d49149b91f5be', | ||
* }); | ||
* | ||
*/ | ||
export function claimClaimableBalance(opts = {}) { | ||
if (typeof opts.balanceId !== 'string') { | ||
throw new Error('must provide a valid claimable balance Id'); | ||
} | ||
const attributes = {}; | ||
attributes.balanceId = xdr.ClaimableBalanceId.fromXDR(opts.balanceId, 'hex'); | ||
const claimClaimableBalanceOp = new xdr.ClaimClaimableBalanceOp(attributes); | ||
|
||
const opAttributes = {}; | ||
opAttributes.body = xdr.OperationBody.claimClaimableBalance( | ||
claimClaimableBalanceOp | ||
); | ||
this.setSourceAccount(opAttributes, opts); | ||
|
||
return new xdr.Operation(opAttributes); | ||
} |
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,63 @@ | ||
import xdr from '../generated/stellar-xdr_generated'; | ||
import { Asset } from '../asset'; | ||
|
||
/** | ||
* Create a new claimable balance operation. | ||
* @function | ||
* @alias Operation.createClaimableBalance | ||
* @param {object} opts Options object | ||
* @param {Asset} opts.asset - The asset for the claimable balance. | ||
* @param {string} opts.amount - Amount. | ||
* @param {Claimant[]} opts.claimants - An array of Claimants | ||
* @param {string} [opts.source] - The source account for the operation. Defaults to the transaction's source account. | ||
* @returns {xdr.Operation} Create claimable balance operation | ||
* | ||
* @example | ||
* const asset = new Asset( | ||
* 'USD', | ||
* 'GDGU5OAPHNPU5UCLE5RDJHG7PXZFQYWKCFOEXSXNMR6KRQRI5T6XXCD7' | ||
* ); | ||
* const amount = '100.0000000'; | ||
* const claimants = [ | ||
* new Claimant( | ||
* 'GCEZWKCA5VLDNRLN3RPRJMRZOX3Z6G5CHCGSNFHEYVXM3XOJMDS674JZ', | ||
* Claimant.predicateBeforeAbsoluteTime("4102444800000") | ||
* ) | ||
* ]; | ||
* | ||
* const op = Operation.createClaimableBalance({ | ||
* asset, | ||
* amount, | ||
* claimants | ||
* }); | ||
* | ||
*/ | ||
export function createClaimableBalance(opts) { | ||
if (!(opts.asset instanceof Asset)) { | ||
throw new Error( | ||
'must provide an asset for create claimable balance operation' | ||
); | ||
} | ||
|
||
if (!this.isValidAmount(opts.amount)) { | ||
throw new TypeError(this.constructAmountRequirementsError('amount')); | ||
} | ||
|
||
if (!Array.isArray(opts.claimants) || opts.claimants.length === 0) { | ||
throw new Error('must provide at least one claimant'); | ||
} | ||
|
||
const attributes = {}; | ||
attributes.asset = opts.asset.toXDRObject(); | ||
attributes.amount = this._toXDRAmount(opts.amount); | ||
attributes.claimants = opts.claimants.map((c) => c.toXDRObject()); | ||
const createClaimableBalanceOp = new xdr.CreateClaimableBalanceOp(attributes); | ||
|
||
const opAttributes = {}; | ||
opAttributes.body = xdr.OperationBody.createClaimableBalance( | ||
createClaimableBalanceOp | ||
); | ||
this.setSourceAccount(opAttributes, opts); | ||
|
||
return new xdr.Operation(opAttributes); | ||
} |
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
Oops, something went wrong.