以太坊開發(fā)(二十七)在私鏈中使用JSON RPC API進(jìn)行以太幣/代幣轉(zhuǎn)賬

1. 前言

前面我們使用過(guò)web3.js進(jìn)行過(guò)以太幣/代幣轉(zhuǎn)賬,這次我們使用以太坊提供的JSON RPC API進(jìn)行以太幣/代幣轉(zhuǎn)賬。

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

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

之前使用web3.js的時(shí)候,我們使用的是第三方節(jié)點(diǎn),不需要自己建立節(jié)點(diǎn)和同步所有區(qū)塊(以太坊開發(fā)(二十三)使用Web3.js查詢以太幣和代幣余額以及轉(zhuǎn)賬)。

但如果是在主網(wǎng)使用JSON RPC API,需要建立自己的節(jié)點(diǎn)并同步所有主網(wǎng)區(qū)塊。原因是自己的節(jié)點(diǎn)可以使用錢包服務(wù),第三方節(jié)點(diǎn)沒有提供錢包服務(wù),就算有,也不敢輕易把私鑰傳過(guò)去吧。

這里為了測(cè)試,我們建立一個(gè)私鏈并啟動(dòng)一個(gè)節(jié)點(diǎn)。關(guān)于建立私鏈和啟動(dòng)節(jié)點(diǎn),可以查看這篇文章以太坊開發(fā)(三)使用 Go-Ethereum 1.8.1搭建以太坊私有鏈。

2. 啟動(dòng)節(jié)點(diǎn)

這里啟動(dòng)節(jié)點(diǎn)和之前不太一樣,主要是需要為節(jié)點(diǎn)開啟RPC通道。

對(duì)比一下區(qū)別:

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
參數(shù)名稱 參數(shù)描述
datadir 設(shè)置當(dāng)前區(qū)塊鏈網(wǎng)絡(luò)數(shù)據(jù)存放的位置
nodiscover 私有鏈地址,不會(huì)被網(wǎng)上看到
console 啟動(dòng)命令行模式,可以在Geth中執(zhí)行命令
identity 區(qū)塊鏈的標(biāo)示,用于標(biāo)示目前網(wǎng)絡(luò)的名字
rpc 開啟rpc通道
rpcapi 要開放哪些rpc api
rpccorsdomain 允許能連接到你的節(jié)點(diǎn)執(zhí)行rpc api的url,使用逗號(hào)分隔。*表示任何url都可以連接
rpcaddr HTTP-RPC服務(wù)器接口地址,默認(rèn)為localhost
rpcport HTTP-RPC服務(wù)器端口地址,默認(rèn)為8545
networkid 網(wǎng)絡(luò)標(biāo)識(shí),私有鏈取一個(gè)大于4的隨意的值

3. 一些主要的RPC API

可以使用PostMan開啟本地節(jié)點(diǎn)后使用Post調(diào)用

注意Content-Type設(shè)置為application/json

3.1 eth_accounts 獲取本地所有賬號(hào)地址

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:賬號(hào)地址列表

3.2 eth_blockNumber 獲取當(dāng)前最新區(qū)塊號(hào)

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:區(qū)塊號(hào)十六進(jìn)制

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 - 指定區(qū)塊號(hào)的十六進(jìn)制
String "earliest" - 表示最初或創(chuàng)世區(qū)塊號(hào)
String "latest" - 表示最新挖出的區(qū)塊號(hào)
String "pending" - 表示pending狀態(tài)的交易

returns:
{
    "jsonrpc": "2.0",
    "id": 666,
    "result": "0x1042e5fe02ea790864"
}

result:余額十六進(jìn)制

3.4 eth_getBlockTransactionCountByNumber 獲取指定區(qū)塊的交易數(shù)

post
http://0.0.0.0:8545

method:
eth_getBlockTransactionCountByNumber

params:
{"jsonrpc":"2.0","method":"eth_getBlockTransactionCountByNumber","params":["0xa"],"id":666}

