decodeFunctionData
Decodes ABI encoded data (4 byte selector & arguments) into a function name and arguments.
The opposite of encodeFunctionData.
Install
import { decodeFunctionData } from "cive";Usage
Below is a very basic example of how to decode a function to calldata.
import { } from "cive";
import { } from "cive/chains";
import { } from "./abi.js";
const { } = ({
: .,
: ,
: "0x18160ddd",
});export const Abi = [
{
inputs: [],
name: "totalSupply",
outputs: [{ name: "", type: "uint256" }],
stateMutability: "view",
type: "function",
},
] as const;Extracting Arguments
If your calldata includes argument(s) after the 4byte function signature, you can extract them with the args return value.
import { } from "cive";
import { } from "cive/chains";
import { } from "./abi";
const { , } = ({
: .,
: ,
: "0x70a082310000000000000000000000001c2e0c63246a30e121042d7121ee79a6a7012218",
});
// { functionName: 'balanceOf', args: ["cfx:aar4hekpybg7j0gjjtvs6ce0t9ae8rn32j67hhjxva"] }export const Abi = [
{
inputs: [{ name: "owner", type: "address" }],
name: "balanceOf",
outputs: [{ name: "", type: "uint256" }],
stateMutability: "view",
type: "function",
},
] as const;Return Value
{
functionName: string;
args: unknown[] | undefined;
}Decoded ABI function data.
functionName
- Type:
string
The decoded function name.
args
- Type:
unknown[] | undefined
The decoded function arguments.
Parameters
networkId
- Type: number
The network ID will be used for decoding the base32 address.
import { mainnet } from "cive/chains";
const { functionName } = decodeFunctionData({
networkId: mainnet.id,
abi: Abi,
data: "0x18160ddd",
});abi
- Type:
Abi
The contract's ABI.
import { mainnet } from "cive/chains";
const { functionName } = decodeFunctionData({
networkId: mainnet.id,
abi: Abi,
data: "0x18160ddd",
});data
- Type:
Hex
The encoded calldata.
import { mainnet } from "cive/chains";
const { functionName } = decodeFunctionData({
networkId: mainnet.id,
abi: Abi,
data: "0x18160ddd",
});