JSON-RPC远程访问智能合约

423 阅读4分钟

持续创作,加速成长!这是我参与「掘金日新计划 · 10 月更文挑战」的第8天,点击查看活动详情

JSON-RPC远程访问智能合约

如果我们只是用命令行指令来调用智能合约的话,那么将会对最终用户非常的不友好,因此为了提供一个友好的操作界面,因此要学会区块链外应用软件系统连接并存取智能合约。

编写智能合约

为了更方便理解和节省时间,我们只需写一个简单一点的智能合约。那我们就提供一个名为DOaAdd的函数,其传入的数值进行加法运算并返回运算后的合运算结果。

// SPDX-License-Identifier: GPL-3.0
pragma solidity ^0.4.0;

contract Add{
        function doAdd(uint ione,uint itwo)public  pure returns(uint){
            return ione+itwo;
        }
    }

将中间码存储为js文件

使用在线编译工具(我使用的是remix),获取编译后的中间码(点击编译详情),将图片中的代码存储为Add.js文件 image.png

  • 点击复制的代码为:
var addContract = new web3.eth.Contract([{"constant":true,"inputs":[{"name":"ione","type":"uint256"},{"name":"itwo","type":"uint256"}],"name":"doAdd","outputs":[{"name":"","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"}]);
var add = addContract.deploy({
     data: '0x608060405234801561001057600080fd5b5060c58061001f6000396000f300608060405260043610603f576000357c0100000000000000000000000000000000000000000000000000000000900463ffffffff168063dcc721d2146044575b600080fd5b348015604f57600080fd5b5060766004803603810190808035906020019092919080359060200190929190505050608c565b6040518082815260200191505060405180910390f35b60008183019050929150505600a165627a7a72305820087d955406273e2a240afb1c18b846b602168e75ec0896b096d9d890ff1494870029', 
     arguments: [
     ]
}).send({
     from: web3.eth.accounts[0], 
     gas: '4700000'
   }, function (e, contract){
    console.log(e, contract);
    if (typeof contract.address !== 'undefined') {
         console.log('Contract mined! address: ' + contract.address + ' transactionHash: ' + contract.transactionHash);
    }
 })

获取的ABI存储为Add.abi文件

  • 点击ABI即复制,然后存储为Add.abi文件 image.png
  • 复制的ABI代码:
[
	{
		"constant": true,
		"inputs": [
			{
				"name": "ione",
				"type": "uint256"
			},
			{
				"name": "itwo",
				"type": "uint256"
			}
		],
		"name": "doAdd",
		"outputs": [
			{
				"name": "",
				"type": "uint256"
			}
		],
		"payable": false,
		"stateMutability": "view",
		"type": "function"
	}
]

将编译后的智能合约上链

为了慎重一点,我们可以使用Geth控制台验证智能合约是否能正确执行。

  • 打开私链后启动控制台
geth ipc:\\.\pipe\geth.ipc
  • 部署上链
loadScript("Add.js")

然后得到合约地址:0xd9145CCE52D386f254917e481eB44e9943F39138

  • 再以智能合约的ABI与合约地址作为eth.contract指令的输入值,并以myContract变量接受它的值,这样myContract变量便会指向该合约。
var myContract=eth.contract([
	{
		"constant": true,
		"inputs": [
			{
				"name": "ione",
				"type": "uint256"
			},
			{
				"name": "itwo",
				"type": "uint256"
			}
		],
		"name": "doAdd",
		"outputs": [
			{
				"name": "",
				"type": "uint256"
			}
		],
		"payable": false,
		"stateMutability": "view",
		"type": "function"
	}
]).at("0xd9145CCE52D386f254917e481eB44e9943F39138");

Geth控制台调用合约doAdd函数

输入测试数据3、5,看能否得到8

myContract.doAdd.call<3,5>;

准备开始与区块链外的应用程序进行整合

我们首先需要启动节点的指令

  • rpc
geth --identity "Node1" --networkid 168 --nodiscover --maxpeers 5 --rpc --rpcapi "web3" --rpcport "8080" --datadir "c:\myGeth\node1" --port "30303" --mine --minerthreads=1 --cache=1024 --unlock 0
  • http
geth --allow-insecure-unlock --unlock=0 --password ./password --networkid 999888 --datadir "nodedata" --http --http.api "admin,debug,web3,eth,txpool,personal,ethash,miner,net" --http.corsdomain="*" --http.port=8548 --http.addr="0.0.0.0" --port 30303 --nodiscover console
  • rpc参数:启动jiedian 程序的RPC API功能
  • rpcapi参数:设置所要开放的API种类
  • recport参数:设置RPC API使用的端口号

尝试通过JSON-RPC来与节点程序进行互动

  • 用户可以通过任何发送HTTP请求的工具来进行互动
  • 使用HTTP POST发送虾类JSON内容给私有链的节点程序
  • RPC地址与端口号:http://127.0.0.1:8080
  • JSON内容:
{
    "josnrpc":"2.0",
    "method":"eth_coinbase",
    "params":[],
    "id":64
}

注:启动节点的指令中需要声明启动eth的API,即:在原本的节点启动指令的rpcapi参数中加入新内容:db,eth,net,web3,personal(参考http启动节点指令)