Contract-Deployment

通過 rpc 呼叫從 EVM 字節碼部署智能合約

  • November 14, 2018

我目前正在嘗試使用奇偶校驗客戶端的“開發”鏈在私有乙太坊鏈中部署帶有字節碼的智能合約。為此,我編制了以下契約:

pragma solidity ^0.4.11;

contract MyContract {
   uint i = (10 + 2) * 2;
}

堅固:

solc --bin test.sol 

======= test.sol:MyContract =======
Binary: 
   608060405260186000553480156014576     
   00080fd5b5060358060226000396000f3006080604052600080fd00a165627a7a72305820d0cc3f28b74510b6f3f34e1a1e1303c584b355320f387a3e30022117e3554e220029

然後我使用以下 rpc 呼叫來創建事務:

Request: {"method": "personal_sendTransaction", "id": 1, "jsonrpc": "2.0", "params": [{"from": "0x0053b6a9527c2ba72cf44486dd12d9930ce25bae", "data": "0x60806040526018600055348015601457600080fd5b5060358060226000396000f3006080604052600080fd00a165627a7a72305820d0cc3f28b74510b6f3f34e1a1e1303c584b355320f387a3e30022117e3554e220029"}, "test"]}.

where0x0053b6a9527c2ba72cf44486dd12d9930ce25bae是私有鏈上的一個賬戶,它有 ether 和 passphrase test

這是我得到的答案:

Response: {"jsonrpc":"2.0","result":"0xf547c28a3f51c05832fadd4b807f5bb769196970d08e446b2644a2ea94359572","id":1}.

但是給出的字節串result不是一個有效的地址。如何獲取我創建的合約的地址?

您得到的結果是“交易雜湊”(tx),然後您可以將其傳遞給“eth_getTransactionReceipt”呼叫以獲取實際的合約地址。

https://github.com/ethereum/wiki/wiki/JSON-RPC#eth_gettransactionreceipt

// Example Call / Request 

curl -X POST --data'{"jsonrpc":"2.0","method":"eth_getTransactionReceipt","params":["0xb903239f8543d04b5dc1ba6579132b143087c68db1b2168786408fcbce568238"],"id":1}'

// Result
{
   "id":1,
   "jsonrpc":"2.0",
   "result": {
        transactionHash: '0xb903239f8543d04b5dc1ba6579132b143087c68db1b2168786408fcbce568238',
        transactionIndex:  '0x1', // 1
        blockNumber: '0xb', // 11
        blockHash: '0xc6ef2fc5426d6ad6fd9e2a26abeab0aa2411b7ab17f30a99d3cb96aed1d1055b',
        cumulativeGasUsed: '0x33bc', // 13244
        gasUsed: '0x4dc', // 1244
        contractAddress: '0xb60e8dd61c5d32be8058bb8eb970870f07233155', // or null, if none was created
        logs: [{
            // logs as returned by getFilterLogs, etc.
        }, ...],
        logsBloom: "0x00...0", // 256 byte bloom filter
        status: '0x1'
   }
}

引用自:https://ethereum.stackexchange.com/questions/62350