TransWikia.com

How to calculate CREATE2 address in golang?

Ethereum Asked by navigante on November 22, 2021

I have a factory pattern smart contract that creates other smart-contracts by cloning the master smart contract via Minimal Proxy Contract and CREATE2 opcode. What is the proper way to calculate CREATE2 final address in golang?

One Answer

go-ethereum have a function for this https://pkg.go.dev/github.com/ethereum/go-ethereum/crypto?tab=doc#CreateAddress2

func CreateAddress2(b common.Address, salt [32]byte, inithash []byte) common.Address

Where b is the factory, salt the salt used, inithash is your init data hash.

You can calculate inithash like that

func getInithash(contractABI, contractBin string, params ...interface{}) []byte {
    parsed, _ := abi.JSON(strings.NewReader(contractABI))
    packedArguments, _ := parsed.Pack("", params...) // constructor params

    initData := append(common.FromHex(contractBin), packedArguments...)
    return crypto.Keccak256(initData)
}

Answered by Tclairet on November 22, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP