终端命令行使用JSON-RPC接口调用智能合约

Posted 乞力马扎罗的雪CYF

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了终端命令行使用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"
    ]

 

以上是关于终端命令行使用JSON-RPC接口调用智能合约的主要内容,如果未能解决你的问题,请参考以下文章

如何将 ERC20 代币发送到智能合约余额?

使用 javascript 调用 solidity 智能合约来铸造代币似乎不起作用

rpc接口调用以太坊智能合约

金钱难寐,大盗独行——以太坊 JSON-RPC 接口多种盗币手法大揭秘

使用 javascript 异步函数通过 json-rpc 请求获取 Phantom 钱包余额的问题?

十分钟部署智能合约