addresses - How is the address of an Ethereum contract computed? - Ethereum Stack Exchange
etherscan - Do ETH have its own contract address in Ethereum Chain? - Ethereum Stack Exchange
erc20 and ETH smart contract addresses
Inconsistent capital letters in Ethereum address while validating transaction
Videos
EDIT Sept 2023: CREATE2 information clarified.
EDIT January 2022: Updated Solidity syntax to ^0.8.0.
The address for an Ethereum contract is deterministically computed from the address of its creator (sender) and how many transactions the creator has sent (nonce). The sender and nonce are RLP encoded and then hashed with Keccak-256.
From pyethereum:
def mk_contract_address(sender, nonce):
return sha3(rlp.encode([normalize_address(sender), nonce]))[12:]
In Solidity:
nonce0= address(uint160(uint256(keccak256(abi.encodePacked(bytes1(0xd6), bytes1(0x94), _origin, bytes1(0x80))))));
nonce1= address(uint160(uint256(keccak256(abi.encodePacked(bytes1(0xd6), bytes1(0x94), _origin, bytes1(0x01))))));
Example with some discussion:
For sender 0x6ac7ea33f8831ea9dcc53393aaa88b25a785dbf0, the contract addresses that it will create are the following:
nonce0= "0xcd234a471b72ba2f1ccf0a70fcaba648a5eecd8d"
nonce1= "0x343c43a37d37dff08ae8c4a11544c718abb4fcf8"
nonce2= "0xf778b86fa74e846c4f0a1fbd1335fe81c00a0c91"
nonce3= "0xfffd933a0bc612844eaf0c6fe3e5b8e9b6c1d19c"
In Java with Web3j:
private String calculateContractAddress(String address, long nonce){
byte[] addressAsBytes = Numeric.hexStringToByteArray(address);
byte[] calculatedAddressAsBytes =
Hash.sha3(RlpEncoder.encode(
new RlpList(
RlpString.create(addressAsBytes),
RlpString.create((nonce)))));
calculatedAddressAsBytes = Arrays.copyOfRange(calculatedAddressAsBytes,
12, calculatedAddressAsBytes.length);
String calculatedAddressAsHex = Numeric.toHexString(calculatedAddressAsBytes);
return calculatedAddressAsHex;
}
Note: As per EIP 161 A Specification contract accounts are initiated with nonce = 1 (in the mainnet). So the first contract address, created by another contract, will be computed with non-zero nonce.
CREATE2
A new opcode, CREATE2 was added in EIP-1014 that is another way that a contract can be created.
For contract created by CREATE2 its address will be:
keccak256( 0xff ++ address(this) ++ salt ++ keccak256(init_code))[12:]
More information will be added here and for the meantime see EIP-1014.
Thanks to eth's answer, it helped a lot to resolve $2000 issue.
Just solved issue with funds, which were sent in main Ethereum network to address of smart contract, deployed to test Ethereum network. We used same wallet to deploy different smart contract in main Ethereum network several times until transaction field nonce achieved the same value 13, as were used to deploy on test network. We called special method of freshly deployed smart contract to reclaim funds. So smart contract was deployed after it was really funded: https://etherscan.io/address/0x9c86825280b1d6c7dB043D4CC86E1549990149f9
Just finished an article about this issue: https://medium.com/@k06a/how-we-sent-eth-to-the-wrong-address-and-successfully-recovered-them-2fc18e09d8f6

hello eveyrone,
I know that in order to get the addresses of every ERC20 token, https://etherscan.io/ and https://tokenlists.org/ are the way to go.
but for ETH? how can I be sure to get all the right intel?
"chainId": 1,"name": "Ethereum","symbol": "ETH","decimals": 18,"address": "0x0000000000000000000000000000000000000000","logoURI": "https://wallet-asset.matic.network/img/tokens/eth.svg"}
I'm building a DeX aggregator using the 0x API and I can't get my hands on ETH (the token) address; can someone provide me a good tokenlist.json file pelase?
using the 0x API I can put the symbol ETH in the sellToken/buyToken param instead of the address but I would rather use addresses instead of symbols