Skip to main content

encodeParameter

Callable

  • encodeParameter(abi: AbiInput, param: unknown): string

  • Encodes a parameter based on its type to its ABI representation.

    @example
     const res = web3.eth.abi.encodeParameter("uint256", "2345675643");
    console.log(res);
    0x000000000000000000000000000000000000000000000000000000008bd02b7b

    const res = web3.eth.abi.encodeParameter("uint", "2345675643");

    console.log(res);
    >0x000000000000000000000000000000000000000000000000000000008bd02b7b

    const res = web3.eth.abi.encodeParameter("bytes32", "0xdf3234");

    console.log(res);
    >0xdf32340000000000000000000000000000000000000000000000000000000000

    const res = web3.eth.abi.encodeParameter("bytes", "0xdf3234");

    console.log(res);
    > 0x00000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000003df32340000000000000000000000000000000000000000000000000000000000

    const res = web3.eth.abi.encodeParameter("bytes32[]", ["0xdf3234", "0xfdfd"]);

    console.log(res);
    > 0x00000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000002df32340000000000000000000000000000000000000000000000000000000000fdfd000000000000000000000000000000000000000000000000000000000000

    const res = web3.eth.abi.encodeParameter(
    {
    ParentStruct: {
    propertyOne: "uint256",
    propertyTwo: "uint256",
    childStruct: {
    propertyOne: "uint256",
    propertyTwo: "uint256",
    },
    },
    },
    {
    propertyOne: 42,
    propertyTwo: 56,
    childStruct: {
    propertyOne: 45,
    propertyTwo: 78,
    },
    }
    );

    console.log(res);
    > 0x000000000000000000000000000000000000000000000000000000000000002a0000000000000000000000000000000000000000000000000000000000000038000000000000000000000000000000000000000000000000000000000000002d000000000000000000000000000000000000000000000000000000000000004e

    Parameters

    Returns string

    • The ABI encoded parameter