如何获取合约创建者的地址

How to get the address of the contract creator

我想知道如果我只知道合约地址和合约接口(ABI),是否有办法获取合约创建者的地址?

没有明确的web3.js方法来查找合约创建者地址。如果你想用 web3.js 来完成这个,你基本上必须遍历所有以前的块和交易,然后通过 web3.eth.getTransactionReceipt 搜索交易收据。这将 return 一个 contractAddress 属性 可以与您拥有的合同地址进行比较。

这是一个利用 web3.js (v1.0.0-beta.37) 的示例:

const contractAddress = '0x61a54d8f8a8ec8bf2ae3436ad915034a5b223f5a';

async function getContractCreatorAddress() {
    let currentBlockNum = await web3.eth.getBlockNumber();
    let txFound = false;

    while(currentBlockNum >= 0 && !txFound) {
        const block = await web3.eth.getBlock(currentBlockNum, true);
        const transactions = block.transactions;

        for(let j = 0; j < transactions.length; j++) {
            // We know this is a Contract deployment
            if(!transactions[j].to) {
                const receipt = await web3.eth.getTransactionReceipt(transactions[j].hash);
                if(receipt.contractAddress && receipt.contractAddress.toLowerCase() === contractAddress.toLowerCase()) {
                    txFound = true;
                    console.log(`Contract Creator Address: ${transactions[j].from}`);
                    break;
                }
            }
        }

        currentBlockNum--;
    }
}

getContractCreatorAddress();