@aeternity/aepp-sdk
Version:
SDK for the æternity blockchain
316 lines (274 loc) • 11.3 kB
JavaScript
import _typeof from "@babel/runtime-corejs3/helpers/typeof";
import _asyncToGenerator from "@babel/runtime-corejs3/helpers/asyncToGenerator";
import _concatInstanceProperty from "@babel/runtime-corejs3/core-js-stable/instance/concat";
import _Promise from "@babel/runtime-corejs3/core-js-stable/promise";
import _regeneratorRuntime from "@babel/runtime-corejs3/regenerator";
/*
* ISC License (ISC)
* Copyright (c) 2018 aeternity developers
*
* Permission to use, copy, modify, and/or distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
* REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
* AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
* INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
* LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
* OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
* PERFORMANCE OF THIS SOFTWARE.
*/
/**
* ContractCompilerAPI module
*
* This is the complement to {@link module:@aeternity/aepp-sdk/es/contract}.
* @module @aeternity/aepp-sdk/es/contract/compiler
* @export ContractCompilerAPI
* @example import { ContractCompilerAPI } from '@aeternity/aepp-sdk'
*/
import ContractBase from './index';
import semverSatisfies from '../utils/semver-satisfies';
import AsyncInit from '../utils/async-init';
import genSwaggerClient from '../utils/swagger';
/**
* Contract Compiler Stamp
*
* This stamp include api call's related to contract compiler functionality.
* @function
* @alias module:@aeternity/aepp-sdk/es/contract/compiler
* @rtype Stamp
* @param {Object} [options={}] - Initializer object
* @param {String} [options.compilerUrl] compilerUrl - Url for compiler API
* @return {Object} Contract compiler instance
* @example ContractCompilerAPI({ compilerUrl: 'COMPILER_URL' })
*/
export default AsyncInit.compose(ContractBase, {
init: function init(_ref) {
var _this = this;
return _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee() {
var compilerUrl, ignoreVersion;
return _regeneratorRuntime.wrap(function _callee$(_context) {
while (1) {
switch (_context.prev = _context.next) {
case 0:
compilerUrl = _ref.compilerUrl, ignoreVersion = _ref.ignoreVersion;
if (compilerUrl) {
_context.next = 3;
break;
}
return _context.abrupt("return");
case 3:
_context.next = 5;
return _this.setCompilerUrl(compilerUrl, {
ignoreVersion: ignoreVersion
});
case 5:
case "end":
return _context.stop();
}
}
}, _callee);
}))();
},
methods: {
setCompilerUrl: function setCompilerUrl(compilerUrl) {
var _arguments = arguments,
_this2 = this;
return _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee2() {
var _ref2, _ref2$ignoreVersion, ignoreVersion, client, _context2;
return _regeneratorRuntime.wrap(function _callee2$(_context3) {
while (1) {
switch (_context3.prev = _context3.next) {
case 0:
_ref2 = _arguments.length > 1 && _arguments[1] !== undefined ? _arguments[1] : {}, _ref2$ignoreVersion = _ref2.ignoreVersion, ignoreVersion = _ref2$ignoreVersion === void 0 ? false : _ref2$ignoreVersion;
if (compilerUrl) {
_context3.next = 3;
break;
}
throw new Error('"compilerUrl" required');
case 3:
compilerUrl = compilerUrl.replace(/\/$/, '');
_context3.next = 6;
return genSwaggerClient("".concat(compilerUrl, "/api"), {
disableBigNumbers: true,
disableCaseConversion: true
});
case 6:
client = _context3.sent;
_this2.compilerVersion = client.spec.info.version;
_this2._compilerApi = client.api;
_this2._isCompiler6 = semverSatisfies(_this2.compilerVersion, COMPILER_6_GE_VERSION, COMPILER_LT_VERSION);
if (!ignoreVersion) {
_context3.next = 12;
break;
}
return _context3.abrupt("return");
case 12:
if (semverSatisfies(_this2.compilerVersion, COMPILER_GE_VERSION, COMPILER_LT_VERSION)) {
_context3.next = 14;
break;
}
throw new Error("Unsupported compiler version ".concat(_this2.compilerVersion, ". ") + _concatInstanceProperty(_context2 = "Supported: >= ".concat(COMPILER_GE_VERSION, " < ")).call(_context2, COMPILER_LT_VERSION));
case 14:
case "end":
return _context3.stop();
}
}
}, _callee2);
}))();
},
_ensureCompilerReady: function _ensureCompilerReady() {
if (!this._compilerApi) throw new Error('Compiler is not ready');
},
_prepareCompilerOptions: function _prepareCompilerOptions() {
var _ref3 = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {},
_ref3$filesystem = _ref3.filesystem,
filesystem = _ref3$filesystem === void 0 ? {} : _ref3$filesystem;
return {
file_system: filesystem
};
},
getCompilerVersion: function getCompilerVersion() {
this._ensureCompilerReady();
return _Promise.resolve(this.compilerVersion);
},
contractEncodeCallDataAPI: function contractEncodeCallDataAPI(source, name) {
var _arguments2 = arguments,
_this3 = this;
return _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee3() {
var args, options, _yield$_this3$_compil, calldata;
return _regeneratorRuntime.wrap(function _callee3$(_context4) {
while (1) {
switch (_context4.prev = _context4.next) {
case 0:
args = _arguments2.length > 2 && _arguments2[2] !== undefined ? _arguments2[2] : [];
options = _arguments2.length > 3 ? _arguments2[3] : undefined;
_this3._ensureCompilerReady();
_context4.next = 5;
return _this3._compilerApi.encodeCalldata({
source: source,
"function": name,
arguments: args,
options: _this3._prepareCompilerOptions(options)
});
case 5:
_yield$_this3$_compil = _context4.sent;
calldata = _yield$_this3$_compil.calldata;
return _context4.abrupt("return", calldata);
case 8:
case "end":
return _context4.stop();
}
}
}, _callee3);
}))();
},
compileContractAPI: function compileContractAPI(code, options) {
var _this4 = this;
return _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee4() {
var _yield$_this4$_compil, bytecode;
return _regeneratorRuntime.wrap(function _callee4$(_context5) {
while (1) {
switch (_context5.prev = _context5.next) {
case 0:
_this4._ensureCompilerReady();
_context5.next = 3;
return _this4._compilerApi.compileContract({
code: code,
options: _this4._prepareCompilerOptions(options)
});
case 3:
_yield$_this4$_compil = _context5.sent;
bytecode = _yield$_this4$_compil.bytecode;
return _context5.abrupt("return", bytecode);
case 6:
case "end":
return _context5.stop();
}
}
}, _callee4);
}))();
},
contractGetACI: function contractGetACI(code, options) {
this._ensureCompilerReady();
return this._compilerApi.generateACI({
code: code,
options: this._prepareCompilerOptions(options)
});
},
contractDecodeCallDataByCodeAPI: function contractDecodeCallDataByCodeAPI(bytecode, calldata) {
this._ensureCompilerReady();
return this._compilerApi.decodeCalldataBytecode({
bytecode: bytecode,
calldata: calldata
});
},
contractDecodeCallDataBySourceAPI: function contractDecodeCallDataBySourceAPI(source, fn, callData, options) {
this._ensureCompilerReady();
return this._compilerApi.decodeCalldataSource({
"function": fn,
source: source,
calldata: callData,
options: this._prepareCompilerOptions(options)
});
},
contractDecodeCallResultAPI: function contractDecodeCallResultAPI(source, fn, callValue, callResult, options) {
this._ensureCompilerReady();
return this._compilerApi.decodeCallResult({
"function": fn,
source: source,
'call-result': callResult,
'call-value': callValue,
options: this._prepareCompilerOptions(options)
});
},
validateByteCodeAPI: function validateByteCodeAPI(bytecode, source, options) {
var _this5 = this;
return _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee5() {
var res;
return _regeneratorRuntime.wrap(function _callee5$(_context6) {
while (1) {
switch (_context6.prev = _context6.next) {
case 0:
_this5._ensureCompilerReady();
_context6.next = 3;
return _this5._compilerApi.validateByteCode({
bytecode: bytecode,
source: source,
options: _this5._prepareCompilerOptions(options)
});
case 3:
res = _context6.sent;
return _context6.abrupt("return", _typeof(res) === 'object' ? true : res);
case 5:
case "end":
return _context6.stop();
}
}
}, _callee5);
}))();
},
getFateAssembler: function getFateAssembler(bytecode, options) {
this._ensureCompilerReady();
return this._compilerApi.getFateAssemblerCode({
bytecode: bytecode,
options: this._prepareCompilerOptions(options)
});
},
getBytecodeCompilerVersion: function getBytecodeCompilerVersion(bytecode, options) {
this._ensureCompilerReady();
return this._compilerApi.getCompilerVersion({
bytecode: bytecode,
options: this._prepareCompilerOptions(options)
});
}
},
props: {
compilerVersion: null
}
});
var COMPILER_GE_VERSION = '4.1.0';
var COMPILER_6_GE_VERSION = '6.0.0';
var COMPILER_LT_VERSION = '7.0.0';
//# sourceMappingURL=compiler.js.map