UNPKG

@aeternity/aepp-calldata

Version:
100 lines (95 loc) 3.59 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.default = void 0; var _AciContractCallEncoder = _interopRequireDefault(require("../AciContractCallEncoder.cjs")); function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; } class AciContractCallEncoder { /** * Creates contract encoder using ACI as type info provider * * @example * const ACI = require('./Test.json') * const encoder = new AciContractCallEncoder(ACI) * * @param {Object} aci - The contract ACI in a canonical (CLI compiler) form as POJO. */ constructor(aci) { this._internalEncoder = new _AciContractCallEncoder.default(aci); } /** * Creates contract call data * * @example * const encoded = encoder.encodeCall('Test', 'test_string', ["whoolymoly"]) * console.log(`Encoded data: ${encoded}`) * // Outputs: * // Encoded data: cb_KxHwzCuVGyl3aG9vbHltb2x5zwMSnw== * * @param {string} contract - The contract name as defined in the ACI. * @param {string} funName - The function name as defined in the ACI. * @param {Array} args - An array of call arguments as Javascript data structures. See README.md * @returns {string} Encoded calldata */ encodeCall(contract, funName, args) { return this._internalEncoder.encodeCall(contract, funName, args); } /** * Decodes contract calldata * * @example * const data = encoder.decodeCall('Test', 'test_string', 'cb_KxHwzCuVGyl3aG9vbHltb2x5zwMSnw==') * console.log(`Decoded data: ${data}`) * // Outputs: * // Decoded data: ["whoolymoly"] * * @param {string} contract - The contract name as defined in the ACI. * @param {string} funName - The function name as defined in the ACI. * @param {string} data - Encoded calldata in canonical format. * @returns {string} Decoded data */ decodeCall(contract, funName, data) { return this._internalEncoder.decodeCall(contract, funName, data); } /** * Decodes successful (resultType = ok) contract call return data * * @example * const decoded = encoder.decode('Test', 'test_string', 'cb_KXdob29seW1vbHlGazSE') * console.log(`Decoded data: ${decoded}`) * // Outputs: * // Decoded data: whoolymoly * * @param {string} contract - The contract name as defined in the ACI. * @param {string} funName - The function name as defined in the ACI. * @param {string} data - The call return value in a canonical format. * @param {'ok'|'revert'|'error'} resultType - The call result type. * @returns {boolean|string|BigInt|Array|Map|Object} * Decoded value as Javascript data structures. See README.md */ decodeResult(contract, funName, data, resultType = 'ok') { return this._internalEncoder.decodeResult(contract, funName, data, resultType); } /** * Decodes contract event * * @example * const data = encoder.decodeEvent('Test', 'cb_dHJpZ2dlcmVk1FYuYA==', [ * 34853523142692495808479485503424878684430196596020091237715106250497712463899n, * 17 * ]) * console.log(data) * // Outputs: * // {EventTwo: [17n, 'triggered']} * * @param {string} contract - The contract name as defined in the ACI. * @param {string} encodedData - Event encoded data * @param {BigInt[]} topics - A list of event topics. * First element should be the implicit topic that carry the event constructor name. */ decodeEvent(contract, data, topics) { return this._internalEncoder.decodeEvent(contract, data, topics); } } var _default = exports.default = AciContractCallEncoder;