test-triam-base-contract
Version:
Low level triam smart cotnract support library
37 lines (33 loc) • 1.24 kB
JavaScript
import {default as xdr} from "../generated/stellar-xdr_generated";
import {Keypair} from "../keypair";
import {StrKey} from "../strkey";
/**
* Create a payment operation.
* @function
* @alias ContrOperation.transfer
* @param {object} opts
* @param {string} opts.destination - The destination account ID.
* @param {Asset} opts.asset - The asset to send.
* @param {string} opts.amount - The amount to send.
* @returns {xdr.Transfer}
*/
export const transfer = function(opts) {
if (!StrKey.isValidEd25519PublicKey(opts.destination)) {
throw new Error("destination is invalid");
}
if (!opts.asset) {
throw new Error("Must provide an asset for a payment operation");
}
if (!this.isValidAmount(opts.amount)) {
throw new TypeError(this.constructAmountRequirementsError('amount'));
}
let attributes = {};
attributes.destination
= Keypair.fromPublicKey(opts.destination).xdrAccountId();
attributes.asset = opts.asset.toXDRObject();
attributes.amount = this._toXDRAmount(opts.amount);
let transfer = new xdr.Transfer(attributes);
let opAttributes = {};
opAttributes.body = xdr.ContrOperationBody.transfer(transfer);
return new xdr.ContrOperation(opAttributes);
};