0

I have a following code that encodes parameters with AbiCoder

const name = "Crown";
const symbol = "Crown";
const decimals = 18;

 const coder = new AbiCoder();

 const nameBytes = coder.encode(['string'], [name]);
 const symbolBytes = coder.encode(['string'], [symbol]);
 const decimalsBytes = coder.encode(['uint256'], [decimals]);

 return coder.encode(['bytes', 'bytes', 'bytes'], [nameBytes, symbolBytes, decimalsBytes]);

I am trying to achieve same result using geth library. I tried to create dummy ABI definition with 3 parameters in order to use abi.Pack function:

const AbiDefinition = `[
    {
        "constant": true,
        "inputs": [
            {
                "name": "name",
                "type": "string"
            },
            {
                "name": "symbol",
                "type": "string"
            },
            {
                "name": "decimals",
                "type": "uint256"
            }
        ],
        "name": "token",
        "outputs": [
                {
                    "name": "",
                    "type": "bool"
                }
            ],
        "payable": false,
        "stateMutability": "view",
        "type": "constructor"
    }
]`


token, _ := erc20.NewIERC20(l1TokenAddress, backend)

name, _ := token.Name(nil)
symbol, _ := token.Symbol(nil)
decimals, _ := token.Decimals(nil)

bridgeDataAbi, err := abi.JSON(strings.NewReader(AbiDefinition))    
return bridgeDataAbi.Pack("", name, symbol, big.NewInt(int64(decimals)))

The results are no the same and I am having trouble to make them same.

Result from ethers.AbiCoder

000000000000000000000000000000000000000000000000000000000000006000000000000000000000000000000000000000000000000000000000000000e0000000000000000000000000000000000000000000000000000000000000016000000000000000000000000000000000000000000000000000000000000000600000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000543726f776e00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000600000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000543726f776e00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000012

Result from geth:

000000000000000000000000000000000000000000000000000000000000006000000000000000000000000000000000000000000000000000000000000000a00000000000000000000000000000000000000000000000000000000000000012000000000000000000000000000000000000000000000000000000000000000543726f776e000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000543726f776e000000000000000000000000000000000000000000000000000000

1 Answer 1

0

After digging in geth documentation I found out the solution for my problem. Here an example how to encode single type as well as tuple without provided abi:

name := "Crown"
symbol := "Crown"
decimals := 18

stringAbiType, _ := abi.NewType("string", "", nil)
uint256AbiType, _ := abi.NewType("uint256", "", nil)
bytesAbiType, _ := abi.NewType("bytes", "", nil)

nameEncoded, err := abi.Arguments{{Type: stringAbiType}}.Pack(name)
if err != nil {
    return nil, err
}
symbolEncoded, err := abi.Arguments{{Type: stringAbiType}}.Pack(symbol)
if err != nil {
    return nil, err
}
decimalsEncoded, err := abi.Arguments{{Type: uint256AbiType}}.Pack(big.NewInt(int64(decimals)))
if err != nil {
    return nil, err
}

tuple, _ := abi.Arguments{
    {Type: bytesAbiType},
    {Type: bytesAbiType},
    {Type: bytesAbiType},
}.Pack(nameEncoded, symbolEncoded, decimalsEncoded)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.