test-triam-base-contract
Version:
Low level triam smart cotnract support library
62 lines (53 loc) • 2.26 kB
JavaScript
import {default as xdr} from "../generated/stellar-xdr_generated";
import {Keypair} from "../keypair";
import isInterger from 'lodash/isInteger';
import isUndefined from 'lodash/isUndefined';
import {Hyper} from "js-xdr";
import {StrKey} from "../strkey";
import isString from 'lodash/isString';
/**
* Create a CreateAsset operation.
* @function
* @alias Operation.createAsset
* @param {object} opts
* @param {Asset} opts.asset - The asset to create.
* @param {string} opts.beneficiary - The beneficiary account ID.
* @param {(number|string)} opts.fee - The amount to send.
* @param {(number|string)} opts.ratio - The amount to send.
* @param {string} opts.minfee - The amount to send.
* The source account for the payment. Defaults to the transaction's source account.
* @param {string} [opts.source]
* @returns {xdr.CreateAssetOp}
*/
export const createAsset = function(opts) {
if (!opts.asset) {
throw new Error("Must provide an asset for a createAsset operation");
}
if (!isUndefined(opts.minfee) && !this.isValidAmount(opts.minfee, true)) {
throw new TypeError(this.constructAmountRequirementsError('min fee'));
}
let attributes = {};
attributes.asset = opts.asset.toXDRObject();
attributes.fee = this._checkUnsignedIntValue("fee", opts.fee);
attributes.ratio = this._checkUnsignedIntValue("ratio", opts.ratio);
attributes.minfee = this._toXDRAmount(opts.minfee);
//attributes.beneficiary = Keypair.fromPublicKey(opts.beneficiary).xdrAccountId();
if (attributes.fee == 0 && attributes.minfee == 0) {
if (!(isString(opts.beneficiary) && opts.beneficiary.length == 0)) {
throw new Error("beneficiary must be empty when fee and minfee are zero");
}
} else {
if (!(isString(opts.beneficiary) && opts.beneficiary.length == 56)) {
throw new Error("beneficiary is invalid");
}
if (!StrKey.isValidEd25519PublicKey(opts.beneficiary)) {
throw new Error("beneficiary is invalid");
}
}
attributes.beneficiary = opts.beneficiary;
let createAsset = new xdr.CreateAssetOp(attributes);
let opAttributes = {};
opAttributes.body = xdr.OperationBody.createAsset(createAsset);
this.setSourceAccount(opAttributes, opts);
return new xdr.Operation(opAttributes);
};