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

1. 前言

前面我們使用過web3.js進(jìn)行過以太幣/代幣轉(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ù)秋茫,就算有,也不敢輕易把私鑰傳過去吧乃秀。

這里為了測(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 通過私鑰和密碼導(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)系作者
  • 序言:七十年代末工扎,一起剝皮案震驚了整個(gè)濱河市,隨后出現(xiàn)的幾起案子衔蹲,更是在濱河造成了極大的恐慌肢娘,老刑警劉巖,帶你破解...
    沈念sama閱讀 206,126評(píng)論 6 481
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件舆驶,死亡現(xiàn)場(chǎng)離奇詭異橱健,居然都是意外死亡,警方通過查閱死者的電腦和手機(jī)沙廉,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 88,254評(píng)論 2 382
  • 文/潘曉璐 我一進(jìn)店門拘荡,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人撬陵,你說我怎么就攤上這事珊皿。” “怎么了巨税?”我有些...
    開封第一講書人閱讀 152,445評(píng)論 0 341
  • 文/不壞的土叔 我叫張陵蟋定,是天一觀的道長(zhǎng)。 經(jīng)常有香客問我草添,道長(zhǎng)驶兜,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 55,185評(píng)論 1 278
  • 正文 為了忘掉前任,我火速辦了婚禮抄淑,結(jié)果婚禮上屠凶,老公的妹妹穿的比我還像新娘。我一直安慰自己蝇狼,他們只是感情好阅畴,可當(dāng)我...
    茶點(diǎn)故事閱讀 64,178評(píng)論 5 371
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著迅耘,像睡著了一般贱枣。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上颤专,一...
    開封第一講書人閱讀 48,970評(píng)論 1 284
  • 那天纽哥,我揣著相機(jī)與錄音,去河邊找鬼栖秕。 笑死春塌,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的簇捍。 我是一名探鬼主播只壳,決...
    沈念sama閱讀 38,276評(píng)論 3 399
  • 文/蒼蘭香墨 我猛地睜開眼,長(zhǎng)吁一口氣:“原來是場(chǎng)噩夢(mèng)啊……” “哼暑塑!你這毒婦竟也來了吼句?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 36,927評(píng)論 0 259
  • 序言:老撾萬榮一對(duì)情侶失蹤事格,失蹤者是張志新(化名)和其女友劉穎惕艳,沒想到半個(gè)月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體驹愚,經(jīng)...
    沈念sama閱讀 43,400評(píng)論 1 300
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡远搪,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 35,883評(píng)論 2 323
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了逢捺。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片谁鳍。...
    茶點(diǎn)故事閱讀 37,997評(píng)論 1 333
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖蒸甜,靈堂內(nèi)的尸體忽然破棺而出棠耕,到底是詐尸還是另有隱情,我是刑警寧澤柠新,帶...
    沈念sama閱讀 33,646評(píng)論 4 322
  • 正文 年R本政府宣布窍荧,位于F島的核電站,受9級(jí)特大地震影響恨憎,放射性物質(zhì)發(fā)生泄漏蕊退。R本人自食惡果不足惜郊楣,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 39,213評(píng)論 3 307
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望瓤荔。 院中可真熱鬧净蚤,春花似錦、人聲如沸输硝。這莊子的主人今日做“春日...
    開封第一講書人閱讀 30,204評(píng)論 0 19
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽点把。三九已至橘荠,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間郎逃,已是汗流浹背哥童。 一陣腳步聲響...
    開封第一講書人閱讀 31,423評(píng)論 1 260
  • 我被黑心中介騙來泰國(guó)打工, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留褒翰,地道東北人贮懈。 一個(gè)月前我還...
    沈念sama閱讀 45,423評(píng)論 2 352
  • 正文 我出身青樓,卻偏偏與公主長(zhǎng)得像优训,于是被迫代替她去往敵國(guó)和親朵你。 傳聞我的和親對(duì)象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 42,722評(píng)論 2 345

推薦閱讀更多精彩內(nèi)容