0

Is it possible to somehow publish a contract at a specific address. I'd like to be able to hardcode some addresses instead of checking to see where it has been published every time I initiate using ganache.

2 Answers 2

2

Blockchain is predictable. So are smart contracts. That is why randomness is never quite possible in the blockchain.

As how EVM calculate the smart contract address, you can do the same and get the same result if you pass the correct arguments.

There is EIP-1014(2018) or CREATE2 introduced in solidity 0.8.0, that can be used to calculate the smart contraact address.

Before solidity 0.8.0, we are used to do it with assembly but we do not need to do that no longer.

Adds a new opcode (CREATE2) at 0xf5, which takes 4 stack arguments: endowment, memory_start, memory_length, salt. Behaves identically to CREATE (0xf0), except using keccak256( 0xff ++ address ++ salt ++ keccak256(init_code))[12:] instead of the usual sender-and-nonce-hash as the address where the contract is initialized at....

You can read more about create2 EIP here.

Instruction by the openzeppelin on how to use CREATE and CREATE2 here.

A sample contract here.

And video example here.

Tell me if it helps!

0
1

Yes, it is possible to publish a contract to a specific address, but you have to calculate that address in advance.

See: How can anyone publish a contract to an address which has been generated in advance?

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.