web3-eth-abi
Advanced tools
Comparing version 4.3.1-dev.ed85cce.0 to 4.3.1-dev.efac906.0
@@ -1,2 +0,2 @@ | ||
import { AbiFunctionFragment } from 'web3-types'; | ||
import { AbiConstructorFragment, AbiFunctionFragment, DecodedParams, HexString } from 'web3-types'; | ||
/** | ||
@@ -93,1 +93,96 @@ * Encodes the function name to its ABI representation, which are the first 4 bytes of the sha3 of the function name including types. | ||
export declare const encodeFunctionCall: (jsonInterface: AbiFunctionFragment, params: unknown[]) => string; | ||
/** | ||
* Decodes a function call data using its `JSON interface` object. | ||
* The JSON interface spec documentation https://docs.soliditylang.org/en/latest/abi-spec.html#json | ||
* @param functionsAbi - The `JSON interface` object of the function. | ||
* @param data - The data to decode | ||
* @param methodSignatureProvided - (Optional) if `false` do not remove the first 4 bytes that would rather contain the function signature. | ||
* @returns - The data decoded according to the passed ABI. | ||
* @example | ||
* ```ts | ||
* const data = | ||
* '0xa413686200000000000000000000000000000000000000000000000000000000000000400000000000000000000000000000000000000000000000000000000000000080000000000000000000000000000000000000000000000000000000000000000548656c6c6f0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000010416e6f74686572204772656574696e6700000000000000000000000000000000'; | ||
* const params = decodeFunctionCall( | ||
* { | ||
* inputs: [ | ||
* { internalType: 'string', name: '_greeting', type: 'string' }, | ||
* { internalType: 'string', name: '_second_greeting', type: 'string' }, | ||
* ], | ||
* name: 'setGreeting', | ||
* outputs: [ | ||
* { internalType: 'bool', name: '', type: 'bool' }, | ||
* { internalType: 'string', name: '', type: 'string' }, | ||
* ], | ||
* stateMutability: 'nonpayable', | ||
* type: 'function', | ||
* }, | ||
* data, | ||
* ); | ||
* console.log(params); | ||
* > { | ||
* > '0': 'Hello', | ||
* > '1': 'Another Greeting', | ||
* > __length__: 2, | ||
* > __method__: 'setGreeting(string,string)', | ||
* > _greeting: 'Hello', | ||
* > _second_greeting: 'Another Greeting', | ||
* > } | ||
* ``` | ||
*/ | ||
export declare const decodeFunctionCall: (functionsAbi: AbiFunctionFragment | AbiConstructorFragment, data: HexString, methodSignatureProvided?: boolean) => DecodedParams & { | ||
__method__: string; | ||
}; | ||
/** | ||
* Decodes a function call data using its `JSON interface` object. | ||
* The JSON interface spec documentation https://docs.soliditylang.org/en/latest/abi-spec.html#json | ||
* @returns - The ABI encoded function call, which, means the function signature and the parameters passed. | ||
* @param functionsAbi - The `JSON interface` object of the function. | ||
* @param returnValues - The data (the function-returned-values) to decoded | ||
* @returns - The function-returned-values decoded according to the passed ABI. If there are multiple values, it returns them as an object as the example below. But if it is a single value, it returns it only for simplicity. | ||
* @example | ||
* ```ts | ||
* // decode a multi-value data of a method | ||
* const data = | ||
* '0x00000000000000000000000000000000000000000000000000000000000000400000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000548656c6c6f000000000000000000000000000000000000000000000000000000'; | ||
* const decodedResult = decodeFunctionReturn( | ||
* { | ||
* inputs: [ | ||
* { internalType: 'string', name: '_greeting', type: 'string' } | ||
* ], | ||
* name: 'setGreeting', | ||
* outputs: [ | ||
* { internalType: 'string', name: '', type: 'string' }, | ||
* { internalType: 'bool', name: '', type: 'bool' }, | ||
* ], | ||
* stateMutability: 'nonpayable', | ||
* type: 'function', | ||
* }, | ||
* data, | ||
* ); | ||
* console.log(decodedResult); | ||
* > { '0': 'Hello', '1': true, __length__: 2 } | ||
* | ||
* | ||
* // decode a single-value data of a method | ||
* const data = | ||
* '0x0000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000548656c6c6f000000000000000000000000000000000000000000000000000000'; | ||
* const decodedResult = decodeFunctionReturn( | ||
* { | ||
* inputs: [ | ||
* { internalType: 'string', name: '_greeting', type: 'string' } | ||
* ], | ||
* name: 'setGreeting', | ||
* outputs: [{ internalType: 'string', name: '', type: 'string' }], | ||
* stateMutability: 'nonpayable', | ||
* type: 'function', | ||
* }, | ||
* data, | ||
* ); | ||
* console.log(decodedResult); | ||
* > 'Hello' | ||
* ``` | ||
*/ | ||
export declare const decodeFunctionReturn: (functionsAbi: AbiFunctionFragment, returnValues?: HexString) => unknown; |
@@ -19,3 +19,3 @@ "use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.encodeFunctionCall = exports.encodeFunctionSignature = void 0; | ||
exports.decodeFunctionReturn = exports.decodeFunctionCall = exports.encodeFunctionCall = exports.encodeFunctionSignature = void 0; | ||
/** | ||
@@ -140,2 +140,126 @@ * | ||
exports.encodeFunctionCall = encodeFunctionCall; | ||
/** | ||
* Decodes a function call data using its `JSON interface` object. | ||
* The JSON interface spec documentation https://docs.soliditylang.org/en/latest/abi-spec.html#json | ||
* @param functionsAbi - The `JSON interface` object of the function. | ||
* @param data - The data to decode | ||
* @param methodSignatureProvided - (Optional) if `false` do not remove the first 4 bytes that would rather contain the function signature. | ||
* @returns - The data decoded according to the passed ABI. | ||
* @example | ||
* ```ts | ||
* const data = | ||
* '0xa413686200000000000000000000000000000000000000000000000000000000000000400000000000000000000000000000000000000000000000000000000000000080000000000000000000000000000000000000000000000000000000000000000548656c6c6f0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000010416e6f74686572204772656574696e6700000000000000000000000000000000'; | ||
* const params = decodeFunctionCall( | ||
* { | ||
* inputs: [ | ||
* { internalType: 'string', name: '_greeting', type: 'string' }, | ||
* { internalType: 'string', name: '_second_greeting', type: 'string' }, | ||
* ], | ||
* name: 'setGreeting', | ||
* outputs: [ | ||
* { internalType: 'bool', name: '', type: 'bool' }, | ||
* { internalType: 'string', name: '', type: 'string' }, | ||
* ], | ||
* stateMutability: 'nonpayable', | ||
* type: 'function', | ||
* }, | ||
* data, | ||
* ); | ||
* console.log(params); | ||
* > { | ||
* > '0': 'Hello', | ||
* > '1': 'Another Greeting', | ||
* > __length__: 2, | ||
* > __method__: 'setGreeting(string,string)', | ||
* > _greeting: 'Hello', | ||
* > _second_greeting: 'Another Greeting', | ||
* > } | ||
* ``` | ||
*/ | ||
const decodeFunctionCall = (functionsAbi, data, methodSignatureProvided = true) => { | ||
const value = methodSignatureProvided && data && data.length >= 10 && data.startsWith('0x') | ||
? data.slice(10) | ||
: data; | ||
if (!functionsAbi.inputs) { | ||
throw new web3_errors_1.Web3ContractError('No inputs found in the ABI'); | ||
} | ||
const result = (0, parameters_api_js_1.decodeParameters)([...functionsAbi.inputs], value); | ||
return Object.assign(Object.assign({}, result), { __method__: (0, utils_js_1.jsonInterfaceMethodToString)(functionsAbi) }); | ||
}; | ||
exports.decodeFunctionCall = decodeFunctionCall; | ||
/** | ||
* Decodes a function call data using its `JSON interface` object. | ||
* The JSON interface spec documentation https://docs.soliditylang.org/en/latest/abi-spec.html#json | ||
* @returns - The ABI encoded function call, which, means the function signature and the parameters passed. | ||
* @param functionsAbi - The `JSON interface` object of the function. | ||
* @param returnValues - The data (the function-returned-values) to decoded | ||
* @returns - The function-returned-values decoded according to the passed ABI. If there are multiple values, it returns them as an object as the example below. But if it is a single value, it returns it only for simplicity. | ||
* @example | ||
* ```ts | ||
* // decode a multi-value data of a method | ||
* const data = | ||
* '0x00000000000000000000000000000000000000000000000000000000000000400000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000548656c6c6f000000000000000000000000000000000000000000000000000000'; | ||
* const decodedResult = decodeFunctionReturn( | ||
* { | ||
* inputs: [ | ||
* { internalType: 'string', name: '_greeting', type: 'string' } | ||
* ], | ||
* name: 'setGreeting', | ||
* outputs: [ | ||
* { internalType: 'string', name: '', type: 'string' }, | ||
* { internalType: 'bool', name: '', type: 'bool' }, | ||
* ], | ||
* stateMutability: 'nonpayable', | ||
* type: 'function', | ||
* }, | ||
* data, | ||
* ); | ||
* console.log(decodedResult); | ||
* > { '0': 'Hello', '1': true, __length__: 2 } | ||
* | ||
* | ||
* // decode a single-value data of a method | ||
* const data = | ||
* '0x0000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000548656c6c6f000000000000000000000000000000000000000000000000000000'; | ||
* const decodedResult = decodeFunctionReturn( | ||
* { | ||
* inputs: [ | ||
* { internalType: 'string', name: '_greeting', type: 'string' } | ||
* ], | ||
* name: 'setGreeting', | ||
* outputs: [{ internalType: 'string', name: '', type: 'string' }], | ||
* stateMutability: 'nonpayable', | ||
* type: 'function', | ||
* }, | ||
* data, | ||
* ); | ||
* console.log(decodedResult); | ||
* > 'Hello' | ||
* ``` | ||
*/ | ||
const decodeFunctionReturn = (functionsAbi, returnValues) => { | ||
// If it is a constructor there is nothing to decode! | ||
if (functionsAbi.type === 'constructor') { | ||
return returnValues; | ||
} | ||
if (!returnValues) { | ||
// Using "null" value intentionally to match legacy behavior | ||
// eslint-disable-next-line no-null/no-null | ||
return null; | ||
} | ||
const value = returnValues.length >= 2 ? returnValues.slice(2) : returnValues; | ||
if (!functionsAbi.outputs) { | ||
// eslint-disable-next-line no-null/no-null | ||
return null; | ||
} | ||
const result = (0, parameters_api_js_1.decodeParameters)([...functionsAbi.outputs], value); | ||
if (result.__length__ === 1) { | ||
return result[0]; | ||
} | ||
return result; | ||
}; | ||
exports.decodeFunctionReturn = decodeFunctionReturn; | ||
//# sourceMappingURL=functions_api.js.map |
@@ -21,6 +21,6 @@ /* | ||
*/ | ||
import { AbiError } from 'web3-errors'; | ||
import { AbiError, Web3ContractError } from 'web3-errors'; | ||
import { sha3Raw } from 'web3-utils'; | ||
import { isAbiFunctionFragment, jsonInterfaceMethodToString } from '../utils.js'; | ||
import { encodeParameters } from './parameters_api.js'; | ||
import { decodeParameters, encodeParameters } from './parameters_api.js'; | ||
/** | ||
@@ -135,2 +135,124 @@ * Encodes the function name to its ABI representation, which are the first 4 bytes of the sha3 of the function name including types. | ||
}; | ||
/** | ||
* Decodes a function call data using its `JSON interface` object. | ||
* The JSON interface spec documentation https://docs.soliditylang.org/en/latest/abi-spec.html#json | ||
* @param functionsAbi - The `JSON interface` object of the function. | ||
* @param data - The data to decode | ||
* @param methodSignatureProvided - (Optional) if `false` do not remove the first 4 bytes that would rather contain the function signature. | ||
* @returns - The data decoded according to the passed ABI. | ||
* @example | ||
* ```ts | ||
* const data = | ||
* '0xa413686200000000000000000000000000000000000000000000000000000000000000400000000000000000000000000000000000000000000000000000000000000080000000000000000000000000000000000000000000000000000000000000000548656c6c6f0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000010416e6f74686572204772656574696e6700000000000000000000000000000000'; | ||
* const params = decodeFunctionCall( | ||
* { | ||
* inputs: [ | ||
* { internalType: 'string', name: '_greeting', type: 'string' }, | ||
* { internalType: 'string', name: '_second_greeting', type: 'string' }, | ||
* ], | ||
* name: 'setGreeting', | ||
* outputs: [ | ||
* { internalType: 'bool', name: '', type: 'bool' }, | ||
* { internalType: 'string', name: '', type: 'string' }, | ||
* ], | ||
* stateMutability: 'nonpayable', | ||
* type: 'function', | ||
* }, | ||
* data, | ||
* ); | ||
* console.log(params); | ||
* > { | ||
* > '0': 'Hello', | ||
* > '1': 'Another Greeting', | ||
* > __length__: 2, | ||
* > __method__: 'setGreeting(string,string)', | ||
* > _greeting: 'Hello', | ||
* > _second_greeting: 'Another Greeting', | ||
* > } | ||
* ``` | ||
*/ | ||
export const decodeFunctionCall = (functionsAbi, data, methodSignatureProvided = true) => { | ||
const value = methodSignatureProvided && data && data.length >= 10 && data.startsWith('0x') | ||
? data.slice(10) | ||
: data; | ||
if (!functionsAbi.inputs) { | ||
throw new Web3ContractError('No inputs found in the ABI'); | ||
} | ||
const result = decodeParameters([...functionsAbi.inputs], value); | ||
return Object.assign(Object.assign({}, result), { __method__: jsonInterfaceMethodToString(functionsAbi) }); | ||
}; | ||
/** | ||
* Decodes a function call data using its `JSON interface` object. | ||
* The JSON interface spec documentation https://docs.soliditylang.org/en/latest/abi-spec.html#json | ||
* @returns - The ABI encoded function call, which, means the function signature and the parameters passed. | ||
* @param functionsAbi - The `JSON interface` object of the function. | ||
* @param returnValues - The data (the function-returned-values) to decoded | ||
* @returns - The function-returned-values decoded according to the passed ABI. If there are multiple values, it returns them as an object as the example below. But if it is a single value, it returns it only for simplicity. | ||
* @example | ||
* ```ts | ||
* // decode a multi-value data of a method | ||
* const data = | ||
* '0x00000000000000000000000000000000000000000000000000000000000000400000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000548656c6c6f000000000000000000000000000000000000000000000000000000'; | ||
* const decodedResult = decodeFunctionReturn( | ||
* { | ||
* inputs: [ | ||
* { internalType: 'string', name: '_greeting', type: 'string' } | ||
* ], | ||
* name: 'setGreeting', | ||
* outputs: [ | ||
* { internalType: 'string', name: '', type: 'string' }, | ||
* { internalType: 'bool', name: '', type: 'bool' }, | ||
* ], | ||
* stateMutability: 'nonpayable', | ||
* type: 'function', | ||
* }, | ||
* data, | ||
* ); | ||
* console.log(decodedResult); | ||
* > { '0': 'Hello', '1': true, __length__: 2 } | ||
* | ||
* | ||
* // decode a single-value data of a method | ||
* const data = | ||
* '0x0000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000548656c6c6f000000000000000000000000000000000000000000000000000000'; | ||
* const decodedResult = decodeFunctionReturn( | ||
* { | ||
* inputs: [ | ||
* { internalType: 'string', name: '_greeting', type: 'string' } | ||
* ], | ||
* name: 'setGreeting', | ||
* outputs: [{ internalType: 'string', name: '', type: 'string' }], | ||
* stateMutability: 'nonpayable', | ||
* type: 'function', | ||
* }, | ||
* data, | ||
* ); | ||
* console.log(decodedResult); | ||
* > 'Hello' | ||
* ``` | ||
*/ | ||
export const decodeFunctionReturn = (functionsAbi, returnValues) => { | ||
// If it is a constructor there is nothing to decode! | ||
if (functionsAbi.type === 'constructor') { | ||
return returnValues; | ||
} | ||
if (!returnValues) { | ||
// Using "null" value intentionally to match legacy behavior | ||
// eslint-disable-next-line no-null/no-null | ||
return null; | ||
} | ||
const value = returnValues.length >= 2 ? returnValues.slice(2) : returnValues; | ||
if (!functionsAbi.outputs) { | ||
// eslint-disable-next-line no-null/no-null | ||
return null; | ||
} | ||
const result = decodeParameters([...functionsAbi.outputs], value); | ||
if (result.__length__ === 1) { | ||
return result[0]; | ||
} | ||
return result; | ||
}; | ||
//# sourceMappingURL=functions_api.js.map |
@@ -1,2 +0,2 @@ | ||
import { AbiFunctionFragment } from 'web3-types'; | ||
import { AbiConstructorFragment, AbiFunctionFragment, DecodedParams, HexString } from 'web3-types'; | ||
/** | ||
@@ -93,2 +93,97 @@ * Encodes the function name to its ABI representation, which are the first 4 bytes of the sha3 of the function name including types. | ||
export declare const encodeFunctionCall: (jsonInterface: AbiFunctionFragment, params: unknown[]) => string; | ||
/** | ||
* Decodes a function call data using its `JSON interface` object. | ||
* The JSON interface spec documentation https://docs.soliditylang.org/en/latest/abi-spec.html#json | ||
* @param functionsAbi - The `JSON interface` object of the function. | ||
* @param data - The data to decode | ||
* @param methodSignatureProvided - (Optional) if `false` do not remove the first 4 bytes that would rather contain the function signature. | ||
* @returns - The data decoded according to the passed ABI. | ||
* @example | ||
* ```ts | ||
* const data = | ||
* '0xa413686200000000000000000000000000000000000000000000000000000000000000400000000000000000000000000000000000000000000000000000000000000080000000000000000000000000000000000000000000000000000000000000000548656c6c6f0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000010416e6f74686572204772656574696e6700000000000000000000000000000000'; | ||
* const params = decodeFunctionCall( | ||
* { | ||
* inputs: [ | ||
* { internalType: 'string', name: '_greeting', type: 'string' }, | ||
* { internalType: 'string', name: '_second_greeting', type: 'string' }, | ||
* ], | ||
* name: 'setGreeting', | ||
* outputs: [ | ||
* { internalType: 'bool', name: '', type: 'bool' }, | ||
* { internalType: 'string', name: '', type: 'string' }, | ||
* ], | ||
* stateMutability: 'nonpayable', | ||
* type: 'function', | ||
* }, | ||
* data, | ||
* ); | ||
* console.log(params); | ||
* > { | ||
* > '0': 'Hello', | ||
* > '1': 'Another Greeting', | ||
* > __length__: 2, | ||
* > __method__: 'setGreeting(string,string)', | ||
* > _greeting: 'Hello', | ||
* > _second_greeting: 'Another Greeting', | ||
* > } | ||
* ``` | ||
*/ | ||
export declare const decodeFunctionCall: (functionsAbi: AbiFunctionFragment | AbiConstructorFragment, data: HexString, methodSignatureProvided?: boolean) => DecodedParams & { | ||
__method__: string; | ||
}; | ||
/** | ||
* Decodes a function call data using its `JSON interface` object. | ||
* The JSON interface spec documentation https://docs.soliditylang.org/en/latest/abi-spec.html#json | ||
* @returns - The ABI encoded function call, which, means the function signature and the parameters passed. | ||
* @param functionsAbi - The `JSON interface` object of the function. | ||
* @param returnValues - The data (the function-returned-values) to decoded | ||
* @returns - The function-returned-values decoded according to the passed ABI. If there are multiple values, it returns them as an object as the example below. But if it is a single value, it returns it only for simplicity. | ||
* @example | ||
* ```ts | ||
* // decode a multi-value data of a method | ||
* const data = | ||
* '0x00000000000000000000000000000000000000000000000000000000000000400000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000548656c6c6f000000000000000000000000000000000000000000000000000000'; | ||
* const decodedResult = decodeFunctionReturn( | ||
* { | ||
* inputs: [ | ||
* { internalType: 'string', name: '_greeting', type: 'string' } | ||
* ], | ||
* name: 'setGreeting', | ||
* outputs: [ | ||
* { internalType: 'string', name: '', type: 'string' }, | ||
* { internalType: 'bool', name: '', type: 'bool' }, | ||
* ], | ||
* stateMutability: 'nonpayable', | ||
* type: 'function', | ||
* }, | ||
* data, | ||
* ); | ||
* console.log(decodedResult); | ||
* > { '0': 'Hello', '1': true, __length__: 2 } | ||
* | ||
* | ||
* // decode a single-value data of a method | ||
* const data = | ||
* '0x0000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000548656c6c6f000000000000000000000000000000000000000000000000000000'; | ||
* const decodedResult = decodeFunctionReturn( | ||
* { | ||
* inputs: [ | ||
* { internalType: 'string', name: '_greeting', type: 'string' } | ||
* ], | ||
* name: 'setGreeting', | ||
* outputs: [{ internalType: 'string', name: '', type: 'string' }], | ||
* stateMutability: 'nonpayable', | ||
* type: 'function', | ||
* }, | ||
* data, | ||
* ); | ||
* console.log(decodedResult); | ||
* > 'Hello' | ||
* ``` | ||
*/ | ||
export declare const decodeFunctionReturn: (functionsAbi: AbiFunctionFragment, returnValues?: HexString) => unknown; | ||
//# sourceMappingURL=functions_api.d.ts.map |
{ | ||
"name": "web3-eth-abi", | ||
"version": "4.3.1-dev.ed85cce.0+ed85cce", | ||
"version": "4.3.1-dev.efac906.0+efac906", | ||
"description": "Web3 module encode and decode EVM in/output.", | ||
@@ -46,6 +46,6 @@ "main": "./lib/commonjs/index.js", | ||
"abitype": "0.7.1", | ||
"web3-errors": "1.3.1-dev.ed85cce.0+ed85cce", | ||
"web3-types": "1.8.2-dev.ed85cce.0+ed85cce", | ||
"web3-utils": "4.3.3-dev.ed85cce.0+ed85cce", | ||
"web3-validator": "2.0.7-dev.ed85cce.0+ed85cce" | ||
"web3-errors": "1.3.1-dev.efac906.0+efac906", | ||
"web3-types": "1.8.2-dev.efac906.0+efac906", | ||
"web3-utils": "4.3.3-dev.efac906.0+efac906", | ||
"web3-validator": "2.0.7-dev.efac906.0+efac906" | ||
}, | ||
@@ -69,3 +69,3 @@ "devDependencies": { | ||
}, | ||
"gitHead": "ed85ccecadc97cbacd0ee00dfb1a2ee7497347b8" | ||
"gitHead": "efac906a9fdcfda895aaf602c458d574645bc549" | ||
} |
@@ -22,7 +22,7 @@ /* | ||
*/ | ||
import { AbiError } from 'web3-errors'; | ||
import { AbiError, Web3ContractError } from 'web3-errors'; | ||
import { sha3Raw } from 'web3-utils'; | ||
import { AbiFunctionFragment } from 'web3-types'; | ||
import { AbiConstructorFragment, AbiFunctionFragment, DecodedParams, HexString } from 'web3-types'; | ||
import { isAbiFunctionFragment, jsonInterfaceMethodToString } from '../utils.js'; | ||
import { encodeParameters } from './parameters_api.js'; | ||
import { decodeParameters, encodeParameters } from './parameters_api.js'; | ||
@@ -147,1 +147,140 @@ /** | ||
}; | ||
/** | ||
* Decodes a function call data using its `JSON interface` object. | ||
* The JSON interface spec documentation https://docs.soliditylang.org/en/latest/abi-spec.html#json | ||
* @param functionsAbi - The `JSON interface` object of the function. | ||
* @param data - The data to decode | ||
* @param methodSignatureProvided - (Optional) if `false` do not remove the first 4 bytes that would rather contain the function signature. | ||
* @returns - The data decoded according to the passed ABI. | ||
* @example | ||
* ```ts | ||
* const data = | ||
* '0xa413686200000000000000000000000000000000000000000000000000000000000000400000000000000000000000000000000000000000000000000000000000000080000000000000000000000000000000000000000000000000000000000000000548656c6c6f0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000010416e6f74686572204772656574696e6700000000000000000000000000000000'; | ||
* const params = decodeFunctionCall( | ||
* { | ||
* inputs: [ | ||
* { internalType: 'string', name: '_greeting', type: 'string' }, | ||
* { internalType: 'string', name: '_second_greeting', type: 'string' }, | ||
* ], | ||
* name: 'setGreeting', | ||
* outputs: [ | ||
* { internalType: 'bool', name: '', type: 'bool' }, | ||
* { internalType: 'string', name: '', type: 'string' }, | ||
* ], | ||
* stateMutability: 'nonpayable', | ||
* type: 'function', | ||
* }, | ||
* data, | ||
* ); | ||
* console.log(params); | ||
* > { | ||
* > '0': 'Hello', | ||
* > '1': 'Another Greeting', | ||
* > __length__: 2, | ||
* > __method__: 'setGreeting(string,string)', | ||
* > _greeting: 'Hello', | ||
* > _second_greeting: 'Another Greeting', | ||
* > } | ||
* ``` | ||
*/ | ||
export const decodeFunctionCall = ( | ||
functionsAbi: AbiFunctionFragment | AbiConstructorFragment, | ||
data: HexString, | ||
methodSignatureProvided = true, | ||
): DecodedParams & { __method__: string } => { | ||
const value = | ||
methodSignatureProvided && data && data.length >= 10 && data.startsWith('0x') | ||
? data.slice(10) | ||
: data; | ||
if (!functionsAbi.inputs) { | ||
throw new Web3ContractError('No inputs found in the ABI'); | ||
} | ||
const result = decodeParameters([...functionsAbi.inputs], value); | ||
return { | ||
...result, | ||
__method__: jsonInterfaceMethodToString(functionsAbi), | ||
}; | ||
}; | ||
/** | ||
* Decodes a function call data using its `JSON interface` object. | ||
* The JSON interface spec documentation https://docs.soliditylang.org/en/latest/abi-spec.html#json | ||
* @returns - The ABI encoded function call, which, means the function signature and the parameters passed. | ||
* @param functionsAbi - The `JSON interface` object of the function. | ||
* @param returnValues - The data (the function-returned-values) to decoded | ||
* @returns - The function-returned-values decoded according to the passed ABI. If there are multiple values, it returns them as an object as the example below. But if it is a single value, it returns it only for simplicity. | ||
* @example | ||
* ```ts | ||
* // decode a multi-value data of a method | ||
* const data = | ||
* '0x00000000000000000000000000000000000000000000000000000000000000400000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000548656c6c6f000000000000000000000000000000000000000000000000000000'; | ||
* const decodedResult = decodeFunctionReturn( | ||
* { | ||
* inputs: [ | ||
* { internalType: 'string', name: '_greeting', type: 'string' } | ||
* ], | ||
* name: 'setGreeting', | ||
* outputs: [ | ||
* { internalType: 'string', name: '', type: 'string' }, | ||
* { internalType: 'bool', name: '', type: 'bool' }, | ||
* ], | ||
* stateMutability: 'nonpayable', | ||
* type: 'function', | ||
* }, | ||
* data, | ||
* ); | ||
* console.log(decodedResult); | ||
* > { '0': 'Hello', '1': true, __length__: 2 } | ||
* | ||
* | ||
* // decode a single-value data of a method | ||
* const data = | ||
* '0x0000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000548656c6c6f000000000000000000000000000000000000000000000000000000'; | ||
* const decodedResult = decodeFunctionReturn( | ||
* { | ||
* inputs: [ | ||
* { internalType: 'string', name: '_greeting', type: 'string' } | ||
* ], | ||
* name: 'setGreeting', | ||
* outputs: [{ internalType: 'string', name: '', type: 'string' }], | ||
* stateMutability: 'nonpayable', | ||
* type: 'function', | ||
* }, | ||
* data, | ||
* ); | ||
* console.log(decodedResult); | ||
* > 'Hello' | ||
* ``` | ||
*/ | ||
export const decodeFunctionReturn = ( | ||
functionsAbi: AbiFunctionFragment, | ||
returnValues?: HexString, | ||
) => { | ||
// If it is a constructor there is nothing to decode! | ||
if (functionsAbi.type === 'constructor') { | ||
return returnValues; | ||
} | ||
if (!returnValues) { | ||
// Using "null" value intentionally to match legacy behavior | ||
// eslint-disable-next-line no-null/no-null | ||
return null; | ||
} | ||
const value = returnValues.length >= 2 ? returnValues.slice(2) : returnValues; | ||
if (!functionsAbi.outputs) { | ||
// eslint-disable-next-line no-null/no-null | ||
return null; | ||
} | ||
const result = decodeParameters([...functionsAbi.outputs], value); | ||
if (result.__length__ === 1) { | ||
return result[0]; | ||
} | ||
return result; | ||
}; |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Manifest confusion
Supply chain riskThis package has inconsistent metadata. This could be malicious or caused by an error when publishing the package.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Manifest confusion
Supply chain riskThis package has inconsistent metadata. This could be malicious or caused by an error when publishing the package.
Found 1 instance in 1 package
429314
8287