test-triam-base-contract
Version:
Low level triam smart cotnract support library
82 lines (63 loc) • 2.9 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.createAsset = undefined;
var _stellarXdr_generated = require("../generated/stellar-xdr_generated");
var _stellarXdr_generated2 = _interopRequireDefault(_stellarXdr_generated);
var _keypair = require("../keypair");
var _isInteger = require("lodash/isInteger");
var _isInteger2 = _interopRequireDefault(_isInteger);
var _isUndefined = require("lodash/isUndefined");
var _isUndefined2 = _interopRequireDefault(_isUndefined);
var _jsXdr = require("js-xdr");
var _strkey = require("../strkey");
var _isString = require("lodash/isString");
var _isString2 = _interopRequireDefault(_isString);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
/**
* 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}
*/
var createAsset = exports.createAsset = function createAsset(opts) {
if (!opts.asset) {
throw new Error("Must provide an asset for a createAsset operation");
}
if (!(0, _isUndefined2.default)(opts.minfee) && !this.isValidAmount(opts.minfee, true)) {
throw new TypeError(this.constructAmountRequirementsError('min fee'));
}
var 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 (!((0, _isString2.default)(opts.beneficiary) && opts.beneficiary.length == 0)) {
throw new Error("beneficiary must be empty when fee and minfee are zero");
}
} else {
if (!((0, _isString2.default)(opts.beneficiary) && opts.beneficiary.length == 56)) {
throw new Error("beneficiary is invalid");
}
if (!_strkey.StrKey.isValidEd25519PublicKey(opts.beneficiary)) {
throw new Error("beneficiary is invalid");
}
}
attributes.beneficiary = opts.beneficiary;
var createAsset = new _stellarXdr_generated2.default.CreateAssetOp(attributes);
var opAttributes = {};
opAttributes.body = _stellarXdr_generated2.default.OperationBody.createAsset(createAsset);
this.setSourceAccount(opAttributes, opts);
return new _stellarXdr_generated2.default.Operation(opAttributes);
};