0xa:要查詢的區(qū)塊號(hào)十六進(jìn)制

returns:
{
    "jsonrpc": "2.0",
    "id": 666,
    "result": "0x0"
}

result:交易數(shù)十六進(jìn)制

3.5 eth_getBlockByNumber 獲取指定高度的區(qū)塊詳情

post
http://0.0.0.0:8545

method:
eth_getBlockByNumber

params:
{"jsonrpc":"2.0","method":"eth_getBlockByNumber","params":["0x107", true],"id":666}

0x107:要查詢的區(qū)塊號(hào)十六進(jìn)制
true:返回區(qū)塊中所有交易的詳情,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:區(qū)塊詳情

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的交易收據(jù)

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:交易收據(jù)

3.8 eth_gasPrice 獲取當(dāng)前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十六進(jìn)制

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十六進(jìn)制

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}

這里估算的是代幣轉(zhuǎn)賬的gas:
from:轉(zhuǎn)賬方地址
to:這里由于是調(diào)用智能合約,所以是合約地址
data:附加的消息。這里由合約中transfer方法,方法參數(shù)一(接收方地址),方法參數(shù)二(代幣數(shù)量)的十六進(jìn)制組成

returns:
{
    "jsonrpc": "2.0",
    "id": 666,
    "result": "0x9268"
}

result:估算gas十六進(jìn)制

3.11 eth_getTransactionCount 返回指定地址發(fā)生的交易數(shù)量

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 - 指定區(qū)塊號(hào)的十六進(jìn)制
String "earliest" - 表示最初或創(chuàng)世區(qū)塊號(hào)
String "latest" - 表示最新挖出的區(qū)塊號(hào)
String "pending" - 表示pending狀態(tài)的交易

returns:
{
    "jsonrpc": "2.0",
    "id": 666,
    "result": "0x15"
}

result:交易數(shù)量十六進(jìn)制

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 創(chuàng)建賬戶

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 以太幣轉(zhuǎn)賬

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:交易順序十六進(jìn)制。由eth_getTransactionCount獲取
from:轉(zhuǎn)賬方地址
to:接收方地址
gas:燃料十六進(jìn)制。由eth_estimateGas獲取
gasPrice:燃料單價(jià)十六進(jìn)制。由eth_gasPrice獲取
value:以太幣數(shù)量十六進(jìn)制


returns:
{
    "jsonrpc": "2.0",
    "id": 666,
    "result": "0x483660c2b6f2ec0525b8494529287037a696d33704e5ed4e1b46f8a531520e4d"
}

result:交易hash

3.19 token_transfer 代幣轉(zhuǎn)賬

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:交易順序十六進(jìn)制。由eth_getTransactionCount獲取
from:轉(zhuǎn)賬方地址
to:代幣合約地址
gas:燃料十六進(jìn)制。由eth_estimateGas獲取
gasPrice:燃料單價(jià)十六進(jìn)制。由eth_gasPrice獲取
value:由于是發(fā)送代幣,這里為0
data:附加的消息。這里由合約中transfer方法,方法參數(shù)一(接收方地址),方法參數(shù)二(代幣數(shù)量)的十六進(jìn)制組成

returns:
{
    "jsonrpc": "2.0",
    "id": 666,
    "result": "0x2e6e02d3cf48f03a78995dd239e07cbda291ae2269b4a01ae5794f511cc6424d"
}

result:交易hash

3.20 token_ decimal 獲取代幣小數(shù)位

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:要調(diào)用的方法名decimals的十六進(jìn)制

HEX String - 指定區(qū)塊號(hào)的十六進(jìn)制
String "earliest" - 表示最初或創(chuàng)世區(qū)塊號(hào)
String "latest" - 表示最新挖出的區(qū)塊號(hào)
String "pending" - 表示pending狀態(tài)的交易

returns:
{
    "jsonrpc": "2.0",
    "id": 666,
    "result": "0x0000000000000000000000000000000000000000000000000000000000000012"
}

