139 lines
4.9 KiB
JavaScript
139 lines
4.9 KiB
JavaScript
"use strict";
|
|
/*
|
|
This file is part of web3.js.
|
|
|
|
web3.js is free software: you can redistribute it and/or modify
|
|
it under the terms of the GNU Lesser General Public License as published by
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
(at your option) any later version.
|
|
|
|
web3.js is distributed in the hope that it will be useful,
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
GNU Lesser General Public License for more details.
|
|
|
|
You should have received a copy of the GNU Lesser General Public License
|
|
along with web3.js. If not, see <http://www.gnu.org/licenses/>.
|
|
*/
|
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
exports.encodeFunctionCall = exports.encodeFunctionSignature = void 0;
|
|
/**
|
|
*
|
|
* @module ABI
|
|
*/
|
|
const web3_errors_1 = require("web3-errors");
|
|
const web3_utils_1 = require("web3-utils");
|
|
const utils_js_1 = require("../utils.js");
|
|
const parameters_api_js_1 = require("./parameters_api.js");
|
|
/**
|
|
* Encodes the function name to its ABI representation, which are the first 4 bytes of the sha3 of the function name including types.
|
|
* The JSON interface spec documentation https://docs.soliditylang.org/en/latest/abi-spec.html#json
|
|
* @param functionName - The function name to encode or the `JSON interface` object of the function.
|
|
* If the passed parameter is a string, it has to be in the form of `functionName(param1Type,param2Type,...)`. eg: myFunction(uint256,uint32[],bytes10,bytes)
|
|
* @returns - The ABI signature of the function.
|
|
* @example
|
|
* ```ts
|
|
* const signature = web3.eth.abi.encodeFunctionSignature({
|
|
* name: "myMethod",
|
|
* type: "function",
|
|
* inputs: [
|
|
* {
|
|
* type: "uint256",
|
|
* name: "myNumber",
|
|
* },
|
|
* {
|
|
* type: "string",
|
|
* name: "myString",
|
|
* },
|
|
* ],
|
|
* });
|
|
* console.log(signature);
|
|
* > 0x24ee0097
|
|
*
|
|
* const signature = web3.eth.abi.encodeFunctionSignature('myMethod(uint256,string)')
|
|
* console.log(signature);
|
|
* > 0x24ee0097
|
|
*
|
|
* const signature = web3.eth.abi.encodeFunctionSignature('safeTransferFrom(address,address,uint256,bytes)');
|
|
* console.log(signature);
|
|
* > 0xb88d4fde
|
|
* ```
|
|
*/
|
|
const encodeFunctionSignature = (functionName) => {
|
|
if (typeof functionName !== 'string' && !(0, utils_js_1.isAbiFunctionFragment)(functionName)) {
|
|
throw new web3_errors_1.AbiError('Invalid parameter value in encodeFunctionSignature');
|
|
}
|
|
let name;
|
|
if (functionName && (typeof functionName === 'function' || typeof functionName === 'object')) {
|
|
name = (0, utils_js_1.jsonInterfaceMethodToString)(functionName);
|
|
}
|
|
else {
|
|
name = functionName;
|
|
}
|
|
return (0, web3_utils_1.sha3Raw)(name).slice(0, 10);
|
|
};
|
|
exports.encodeFunctionSignature = encodeFunctionSignature;
|
|
/**
|
|
* Encodes a function call using its `JSON interface` object and given parameters.
|
|
* The JSON interface spec documentation https://docs.soliditylang.org/en/latest/abi-spec.html#json
|
|
* @param jsonInterface - The `JSON interface` object of the function.
|
|
* @param params - The parameters to encode
|
|
* @returns - The ABI encoded function call, which, means the function signature and the parameters passed.
|
|
* @example
|
|
* ```ts
|
|
* const sig = web3.eth.abi.encodeFunctionCall(
|
|
* {
|
|
* name: "myMethod",
|
|
* type: "function",
|
|
* inputs: [
|
|
* {
|
|
* type: "uint256",
|
|
* name: "myNumber",
|
|
* },
|
|
* {
|
|
* type: "string",
|
|
* name: "myString",
|
|
* },
|
|
* ],
|
|
* },
|
|
* ["2345675643", "Hello!%"]
|
|
* );
|
|
* console.log(sig);
|
|
* > 0x24ee0097000000000000000000000000000000000000000000000000000000008bd02b7b0000000000000000000000000000000000000000000000000000000000000040000000000000000000000000000000000000000000000000000000000000000748656c6c6f212500000000000000000000000000000000000000000000000000
|
|
*
|
|
*
|
|
*
|
|
* const sig = web3.eth.abi.encodeFunctionCall(
|
|
* {
|
|
* inputs: [
|
|
* {
|
|
* name: "account",
|
|
* type: "address",
|
|
* },
|
|
* ],
|
|
* name: "balanceOf",
|
|
* outputs: [
|
|
* {
|
|
* name: "",
|
|
* type: "uint256",
|
|
* },
|
|
* ],
|
|
* stateMutability: "view",
|
|
* type: "function",
|
|
* },
|
|
* ["0x1234567890123456789012345678901234567890"]
|
|
* );
|
|
*
|
|
* console.log(sig);
|
|
* > 0x70a082310000000000000000000000001234567890123456789012345678901234567890
|
|
* ```
|
|
*/
|
|
const encodeFunctionCall = (jsonInterface, params) => {
|
|
var _a;
|
|
if (!(0, utils_js_1.isAbiFunctionFragment)(jsonInterface)) {
|
|
throw new web3_errors_1.AbiError('Invalid parameter value in encodeFunctionCall');
|
|
}
|
|
return `${(0, exports.encodeFunctionSignature)(jsonInterface)}${(0, parameters_api_js_1.encodeParameters)((_a = jsonInterface.inputs) !== null && _a !== void 0 ? _a : [], params !== null && params !== void 0 ? params : []).replace('0x', '')}`;
|
|
};
|
|
exports.encodeFunctionCall = encodeFunctionCall;
|
|
//# sourceMappingURL=functions_api.js.map
|