erc20 and ETH smart contract addresses
etherscan - Do ETH have its own contract address in Ethereum Chain? - Ethereum Stack Exchange
addresses - What is ETH (or the native token of each network) smart contract address? - Ethereum Stack Exchange
addresses - How is the address of an Ethereum contract computed? - Ethereum Stack Exchange
Videos
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
As you say, ETH(or the native token), they are native, and do not need a contract. But there are contracts that wraps native token, Wrapped ETH, Wrapped BTC(on ethereum), Wrapped Matic, with different contract address.
https://polygonscan.com/token/0xdd185af1bb417469461edbc95f22df9781a04624 https://etherscan.io/token/0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2
Your question touches the foundamental of how Ethereum and other blockchains work.I will try my best to explain this.
Basically all blockchains can be seen as distributed ledgers, including Ethereum, the native token is associated with every account.
Modern blockchains support hosting small applications in addresses too, which is called a smart contract.
The token(ERC-20) or an NFT(ERC-721) you are thinking of are smart contracts. You can see them as a ledger in a ledger.
Taking USDT on etherscan as an example:
When you hold USDTs, you don't really hold an actual token(unlike eth) in your address. But instead, your account balance is stored in the USDT smart contract.
To make an USDT transfer, you need to send a message on ethereum; in the message, you specify the target(USDT smart contract) and content(making a transfer to address xxxx). After a validator validates your message, it is then finalised on the Chain. USDT smart contract will be updated with the new account balances. In this process, your account details does not change.
When you transfer Ethers, both of your account and target address will be updated with new balance. Infact native tokens like Ethers are primary used as "fuels/gas fee" for transactions. It won't serve this purpose if it is a smart contract.
Hope it helps.
Wuzhong / Chainstack
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
