UNPKG

test-triam-base-contract

Version:

Low level triam smart cotnract support library

55 lines (44 loc) 1.64 kB
import {default as xdr} from "../generated/stellar-xdr_generated"; import {Keypair} from "../keypair"; import {StrKey} from "../strkey"; /** * Create a sendAsset operation. * @function * @alias Operation.sendAsset * @param {object} opts * @param {string} opts.contractId - The destination contract ID. * @param {Asset} opts.asset - The asset to send. * @param {string} opts.amount - The amount to send. * @param {string} [opts.source] - The source account for the send asset. Defaults to the transaction's source account. * @returns {xdr.SendAssetOp} */ export const sendAsset = function(opts) { // thuannd start: thuannd notes if (!StrKey.isValidContractKey(opts.contractId)) { throw new Error("contract is invalid"); } // thuannd end if (!opts.asset) { throw new Error("Must provide an asset for a sendAsset operation"); } if (!this.isValidAmount(opts.amount)) { throw new TypeError(this.constructAmountRequirementsError('amount')); } let attributes = {}; attributes.contractId = Keypair.fromContractKey(opts.contractId).xdrContractId(); // now, just support native asset // if (!opts.asset.isNative()) // { // throw new Error("Just support the native asset now!"); // } attributes.asset = opts.asset.toXDRObject(); attributes.amount = this._toXDRAmount(opts.amount); attributes.state = ""; attributes.errFlag = 0; let sendAsset = new xdr.SendAssetOp(attributes); let opAttributes = {}; opAttributes.body = xdr.OperationBody.sendAsset(sendAsset); this.setSourceAccount(opAttributes, opts); return new xdr.Operation(opAttributes); };