test-triam-base-contract
Version:
Low level triam smart cotnract support library
107 lines (77 loc) • 3.8 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.createContract = undefined;
var _stellarXdr_generated = require("../generated/stellar-xdr_generated");
var _stellarXdr_generated2 = _interopRequireDefault(_stellarXdr_generated);
var _keypair = require("../keypair");
var _strkey = require("../strkey");
var _clone = require("lodash/clone");
var _clone2 = _interopRequireDefault(_clone);
var _isUndefined = require("lodash/isUndefined");
var _isUndefined2 = _interopRequireDefault(_isUndefined);
var _isString = require("lodash/isString");
var _isString2 = _interopRequireDefault(_isString);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
/**
* 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}
*/
var createContract = exports.createContract = function createContract(opts) {
if (!this.isValidAmount(opts.startingBalance)) {
throw new TypeError(this.constructAmountRequirementsError('startingBalance'));
}
var attributes = {};
attributes.contractId = "";
attributes.startingBalance = this._toXDRAmount(opts.startingBalance);
if (!(0, _isUndefined2.default)(opts.contractAddr) && !(0, _isString2.default)(opts.contractAddr)) {
throw new TypeError('contractAddr argument must be of type String');
}
//attributes.contractAddr = opts.contractAddr;
if (!(0, _isString2.default)(opts.contractAddr) && !Buffer.isBuffer(opts.contractAddr) && opts.contractAddr !== null) {
throw new Error("contractAddr must be a string, Buffer or null");
}
if ((0, _isString2.default)(opts.contractAddr)) {
opts.contractAddr = Buffer.from(opts.contractAddr);
}
attributes.contractAddr = opts.contractAddr;
// thuannd notes start
if (!(0, _isUndefined2.default)(opts.data.funcName) && !(0, _isString2.default)(opts.data.funcName)) {
throw new TypeError('funcName argument must be of type String');
}
if (!(0, _isString2.default)(opts.data.contractParams) && !Buffer.isBuffer(opts.data.contractParams) && opts.data.contractParams !== null) {
throw new Error("contractParams must be a string, Buffer or null");
}
if ((0, _isString2.default)(opts.data.contractParams)) {
opts.data.contractParams = Buffer.from(opts.data.contractParams);
}
attributes.data = new _stellarXdr_generated2.default.ContractInput({
funcName: opts.data.funcName,
contractParams: opts.data.contractParams
});
attributes.state = "";
if (!((0, _isString2.default)(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
var createContract = new _stellarXdr_generated2.default.CreateContractOp(attributes);
var opAttributes = {};
opAttributes.body = _stellarXdr_generated2.default.OperationBody.createContract(createContract);
this.setSourceAccount(opAttributes, opts);
return new _stellarXdr_generated2.default.Operation(opAttributes);
};