result:代幣小數(shù)位十六進(jìn)制

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:要調(diào)用的方法名balanceOf和指定地址的十六進(jìn)制

HEX String - 指定區(qū)塊號(hào)的十六進(jìn)制
String "earliest" - 表示最初或創(chuàng)世區(qū)塊號(hào)
String "latest" - 表示最新挖出的區(qū)塊號(hào)
String "pending" - 表示pending狀態(tài)的交易

returns:
{
    "jsonrpc": "2.0",
    "id": 666,
    "result": "0x000000000000000000000000000000000000000000000015af1d78b58c400000"
}

result:代幣余額十六進(jìn)制

3.22 personal_importRawKey 通過(guò)私鑰和密碼導(dǎo)入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. 相關(guān)工具

參數(shù)及返回值多數(shù)為十六進(jìn)制的值,可以使用進(jìn)制轉(zhuǎn)換工具快速轉(zhuǎn)換為十進(jìn)制的值查看進(jìn)制轉(zhuǎn)換。

如果代幣的小數(shù)位與以太幣一樣,都是18個(gè)0,可以使用Ethereum unit converter快速轉(zhuǎn)換。

5. 以太幣轉(zhuǎn)賬

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交易順序十六進(jìn)制。由eth_getTransactionCount獲取
  • from轉(zhuǎn)賬方地址
  • to接收方地址
  • gas燃料十六進(jìn)制。由eth_estimateGas獲取
  • gasPrice燃料單價(jià)十六進(jìn)制。由eth_gasPrice獲取
  • value以太幣數(shù)量十六進(jìn)制
  1. 首先,保證轉(zhuǎn)賬方有足夠的以太幣支付要轉(zhuǎn)賬的以太幣以及手續(xù)費(fèi)

  2. 調(diào)用personal_unlockAccount解鎖轉(zhuǎn)賬方賬戶

  3. 調(diào)用上面的方法轉(zhuǎn)賬,獲取到交易hash

  4. 在私鏈上,調(diào)用miner_start開啟挖礦

  5. 調(diào)用eth_getTransactionByHasheth_getTransactionReceipt查詢是否交易打包入?yún)^(qū)塊并得到確認(rèn)

  6. 調(diào)用eth_getBalance查詢是否到賬

6. 代幣轉(zhuǎn)賬

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交易順序十六進(jìn)制。由eth_getTransactionCount獲取
  • from轉(zhuǎn)賬方地址
  • to代幣合約地址
  • gas燃料十六進(jìn)制。由eth_estimateGas獲取
  • gasPrice燃料單價(jià)十六進(jìn)制。由eth_gasPrice獲取
  • value由于是發(fā)送代幣,這里為0
  • data附加的消息。這里由合約中transfer方法,方法參數(shù)一(接收方地址),方法參數(shù)二(代幣數(shù)量)的十六進(jìn)制組成
  1. 首先需要將ERC20標(biāo)準(zhǔn)的代幣合約部署在私鏈上。部署方法見以太坊開發(fā)(五)使用 Browser-solidity 在 Go-Ethereum1.8.1 上進(jìn)行簡(jiǎn)單的智能合約部署

  2. 保證轉(zhuǎn)賬方有足夠的以太幣支付手續(xù)費(fèi),有足夠的代幣轉(zhuǎn)賬

  3. 調(diào)用personal_unlockAccount解鎖轉(zhuǎn)賬方賬戶

  4. 調(diào)用上面的方法進(jìn)行代幣轉(zhuǎn)賬。data的拼接方法見以太坊開發(fā)(二十三)使用Web3.js查詢以太幣和代幣余額以及轉(zhuǎn)賬

  5. 在私鏈上,調(diào)用miner_start開啟挖礦

  6. 調(diào)用eth_getTransactionByHasheth_getTransactionReceipt查詢是否交易打包入?yún)^(qū)塊并得到確認(rèn)

  7. 調(diào)用token_balanceOf查詢是否到賬

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容