0

How can I get the creator's address of a contract? I'm using truffle, so usually I do

let instance = await VM.instance()

and then get the contract's address via

instance.address

Then, how can I get the creator? (Setting the owner via a constructor as in an Ownable contract is not an option)

  • 2
    You have to find the transaction which created the contract and then check who sent the transaction. Hopefully someone else can help you with the code for that. – Lauri Peltonen Jan 8 at 9:28
1

This code snippet should work in general, with the only input being the contract address. You can try it out here:

https://shawntabrizi.github.io/ethereum-find-contract-creator/

async function search_contract_cretion_block(contract_address) {
    var highest_block = await web3.eth.getBlockNumber();
    var lowest_block = 0;

    var contract_code = await web3.eth.getCode(contract_address, highest_block);
    if (contract_code == "0x") {
        console.error("Contract " + contract_address + " does not exist!");
        return -1;
    }

    while (lowest_block <= highest_block) {
        let search_block = parseInt((lowest_block + highest_block) / 2)
        contract_code = await web3.eth.getCode(contract_address, search_block);

        //console.log(highest_block, lowest_block, search_block, contract_code);

        if (contract_code != "0x") {
            highest_block = search_block;
        } else if (contract_code == "0x") {
            lowest_block = search_block;
        }

        if (highest_block == lowest_block + 1) {
            return highest_block;
        }
    }

}

async function search_contract_creator (contract_address, block) {
    var block = await web3.eth.getBlock(block);

    var transactions = block.transactions;

    for (transaction in transactions) {
        let receipt = await web3.eth.getTransactionReceipt(transactions[transaction]);

        //console.log(receipt);

        if (receipt.contractAddress == contract_address) {
            return receipt.from
        }
    }

    return -1;
}

async function find_contract_creator (contract_address) {
    var block = await search_contract_cretion_block(contract_address);
    var creator = await search_contract_creator(contract_address, block);
    return creator;
}

Basically, we do a binary search to find the first instance of a block which contains the contract, and then we search that block for the transaction which created that contract.

EDIT: It seems like this process does not work if the contract was created by another contract call. Not quite sure what to check for in that case...

0

First of all you'll need the transaction hash of the contract creation transaction. Then you can use ethereum JSON RPC to call the node and get the information of the transaction.

For the node you can use Infura or any other you like

axios.post(NODE,           
      {
        "jsonrpc":"2.0",
        "method":"eth_getTransactionByHash",
        "params":["0x3080a77bb681dd53d6b496120445a6624b0079f944b5c1a3038744596aa2228e"],
        "id":1
      }).then(res => {
           console.log(res.result.from)
 })

The response will be enter image description here OR If using web3js you can use

web3.eth.getTransaction(transactionHash [, callback])

It will return an object

{
    "hash": "0x9fc76417374aa880d4449a1f7f31ec597f00b1f6f3dd2d66f4c9c6c445836d8b",
    "nonce": 2,
    "blockHash": "0xef95f2f1ed3ca60b048b4bf67cde2195961e0bba6f70bcbea9a2c4e133e34b46",
    "blockNumber": 3,
    "transactionIndex": 0,
    "from": "0xa94f5374fce5edbc8e2a8697c15331677e6ebf0b",
    "to": "0x6295ee1b4f6dd65047762f924ecd367c17eabf8f",
    "value": '123450000000000000',
    "gas": 314159,
    "gasPrice": '2000000000000',
    "input": "0x57cb2fc4"
}

from which you can take out the from key

Your Answer

By clicking "Post Your Answer", you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.