Hot answers tagged

101

EDIT April 2019: CREATE2 information added. 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....


13

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 ...


9

Here's a node.js script that deterministically computes an Ethereum contract address given the contract creator's public address and nonce value. Let me know if anyone has questions about inputs, etc. //node version: v9.10.0 //module versions: //rlp@2.0.0 //keccak@1.4.0 const rlp = require('rlp'); const keccak = require('keccak'); var nonce = 0x00; //The ...


8

CREATE, in a way, does a built in CALL. What actually happens is that the data passed to call isn't the contract bytecode, it's the init bytecode. When CREATE opcode is executed, the EVM creates a call frame in the context of the new contract (e.g. address(this) is the new contracts address). This executes the data passed to CREATE as the code, which in ...


6

This worked for CREATE2. Maybe it also works on CREATE? abi.encodePacked(bytecode, abi.encode(arg1, arg2)) Example (CREATE2) pragma solidity ^0.5.11; contract Wallet { address public owner; uint public foo; constructor(address _owner, uint _foo) public { owner = _owner; foo = _foo; } } contract Factory { event ...


5

Here is updated Python version for modern Ethereum libraries (eth-utils): import rlp from eth_utils import keccak, to_checksum_address, to_bytes def mk_contract_address(sender: str, nonce: int) -> str: """Create a contract address using eth-utils. # https://ethereum.stackexchange.com/a/761/620 """ ...


3

You need to abi-encode the input arguments, and then append the result at the end of the bytecode before passing it to the create function. Suppose you have input arguments arg1, arg2 and arg3. I believe that you can more or less achieve that by changing this: bytes memory bytecode = _bytecode; To this: bytes memory bytecode = abi.encode(_bytecode, arg1,...


2

RLP done in solidity (did not test this though, beware! just for understanding): function addressFrom(address _origin, uint _nonce) public pure returns (address) { if(_nonce == 0x00) return address(keccak256(byte(0xd6), byte(0x94), _origin, byte(0x80))); if(_nonce <= 0x7f) return address(keccak256(byte(0xd6), byte(0x94), ...


2

This is a wonderful question! The problem you're running into is that in Ethereum the code that you provide to CREATE is executed. We call this code "init code" and it's expected to return the resulting "deployed code". So what you really need is some piece of code that when executed will use the RETURN opcode to return the _bytecode that ...


1

You have to encode arguments with abi.encode and bytes with abi.encodePacked. Compiles with Solidity 0.4.0. contract DeployContract { function deploy(bytes _bytecode) external returns(address addr) { bytes memory bytecode = abi.encodePacked(_bytecode, abi.encode(msg.sender)); assembly { addr := create(0, add(bytecode, 0x20),...


1

I got this to compile with solidity 0.4.18; pragma solidity ^0.4.17; import "https://raw.githubusercontent.com/OpenZeppelin/openzeppelin-solidity/v1.6.0/contracts/token/ERC20/StandardToken.sol"; contract MyToken is StandardToken { string public name = "MyTok"; string public symbol = "MTK"; uint8 public decimals = 2; uint public ...


1

Here is a pure ethers.js implementation in TypeScript, returning a checksummed address. nonce is expected to be a regular number. (ethers.js does have a function called getContractAddress too, but it cannot be used for any nonce) import { ethers } from 'hardhat'; static getContractAddress(address: string, nonce: number): string { const rlp_encoded = ...


Only top voted, non community-wiki answers of a minimum length are eligible