test-triam-base-contract
Version:
Low level triam smart cotnract support library
57 lines (47 loc) • 1.81 kB
JavaScript
import {default as xdr} from "../generated/stellar-xdr_generated";
import {Keypair} from "../keypair";
import {StrKey} from "../strkey";
import clone from "lodash/clone";
import isUndefined from 'lodash/isUndefined';
import isString from 'lodash/isString';
/**
* Call a contract operation.
* @function
* @alias Operation.callContract
* @param {object} opts
* @param {string} opts.contractId - Contract account ID.
* @param {object} opts.data - Address of contract
* @param {string} [opts.data.funcName] - function name
* @param {string|Buffer} [opts.data.contractParams] - params of contract functions
*
* @returns {xdr.CallContractOp}
*/
export const callContract = function(opts) {
if (!StrKey.isValidContractKey(opts.contractId)) {
throw new Error("contract is invalid");
}
let attributes = {};
attributes.contractId
= Keypair.fromContractKey(opts.contractId).xdrContractId();
//-----------------------
if (!isUndefined(opts.data.funcName) && !isString(opts.data.funcName)) {
throw new TypeError('funcName argument must be of type String');
}
if (!isString(opts.data.contractParams) &&
!Buffer.isBuffer(opts.data.contractParams) &&
opts.data.contractParams !== null) {
throw new Error("contractParams must be a string, Buffer or null");
}
if (isString(opts.data.contractParams)) {
opts.data.contractParams = Buffer.from(opts.data.contractParams);
}
attributes.data = new xdr.ContractInput(opts.data);
attributes.contrOps = [];
attributes.errFlag = 0;
//-----------------------
let callContract = new xdr.CallContractOp(attributes);
let opAttributes = {};
opAttributes.body = xdr.OperationBody.callContract(callContract);
this.setSourceAccount(opAttributes, opts);
return new xdr.Operation(opAttributes);
};