使用EOS的RPC介面來轉賬
來自專欄 EOSDEVS開發者社區
使用EOS的RPC介面來轉賬
本教程主要使用EOS的RPC介面來對發行的代幣使用RPC介面來進行操作,下列實驗均基於eosio/eos:20180521
這個版本的EOS完成。
發布代幣合約
創建賬號
cleos create account eosio eosio.token EOS5ySgzeHp9G7TqNDGpyzaCtahAeRcTvPRPJbFey5CmySL3vKYgE EOS5ySgzeHp9G7TqNDGpyzaCtahAeRcTvPRPJbFey5CmySL3vKYgE
操作結果:
executed transaction: 4a8b53ae6fa5e22ded33b50079e45550e39f3cb72ffa628e771ea21758844039 200 bytes 339 us# eosio <= eosio::newaccount {"creator":"eosio","name":"eosio.token","owner":{"threshold":1,"keys":[{"key":"EOS5ySgzeHp9G7TqNDGpy...
部署合約
cleos set contract eosio.token /opt/eosio/bin/data-dir/contracts/eosio.token -p eosio.token
結果:
Reading WAST/WASM from /opt/eosio/bin/data-dir/contracts/eosio.token/eosio.token.wasm...Using already assembled WASM...Publishing contract...executed transaction: 41677b5fd5c701ca67a153abb09f79c04085cc51a9d021436e7ee5afda1781bd 8048 bytes 1212 us# eosio <= eosio::setcode {"account":"eosio.token","vmtype":0,"vmversion":0,"code":"0061736d01000000017f1560037f7e7f0060057f7e...# eosio <= eosio::setabi {"account":"eosio.token","abi":"0e656f73696f3a3a6162692f312e30010c6163636f756e745f6e616d65046e616d65...
創建EOS代幣
cleos push action eosio.token create ["eosio", "10000000000.0000 EOS",0,0,0] -p eosio.token
結果:
executed transaction: 566693cba0b0d5d11d85e40cdfb095d525612c5915e17ce75d309054e1912235 120 bytes 552 us# eosio.token <= eosio.token::create {"issuer":"eosio","maximum_supply":"10000000000.0000 EOS"}
給eosio發幣
cleos push action eosio.token issue ["eosio","1000000000.0000 EOS", "issue"] -p eosio
結果:
executed transaction: 73f72879d220c720fcefb16b6aaf3db0ba492bd62020853b2cd5051557d5fa87 128 bytes 677 us# eosio.token <= eosio.token::issue {"to":"eosio","quantity":"1000000000.0000 EOS","memo":"issue"}
查看餘額
上面操作無誤的話應該可以查詢到餘額:
cleos get currency balance eosio.token eosio
結果為:
1000000000.0000 EOS
部署系統合約
部署eosio.system
合約
cleos set contract eosio.token /opt/eosio/bin/data-dir/contracts/eosio.system -p eosio.token
執行結果:
Reading WAST/WASM from /opt/eosio/bin/data-dir/contracts/eosio.system/eosio.system.wasm...Using already assembled WASM...Publishing contract...executed transaction: f6fab6802bf8089b3ba48705f899e36fd681e58c622661ba2032eb1a85ee2d64 40440 bytes 8081 us# eosio <= eosio::setcode "00a6823403ea30550000809d070061736d0100000001ba022f60027f7e0060067f7e7e7f7f7f0060057f7e7e7f7f0060047...# eosio <= eosio::setabi "00a6823403ea3055a8250e656f73696f3a3a6162692f312e30050c6163636f756e745f6e616d65046e616d650f7065726d6...
轉賬RPC
abi_json_to_bin
首先我們需要序列化轉賬的json:
POST http://127.0.0.1:8888/v1/chain/abi_json_to_bin
Data:
{ "code": "eosio.token", "action": "transfer", "args": { "from": "eosio", "to": "noprom", "quantity": "1.0000 EOS", "memo": "created by noprom" }}
參數的意思分別為:
參數名稱參數類型描述codestring智能合約的名字,這裡用的是eosio.tokenactionstring智能合約中的action, 這裡用轉賬:transferfromstringtransfer方法裡面的參數,從哪個賬戶轉賬tostringtransfer方法裡面的參數,轉賬到哪個賬戶quantitystringtransfer方法裡面的參數,轉賬數量,這裡代幣名稱為EOS,還可以有其他代幣memostringtransfer方法裡面的參數,轉賬備註
這樣會得到一個二進位的字元串,會用作後面的步驟中的data
參數:
{ "binargs": "0000000000ea305500000000487a2b9d102700000000000004454f53000000001163726561746564206279206e6f70726f6d"}
參數名稱參數類型描述binargsstring序列化的結果,在sign_transaction 和 push_transaction 中作為data請求參數
get_info
我們使用get_info來獲取最新的區塊號:
GET http://127.0.0.1:8888/v1/chain/get_info
參數名稱參數類型描述head_block_numnumber最新區塊號
得到結果:
{ "server_version": "4e99cf47", "head_block_num": 246190, "last_irreversible_block_num": 246189, "last_irreversible_block_id": "0003c1ade32d4660e7872abaedb9413d7de2eefea63dd17eea6bd36dc3c11ac4", "head_block_id": "0003c1ae3018e5cb2bda7c6897b418e60eaf8d1a00622d731d7337c264dca453", "head_block_time": "2018-05-24T14:39:32", "head_block_producer": "eosio", "virtual_block_cpu_limit": 100000000, "virtual_block_net_limit": 1048576000, "block_cpu_limit": 99900, "block_net_limit": 1048576}
get_block
使用 get_block來獲取最新的區塊具體信息:
POST http://127.0.0.1:8888/v1/chain/get_block
DATA:
{"block_num_or_id":246190}
請求參數:
參數名稱參數類型描述block_num_or_idnumber區塊號
得到結果:
{ "timestamp": "2018-05-24T14:39:32.000", "producer": "eosio", "confirmed": 0, "previous": "0003c1ade32d4660e7872abaedb9413d7de2eefea63dd17eea6bd36dc3c11ac4", "transaction_mroot": "0000000000000000000000000000000000000000000000000000000000000000", "action_mroot": "953a01dbf975cc120285e0c4e9fbe4be181189a821de7f2da467bcb373dce8f2", "schedule_version": 0, "new_producers": null, "header_extensions": [], "producer_signature": "SIG_K1_JvQ5qzgSJ4L1jYYZH2L7oM7D7hp37ejENN8UQL9AvnKpNvbCeF8gAzAKXUYvocH9fEUg2bxKGbU66QGRCjVGzxEXN7HTL6", "transactions": [], "block_extensions": [], "id": "0003c1ae3018e5cb2bda7c6897b418e60eaf8d1a00622d731d7337c264dca453", "block_num": 246190, "ref_block_prefix": 1753012779}
參數含義:
參數名稱參數類型描述timestampstring區塊對應的時間戳block_numnumber區塊號,作為sign_transaction 和 push_transaction中的ref_block_num請求參數ref_block_prefixnumber作為sign_transaction 和 push_transaction中的ref_block_prefix請求參數
unlock
在簽名交易之前需要解鎖錢包:
POST http://127.0.0.1:8888/v1/wallet/unlock
Data:
["noprom", "PW5KExxxxxS1HQzF1qJtWbxxxEFqSxdWBpgYUsxxxxxoxVRmjVw7Y"]
得到結果, 如果為空就返回下面的結果:
{}
get_required_keys
我們需要用這個介面來獲取所需要的key
POST http://127.0.0.1:8888/v1/chain/get_required_keys
DATA:
{ "available_keys": [ "EOS5ySgzeHp9G7TqNDGpyzaCtahAeRcTvPRPJbFey5CmySL3vKYgE", "EOS6MRyAjQq8ud7hVNYcfnVPJqcVpscN5So8BhtHuGYqET5GDW5CV", "EOS6gXwNz2SKUNAZcyjzVvg6KdNgA1bSuVzCr8c5yWkGij52JKx8V" ], "transaction": { "actions": [ { "account": "eosio.token", "authorization": [ { "actor": "eosio", "permission": "active" } ], "data": "0000000000ea305500000000487a2b9d102700000000000004454f53000000001163726561746564206279206e6f70726f6d", "name": "transfer" } ], "context_free_actions": [ ], "context_free_data": [ ], "delay_sec": 0, "expiration": "2018-05-24T15:20:30.500", "max_kcpu_usage": 0, "max_net_usage_words": 0, "ref_block_num": 245107, "ref_block_prefix": 801303063, "signatures": [ ] }}
得到結果:
{ "required_keys": [ "EOS6gXwNz2SKUNAZcyjzVvg6KdNgA1bSuVzCr8c5yWkGij52JKx8V" ]}
結果中的key將作為sign_transaction的key
sign_transaction
接著我們需要對轉賬交易進行簽名:
POST http://127.0.0.1:8888/v1/wallet/sign_transaction
Data:
[ { "ref_block_num": 246190, "ref_block_prefix": 1753012779, "expiration": "2018-05-24T15:30:32.000", "actions": [ { "account": "eosio.token", "name": "transfer", "authorization": [ { "actor": "eosio", "permission": "active" } ], "data": "0000000000ea305500000000487a2b9d102700000000000004454f53000000001163726561746564206279206e6f70726f6d" } ], "signatures": [] }, [ "EOS6gXwNz2SKUNAZcyjzVvg6KdNgA1bSuVzCr8c5yWkGij52JKx8V" ], ""]
參數含義:
參數名稱參數類型描述ref_block_numnumberget_block獲得的最新區塊號ref_block_prefixnumberget_block獲得的最新區塊號相關信息expirationstring過期時間 = timestamp 加上 一段時間 ,例如1分鐘accountstring調用系統智能合約賬號名,這裡為eosio.tokennamestringeosio.token合約的transfer方法authorization. actorstring執行操作的用戶名authorization. permissionstring執行操作的許可權datastringabi_json_to_bin序列化後的 值 binargsstring創建者的公鑰
得到結果:
{ "expiration": "2018-05-24T15:30:32", "ref_block_num": 49582, "ref_block_prefix": 1753012779, "max_net_usage_words": 0, "max_cpu_usage_ms": 0, "delay_sec": 0, "context_free_actions": [], "actions": [ { "account": "eosio.token", "name": "transfer", "authorization": [ { "actor": "eosio", "permission": "active" } ], "data": "0000000000ea305500000000487a2b9d102700000000000004454f53000000001163726561746564206279206e6f70726f6d" } ], "transaction_extensions": [], "signatures": [ "SIG_K1_Khn918pY1NHmnbF41bsqFE7sPYrniZPtTns68qUo3m92jp6gbegkpRHYSp9RH95T3u82XUvjZLM33AP83ZqiGApBo7JnBF" ], "context_free_data": []}
push_transaction
最後就是發送交易:
POST http://127.0.0.1:8888/v1/chain/push_transaction
Data:
{ "compression": "none", "transaction": { "expiration": "2018-05-24T15:30:32", "ref_block_num": 49582, "ref_block_prefix": 1753012779, "context_free_actions": [], "actions": [ { "account": "eosio.token", "name": "transfer", "authorization": [ { "actor": "eosio", "permission": "active" } ], "data": "0000000000ea305500000000487a2b9d102700000000000004454f53000000001163726561746564206279206e6f70726f6d" } ], "transaction_extensions": [] }, "signatures": [ "SIG_K1_Khn918pY1NHmnbF41bsqFE7sPYrniZPtTns68qUo3m92jp6gbegkpRHYSp9RH95T3u82XUvjZLM33AP83ZqiGApBo7JnBF" ]}
參數含義:
參數名稱參數類型描述ref_block_numnumberget_block獲得的最新區塊號ref_block_prefixnumberget_block獲得的最新區塊號相關信息expirationstring過期時間 = timestamp 加上 一段時間 ,例如1分鐘accountstring調用系統智能合約賬號名,這裡為eosio.tokennamestringeosio.token合約的transfer方法authorization. actorstring執行操作的用戶名authorization. permissionstring執行操作的許可權signaturesstringsign_transaction得到的簽名
得到結果
{ "transaction_id": "015ba92c7ad7294f0d70c772e7ba6ed678b11734418bf9ec48b001ce65c48e2f", "processed": { "id": "015ba92c7ad7294f0d70c772e7ba6ed678b11734418bf9ec48b001ce65c48e2f", "receipt": { "status": "executed", "cpu_usage_us": 644, "net_usage_words": 18 }, "elapsed": 644, "net_usage": 144, "scheduled": false, "action_traces": [ { "receipt": { "receiver": "eosio.token", "act_digest": "77d11fbbb7e6a5d67c28c2578bc2042704ed76d494d7e426eaecb54dceb0dc0b", "global_sequence": 246506, "recv_sequence": 12, "auth_sequence": [ [ "eosio", 246499 ] ], "code_sequence": 1, "abi_sequence": 1 }, "act": { "account": "eosio.token", "name": "transfer", "authorization": [ { "actor": "eosio", "permission": "active" } ], "data": { "from": "eosio", "to": "noprom", "quantity": "1.0000 EOS", "memo": "created by noprom" }, "hex_data": "0000000000ea305500000000487a2b9d102700000000000004454f53000000001163726561746564206279206e6f70726f6d" }, "elapsed": 446, "cpu_usage": 0, "console": "", "total_cpu_usage": 0, "trx_id": "015ba92c7ad7294f0d70c772e7ba6ed678b11734418bf9ec48b001ce65c48e2f", "inline_traces": [ { "receipt": { "receiver": "eosio", "act_digest": "77d11fbbb7e6a5d67c28c2578bc2042704ed76d494d7e426eaecb54dceb0dc0b", "global_sequence": 246507, "recv_sequence": 246488, "auth_sequence": [ [ "eosio", 246500 ] ], "code_sequence": 1, "abi_sequence": 1 }, "act": { "account": "eosio.token", "name": "transfer", "authorization": [ { "actor": "eosio", "permission": "active" } ], "data": { "from": "eosio", "to": "noprom", "quantity": "1.0000 EOS", "memo": "created by noprom" }, "hex_data": "0000000000ea305500000000487a2b9d102700000000000004454f53000000001163726561746564206279206e6f70726f6d" }, "elapsed": 3, "cpu_usage": 0, "console": "", "total_cpu_usage": 0, "trx_id": "015ba92c7ad7294f0d70c772e7ba6ed678b11734418bf9ec48b001ce65c48e2f", "inline_traces": [] }, { "receipt": { "receiver": "noprom", "act_digest": "77d11fbbb7e6a5d67c28c2578bc2042704ed76d494d7e426eaecb54dceb0dc0b", "global_sequence": 246508, "recv_sequence": 7, "auth_sequence": [ [ "eosio", 246501 ] ], "code_sequence": 1, "abi_sequence": 1 }, "act": { "account": "eosio.token", "name": "transfer", "authorization": [ { "actor": "eosio", "permission": "active" } ], "data": { "from": "eosio", "to": "noprom", "quantity": "1.0000 EOS", "memo": "created by noprom" }, "hex_data": "0000000000ea305500000000487a2b9d102700000000000004454f53000000001163726561746564206279206e6f70726f6d" }, "elapsed": 4, "cpu_usage": 0, "console": "", "total_cpu_usage": 0, "trx_id": "015ba92c7ad7294f0d70c772e7ba6ed678b11734418bf9ec48b001ce65c48e2f", "inline_traces": [] } ] } ], "except": null }}
這樣就完成了用RPC API來轉賬的功能。
項目地址
本文項目地址為: 使用RPC介面來轉賬
英文版本地址: Transfer-Tokens-Using-RPC
版權說明
本文版權歸原作者所有,若有轉載請註明出處,侵權必究。
原文地址
使用EOS的RPC介面來轉賬 - EOSDevs | EOS開發者社區
推薦閱讀:
※Cardano以及其他區塊鏈項目能避免類似 EOS 此次的漏洞嗎?
※EOS安全漏洞事件始末~
※坐等空投?這個EOS糖果你該這麼領!
※OTCBTC 關於EOS分叉幣與空投糖果的公告(附評論)
※【熱搜熱說】 EOS主網臨近啟動,將有什麼大事會發生?柚粉要注意什麼?