2

I am sure that a contract address can be deterministically created using msg.sender and nonce.I read that they used RLPEncoding in python https://ethereum.stackexchange.com/questions/760/how-is-the-address-of-an-ethereum-contract-computed/47083#47083. But I am unsure how to do that in solidity.

function createAddress(address creator, bytes nonce) public returns(address conractAdd) {
//SomeBody help me writing this part                                          
}
ToniyaSundaram
  • 191
  • 3
  • 12

2 Answers2

0

new contract address depends on the current address of the contract and nonce of the contract that you are creating based on shared link. nonce is counter maintained internally by a contract referring to the number of contracts created by it but unfortunately it is not available within the contract. So you cannot create address without nonce.

Even though, contract creating address depends on the parent contract address and the nonce of the contract, there must be other variables that creates the contract address. because, if you want to create two separate contract's address in same contract. contract address will be same and it is likely that nonce of the contracts that you are creating might be same as well, because nonce is essentially just a scalar number. In this case you would be creating same contract addresses.

Yilmaz
  • 35,338
  • 10
  • 157
  • 202
0

python

import rlp
from hexbytes import HexBytes
from web3 import Web3
def computeContractAddress(_sender, _nonce):
    _sender = bytes.fromhex(_sender[2:])
    _address = Web3.keccak(HexBytes(rlp.encode([_sender, _nonce]))).hex()[-40:]
    return Web3.toChecksumAddress(_address)

Solidity

function computeContractAddress(address _origin, uint _nonce) external pure returns (address _address) {
    bytes memory data;
    if(_nonce == 0x00)          data = abi.encodePacked(bytes1(0xd6), bytes1(0x94), _origin, bytes1(0x80));
    else if(_nonce <= 0x7f)     data = abi.encodePacked(bytes1(0xd6), bytes1(0x94), _origin, uint8(_nonce));
    else if(_nonce <= 0xff)     data = abi.encodePacked(bytes1(0xd7), bytes1(0x94), _origin, bytes1(0x81), uint8(_nonce));
    else if(_nonce <= 0xffff)   data = abi.encodePacked(bytes1(0xd8), bytes1(0x94), _origin, bytes1(0x82), uint16(_nonce));
    else if(_nonce <= 0xffffff) data = abi.encodePacked(bytes1(0xd9), bytes1(0x94), _origin, bytes1(0x83), uint24(_nonce));
    else                        data = abi.encodePacked(bytes1(0xda), bytes1(0x94), _origin, bytes1(0x84), uint32(_nonce));
    bytes32 hash = keccak256(data);
    assembly {
        mstore(0, hash)
        _address := mload(0)
    }
}
Snipe Dev
  • 1
  • 1