美文网首页
以太坊开发(二十七)在私链中使用JSON RPC API进行以太

以太坊开发(二十七)在私链中使用JSON RPC API进行以太

作者: yuyangray | 来源:发表于2018-10-11 16:20 被阅读1462次

    1. 前言

    前面我们使用过web3.js进行过以太币/代币转账,这次我们使用以太坊提供的JSON RPC API进行以太币/代币转账。

    官方文档:https://ethereum.gitbooks.io/frontier-guide/content/rpc.html

    中文版:http://cw.hubwiz.com/card/c/ethereum-json-rpc-api/

    之前使用web3.js的时候,我们使用的是第三方节点,不需要自己建立节点和同步所有区块(以太坊开发(二十三)使用Web3.js查询以太币和代币余额以及转账)。

    但如果是在主网使用JSON RPC API,需要建立自己的节点并同步所有主网区块。原因是自己的节点可以使用钱包服务,第三方节点没有提供钱包服务,就算有,也不敢轻易把私钥传过去吧。

    这里为了测试,我们建立一个私链并启动一个节点。关于建立私链和启动节点,可以查看这篇文章以太坊开发(三)使用 Go-Ethereum 1.8.1搭建以太坊私有链

    2. 启动节点

    这里启动节点和之前不太一样,主要是需要为节点开启RPC通道。

    对比一下区别:

    geth  --datadir "./chain"  --nodiscover  console
    
    geth --identity "rpc etherum" --datadir "./chain"  --nodiscover --rpc --rpcapi "web3,eth,personal,miner" --rpccorsdomain "*"  --rpcaddr 0.0.0.0 --rpcport 8545 --networkid 666 console
    
    参数名称 参数描述
    datadir 设置当前区块链网络数据存放的位置
    nodiscover 私有链地址,不会被网上看到
    console 启动命令行模式,可以在Geth中执行命令
    identity 区块链的标示,用于标示目前网络的名字
    rpc 开启rpc通道
    rpcapi 要开放哪些rpc api
    rpccorsdomain 允许能连接到你的节点执行rpc api的url,使用逗号分隔。*表示任何url都可以连接
    rpcaddr HTTP-RPC服务器接口地址,默认为localhost
    rpcport HTTP-RPC服务器端口地址,默认为8545
    networkid 网络标识,私有链取一个大于4的随意的值

    3. 一些主要的RPC API

    可以使用PostMan开启本地节点后使用Post调用

    注意Content-Type设置为application/json

    3.1 eth_accounts 获取本地所有账号地址

    post
    http://0.0.0.0:8545
    
    method:
    eth_accounts
    
    params:
    {"jsonrpc":"2.0","method":"eth_accounts","params":[],"id":666}
    
    returns:
    {
        "jsonrpc": "2.0",
        "id": 666,
        "result": [
            "0xb6cd75af6594f46374378cf3a7d9cbfc06485994",
            "0x696d69b81c6bdf6d46ddb66ee2175df7f9de7c46",
            "0x04a2dfca5b31be1a5d5a56b6a242e7786b24859d",
            "0x460c6c45f500c63209ae99de0cd1b4b8ba90a680"
        ]
    }
    
    result:账号地址列表
    

    3.2 eth_blockNumber 获取当前最新区块号

    post
    http://0.0.0.0:8545
    
    method:
    eth_blockNumber
    
    params:
    {"jsonrpc":"2.0","method":"eth_blockNumber","params":[],"id":666}
    
    returns:
    {
        "jsonrpc": "2.0",
        "id": 666,
        "result": "0x84"
    }
    
    result:区块号十六进制
    

    3.3 eth_getBalance 获取指定地址的以太币余额

    post
    http://0.0.0.0:8545
    
    method:
    eth_getBalance
    
    params:
    {"jsonrpc":"2.0","method":"eth_getBalance","params":["0x696d69b81c6bdf6d46ddb66ee2175df7f9de7c46", "latest"],"id":666}
    
    0x696d69b81c6bdf6d46ddb66ee2175df7f9de7c46:要查询的地址
    
    HEX String - 指定区块号的十六进制
    String "earliest" - 表示最初或创世区块号
    String "latest" - 表示最新挖出的区块号
    String "pending" - 表示pending状态的交易
    
    returns:
    {
        "jsonrpc": "2.0",
        "id": 666,
        "result": "0x1042e5fe02ea790864"
    }
    
    result:余额十六进制
    

    3.4 eth_getBlockTransactionCountByNumber 获取指定区块的交易数

    post
    http://0.0.0.0:8545
    
    method:
    eth_getBlockTransactionCountByNumber
    
    params:
    {"jsonrpc":"2.0","method":"eth_getBlockTransactionCountByNumber","params":["0xa"],"id":666}
    
    0xa:要查询的区块号十六进制
    
    returns:
    {
        "jsonrpc": "2.0",
        "id": 666,
        "result": "0x0"
    }
    
    result:交易数十六进制
    

    3.5 eth_getBlockByNumber 获取指定高度的区块详情

    post
    http://0.0.0.0:8545
    
    method:
    eth_getBlockByNumber
    
    params:
    {"jsonrpc":"2.0","method":"eth_getBlockByNumber","params":["0x107", true],"id":666}
    
    0x107:要查询的区块号十六进制
    true:返回区块中所有交易的详情,false只返回交易的hash
    
    returns:
    {
        "jsonrpc": "2.0",
        "id": 666,
        "result": {
            "difficulty": "0x20140",
            "extraData": "0xd983010810846765746888676f312e31302e328664617277696e",
            "gasLimit": "0xc5fd78f3",
            "gasUsed": "0x0",
            "hash": "0xf038ea74b0a7173d8f45e7eee2fa89c850ce4bd4d64327f1af00dacf4ab87baa",
            "logsBloom": "0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000",
            "miner": "0xb6cd75af6594f46374378cf3a7d9cbfc06485994",
            "mixHash": "0x2f9588bb48918d5dcad5ec0f953b990bb382dfceedcbe3a3f411fbc31637b5ae",
            "nonce": "0x2fb85c37efce98c1",
            "number": "0x107",
            "parentHash": "0x87f7146316a1f324f42658ed08647b51998e7ea76f192462d89888ff80ab54c8",
            "receiptsRoot": "0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421",
            "sha3Uncles": "0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347",
            "size": "0x21c",
            "stateRoot": "0xb704f1b78ccdc27a7a84636b9611a72a6a7a428a58072a7d019a3e09f7e7bf16",
            "timestamp": "0x5bbabd70",
            "totalDifficulty": "0x215591f",
            "transactions": [],
            "transactionsRoot": "0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421",
            "uncles": []
        }
    }
    
    result:区块详情
    

    3.6 eth_getTransactionByHash 获取指定hash的交易详情

    post
    http://0.0.0.0:8545
    
    method:
    eth_getTransactionByHash
    
    params:
    {"jsonrpc":"2.0","method":"eth_getTransactionByHash","params":["0x9bee05a0167af7a816eac4bc373befa80c794816669b0e421c77b798eeb40b56"],"id":666}
    
    0x9bee05a0167af7a816eac4bc373befa80c794816669b0e421c77b798eeb40b56:要查询的交易hash
    
    returns:
    {
        "jsonrpc": "2.0",
        "id": 666,
        "result": {
            "blockHash": "0x1bb5f1e39d01c871e636398007dbdea51ed2e7c8ae75db5c2d33dc1da5c46fda",
            "blockNumber": "0x1c7",
            "from": "0xb6cd75af6594f46374378cf3a7d9cbfc06485994",
            "gas": "0xea60",
            "gasPrice": "0x430e23400",
            "hash": "0x9bee05a0167af7a816eac4bc373befa80c794816669b0e421c77b798eeb40b56",
            "input": "0xa9059cbb000000000000000000000000696d69b81c6bdf6d46ddb66ee2175df7f9de7c4600000000000000000000000000000000000000000000000ad78ebc5ac6200000",
            "nonce": "0x13",
            "to": "0x60be0313411e34e8e2ec7094b27a291d827d9b9c",
            "transactionIndex": "0x0",
            "value": "0x0",
            "v": "0x38",
            "r": "0x6ddd33756637d17be39ff1f29365ef8add8ab71888e9f0d8825248ccf13df1be",
            "s": "0x27bd77925bc168fccfe4c4b2d3b4d1b26808f6178b9516dc927647abb1a155ba"
        }
    }
    
    result:交易详情
    

    3.7 eth_getTransactionReceipt 获取指定hash的交易收据

    post
    http://0.0.0.0:8545
    
    method:
    eth_getTransactionReceipt
    
    params:
    {"jsonrpc":"2.0","method":"eth_getTransactionReceipt","params":["0xe95867a0315c3d2eac8e8ccb3f8904606458fe69c4ea32a2768a4b29f42b5d1a"],"id":666}
    
    0xe95867a0315c3d2eac8e8ccb3f8904606458fe69c4ea32a2768a4b29f42b5d1a:要查询的交易hash
    
    returns:
    {
        "jsonrpc": "2.0",
        "id": 666,
        "result": {
            "blockHash": "0x7bab534f23833a4f0e51a130ae155d4b23bd2693e43258e78ae61b501a9995e3",
            "blockNumber": "0x1ac",
            "contractAddress": null,
            "cumulativeGasUsed": "0xea60",
            "from": "0xb6cd75af6594f46374378cf3a7d9cbfc06485994",
            "gasUsed": "0xea60",
            "logs": [],
            "logsBloom": "0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000",
            "root": "0xc452521d43903eb577a631f3feac2cfd71e2e7fecacd7b444a1f6595f5334a45",
            "to": "0x60be0313411e34e8e2ec7094b27a291d827d9b9c",
            "transactionHash": "0xe95867a0315c3d2eac8e8ccb3f8904606458fe69c4ea32a2768a4b29f42b5d1a",
            "transactionIndex": "0x0"
        }
    }
    
    result:交易收据
    

    3.8 eth_gasPrice 获取当前gasPrice

    post
    http://0.0.0.0:8545
    
    method:
    eth_gasPrice
    
    params:
    {"jsonrpc":"2.0","method":"eth_gasPrice","params":[],"id":666}
    
    returns:
    {
        "jsonrpc": "2.0",
        "id": 666,
        "result": "0x3b9aca00"
    }
    
    result:gasPrice十六进制
    

    3.9 eth_gasPrice 获取gasPrice

    post
    http://0.0.0.0:8545
    
    method:
    eth_gasPrice
    
    params:
    {"jsonrpc":"2.0","method":"eth_gasPrice","params":[],"id":666}
    
    returns:
    {
        "jsonrpc": "2.0",
        "id": 666,
        "result": "0x3b9aca00"
    }
    
    result:gasPrice十六进制
    

    3.10 eth_estimateGas 估算gas

    post
    http://0.0.0.0:8545
    
    method:
    eth_estimateGas
    
    params:
    {"jsonrpc":"2.0","method":"eth_estimateGas","params":[{
      "from": "0xb6cd75af6594f46374378cf3a7d9cbfc06485994",
      "to": "0x60be0313411e34e8e2ec7094b27a291d827d9b9c",
      "data": "0xa9059cbb000000000000000000000000696d69b81c6bdf6d46ddb66ee2175df7f9de7c4600000000000000000000000000000000000000000000000ad78ebc5ac6200000"
    }],"id":666}
    
    这里估算的是代币转账的gas:
    from:转账方地址
    to:这里由于是调用智能合约,所以是合约地址
    data:附加的消息。这里由合约中transfer方法,方法参数一(接收方地址),方法参数二(代币数量)的十六进制组成
    
    returns:
    {
        "jsonrpc": "2.0",
        "id": 666,
        "result": "0x9268"
    }
    
    result:估算gas十六进制
    

    3.11 eth_getTransactionCount 返回指定地址发生的交易数量

    post
    http://0.0.0.0:8545
    
    method:
    eth_getTransactionCount
    
    params:
    {"jsonrpc":"2.0","method":"eth_getTransactionCount","params":["0xb6cd75af6594f46374378cf3a7d9cbfc06485994","pending"],"id":666}
    
    0xb6cd75af6594f46374378cf3a7d9cbfc06485994:账户地址
    
    HEX String - 指定区块号的十六进制
    String "earliest" - 表示最初或创世区块号
    String "latest" - 表示最新挖出的区块号
    String "pending" - 表示pending状态的交易
    
    returns:
    {
        "jsonrpc": "2.0",
        "id": 666,
        "result": "0x15"
    }
    
    result:交易数量十六进制
    

    3.12 personal_listAccounts 获取所有本地账户地址

    post
    http://0.0.0.0:8545
    
    method:
    personal_listAccounts
    
    params:
    {"jsonrpc":"2.0","method":"personal_listAccounts","params":[],"id":666}
    
    returns:
    {
        "jsonrpc": "2.0",
        "id": 666,
        "result": [
            "0xb6cd75af6594f46374378cf3a7d9cbfc06485994",
            "0x696d69b81c6bdf6d46ddb66ee2175df7f9de7c46",
            "0x04a2dfca5b31be1a5d5a56b6a242e7786b24859d",
            "0x460c6c45f500c63209ae99de0cd1b4b8ba90a680"
        ]
    }
    
    result:账户地址列表
    

    3.13 personal_listWallets 获取所有本地钱包信息

    post
    http://0.0.0.0:8545
    
    method:
    personal_listWallets
    
    params:
    {"jsonrpc":"2.0","method":"personal_listWallets","params":[],"id":666}
    
    returns:
    {
        "jsonrpc": "2.0",
        "id": 666,
        "result": [
            {
                "url": "keystore:///Users/yuyang/Test/chain/keystore/UTC--2018-02-24T13-35-08.661904324Z--b6cd75af6594f46374378cf3a7d9cbfc06485994",
                "status": "Locked",
                "accounts": [
                    {
                        "address": "0xb6cd75af6594f46374378cf3a7d9cbfc06485994",
                        "url": "keystore:///Users/yuyang/Test/chain/keystore/UTC--2018-02-24T13-35-08.661904324Z--b6cd75af6594f46374378cf3a7d9cbfc06485994"
                    }
                ]
            },
            {
                "url": "keystore:///Users/yuyang/Test/chain/keystore/UTC--2018-02-28T11-03-30.804079385Z--696d69b81c6bdf6d46ddb66ee2175df7f9de7c46",
                "status": "Locked",
                "accounts": [
                    {
                        "address": "0x696d69b81c6bdf6d46ddb66ee2175df7f9de7c46",
                        "url": "keystore:///Users/yuyang/Test/chain/keystore/UTC--2018-02-28T11-03-30.804079385Z--696d69b81c6bdf6d46ddb66ee2175df7f9de7c46"
                    }
                ]
            },
            {
                "url": "keystore:///Users/yuyang/Test/chain/keystore/UTC--2018-03-03T03-12-35.273045924Z--04a2dfca5b31be1a5d5a56b6a242e7786b24859d",
                "status": "Locked",
                "accounts": [
                    {
                        "address": "0x04a2dfca5b31be1a5d5a56b6a242e7786b24859d",
                        "url": "keystore:///Users/yuyang/Test/chain/keystore/UTC--2018-03-03T03-12-35.273045924Z--04a2dfca5b31be1a5d5a56b6a242e7786b24859d"
                    }
                ]
            },
            {
                "url": "keystore:///Users/yuyang/Test/chain/keystore/UTC--2018-03-05T08-36-03.391774453Z--460c6c45f500c63209ae99de0cd1b4b8ba90a680",
                "status": "Locked",
                "accounts": [
                    {
                        "address": "0x460c6c45f500c63209ae99de0cd1b4b8ba90a680",
                        "url": "keystore:///Users/yuyang/Test/chain/keystore/UTC--2018-03-05T08-36-03.391774453Z--460c6c45f500c63209ae99de0cd1b4b8ba90a680"
                    }
                ]
            }
        ]
    }
    
    result:钱包列表
    

    3.14 personal_newAccount 创建账户

    post
    http://0.0.0.0:8545
    
    method:
    personal_newAccount
    
    params:
    {"jsonrpc":"2.0","method":"personal_newAccount","params":["123456"],"id":666}
    
    123456:钱包密码
    
    returns:
    {
        "jsonrpc": "2.0",
        "id": 666,
        "result": "0x6f34d26d8e4e7af3c184b170bda70a77ffb70d5e"
    }
    
    result:新账户地址
    

    3.15 personal_unlockAccount 解锁指定账户

    post
    http://0.0.0.0:8545
    
    method:
    personal_unlockAccount
    
    params:
    {"jsonrpc":"2.0","method":"personal_unlockAccount","params":["0xb6cd75af6594f46374378cf3a7d9cbfc06485994", "123456"],"id":666}
    
    0xb6cd75af6594f46374378cf3a7d9cbfc06485994:要解锁的账户地址
    123456:钱包密码
    
    returns:
    {
        "jsonrpc": "2.0",
        "id": 666,
        "result": true
    }
    
    result:true表示解锁成功
    

    3.16 miner_start 开启挖矿

    post
    http://0.0.0.0:8545
    
    method:
    miner_start
    
    params:
    {"jsonrpc":"2.0","method":"miner_start","params":[],"id":666}
    
    returns:
    {
        "jsonrpc": "2.0",
        "id": 666,
        "result": null
    }
    
    

    3.17 miner_stop 停止挖矿

    post
    http://0.0.0.0:8545
    
    method:
    miner_stop
    
    params:
    {"jsonrpc":"2.0","method":"miner_stop","params":[],"id":666}
    
    returns:
    {
        "jsonrpc": "2.0",
        "id": 666,
        "result": null
    }
    
    

    3.18 eth_transfer 以太币转账

    post
    http://0.0.0.0:8545
    
    method:
    eth_sendTransaction
    
    params:
    {"jsonrpc":"2.0","method":"eth_sendTransaction","params":[{
      "nonce":"0x10",
      "from": "0xb6cd75af6594f46374378cf3a7d9cbfc06485994",
      "to": "0x696d69b81c6bdf6d46ddb66ee2175df7f9de7c46",
      "gas": "0x5208", 
      "gasPrice": "0x3b9aca00", 
      "value": "0x56bc75e2d63100000", 
      "data": ""
    }],"id":666}
    
    nonce:交易顺序十六进制。由eth_getTransactionCount获取
    from:转账方地址
    to:接收方地址
    gas:燃料十六进制。由eth_estimateGas获取
    gasPrice:燃料单价十六进制。由eth_gasPrice获取
    value:以太币数量十六进制
    
    
    returns:
    {
        "jsonrpc": "2.0",
        "id": 666,
        "result": "0x483660c2b6f2ec0525b8494529287037a696d33704e5ed4e1b46f8a531520e4d"
    }
    
    result:交易hash
    

    3.19 token_transfer 代币转账

    post
    http://0.0.0.0:8545
    
    method:
    eth_sendTransaction
    
    params:
    {"jsonrpc":"2.0","method":"eth_sendTransaction","params":[{
      "nonce":"0x15",
      "from": "0xb6cd75af6594f46374378cf3a7d9cbfc06485994",
      "to": "0x60be0313411e34e8e2ec7094b27a291d827d9b9c",
      "gas": "0xea60", 
      "gasPrice": "0x3b9aca00", 
      "value": "0x0", 
      "data": "0xa9059cbb000000000000000000000000696d69b81c6bdf6d46ddb66ee2175df7f9de7c4600000000000000000000000000000000000000000000000ad78ebc5ac6200000"
    }],"id":666}
    
    nonce:交易顺序十六进制。由eth_getTransactionCount获取
    from:转账方地址
    to:代币合约地址
    gas:燃料十六进制。由eth_estimateGas获取
    gasPrice:燃料单价十六进制。由eth_gasPrice获取
    value:由于是发送代币,这里为0
    data:附加的消息。这里由合约中transfer方法,方法参数一(接收方地址),方法参数二(代币数量)的十六进制组成
    
    returns:
    {
        "jsonrpc": "2.0",
        "id": 666,
        "result": "0x2e6e02d3cf48f03a78995dd239e07cbda291ae2269b4a01ae5794f511cc6424d"
    }
    
    result:交易hash
    

    3.20 token_ decimal 获取代币小数位

    post
    http://0.0.0.0:8545
    
    method:
    eth_call
    
    params:
    {"jsonrpc":"2.0","method":"eth_call","params":[{
      "to": "0x60be0313411e34e8e2ec7094b27a291d827d9B9c",
      "data": "0x313ce567"
    },"latest"],"id":666}
    
    to:代币合约地址
    data:要调用的方法名decimals的十六进制
    
    HEX String - 指定区块号的十六进制
    String "earliest" - 表示最初或创世区块号
    String "latest" - 表示最新挖出的区块号
    String "pending" - 表示pending状态的交易
    
    returns:
    {
        "jsonrpc": "2.0",
        "id": 666,
        "result": "0x0000000000000000000000000000000000000000000000000000000000000012"
    }
    
    result:代币小数位十六进制
    

    3.21 token_balanceOf 获取指定地址代币余额

    post
    http://0.0.0.0:8545
    
    method:
    eth_call
    
    params:
    {"jsonrpc":"2.0","method":"eth_call","params":[{
      "to": "0x60be0313411e34e8e2ec7094b27a291d827d9B9c",
      "data": "0x70a08231000000000000000000000000696d69b81c6bdf6d46ddb66ee2175df7f9de7c46"
    },"latest"],"id":666}
    
    to:代币合约地址
    data:要调用的方法名balanceOf和指定地址的十六进制
    
    HEX String - 指定区块号的十六进制
    String "earliest" - 表示最初或创世区块号
    String "latest" - 表示最新挖出的区块号
    String "pending" - 表示pending状态的交易
    
    returns:
    {
        "jsonrpc": "2.0",
        "id": 666,
        "result": "0x000000000000000000000000000000000000000000000015af1d78b58c400000"
    }
    
    result:代币余额十六进制
    

    3.22 personal_importRawKey 通过私钥和密码导入keystore文件

    post
    http://0.0.0.0:8545
    
    method:
    personal_importRawKey
    
    params:
    {"jsonrpc":"2.0","method":"personal_importRawKey","params":["6059654cc18c2f33a5a42043d44e067daf5017433b9801f376ee4e5ba71f942e", "123456"],"id":666}
    
    6059654cc18c2f33a5a42043d44e067daf5017433b9801f376ee4e5ba71f942e:私钥
    123456:密码
    
    returns:
    {
        "jsonrpc": "2.0",
        "id": 666,
        "result": "0x3e48e2658b45050df76c9b09607072df5acf10c3"
    }
    
    result:账户地址
    

    4. 相关工具

    参数及返回值多数为十六进制的值,可以使用进制转换工具快速转换为十进制的值查看进制转换

    如果代币的小数位与以太币一样,都是18个0,可以使用Ethereum unit converter快速转换。

    5. 以太币转账

    post
    http://0.0.0.0:8545
    
    method:
    eth_sendTransaction
    
    params:
    {"jsonrpc":"2.0","method":"eth_sendTransaction","params":[{
      "nonce":"0x10",
      "from": "0xb6cd75af6594f46374378cf3a7d9cbfc06485994",
      "to": "0x696d69b81c6bdf6d46ddb66ee2175df7f9de7c46",
      "gas": "0x5208", 
      "gasPrice": "0x3b9aca00", 
      "value": "0x56bc75e2d63100000", 
      "data": ""
    }],"id":666}
    
    returns:
    {
        "jsonrpc": "2.0",
        "id": 666,
        "result": "0x483660c2b6f2ec0525b8494529287037a696d33704e5ed4e1b46f8a531520e4d"
    }
    
    result:交易hash
    
    • nonce交易顺序十六进制。由eth_getTransactionCount获取
    • from转账方地址
    • to接收方地址
    • gas燃料十六进制。由eth_estimateGas获取
    • gasPrice燃料单价十六进制。由eth_gasPrice获取
    • value以太币数量十六进制
    1. 首先,保证转账方有足够的以太币支付要转账的以太币以及手续费

    2. 调用personal_unlockAccount解锁转账方账户

    3. 调用上面的方法转账,获取到交易hash

    4. 在私链上,调用miner_start开启挖矿

    5. 调用eth_getTransactionByHasheth_getTransactionReceipt查询是否交易打包入区块并得到确认

    6. 调用eth_getBalance查询是否到账

    6. 代币转账

    post
    http://0.0.0.0:8545
    
    method:
    eth_sendTransaction
    
    params:
    {"jsonrpc":"2.0","method":"eth_sendTransaction","params":[{
      "nonce":"0x15",
      "from": "0xb6cd75af6594f46374378cf3a7d9cbfc06485994",
      "to": "0x60be0313411e34e8e2ec7094b27a291d827d9b9c",
      "gas": "0xea60", 
      "gasPrice": "0x3b9aca00", 
      "value": "0x0", 
      "data": "0xa9059cbb000000000000000000000000696d69b81c6bdf6d46ddb66ee2175df7f9de7c4600000000000000000000000000000000000000000000000ad78ebc5ac6200000"
    }],"id":666}
    
    returns:
    {
        "jsonrpc": "2.0",
        "id": 666,
        "result": "0x2e6e02d3cf48f03a78995dd239e07cbda291ae2269b4a01ae5794f511cc6424d"
    }
    
    result:交易hash
    
    • nonce交易顺序十六进制。由eth_getTransactionCount获取
    • from转账方地址
    • to代币合约地址
    • gas燃料十六进制。由eth_estimateGas获取
    • gasPrice燃料单价十六进制。由eth_gasPrice获取
    • value由于是发送代币,这里为0
    • data附加的消息。这里由合约中transfer方法,方法参数一(接收方地址),方法参数二(代币数量)的十六进制组成
    1. 首先需要将ERC20标准的代币合约部署在私链上。部署方法见以太坊开发(五)使用 Browser-solidity 在 Go-Ethereum1.8.1 上进行简单的智能合约部署

    2. 保证转账方有足够的以太币支付手续费,有足够的代币转账

    3. 调用personal_unlockAccount解锁转账方账户

    4. 调用上面的方法进行代币转账。data的拼接方法见以太坊开发(二十三)使用Web3.js查询以太币和代币余额以及转账

    5. 在私链上,调用miner_start开启挖矿

    6. 调用eth_getTransactionByHasheth_getTransactionReceipt查询是否交易打包入区块并得到确认

    7. 调用token_balanceOf查询是否到账

    相关文章

      网友评论

          本文标题:以太坊开发(二十七)在私链中使用JSON RPC API进行以太

          本文链接:https://www.haomeiwen.com/subject/geipaftx.html