test-triam-base-contract
Version:
Low level triam smart cotnract support library
89 lines (71 loc) • 3.11 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';
/**
* Create and fund a non existent contract.
* @function
* @alias Operation.createContract
* @param {object} opts
* @param {string} opts.startingBalance - Amount in XLM the account should be funded for. Must be greater
* than the [reserve balance amount](https://www.stellar.org/developers/learn/concepts/fees.html).
* @param {string|Buffer} opts.contractAddr - Address of contract
* @param {object} opts.data - Address of contract
* @param {string} [opts.data.funcName] - function name
* @param {string|Buffer} [opts.data.contractParams] - params of contract functions
* @param {string} opts.fileHash
*
* @param {string} [opts.source] - The source account for the create contract. Defaults to the transaction's source account.
* @returns {xdr.CreateContractOp}
*/
export const createContract = function(opts) {
if (!this.isValidAmount(opts.startingBalance)) {
throw new TypeError(
this.constructAmountRequirementsError('startingBalance'));
}
let attributes = {};
attributes.contractId = "";
attributes.startingBalance = this._toXDRAmount(opts.startingBalance);
if (!isUndefined(opts.contractAddr) && !isString(opts.contractAddr)) {
throw new TypeError('contractAddr argument must be of type String');
}
//attributes.contractAddr = opts.contractAddr;
if (!isString(opts.contractAddr) && !Buffer.isBuffer(opts.contractAddr) &&
opts.contractAddr !== null) {
throw new Error("contractAddr must be a string, Buffer or null");
}
if (isString(opts.contractAddr)) {
opts.contractAddr = Buffer.from(opts.contractAddr);
}
attributes.contractAddr = opts.contractAddr;
// thuannd notes start
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({
funcName: opts.data.funcName,
contractParams: opts.data.contractParams
});
attributes.state = "";
if (!(isString(opts.fileHash) && opts.fileHash.length <= 64)) {
throw new Error("fileHash must be a string, up to 64 characters");
}
attributes.fileHash = opts.fileHash;
attributes.errFlag = 0;
// thuannd notes end
let createContract = new xdr.CreateContractOp(attributes);
let opAttributes = {};
opAttributes.body = xdr.OperationBody.createContract(createContract);
this.setSourceAccount(opAttributes, opts);
return new xdr.Operation(opAttributes);
};