AbiFunction.from
Parses an arbitrary JSON ABI Function or Human Readable ABI Function into a typed AbiFunction.AbiFunction
.
Imports
Named
import { AbiFunction } from 'ox'
Examples
JSON ABIs
import { AbiFunction } from 'ox'
const approve = AbiFunction.from({
type: 'function',
name: 'approve',
stateMutability: 'nonpayable',
inputs: [
{
name: 'spender',
type: 'address',
},
{
name: 'amount',
type: 'uint256',
},
],
outputs: [{ type: 'bool' }],
})
const approve: {
readonly type: "function";
readonly name: "approve";
readonly stateMutability: "nonpayable";
readonly inputs: readonly [{
readonly name: "spender";
readonly type: "address";
}, {
readonly name: "amount";
readonly type: "uint256";
}];
readonly outputs: readonly [...];
}approve
Human Readable ABIs
A Human Readable ABI can be parsed into a typed ABI object:
import { AbiFunction } from 'ox'
const approve = AbiFunction.from(
'function approve(address spender, uint256 amount) returns (bool)'
)
const approve: {
readonly name: "approve";
readonly type: "function";
readonly stateMutability: "nonpayable";
readonly inputs: readonly [{
readonly type: "address";
readonly name: "spender";
}, {
readonly type: "uint256";
readonly name: "amount";
}];
readonly outputs: readonly [...];
}approve
It is possible to specify struct
s along with your definitions:
import { AbiFunction } from 'ox'
const approve = AbiFunction.from([
'struct Foo { address spender; uint256 amount; }',
'function approve(Foo foo) returns (bool)',
])
const approve: {
readonly name: "approve";
readonly type: "function";
readonly stateMutability: "nonpayable";
readonly inputs: readonly [{
readonly type: "tuple";
readonly components: readonly [{
readonly type: "address";
readonly name: "spender";
}, {
...;
}];
readonly name: "foo";
}];
readonly outputs: readonly [...];
}approve
Definition
function from<abiFunction>(
abiFunction: (abiFunction | AbiFunction | string | readonly string[]) & ((abiFunction extends string ? internal.Signature<abiFunction> : never) | (abiFunction extends readonly string[] ? internal.Signatures<abiFunction> : never) | AbiFunction),
options?: from.Options,
): from.ReturnType<abiFunction>
Source: src/core/AbiFunction.ts
Parameters
abiFunction
- Type:
(abiFunction | AbiFunction | string | readonly string[]) & ((abiFunction extends string ? internal.Signature<abiFunction> : never) | (abiFunction extends readonly string[] ? internal.Signatures<abiFunction> : never) | AbiFunction)
The ABI Function to parse.
options
- Type:
from.Options
- Optional
options.prepare
- Type:
boolean
- Optional
Whether or not to prepare the extracted function (optimization for encoding performance).
When true
, the hash
property is computed and included in the returned value.
Return Type
Typed ABI Function.
from.ReturnType<abiFunction>