UNPKG

test-triam-base-contract

Version:

Low level triam smart cotnract support library

273 lines (212 loc) 8.36 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.ContrOperation = undefined; var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }(); var _stellarXdr_generated = require("./generated/stellar-xdr_generated"); var _stellarXdr_generated2 = _interopRequireDefault(_stellarXdr_generated); var _keypair = require("./keypair"); var _hashing = require("./hashing"); var _strkey = require("./strkey"); var _jsXdr = require("js-xdr"); var _asset = require("./asset"); var _bignumber = require("bignumber.js"); var _bignumber2 = _interopRequireDefault(_bignumber); var _continued_fraction = require("./util/continued_fraction"); var _trimEnd = require("lodash/trimEnd"); var _trimEnd2 = _interopRequireDefault(_trimEnd); var _isUndefined = require("lodash/isUndefined"); var _isUndefined2 = _interopRequireDefault(_isUndefined); var _isString = require("lodash/isString"); var _isString2 = _interopRequireDefault(_isString); var _isNumber = require("lodash/isNumber"); var _isNumber2 = _interopRequireDefault(_isNumber); var _isFinite = require("lodash/isFinite"); var _isFinite2 = _interopRequireDefault(_isFinite); var _index = require("./contr_operations/index"); var ops = _interopRequireWildcard(_index); function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) newObj[key] = obj[key]; } } newObj.default = obj; return newObj; } } function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } var ONE = 10000000; var MAX_INT64 = '9223372036854775807'; /** * `ContrOperation` class represents [contrOperations] in Stellar network. * Use one of static methods to create contract operations: * * thuannd start * * `{@link ContrOperation.check}` * * `{@link ContrOperation.invoke}` * * `{@link ContrOperation.query}` * * `{@link ContrOperation.transfer}` * * thuannd end * * @class ContrOperation */ var ContrOperation = exports.ContrOperation = function () { function ContrOperation() { _classCallCheck(this, ContrOperation); } _createClass(ContrOperation, null, [{ key: "fromXDRObject", /** * Converts the XDR Contract Operation object to the opts object used to create the XDR * contract operation. * @param {xdr.ContrOperation} contrOperation - An XDR contract operation. * @return {ContrOperation} */ value: function fromXDRObject(contrOperation) { function accountIdtoAddress(accountId) { return _strkey.StrKey.encodeEd25519PublicKey(accountId.ed25519()); } var result = {}; var attrs = contrOperation.body().value(); switch (contrOperation.body().switch().name) { case "check": result.type = "check"; result.sourceAccId = accountIdtoAddress(attrs.sourceAccId()); result.op = attrs.op().toString('ascii'); result.amount = this._fromXDRAmount(attrs.amount()); break; case "invoke": result.type = "invoke"; result.newState = attrs.newState(); break; case "query": result.type = "query"; result.data = attrs.data(); break; case "transfer": result.destination = accountIdtoAddress(attrs.destination()); result.asset = _asset.Asset.fromOperation(attrs.asset()); result.amount = this._fromXDRAmount(attrs.amount()); break; default: throw new Error("Unknown contract opration"); } return result; } }, { key: "isValidAmount", value: function isValidAmount(value) { var allowZero = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false; if (!(0, _isString2.default)(value)) { return false; } var amount = void 0; try { amount = new _bignumber2.default(value); } catch (e) { return false; } switch (true) { // == 0 case !allowZero && amount.isZero(): // < 0 case amount.isNegative(): // > Max value case amount.times(ONE).greaterThan(new _bignumber2.default(MAX_INT64).toString()): // Decimal places (max 7) case amount.decimalPlaces() > 7: // NaN or Infinity case amount.isNaN() || !amount.isFinite(): return false; default: return true; } } }, { key: "constructAmountRequirementsError", value: function constructAmountRequirementsError(arg) { return arg + " argument must be of type String, represent a positive number and have at most 7 digits after the decimal"; } /** * Returns value converted to uint32 value or undefined. * If `value` is not `Number`, `String` or `Undefined` then throws an error. * Used in {@link Operation.setOptions}. * @private * @param {string} name Name of the property (used in error message only) * @param {*} value Value to check * @param {function(value, name)} isValidFunction Function to check other constraints (the argument will be a `Number`) * @returns {undefined|Number} * @private */ }, { key: "_checkUnsignedIntValue", value: function _checkUnsignedIntValue(name, value) { var isValidFunction = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : null; if ((0, _isUndefined2.default)(value)) { return undefined; } if ((0, _isString2.default)(value)) { value = parseFloat(value); } switch (true) { case !(0, _isNumber2.default)(value) || !(0, _isFinite2.default)(value) || value % 1 !== 0: throw new Error(name + " value is invalid"); case value < 0: throw new Error(name + " value must be unsigned"); case !isValidFunction || isValidFunction && isValidFunction(value, name): return value; default: throw new Error(name + " value is invalid"); } } /** * @private */ }, { key: "_toXDRAmount", value: function _toXDRAmount(value) { var amount = new _bignumber2.default(value).mul(ONE); return _jsXdr.Hyper.fromString(amount.toString()); } /** * @private */ }, { key: "_fromXDRAmount", value: function _fromXDRAmount(value) { return new _bignumber2.default(value).div(ONE).toString(); } /** * @private */ }, { key: "_fromXDRPrice", value: function _fromXDRPrice(price) { var n = new _bignumber2.default(price.n()); return n.div(new _bignumber2.default(price.d())).toString(); } /** * @private */ }, { key: "_toXDRPrice", value: function _toXDRPrice(price) { var xdrObject = void 0; if (price.n && price.d) { xdrObject = new _stellarXdr_generated2.default.Price(price); } else { price = new _bignumber2.default(price); var approx = (0, _continued_fraction.best_r)(price); xdrObject = new _stellarXdr_generated2.default.Price({ n: parseInt(approx[0]), d: parseInt(approx[1]) }); } if (xdrObject.n() < 0 || xdrObject.d() < 0) { throw new Error('price must be positive'); } return xdrObject; } }]); return ContrOperation; }(); // Attach all imported operations as static methods on the Operation class // thuannd start ContrOperation.check = ops.check; ContrOperation.invoke = ops.invoke; ContrOperation.query = ops.query; ContrOperation.transfer = ops.transfer; // thuannd end