Skip to main content
Simply example by removing logic that fetch token data
Source Link
const token = IERC20MetadataFactory.connect(l1TokenAddress, provider);

const name = await token.name(); // Crown"Crown";
const symbol = await token.symbol(); // Crown"Crown";
const decimals = await token.decimals(); // 1818;

 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]);
const token = IERC20MetadataFactory.connect(l1TokenAddress, provider);

const name = await token.name(); // Crown
const symbol = await token.symbol(); // Crown
const decimals = await token.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]);
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]);
added 2 characters in body
Source Link

I have a following code that encodes parameters inwith AbiCoder

I have a following code that encodes parameters in AbiCoder

I have a following code that encodes parameters with AbiCoder

Source Link

What is an alternative for ethers.AbiCoder in geth

I have a following code that encodes parameters in AbiCoder

const token = IERC20MetadataFactory.connect(l1TokenAddress, provider);

const name = await token.name(); // Crown
const symbol = await token.symbol(); // Crown
const decimals = await token.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