web3-eth-abi
Advanced tools
Comparing version 4.3.0 to 4.3.1-dev.3687070.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.0", | ||
"version": "4.3.1-dev.3687070.0+3687070", | ||
"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.0", | ||
"web3-types": "^1.8.1", | ||
"web3-utils": "^4.3.2", | ||
"web3-validator": "^2.0.6" | ||
"web3-errors": "1.3.1-dev.3687070.0+3687070", | ||
"web3-types": "1.8.2-dev.3687070.0+3687070", | ||
"web3-utils": "4.3.3-dev.3687070.0+3687070", | ||
"web3-validator": "2.0.7-dev.3687070.0+3687070" | ||
}, | ||
@@ -69,3 +69,3 @@ "devDependencies": { | ||
}, | ||
"gitHead": "068f4b639d95c5dcdca9c0111349ea92792b31e7" | ||
"gitHead": "3687070dc165d414a0e7f2cbed91cd448b8da13a" | ||
} |
@@ -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
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
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
429314
8287
2
1
- Removed@noble/curves@1.4.2(transitive)
- Removed@noble/hashes@1.4.0(transitive)
- Removed@scure/base@1.1.9(transitive)
- Removed@scure/bip32@1.4.0(transitive)
- Removed@scure/bip39@1.3.0(transitive)
- Removedavailable-typed-arrays@1.0.7(transitive)
- Removedcall-bind@1.0.7(transitive)
- Removeddefine-data-property@1.1.4(transitive)
- Removedes-define-property@1.0.0(transitive)
- Removedes-errors@1.3.0(transitive)
- Removedethereum-cryptography@2.2.1(transitive)
- Removedeventemitter3@5.0.1(transitive)
- Removedfor-each@0.3.3(transitive)
- Removedfunction-bind@1.1.2(transitive)
- Removedget-intrinsic@1.2.4(transitive)
- Removedgopd@1.0.1(transitive)
- Removedhas-property-descriptors@1.0.2(transitive)
- Removedhas-proto@1.0.3(transitive)
- Removedhas-symbols@1.0.3(transitive)
- Removedhas-tostringtag@1.0.2(transitive)
- Removedhasown@2.0.2(transitive)
- Removedinherits@2.0.4(transitive)
- Removedis-arguments@1.1.1(transitive)
- Removedis-callable@1.2.7(transitive)
- Removedis-generator-function@1.0.10(transitive)
- Removedis-typed-array@1.1.13(transitive)
- Removedpossible-typed-array-names@1.0.0(transitive)
- Removedset-function-length@1.2.2(transitive)
- Removedutil@0.12.5(transitive)
- Removedweb3-errors@1.3.0(transitive)
- Removedweb3-types@1.8.1(transitive)
- Removedweb3-utils@4.3.2(transitive)
- Removedweb3-validator@2.0.6(transitive)
- Removedwhich-typed-array@1.1.15(transitive)
- Removedzod@3.23.8(transitive)