@aeternity/aepp-sdk
Version:
SDK for the æternity blockchain
298 lines (259 loc) • 12.6 kB
JavaScript
import _Object$getOwnPropertySymbols from "@babel/runtime-corejs3/core-js-stable/object/get-own-property-symbols";
import _filterInstanceProperty from "@babel/runtime-corejs3/core-js-stable/instance/filter";
import _Object$getOwnPropertyDescriptor from "@babel/runtime-corejs3/core-js-stable/object/get-own-property-descriptor";
import _forEachInstanceProperty from "@babel/runtime-corejs3/core-js-stable/instance/for-each";
import _Object$getOwnPropertyDescriptors from "@babel/runtime-corejs3/core-js-stable/object/get-own-property-descriptors";
import _Object$defineProperties from "@babel/runtime-corejs3/core-js-stable/object/define-properties";
import _Object$defineProperty from "@babel/runtime-corejs3/core-js-stable/object/define-property";
import _asyncToGenerator from "@babel/runtime-corejs3/helpers/asyncToGenerator";
import _last from "ramda/src/last";
import _pick from "ramda/src/pick";
import _toConsumableArray from "@babel/runtime-corejs3/helpers/toConsumableArray";
import _defineProperty from "@babel/runtime-corejs3/helpers/defineProperty";
import _regeneratorRuntime from "@babel/runtime-corejs3/regenerator";
function ownKeys(object, enumerableOnly) { var keys = _Object$keys(object); if (_Object$getOwnPropertySymbols) { var symbols = _Object$getOwnPropertySymbols(object); if (enumerableOnly) symbols = _filterInstanceProperty(symbols).call(symbols, function (sym) { return _Object$getOwnPropertyDescriptor(object, sym).enumerable; }); keys.push.apply(keys, symbols); } return keys; }
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { var _context14; _forEachInstanceProperty(_context14 = ownKeys(Object(source), true)).call(_context14, function (key) { _defineProperty(target, key, source[key]); }); } else if (_Object$getOwnPropertyDescriptors) { _Object$defineProperties(target, _Object$getOwnPropertyDescriptors(source)); } else { var _context15; _forEachInstanceProperty(_context15 = ownKeys(Object(source))).call(_context15, function (key) { _Object$defineProperty(target, key, _Object$getOwnPropertyDescriptor(source, key)); }); } } return target; }
import _findInstanceProperty from "@babel/runtime-corejs3/core-js-stable/instance/find";
import _concatInstanceProperty from "@babel/runtime-corejs3/core-js-stable/instance/concat";
import _mapInstanceProperty from "@babel/runtime-corejs3/core-js-stable/instance/map";
import _reduceInstanceProperty from "@babel/runtime-corejs3/core-js-stable/instance/reduce";
import _Object$assign from "@babel/runtime-corejs3/core-js-stable/object/assign";
import _sliceInstanceProperty from "@babel/runtime-corejs3/core-js-stable/instance/slice";
import _Object$values from "@babel/runtime-corejs3/core-js-stable/object/values";
import _JSON$stringify from "@babel/runtime-corejs3/core-js-stable/json/stringify";
import _Promise from "@babel/runtime-corejs3/core-js-stable/promise";
import _Object$keys from "@babel/runtime-corejs3/core-js-stable/object/keys";
import { unpackTx } from '../../tx/builder';
import { decodeEvents as unpackEvents, transform, transformDecodedData, validateArguments } from './transformation';
/**
* Get function schema from contract ACI object
* @param {Object} aci Contract ACI object
* @param {String} name Function name
* @param external
* @return {Object} function ACI
*/
export function getFunctionACI(aci, name, _ref) {
var _context, _context2;
var external = _ref.external;
if (!aci) throw new Error('ACI required');
var fn = _findInstanceProperty(_context = aci.functions).call(_context, function (f) {
return f.name === name;
});
if (!fn && name !== 'init') throw new Error("Function ".concat(name, " doesn't exist in contract"));
return _objectSpread(_objectSpread({}, fn), {}, {
bindings: _concatInstanceProperty(_context2 = [{
state: aci.state,
type_defs: aci.type_defs,
name: aci.name
}]).call(_context2, _toConsumableArray(_mapInstanceProperty(external).call(external, _pick(['state', 'type_defs', 'name'])))),
event: aci.event ? aci.event.variant : []
});
}
/**
* Build contract methods base on ACI
* @return {Object} Contract instance methods
*/
export var buildContractMethods = function buildContractMethods(instance) {
return function () {
var _context3;
return _objectSpread(_objectSpread({}, instance.aci ? _reduceInstanceProperty(_context3 = instance.aci.functions).call(_context3, function (acc, _ref2) {
var name = _ref2.name,
aciArgs = _ref2.arguments,
stateful = _ref2.stateful;
return _objectSpread(_objectSpread({}, acc), {}, _defineProperty({}, name, _Object$assign(function () {
var _parseArguments = parseArguments(aciArgs)(arguments),
opt = _parseArguments.opt,
args = _parseArguments.args;
if (name === 'init') return instance.deploy(args, opt);
return instance.call(name, args, _objectSpread({
callStatic: !stateful
}, opt));
}, {
get: function get() {
var _parseArguments2 = parseArguments(aciArgs)(arguments),
opt = _parseArguments2.opt,
args = _parseArguments2.args;
return instance.call(name, args, _objectSpread(_objectSpread({}, opt), {}, {
callStatic: true
}));
},
send: function send() {
var _parseArguments3 = parseArguments(aciArgs)(arguments),
opt = _parseArguments3.opt,
args = _parseArguments3.args;
if (name === 'init') return instance.deploy(args, opt);
return instance.call(name, args, _objectSpread(_objectSpread({}, opt), {}, {
callStatic: false
}));
},
decodeEvents: function decodeEvents(events) {
return instance.decodeEvents(name, events);
}
})));
}, {}) : {}), instance.aci ? {
init: _Object$assign(function () {
var _getFunctionACI = getFunctionACI(instance.aci, 'init', {
external: instance.externalAci
}),
aciArgs = _getFunctionACI.arguments;
var _parseArguments4 = parseArguments(aciArgs)(arguments),
opt = _parseArguments4.opt,
args = _parseArguments4.args;
return instance.deploy(args, opt);
}, {
get: function get() {
var _getFunctionACI2 = getFunctionACI(instance.aci, 'init', {
external: instance.externalAci
}),
aciArgs = _getFunctionACI2.arguments;
var _parseArguments5 = parseArguments(aciArgs)(arguments),
opt = _parseArguments5.opt,
args = _parseArguments5.args;
return instance.deploy(args, _objectSpread(_objectSpread({}, opt), {}, {
callStatic: true
}));
},
send: function send() {
var _getFunctionACI3 = getFunctionACI(instance.aci, 'init', {
external: instance.externalAci
}),
aciArgs = _getFunctionACI3.arguments;
var _parseArguments6 = parseArguments(aciArgs)(arguments),
opt = _parseArguments6.opt,
args = _parseArguments6.args;
return instance.deploy(args, _objectSpread(_objectSpread({}, opt), {}, {
callStatic: false
}));
}
})
} : {});
};
};
export var parseArguments = function parseArguments() {
var aciArgs = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : [];
return function (args) {
var _context4;
return {
opt: args.length > aciArgs.length ? _last(args) : {},
args: _sliceInstanceProperty(_context4 = _Object$values(args)).call(_context4, 0, aciArgs.length)
};
};
};
export var unpackByteCode = function unpackByteCode(bytecode) {
return unpackTx(bytecode, false, 'cb').tx;
};
/**
* Validated contract call arguments using contract ACI
* @function validateCallParams
* @rtype (aci: Object, params: Array) => Object
* @param {Object} aci Contract ACI
* @param {Array} params Contract call arguments
* @return Promise{Array} Object with validation errors
*/
export var prepareArgsForEncode = /*#__PURE__*/function () {
var _ref3 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee2(aci, params) {
var _context9;
var _context5, _context6, _context7, _context8, bindings;
return _regeneratorRuntime.wrap(function _callee2$(_context11) {
while (1) {
switch (_context11.prev = _context11.next) {
case 0:
if (!(!aci || !aci.arguments)) {
_context11.next = 2;
break;
}
return _context11.abrupt("return", params);
case 2:
if (!(aci.arguments.length > params.length)) {
_context11.next = 4;
break;
}
throw new Error(_concatInstanceProperty(_context5 = _concatInstanceProperty(_context6 = _concatInstanceProperty(_context7 = "Function \"".concat(aci.name, "\" require ")).call(_context7, aci.arguments.length, " arguments of types [")).call(_context6, _mapInstanceProperty(_context8 = aci.arguments).call(_context8, function (a) {
return _JSON$stringify(a.type);
}), "] but get [")).call(_context5, _mapInstanceProperty(params).call(params, _JSON$stringify), "]"));
case 4:
validateArguments(aci, params);
bindings = aci.bindings; // Cast argument from JS to Sophia type
return _context11.abrupt("return", _Promise.all(_mapInstanceProperty(_context9 = aci.arguments).call(_context9, /*#__PURE__*/function () {
var _ref5 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee(_ref4, i) {
var type;
return _regeneratorRuntime.wrap(function _callee$(_context10) {
while (1) {
switch (_context10.prev = _context10.next) {
case 0:
type = _ref4.type;
return _context10.abrupt("return", transform(type, params[i], {
bindings: bindings
}));
case 2:
case "end":
return _context10.stop();
}
}
}, _callee);
}));
return function (_x3, _x4) {
return _ref5.apply(this, arguments);
};
}())));
case 7:
case "end":
return _context11.stop();
}
}
}, _callee2);
}));
return function prepareArgsForEncode(_x, _x2) {
return _ref3.apply(this, arguments);
};
}();
export var decodeEvents = function decodeEvents(events, fnACI) {
var _context12;
if (!fnACI || !fnACI.event || !fnACI.event.length) return [];
var eventsSchema = _mapInstanceProperty(_context12 = fnACI.event).call(_context12, function (e) {
var name = _Object$keys(e)[0];
return {
name: name,
types: e[name]
};
});
return unpackEvents(events, {
schema: eventsSchema
});
};
export var decodeCallResult = /*#__PURE__*/function () {
var _ref6 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee3(result, fnACI, opt) {
return _regeneratorRuntime.wrap(function _callee3$(_context13) {
while (1) {
switch (_context13.prev = _context13.next) {
case 0:
_context13.t0 = transformDecodedData;
_context13.t1 = fnACI.returns;
_context13.next = 4;
return result.decode();
case 4:
_context13.t2 = _context13.sent;
_context13.t3 = _objectSpread(_objectSpread({}, opt), {}, {
bindings: fnACI.bindings
});
_context13.next = 8;
return (0, _context13.t0)(_context13.t1, _context13.t2, _context13.t3);
case 8:
_context13.t4 = _context13.sent;
_context13.t5 = decodeEvents(result.result.log, fnACI);
return _context13.abrupt("return", {
decodedResult: _context13.t4,
decodedEvents: _context13.t5
});
case 11:
case "end":
return _context13.stop();
}
}
}, _callee3);
}));
return function decodeCallResult(_x5, _x6, _x7) {
return _ref6.apply(this, arguments);
};
}();
//# sourceMappingURL=helpers.js.map