0

I just have a quick question about addresses of contracts.

If I want to make a contract to transfere tokens, how do I get the correct addresses of the token I want?

For Exsample:

function transfer() external {
        address a  = address(MyToken_ERC20());
        MyToken_ERC20 token = MyToken_ERC20(a);
        token.transfer(msg.sender, 100);
    }

If I would deploy "MyToken_ERC20" two times there would be two addresses of the token right? And if yes ... how can I make sure, that I got the right address, only by hardcoding the address into my new contract?

MaTok
  • 321
  • 3
  • 11

1 Answers1

1
  • If I would deploy "MyToken_ERC20" two times there would be two addresses of the token right?

Yes. Each time a contract is deployed, a new address is generated and a new contract, with a different storage than the previous ones, is obtained. For example, if you deploy two times the same erc20 contract, you will obtain two distinct tokens.

  • how can I make sure, that I got the right address, only by hardcoding the address into my new contract?

It is not possible to hardcode the address of the contract as it is generated in a deterministic way (cf How is the address of an Ethereum contract computed?).

There are many ways to get the right address of a new deployed contract. If you use Truffle, the address is indicated in the logs when deploying. In the case of a custom script you could use this web3 event : .on('receipt', function(receipt) { console.log(receipt.contractAddress) // contains the new contract address }) (cf https://web3js.readthedocs.io/en/v1.2.7/web3-eth-contract.html#deploy). Another way is to simply check your transaction record, looking for the transaction responsible for the deployment (on Etherscan for example).

clement
  • 4,302
  • 2
  • 17
  • 35