1

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?

2

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)
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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