test-triam-base-contract
Version:
Low level triam smart cotnract support library
46 lines (38 loc) • 1.47 kB
JavaScript
import {default as xdr} from "../generated/stellar-xdr_generated";
import {Keypair} from "../keypair";
import {StrKey} from "../strkey";
import isString from 'lodash/isString';
/**
* Create a ChangeAsset operation.
* @function
* @alias Operation.changeAsset
* @param {object} opts
* @param {Asset} opts.asset - The asset to create.
* @param {string} opts.beneficiary - The beneficiary account ID.
* The source account for the payment. Defaults to the transaction's source account.
* @param {string} [opts.source]
* @returns {xdr.ChangeAssetOp}
*/
export const changeAsset = function(opts) {
if (!opts.asset) {
throw new Error("Must provide an asset for a changeAsset operation");
}
let attributes = {};
attributes.asset = opts.asset.toXDRObject();
if (isString(opts.beneficiary)) {
if ( opts.beneficiary.length != 0 && opts.beneficiary.length != 56) {
throw new Error(" invalid beneficiary's address's length");
}
if ( opts.beneficiary.length == 56 && !StrKey.isValidEd25519PublicKey(opts.beneficiary)) {
throw new Error("invalid beneficiary's address's format");
}
} else {
throw new Error("beneficiary is invalid");
}
attributes.beneficiary = opts.beneficiary;
let changeAsset = new xdr.ChangeAssetOp(attributes);
let opAttributes = {};
opAttributes.body = xdr.OperationBody.changeAsset(changeAsset);
this.setSourceAccount(opAttributes, opts);
return new xdr.Operation(opAttributes);
};