1. 程式人生 > >終端命令列使用JSON-RPC介面呼叫智慧合約

終端命令列使用JSON-RPC介面呼叫智慧合約

(0)合約

contract Multiply7 {

   event Print(uint);

   function multiply(uint input) returns (uint) {

      Print(input * 7);

      return input * 7;

   }

}

(1)編譯合約

curl --data '{"jsonrpc":"2.0","method": "eth_compileSolidity", "params": ["contract Multiply7 {event Print(uint);function multiply(uint input) returns (uint) {Print(input * 7);return input * 7;}}"], "id": 5}' localhost:8545

結果:

{
    "jsonrpc":"2.0",
    "id":5,
    "result":{
        "Multiply7":{
            "code":"0x606060405260788060106000396000f3606060405260e060020a6000350463c6888fa18114601c575b6002565b3460025760666004356040805160078302815290516000917f24abdb5865df5079dcc5ac590ff6f01d5c16edbc5fab4e195d9febd1114503da919081900360200190a15060070290565b60408051918252519081900360200190f3",
            "info":{
                "source":"contract Multiply7 {event Print(uint);function multiply(uint input) returns (uint) {Print(input * 7);return input * 7;}}",
                "language":"Solidity",
                "languageVersion":"0.4.4",
                "compilerVersion":"0.4.4",
                "compilerOptions":"--combined-json bin,abi,userdoc,devdoc --add-std --optimize",
                "abiDefinition":[
                    {
                        "constant":false,
                        "inputs":[
                            {
                                "name":"input",
                                "type":"uint256"
                            }
                        ],
                        "name":"multiply",
                        "outputs":[
                            {
                                "name":"",
                                "type":"uint256"
                            }
                        ],
                        "payable":false,
                        "type":"function"
                    },
                    {
                        "anonymous":false,
                        "inputs":[
                            {
                                "indexed":false,
                                "name":"",
                                "type":"uint256"
                            }
                        ],
                        "name":"Print",
                        "type":"event"
                    }
                ],
                "userDoc":{
                    "methods":{

                    }
                },
                "developerDoc":{
                    "methods":{

                    }
                }
            }
        }
    }
}

(2)部署合約

curl --data '{"jsonrpc":"2.0","method": "eth_sendTransaction", "params": [{"from": "0x90c2323cdeff75fd82e65ac496fc45eafadf4563", "gas": "0xb8a9", "data": "0x6060604052605f8060106000396000f3606060405260e060020a6000350463c6888fa18114601a575b005b60586004356007810260609081526000907f24abdb5865df5079dcc5ac590ff6f01d5c16edbc5fab4e195d9febd1114503da90602090a15060070290565b5060206060f3"}], "id": 6}' localhost:8545

結果:

{
    "jsonrpc":"2.0",
    "id":6,
    "result":"0x2d3dbda9621c3cf08faf257cfae698e36a0ec7ec5d9097761680044c8aba487d"
}

(3)呼叫合約方法

curl --data '{"jsonrpc":"2.0","method": "eth_sendTransaction", "params": [{"from": "0x90c2323cdeff75fd82e65ac496fc45eafadf4563", "to": "0xf07dbae07d57f6c6a4438a39d7379e777d7f5dc5", "data": "0xc6888fa10000000000000000000000000000000000000000000000000000000000000006"}], "id": 8}' localhost:8545

結果:

{
    "jsonrpc":"2.0",
    "id":8,
    "result":"0x1bfadcc087a034c156dc8362eec9bf9d437658e14a4190143a066cd45bc78f8c"
}

(4)根據交易hash查詢詳情

curl --data '{"jsonrpc":"2.0","method":"eth_getTransactionReceipt","params":["0x1bfadcc087a034c156dc8362eec9bf9d437658e14a4190143a066cd45bc78f8c"],"id":1}' localhost:8545

結果:

{

    "jsonrpc":"2.0",

    "id":1,

    "result":{

        "blockHash":"0x2f3b0ff8c38184e1570c83d9906735ea0e02dcfbda7dfde42ba2e2d2b6f679c3",

        "blockNumber":"0x21d8",

        "contractAddress":null,

        "cumulativeGasUsed":"0x5867",

        "from":"0x90c2323cdeff75fd82e65ac496fc45eafadf4563",

        "gasUsed":"0x5867",

        "logs":[

            {

                "address":"0xf07dbae07d57f6c6a4438a39d7379e777d7f5dc5",

                "topics":[

                    "0x24abdb5865df5079dcc5ac590ff6f01d5c16edbc5fab4e195d9febd1114503da"

                ],

                "data":"0x000000000000000000000000000000000000000000000000000000000000002a",

                "blockNumber":"0x21d8",

                "transactionIndex":"0x0",

                "transactionHash":"0x1bfadcc087a034c156dc8362eec9bf9d437658e14a4190143a066cd45bc78f8c",

                "blockHash":"0x2f3b0ff8c38184e1570c83d9906735ea0e02dcfbda7dfde42ba2e2d2b6f679c3",

                "logIndex":"0x0"

            }

        ],

        "logsBloom":"0x00000000000000000000010000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000020000000000000000000200000000000000000000000002000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000",

        "root":"0x8815864cc69c11dee4f95c946cecd4762bd25092d62281f48a4b0d16007bc959",

        "to":"0xf07dbae07d57f6c6a4438a39d7379e777d7f5dc5",

        "transactionHash":"0x1bfadcc087a034c156dc8362eec9bf9d437658e14a4190143a066cd45bc78f8c",

        "transactionIndex":"0x0"

    }

}

(5)檢視挖礦基地址:

curl --data '{"jsonrpc":"2.0","method":"eth_coinbase", "id":1}' localhost:8545

結果:

{
    "jsonrpc":"2.0",
    "id":1,
    "result":"0x90c2323cdeff75fd82e65ac496fc45eafadf4563"
}

(6)檢視賬戶餘額

curl --data '{"jsonrpc":"2.0","method":"eth_getBalance", "params": ["0x90c2323cdeff75fd82e65ac496fc45eafadf4563", "latest"], "id":2}' localhost:8545

結果:

{
    "jsonrpc":"2.0",
    "id":2,
    "result":"0x27b41fd348ff2fc32d0"
}

(7)獲得編譯器

curl --data '{"jsonrpc":"2.0","method": "eth_getCompilers", "id": 3}' localhost:8545

結果:

{
    "jsonrpc":"2.0",
    "id":3,
    "result":[
        "Solidity"
    ]
}