EOS开发(十二)使用RPC API抵押/赎回CPU和NET资源

1. 前言

本文继续对EOS资源进行一个大概的讲解,并通过实例演示如何通过RPC API,操作CPU和NET资源的抵押/赎回。

我们依然在测试网络http://jungle.cryptolions.io:18888上,使用Postman对API进行测试。

2. CPU和NET

CPU和NET资源是通过抵押EOS Token获得的,可以由其他账户为你抵押,也可以自己抵押。如果是由他人为你抵押,他可以选择抵押的资源是否可以由你赎回,所获得的EOS也归于你;或者这部分资源只能由他赎回,所获得的EOS也归于他,简单来说就是资源是他送给你的还是只是租借给你的。当然,自己抵押的资源,赎回时会将EOS打回给自己的账户。

和内存一样,抵押和赎回也是通过eosio.tokentransfer方法,与系统账户eosio.stake相互转账。系统不会收取手续费。

赎回资源时,EOS不会马上到账,而是会在三天后到账。你无法赎回已经使用的资源,需要在该资源自动释放后才可赎回。

CPU和NET资源是系统自动释放的,也就是已使用的这部分资源会在一段时间后恢复并可以再次使用。

3. 目标

这篇文章将演示三种情况:

  1. A以租借的形式为B抵押资源,A可以赎回,B无法赎回

  2. A以赠送的方式为B抵押资源,A无法赎回,B可以赎回

  3. B自己抵押资源并且可以赎回

4. 准备工作

我们重新创建一个账户testnetstake。创建时只为其购买一些内存,不抵押CPU和NET资源。

生成一个新的钱包stake.wallet

yuyangdeMacBook-Pro:cleos yuyang$ cleos wallet create -n stake --to-console
Creating wallet: stake
Save password to use in the future to unlock this wallet.
Without password imported keys will not be retrievable.
"PW5Kxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"

生成两个公钥:

EOS7RKPFD8228owd1BG1aiwYThSTLp3huiq9J3gWCwW3CjAoBtsJf
EOS6KDP2yjhD5E6ESyviF2qgUoH2bsyy6ybm5KTrMJstb6Mbxy3up

创建账户,注意抵押的CPU和NET资源的EOS都为0

yuyangdeMacBook-Pro:cleos yuyang$ cleos -u "http://jungle.cryptolions.io:18888" system newaccount --stake-net '0 EOS' --stake-cpu '0 EOS' --buy-ram '100 EOS' testnetyy111 testnetstake EOS7RKPFD8228owd1BG1aiwYThSTLp3huiq9J3gWCwW3CjAoBtsJf EOS6KDP2yjhD5E6ESyviF2qgUoH2bsyy6ybm5KTrMJstb6Mbxy3up

executed transaction: 9338daa79f6e41dfaf46b3ef147c63335e1df7314fb47a5cc7e4a97a7512be23  264 bytes  6678 us
#         eosio <= eosio::newaccount            {"creator":"testnetyy111","name":"testnetstake","owner":{"threshold":1,"keys":[{"key":"EOS7RKPFD8228...
#         eosio <= eosio::buyram                {"payer":"testnetyy111","receiver":"testnetstake","quant":"100.0000 EOS"}
#   eosio.token <= eosio.token::transfer        {"from":"testnetyy111","to":"eosio.ram","quantity":"99.5000 EOS","memo":"buy ram"}
#  testnetyy111 <= eosio.token::transfer        {"from":"testnetyy111","to":"eosio.ram","quantity":"99.5000 EOS","memo":"buy ram"}
#     eosio.ram <= eosio.token::transfer        {"from":"testnetyy111","to":"eosio.ram","quantity":"99.5000 EOS","memo":"buy ram"}
#   eosio.token <= eosio.token::transfer        {"from":"testnetyy111","to":"eosio.ramfee","quantity":"0.5000 EOS","memo":"ram fee"}
#  testnetyy111 <= eosio.token::transfer        {"from":"testnetyy111","to":"eosio.ramfee","quantity":"0.5000 EOS","memo":"ram fee"}
#  eosio.ramfee <= eosio.token::transfer        {"from":"testnetyy111","to":"eosio.ramfee","quantity":"0.5000 EOS","memo":"ram fee"}

查看账号情况,确认CPU和NET资源都为0

yuyangdeMacBook-Pro:cleos yuyang$ cleos -u "http://jungle.cryptolions.io:18888" get account testnetstake
permissions: 
     owner     1:    1 EOS7RKPFD8228owd1BG1aiwYThSTLp3huiq9J3gWCwW3CjAoBtsJf
        active     1:    1 EOS6KDP2yjhD5E6ESyviF2qgUoH2bsyy6ybm5KTrMJstb6Mbxy3up
memory: 
     quota:     1.552 MiB    used:     2.926 KiB  

net bandwidth: 
     delegated:       0.0000 EOS           (total staked delegated to account from others)
     used:                 0 bytes
     available:            0 bytes
     limit:                0 bytes

cpu bandwidth:
     delegated:       0.0000 EOS           (total staked delegated to account from others)
     used:                 0 us   
     available:            0 us   
     limit:                0 us   

5. A以租借的形式为B抵押资源

5.1 生成抵押的bin字符串

  1. 调用eosio合约的delegatebw方法

  2. 传入抵押者账户名称和接收者账户名称

  3. 指定要抵押的资源的类型和对应数量,单位为EOS。如果只想抵押其中一种资源,只需将另一项资源的值设为0

  4. transfer0,表示是以delegated租借的方式抵押资源

api
http://jungle.cryptolions.io:18888/v1/chain/abi_json_to_bin

params
{
    "code": "eosio",
    "action": "delegatebw",
    "args": {
        "from": "testnetyy111",
        "receiver": "testnetstake",
        "stake_net_quantity": "100.0000 EOS",
        "stake_cpu_quantity": "100.0000 EOS",
        "transfer": 0,
    }
}

return
{
    "binargs": "1042f03eab99b1caa0a0c938ab99b1ca40420f000000000004454f530000000040420f000000000004454f530000000000"
}

5.2 获取当前最新的区块号

api
http://jungle.cryptolions.io:18888/v1/chain/get_info

params
无

return
{
    "server_version": "08819aae",
    "chain_id": "038f4b0fc8ff18a4f0842a8f0564611f6e96e8535901dd45e43ac8691a1c4dca",
    "head_block_num": 14157128,
    "last_irreversible_block_num": 14156794,
    "last_irreversible_block_id": "00d803faf226087020c201b3b18f6aea1d1e0ec654f926af6f4eca46683ec8a8",
    "head_block_id": "00d805487415a2d5c5f7f03ae719eac64274c23abcc524dbefdc6ef60531e206",
    "head_block_time": "2018-09-13T07:55:16.500",
    "head_block_producer": "owlinthedark",
    "virtual_block_cpu_limit": 200000000,
    "virtual_block_net_limit": 1048576000,
    "block_cpu_limit": 199900,
    "block_net_limit": 1048576,
    "server_version_string": "v1.2.5-dirty"
}

获取到区块号"head_block_num": 14157128

5.3 获取当前区块详情

api
http://jungle.cryptolions.io:18888/v1/chain/get_block

params
{"block_num_or_id":"14157128"}

return
{
    "timestamp": "2018-09-13T07:55:16.500",
    "producer": "owlinthedark",
    "confirmed": 0,
    "previous": "00d805471896a4b1f0214118d7d03395b2f350805e3352d0661eca313f1252f7",
    "transaction_mroot": "0000000000000000000000000000000000000000000000000000000000000000",
    "action_mroot": "2274d5cbc0ab5d00912ecc035f0157af143b2a27f7c48583c5382b518f6890fb",
    "schedule_version": 222,
    "new_producers": null,
    "header_extensions": [],
    "producer_signature": "SIG_K1_K1c4VLrKocDfjR1EQJzzriZHdxWsHfnddDU2hxc7v2E9kDUr8MbiCYbiMQ1F7YfHdBK5xMNnxkknBTpxrHvEYQgBAM37oC",
    "transactions": [],
    "block_extensions": [],
    "id": "00d805487415a2d5c5f7f03ae719eac64274c23abcc524dbefdc6ef60531e206",
    "block_num": 14157128,
    "ref_block_prefix": 988870597
}

获取到"timestamp": "2018-09-13T07:55:16.500""ref_block_prefix": 988870597

5.4 签署交易

  1. 过期时间我这里加了20分钟。2018-09-13T07:55:16.500 ==> 2018-09-13T08:15:16.500

  2. 注意这里钱包服务的端口为8899

api
http://127.0.0.1:8899/v1/wallet/sign_transaction

params
[{
        "ref_block_num": 14157128,
        "ref_block_prefix": 988870597,
        "expiration": "2018-09-13T08:15:16.500",
        "actions": [{
            "account": "eosio",
            "name": "delegatebw",
            "authorization": [{
                "actor": "testnetyy111",
                "permission": "active"
            }],
            "data": "1042f03eab99b1caa0a0c938ab99b1ca40420f000000000004454f530000000040420f000000000004454f530000000000"
        }],
        "signatures": []
    },
    ["EOS6Z7mUQeFC2cQTT3xMyZh2wsLQoHih1bTMgRhr3dbichprTi7Rc"], "038f4b0fc8ff18a4f0842a8f0564611f6e96e8535901dd45e43ac8691a1c4dca"
]

return
{
    "expiration": "2018-09-13T08:15:16",
    "ref_block_num": 1352,
    "ref_block_prefix": 988870597,
    "max_net_usage_words": 0,
    "max_cpu_usage_ms": 0,
    "delay_sec": 0,
    "context_free_actions": [],
    "actions": [
        {
            "account": "eosio",
            "name": "delegatebw",
            "authorization": [
                {
                    "actor": "testnetyy111",
                    "permission": "active"
                }
            ],
            "data": "1042f03eab99b1caa0a0c938ab99b1ca40420f000000000004454f530000000040420f000000000004454f530000000000"
        }
    ],
    "transaction_extensions": [],
    "signatures": [
        "SIG_K1_K86bpaTi2vhSoysvjbJm65e879JpkE64TR18gsgxVZ9yaKfbrpkju5iNWT7QTn6ch3goESm4Nz3ZaF3ZkqQTFGYjpF5kum"
    ],
    "context_free_data": []
}

获取到signatures

5.5 提交交易

api
http://jungle.cryptolions.io:18888/v1/chain/push_transaction

params
{
  "compression": "none",
  "transaction": {
    "expiration": "2018-09-13T08:15:16.500",
    "ref_block_num": 14157128,
    "ref_block_prefix": 988870597,
    "context_free_actions": [],
    "actions": [
        {
            "account": "eosio",
            "name": "delegatebw",
            "authorization": [
                {
                    "actor": "testnetyy111",
                    "permission": "active"
                }
            ],
            "data": "1042f03eab99b1caa0a0c938ab99b1ca40420f000000000004454f530000000040420f000000000004454f530000000000"
        }
    ],
    "transaction_extensions": []
  },
  "signatures": [
        "SIG_K1_K86bpaTi2vhSoysvjbJm65e879JpkE64TR18gsgxVZ9yaKfbrpkju5iNWT7QTn6ch3goESm4Nz3ZaF3ZkqQTFGYjpF5kum"
   ]
}

return
{
    "transaction_id": "4de32a7b16e567bc95ef8605ca44965f86cabc33afad08f3138c1ef4073b31d4",
    "processed": {
        "id": "4de32a7b16e567bc95ef8605ca44965f86cabc33afad08f3138c1ef4073b31d4",
        "receipt": {
            "status": "executed",
            "cpu_usage_us": 4895,
            "net_usage_words": 18
        },
        "elapsed": 4895,
        "net_usage": 144,
        "scheduled": false,
        "action_traces": [
            {
                "receipt": {
                    "receiver": "eosio",
                    "act_digest": "9191ba96bca789221fc5d3b6ae56f517fe4d4ca52e155992a7abdb2ff486e3e3",
                    "global_sequence": 33560983,
                    "recv_sequence": 15613483,
                    "auth_sequence": [
                        [
                            "testnetyy111",
                            88
                        ]
                    ],
                    "code_sequence": 12,
                    "abi_sequence": 13
                },
                "act": {
                    "account": "eosio",
                    "name": "delegatebw",
                    "authorization": [
                        {
                            "actor": "testnetyy111",
                            "permission": "active"
                        }
                    ],
                    "data": {
                        "from": "testnetyy111",
                        "receiver": "testnetstake",
                        "stake_net_quantity": "100.0000 EOS",
                        "stake_cpu_quantity": "100.0000 EOS",
                        "transfer": 0
                    },
                    "hex_data": "1042f03eab99b1caa0a0c938ab99b1ca40420f000000000004454f530000000040420f000000000004454f530000000000"
                },
                "elapsed": 3198,
                "cpu_usage": 0,
                "console": "",
                "total_cpu_usage": 0,
                "trx_id": "4de32a7b16e567bc95ef8605ca44965f86cabc33afad08f3138c1ef4073b31d4",
                "inline_traces": [
                    {
                        "receipt": {
                            "receiver": "eosio.token",
                            "act_digest": "047df0b43c3db6ad34ac4a84b025509e529aef9d3442fa54d3fe936ce7863553",
                            "global_sequence": 33560984,
                            "recv_sequence": 1728899,
                            "auth_sequence": [
                                [
                                    "testnetyy111",
                                    89
                                ]
                            ],
                            "code_sequence": 4,
                            "abi_sequence": 4
                        },
                        "act": {
                            "account": "eosio.token",
                            "name": "transfer",
                            "authorization": [
                                {
                                    "actor": "testnetyy111",
                                    "permission": "active"
                                }
                            ],
                            "data": {
                                "from": "testnetyy111",
                                "to": "eosio.stake",
                                "quantity": "200.0000 EOS",
                                "memo": "stake bandwidth"
                            },
                            "hex_data": "1042f03eab99b1ca0014341903ea305580841e000000000004454f53000000000f7374616b652062616e647769647468"
                        },
                        "elapsed": 982,
                        "cpu_usage": 0,
                        "console": "",
                        "total_cpu_usage": 0,
                        "trx_id": "4de32a7b16e567bc95ef8605ca44965f86cabc33afad08f3138c1ef4073b31d4",
                        "inline_traces": [
                            {
                                "receipt": {
                                    "receiver": "testnetyy111",
                                    "act_digest": "047df0b43c3db6ad34ac4a84b025509e529aef9d3442fa54d3fe936ce7863553",
                                    "global_sequence": 33560985,
                                    "recv_sequence": 30,
                                    "auth_sequence": [
                                        [
                                            "testnetyy111",
                                            90
                                        ]
                                    ],
                                    "code_sequence": 4,
                                    "abi_sequence": 4
                                },
                                "act": {
                                    "account": "eosio.token",
                                    "name": "transfer",
                                    "authorization": [
                                        {
                                            "actor": "testnetyy111",
                                            "permission": "active"
                                        }
                                    ],
                                    "data": {
                                        "from": "testnetyy111",
                                        "to": "eosio.stake",
                                        "quantity": "200.0000 EOS",
                                        "memo": "stake bandwidth"
                                    },
                                    "hex_data": "1042f03eab99b1ca0014341903ea305580841e000000000004454f53000000000f7374616b652062616e647769647468"
                                },
                                "elapsed": 148,
                                "cpu_usage": 0,
                                "console": "",
                                "total_cpu_usage": 0,
                                "trx_id": "4de32a7b16e567bc95ef8605ca44965f86cabc33afad08f3138c1ef4073b31d4",
                                "inline_traces": []
                            },
                            {
                                "receipt": {
                                    "receiver": "eosio.stake",
                                    "act_digest": "047df0b43c3db6ad34ac4a84b025509e529aef9d3442fa54d3fe936ce7863553",
                                    "global_sequence": 33560986,
                                    "recv_sequence": 238796,
                                    "auth_sequence": [
                                        [
                                            "testnetyy111",
                                            91
                                        ]
                                    ],
                                    "code_sequence": 4,
                                    "abi_sequence": 4
                                },
                                "act": {
                                    "account": "eosio.token",
                                    "name": "transfer",
                                    "authorization": [
                                        {
                                            "actor": "testnetyy111",
                                            "permission": "active"
                                        }
                                    ],
                                    "data": {
                                        "from": "testnetyy111",
                                        "to": "eosio.stake",
                                        "quantity": "200.0000 EOS",
                                        "memo": "stake bandwidth"
                                    },
                                    "hex_data": "1042f03eab99b1ca0014341903ea305580841e000000000004454f53000000000f7374616b652062616e647769647468"
                                },
                                "elapsed": 27,
                                "cpu_usage": 0,
                                "console": "",
                                "total_cpu_usage": 0,
                                "trx_id": "4de32a7b16e567bc95ef8605ca44965f86cabc33afad08f3138c1ef4073b31d4",
                                "inline_traces": []
                            }
                        ]
                    }
                ]
            }
        ],
        "except": null
    }
}

5.6 查看账户

yuyangdeMacBook-Pro:cleos yuyang$ cleos -u "http://jungle.cryptolions.io:18888" get account testnetstake
permissions: 
     owner     1:    1 EOS7RKPFD8228owd1BG1aiwYThSTLp3huiq9J3gWCwW3CjAoBtsJf
        active     1:    1 EOS6KDP2yjhD5E6ESyviF2qgUoH2bsyy6ybm5KTrMJstb6Mbxy3up
memory: 
     quota:     1.552 MiB    used:     2.926 KiB  

net bandwidth: 
     delegated:     100.0000 EOS           (total staked delegated to account from others)
     used:                 0 bytes
     available:        18.29 MiB  
     limit:            18.29 MiB  

cpu bandwidth:
     delegated:     100.0000 EOS           (total staked delegated to account from others)
     used:                 0 us   
     available:        3.651 sec  
     limit:            3.651 sec  

可以看到CPU和NET各租借了100EOS

5.7 testnetstake尝试赎回资源

我们先尝试使用testnetstake账户赎回由testnetyy111租借给自己的资源。打开并解锁钱包stake.wallet,将testnetstake的私钥导入。下面需要由testnetstake签署交易

5.8 生成赎回资源的bin字符串

  1. 调用eosio合约的undelegatebw方法

  2. 传入赎回者账户名称和接收者账户名称

  3. 赎回价值100EOS的CPU和NET

api
http://jungle.cryptolions.io:18888/v1/chain/abi_json_to_bin

params
{
    "code": "eosio",
    "action": "undelegatebw",
    "args": {
        "from": "testnetstake",
        "receiver": "testnetstake",
        "unstake_net_quantity": "100.0000 EOS",
        "unstake_cpu_quantity": "100.0000 EOS"
    }
}

return
{
    "binargs": "a0a0c938ab99b1caa0a0c938ab99b1ca40420f000000000004454f530000000040420f000000000004454f5300000000"
}

5.9 获取当前最新的区块号

api
http://jungle.cryptolions.io:18888/v1/chain/get_info

params
无

return
{
    "server_version": "08819aae",
    "chain_id": "038f4b0fc8ff18a4f0842a8f0564611f6e96e8535901dd45e43ac8691a1c4dca",
    "head_block_num": 14163505,
    "last_irreversible_block_num": 14163180,
    "last_irreversible_block_id": "00d81cecbc3f09fef84f00732ccb8400224af86bde742823356e704f00a6afc2",
    "head_block_id": "00d81e3160bc55a2de6e7ca13f771d8c481e9906ef83cfb1ab770d36b3144c69",
    "head_block_time": "2018-09-13T08:50:24.500",
    "head_block_producer": "atticlabjbpn",
    "virtual_block_cpu_limit": 200000000,
    "virtual_block_net_limit": 1048576000,
    "block_cpu_limit": 199900,
    "block_net_limit": 1048576,
    "server_version_string": "v1.2.5-dirty"
}

获取到区块号"head_block_num": 14163505

5.10 获取当前区块详情

api
http://jungle.cryptolions.io:18888/v1/chain/get_block

params
{"block_num_or_id":"14163505"}

return
{
    "timestamp": "2018-09-13T08:50:24.500",
    "producer": "atticlabjbpn",
    "confirmed": 0,
    "previous": "00d81e30541ebd7f20b776eb4d08f6caaefa774e540784c65cc152c4a8776579",
    "transaction_mroot": "ca199973357ca4587d01e875b2478ad6fcd1c443de6ab66d8b34933fdf791761",
    "action_mroot": "fe4bab83f379ec92f5695be402014b73bd6f38ab3aa2c4c6d2b8ceb9daa09959",
    "schedule_version": 223,
    "new_producers": null,
    "header_extensions": [],
    "producer_signature": "SIG_K1_JvUmSxB1NSDW6H6UkqxXxyTAZ9cehCr2pEK7F1G5zYnsYVyDfpFgpDQviYNH61gbNbKRkJtf3iXoX7s7JXbHrBtzrJQope",
    "transactions": [
        {
            "status": "executed",
            "cpu_usage_us": 1159,
            "net_usage_words": 20,
            "trx": {
                "id": "56f8f7b3c231292eb27d1711ba0297f8008cab1917f79866614cd0aff3a549c8",
                "signatures": [
                    "SIG_K1_KVg8FwTN6tewinydJ6AoMa8Sg2FX8RxWi9ErmFdS9ypKogGraoEs3jzRK3SyZ7omv33SeuyJcEd9DhWDNPfCCo32JSHC3w"
                ],
                "compression": "none",
                "packed_context_free_data": "",
                "context_free_data": [],
                "packed_trx": "ee249a5bec1cf84f00730000000100408c7a02ea3055000000000085269d0008cf3238ccbc750500017055ce8e6788683c0000000080ac14cf0180277591e6ea2f3200000000a8ed323225245745204c4f564520424d20e299a52e204a756e676c6520746573742e202320313937363300",
                "transaction": {
                    "expiration": "2018-09-13T08:50:54",
                    "ref_block_num": 7404,
                    "ref_block_prefix": 1929400312,
                    "max_net_usage_words": 0,
                    "max_cpu_usage_ms": 0,
                    "delay_sec": 0,
                    "context_free_actions": [
                        {
                            "account": "eosio.null",
                            "name": "nonce",
                            "authorization": [],
                            "data": "cf3238ccbc750500"
                        }
                    ],
                    "actions": [
                        {
                            "account": "blocktwitter",
                            "name": "tweet",
                            "authorization": [
                                {
                                    "actor": "acryptolions",
                                    "permission": "active"
                                }
                            ],
                            "data": {
                                "message": "WE LOVE BM ♥. Jungle test. # 19763"
                            },
                            "hex_data": "245745204c4f564520424d20e299a52e204a756e676c6520746573742e2023203139373633"
                        }
                    ],
                    "transaction_extensions": []
                }
            }
        }
    ],
    "block_extensions": [],
    "id": "00d81e3160bc55a2de6e7ca13f771d8c481e9906ef83cfb1ab770d36b3144c69",
    "block_num": 14163505,
    "ref_block_prefix": 2709286622
}

获取到"timestamp": "2018-09-13T08:50:24.500""ref_block_prefix": 2709286622

5.11 签署交易

  1. 过期时间我这里加了20分钟。2018-09-13T08:50:24.500 ==> 2018-09-13T09:10:24.500
api
http://127.0.0.1:8899/v1/wallet/sign_transaction

params
[{
        "ref_block_num": 14163505,
        "ref_block_prefix": 2709286622,
        "expiration": "2018-09-13T09:10:24.500",
        "actions": [{
            "account": "eosio",
            "name": "undelegatebw",
            "authorization": [{
                "actor": "testnetstake",
                "permission": "active"
            }],
            "data": "a0a0c938ab99b1caa0a0c938ab99b1ca40420f000000000004454f530000000040420f000000000004454f5300000000"
        }],
        "signatures": []
    },
    ["EOS6KDP2yjhD5E6ESyviF2qgUoH2bsyy6ybm5KTrMJstb6Mbxy3up"], "038f4b0fc8ff18a4f0842a8f0564611f6e96e8535901dd45e43ac8691a1c4dca"
]

return
{
    "expiration": "2018-09-13T09:10:24",
    "ref_block_num": 7729,
    "ref_block_prefix": 2709286622,
    "max_net_usage_words": 0,
    "max_cpu_usage_ms": 0,
    "delay_sec": 0,
    "context_free_actions": [],
    "actions": [
        {
            "account": "eosio",
            "name": "undelegatebw",
            "authorization": [
                {
                    "actor": "testnetstake",
                    "permission": "active"
                }
            ],
            "data": "a0a0c938ab99b1caa0a0c938ab99b1ca40420f000000000004454f530000000040420f000000000004454f5300000000"
        }
    ],
    "transaction_extensions": [],
    "signatures": [
        "SIG_K1_JzBA4onsfRYecBwZr21yVshj9ii2e5nUB5kMVvfMS5BVwAPsj1NH6SVW5LBraoBpQX8KoWjpgXP3W4PQe9wNkZ9aQ8K3wm"
    ],
    "context_free_data": []
}

获取到signatures

5.12 提交交易

api
http://jungle.cryptolions.io:18888/v1/chain/push_transaction

params
{
  "compression": "none",
  "transaction": {
    "expiration": "2018-09-13T09:10:24.500",
    "ref_block_num": 14163505,
    "ref_block_prefix": 2709286622,
    "context_free_actions": [],
    "actions": [
        {
            "account": "eosio",
            "name": "undelegatebw",
            "authorization": [
                {
                    "actor": "testnetstake",
                    "permission": "active"
                }
            ],
            "data": "a0a0c938ab99b1caa0a0c938ab99b1ca40420f000000000004454f530000000040420f000000000004454f5300000000"
        }
    ],
    "transaction_extensions": []
  },
  "signatures": [
        "SIG_K1_JzBA4onsfRYecBwZr21yVshj9ii2e5nUB5kMVvfMS5BVwAPsj1NH6SVW5LBraoBpQX8KoWjpgXP3W4PQe9wNkZ9aQ8K3wm"
   ]
}

return
{
    "code": 500,
    "message": "Internal Service Error",
    "error": {
        "code": 3050003,
        "name": "eosio_assert_message_exception",
        "what": "eosio_assert_message assertion failure",
        "details": [
            {
                "message": "assertion failure with message: insufficient staked net bandwidth",
                "file": "wasm_interface.cpp",
                "line_number": 930,
                "method": "eosio_assert"
            },
            {
                "message": "pending console output: ",
                "file": "apply_context.cpp",
                "line_number": 61,
                "method": "exec_one"
            }
        ]
    }
}

报错提示staked资源不足。和我们预想的一样,无法将别人租借给自己的资源,赎回成EOS给自己的账户。接下来我们使用testnetyy111来赎回租借给testnetstake的资源

5.13 生成赎回资源的bin字符串

  • from : 解除用哪个账号所抵押的代币

  • receiver : 解除作用在哪个账号上的抵押代币

api
http://jungle.cryptolions.io:18888/v1/chain/abi_json_to_bin

params
{
    "code": "eosio",
    "action": "undelegatebw",
    "args": {
        "from": "testnetyy111",
        "receiver": "testnetstake",
        "unstake_net_quantity": "100.0000 EOS",
        "unstake_cpu_quantity": "100.0000 EOS"
    }
}

return
{
    "binargs": "1042f03eab99b1caa0a0c938ab99b1ca40420f000000000004454f530000000040420f000000000004454f5300000000"
}

5.14 获取当前最新的区块号

api
http://jungle.cryptolions.io:18888/v1/chain/get_info

params
无

return
{
    "server_version": "08819aae",
    "chain_id": "038f4b0fc8ff18a4f0842a8f0564611f6e96e8535901dd45e43ac8691a1c4dca",
    "head_block_num": 14168523,
    "last_irreversible_block_num": 14168193,
    "last_irreversible_block_id": "00d83081e104e331a6904f88310ba656c04e52921b20f893e99e76b785996793",
    "head_block_id": "00d831cbeea798d092dc105e5927b9d247e92072453699eb2dacd5b707c9bdc3",
    "head_block_time": "2018-09-13T09:34:40.500",
    "head_block_producer": "blockgenesys",
    "virtual_block_cpu_limit": 200000000,
    "virtual_block_net_limit": 1048576000,
    "block_cpu_limit": 199089,
    "block_net_limit": 1048416,
    "server_version_string": "v1.2.5-dirty"
}

获取到区块号"head_block_num": 14168523

5.15 获取当前区块详情

api
http://jungle.cryptolions.io:18888/v1/chain/get_block

params
{"block_num_or_id":"14168523"}

return
{
    "timestamp": "2018-09-13T09:34:40.500",
    "producer": "blockgenesys",
    "confirmed": 0,
    "previous": "00d831cadfecabb333f5c2a3a72924186dc860057ee139573ba90b219fc5560e",
    "transaction_mroot": "ae2a66f513cbc9589d00be07102fb9144fe9ca3490c23489953c46258f2c9345",
    "action_mroot": "c923f03e82cce8193100a47d9c58798bf2b0b6472f950da983889e4c94a05065",
    "schedule_version": 223,
    "new_producers": null,
    "header_extensions": [],
    "producer_signature": "SIG_K1_K4rAkncocM8JVA7H5ZKDAP3bbNuEku9LmdhXGvG45uYwjdhbrwMCqPdCrBecPvahJfkZn5Ydi1KWysY2gPRpFK5Emp7PpQ",
    "transactions": [
        {
            "status": "executed",
            "cpu_usage_us": 637,
            "net_usage_words": 20,
            "trx": {
                "id": "b90962a0a0301f5239d079bddfcaa858ef819da860c8d60160c72894722b9f57",
                "signatures": [
                    "SIG_K1_Kf5QjHpFVCpM9fcHnCWPFgwvrqvEb4hpHe7mEjGGCc7sErq5tUp4du75FBSac3j8i6874LExQX7EUSrg88spYAyTgWUecA"
                ],
                "compression": "none",
                "packed_context_free_data": "",
                "context_free_data": [],
                "packed_trx": "4e2f9a5b8130a6904f880000000100408c7a02ea3055000000000085269d0008ceee886abd750500017055ce8e6788683c0000000080ac14cf0180277591e6ea2f3200000000a8ed323225245745204c4f564520424d20e299a52e204a756e676c6520746573742e202320333136343500",
                "transaction": {
                    "expiration": "2018-09-13T09:35:10",
                    "ref_block_num": 12417,
                    "ref_block_prefix": 2286915750,
                    "max_net_usage_words": 0,
                    "max_cpu_usage_ms": 0,
                    "delay_sec": 0,
                    "context_free_actions": [
                        {
                            "account": "eosio.null",
                            "name": "nonce",
                            "authorization": [],
                            "data": "ceee886abd750500"
                        }
                    ],
                    "actions": [
                        {
                            "account": "blocktwitter",
                            "name": "tweet",
                            "authorization": [
                                {
                                    "actor": "acryptolions",
                                    "permission": "active"
                                }
                            ],
                            "data": {
                                "message": "WE LOVE BM ♥. Jungle test. # 31645"
                            },
                            "hex_data": "245745204c4f564520424d20e299a52e204a756e676c6520746573742e2023203331363435"
                        }
                    ],
                    "transaction_extensions": []
                }
            }
        }
    ],
    "block_extensions": [],
    "id": "00d831cbeea798d092dc105e5927b9d247e92072453699eb2dacd5b707c9bdc3",
    "block_num": 14168523,
    "ref_block_prefix": 1578163346
}

获取到"timestamp": "2018-09-13T09:34:40.500""ref_block_prefix": 1578163346

5.16 签署交易

  1. 过期时间我这里加了20分钟。2018-09-13T09:34:40.500 ==> 2018-09-13T09:54:40.500

  2. 注意这里使用的是含有testnetyy111密钥的钱包

api
http://127.0.0.1:8899/v1/wallet/sign_transaction

params
[{
        "ref_block_num": 14168523,
        "ref_block_prefix": 1578163346,
        "expiration": "2018-09-13T09:54:40.500",
        "actions": [{
            "account": "eosio",
            "name": "undelegatebw",
            "authorization": [{
                "actor": "testnetyy111",
                "permission": "active"
            }],
            "data": "1042f03eab99b1caa0a0c938ab99b1ca40420f000000000004454f530000000040420f000000000004454f5300000000"
        }],
        "signatures": []
    },
    ["EOS6Z7mUQeFC2cQTT3xMyZh2wsLQoHih1bTMgRhr3dbichprTi7Rc"], "038f4b0fc8ff18a4f0842a8f0564611f6e96e8535901dd45e43ac8691a1c4dca"
]

return
{
    "expiration": "2018-09-13T09:54:40",
    "ref_block_num": 12747,
    "ref_block_prefix": 1578163346,
    "max_net_usage_words": 0,
    "max_cpu_usage_ms": 0,
    "delay_sec": 0,
    "context_free_actions": [],
    "actions": [
        {
            "account": "eosio",
            "name": "undelegatebw",
            "authorization": [
                {
                    "actor": "testnetyy111",
                    "permission": "active"
                }
            ],
            "data": "1042f03eab99b1caa0a0c938ab99b1ca40420f000000000004454f530000000040420f000000000004454f5300000000"
        }
    ],
    "transaction_extensions": [],
    "signatures": [
        "SIG_K1_KjS2QqDGc5Pt6ezznN837dt5W5NCaczW6yKJvmfsFkrUBBk3xsjdv2KiHtJReSnBWhzf172movkGhfzyDfCaXgLdY7d56t"
    ],
    "context_free_data": []
}

获取到signatures

5.17 提交交易

api
http://jungle.cryptolions.io:18888/v1/chain/push_transaction

params
{
  "compression": "none",
  "transaction": {
    "expiration": "2018-09-13T09:54:40.500",
    "ref_block_num": 14168523,
    "ref_block_prefix": 1578163346,
    "context_free_actions": [],
    "actions": [
        {
            "account": "eosio",
            "name": "undelegatebw",
            "authorization": [
                {
                    "actor": "testnetyy111",
                    "permission": "active"
                }
            ],
            "data": "1042f03eab99b1caa0a0c938ab99b1ca40420f000000000004454f530000000040420f000000000004454f5300000000"
        }
    ],
    "transaction_extensions": []
  },
  "signatures": [
        "SIG_K1_KjS2QqDGc5Pt6ezznN837dt5W5NCaczW6yKJvmfsFkrUBBk3xsjdv2KiHtJReSnBWhzf172movkGhfzyDfCaXgLdY7d56t"
   ]
}

return
{
    "transaction_id": "d234efd776806e2be3f611c25dd79e12cecb4de813c610301ac0311066a65db6",
    "processed": {
        "id": "d234efd776806e2be3f611c25dd79e12cecb4de813c610301ac0311066a65db6",
        "receipt": {
            "status": "executed",
            "cpu_usage_us": 4044,
            "net_usage_words": 23
        },
        "elapsed": 4044,
        "net_usage": 184,
        "scheduled": false,
        "action_traces": [
            {
                "receipt": {
                    "receiver": "eosio",
                    "act_digest": "2a9b1145aa3051eab4dffd31b7cb6b18a688a190caf1d7f4b472a97e888fcc6a",
                    "global_sequence": 33588135,
                    "recv_sequence": 15624244,
                    "auth_sequence": [
                        [
                            "testnetyy111",
                            92
                        ]
                    ],
                    "code_sequence": 12,
                    "abi_sequence": 13
                },
                "act": {
                    "account": "eosio",
                    "name": "undelegatebw",
                    "authorization": [
                        {
                            "actor": "testnetyy111",
                            "permission": "active"
                        }
                    ],
                    "data": {
                        "from": "testnetyy111",
                        "receiver": "testnetstake",
                        "unstake_net_quantity": "100.0000 EOS",
                        "unstake_cpu_quantity": "100.0000 EOS"
                    },
                    "hex_data": "1042f03eab99b1caa0a0c938ab99b1ca40420f000000000004454f530000000040420f000000000004454f5300000000"
                },
                "elapsed": 3544,
                "cpu_usage": 0,
                "console": "",
                "total_cpu_usage": 0,
                "trx_id": "d234efd776806e2be3f611c25dd79e12cecb4de813c610301ac0311066a65db6",
                "inline_traces": []
            }
        ],
        "except": null
    }
}

5.18 查看账户

yuyangdeMacBook-Pro:cleos yuyang$ cleos -u "http://jungle.cryptolions.io:18888" get account testnetstake
permissions: 
     owner     1:    1 EOS7RKPFD8228owd1BG1aiwYThSTLp3huiq9J3gWCwW3CjAoBtsJf
        active     1:    1 EOS6KDP2yjhD5E6ESyviF2qgUoH2bsyy6ybm5KTrMJstb6Mbxy3up
memory: 
     quota:     1.552 MiB    used:     2.926 KiB  

net bandwidth: 
     delegated:       0.0000 EOS           (total staked delegated to account from others)
     used:                 0 bytes
     available:            0 bytes
     limit:                0 bytes

cpu bandwidth:
     delegated:       0.0000 EOS           (total staked delegated to account from others)
     used:                 0 us   
     available:            0 us   
     limit:                0 us  

可以看到账户testnetstake的抵押资源已被赎回,都已经为0了。

我们再看下testnetyy111账户的情况:

yuyangdeMacBook-Pro:cleos yuyang$ cleos -u "http://jungle.cryptolions.io:18888" get account testnetyy111
permissions: 
     owner     1:    1 EOS6cnhSLTn4eSUEqS4nC8frYTsVsjeH2M3hos1TUeCgme2Yim5Q5
        active     1:    1 EOS6Z7mUQeFC2cQTT3xMyZh2wsLQoHih1bTMgRhr3dbichprTi7Rc
memory: 
     quota:     3.377 MiB    used:       191 KiB  

net bandwidth: 
     staked:        100.0000 EOS           (total stake delegated from account to self)
     delegated:       0.0000 EOS           (total staked delegated to account from others)
     used:              1.11 KiB  
     available:        18.29 MiB  
     limit:            18.29 MiB  

cpu bandwidth:
     staked:        100.0000 EOS           (total stake delegated from account to self)
     delegated:       0.0000 EOS           (total staked delegated to account from others)
     used:             38.27 ms   
     available:        3.613 sec  
     limit:            3.651 sec  

unstaking:
     time of unstake request:  2018-09-13T09:39:42 (funds will be available in 71.65 hr)
     from net bandwidth:            100.0000 EOS
     from cpu bandwidth:            100.0000 EOS
     total:                         200.0000 EOS

EOS balances: 
     liquid:         8562.2670 EOS
     staked:          200.0000 EOS
     unstaking:       200.0000 EOS
     total:          8962.2670 EOS

producers:     <not voted>

出现了赎回信息unstaking,共有200EOS将在72小时之内到账。

5. A以赠送的形式为B抵押资源

这里不再详细贴代码的。首先生成bin字符串,这次将transfer设为1

{
    "code": "eosio",
    "action": "delegatebw",
    "args": {
        "from": "testnetyy111",
        "receiver": "testnetstake",
        "stake_net_quantity": "100.0000 EOS",
        "stake_cpu_quantity": "100.0000 EOS",
        "transfer": 1,
    }
}

按之前的流程,抵押成功后查看下testnetstake账户情况:

yuyangdeMacBook-Pro:cleos yuyang$ cleos -u "http://jungle.cryptolions.io:18888" get account testnetstake
permissions: 
     owner     1:    1 EOS7RKPFD8228owd1BG1aiwYThSTLp3huiq9J3gWCwW3CjAoBtsJf
        active     1:    1 EOS6KDP2yjhD5E6ESyviF2qgUoH2bsyy6ybm5KTrMJstb6Mbxy3up
memory: 
     quota:     1.552 MiB    used:     3.365 KiB  

net bandwidth: 
     staked:        100.0000 EOS           (total stake delegated from account to self)
     delegated:       0.0000 EOS           (total staked delegated to account from others)
     used:                 0 bytes
     available:        18.29 MiB  
     limit:            18.29 MiB  

cpu bandwidth:
     staked:        100.0000 EOS           (total stake delegated from account to self)
     delegated:       0.0000 EOS           (total staked delegated to account from others)
     used:                 0 us   
     available:        3.651 sec  
     limit:            3.651 sec  

producers:     <not voted>

可以看到抵押资源的形式为staked

由于赎回资源需要使用到CPU和NET资源,所以我们无法将各100EOS的资源全部赎回,否则会报错,这里我们只各赎回90EOS的资源。

生成赎回资源的字符串,testnetstake自己赎回资源:

{
    "code": "eosio",
    "action": "undelegatebw",
    "args": {
        "from": "testnetstake",
        "receiver": "testnetstake",
        "unstake_net_quantity": "90.0000 EOS",
        "unstake_cpu_quantity": "90.0000 EOS"
    }
}

按流程来,最后提交交易成功后,查看下testnetstake的账户情况:

yuyangdeMacBook-Pro:cleos yuyang$ cleos -u "http://jungle.cryptolions.io:18888" get account testnetstake
permissions: 
     owner     1:    1 EOS7RKPFD8228owd1BG1aiwYThSTLp3huiq9J3gWCwW3CjAoBtsJf
        active     1:    1 EOS6KDP2yjhD5E6ESyviF2qgUoH2bsyy6ybm5KTrMJstb6Mbxy3up
memory: 
     quota:     1.552 MiB    used:     3.951 KiB  

net bandwidth: 
     staked:         10.0000 EOS           (total stake delegated from account to self)
     delegated:       0.0000 EOS           (total staked delegated to account from others)
     used:               185 bytes
     available:        1.829 MiB  
     limit:            1.829 MiB  

cpu bandwidth:
     staked:         10.0000 EOS           (total stake delegated from account to self)
     delegated:       0.0000 EOS           (total staked delegated to account from others)
     used:             3.275 ms   
     available:        361.9 ms   
     limit:            365.1 ms   

unstaking tokens:
     time of unstake request:  2018-09-13T10:15:54 (funds will be available in 71.94 hr)
     from net bandwidth:             90.0000 EOS
     from cpu bandwidth:             90.0000 EOS
     total:                         180.0000 EOS

producers:     <not voted>

可以看到账户中的CPU和NET资源各只剩下10EOS。并且出现了赎回信息,共有180EOS将在72小时之内到账。

我们继续试验,A以赠送的形式为B抵押资源,那么A是否可以将抵押给B的资源赎回并将EOS发送回自己的账户呢?

首先,testnetyy111再以赠送的方式为testnetstake抵押各100EOS的资源:

{
    "code": "eosio",
    "action": "delegatebw",
    "args": {
        "from": "testnetyy111",
        "receiver": "testnetstake",
        "stake_net_quantity": "100.0000 EOS",
        "stake_cpu_quantity": "100.0000 EOS",
        "transfer": 1,
    }
}

最后提交抵押请求成功后,查看下testnetstake账户的情况:

yuyangdeMacBook-Pro:cleos yuyang$ cleos -u "http://jungle.cryptolions.io:18888" get account testnetstake
permissions: 
     owner     1:    1 EOS7RKPFD8228owd1BG1aiwYThSTLp3huiq9J3gWCwW3CjAoBtsJf
        active     1:    1 EOS6KDP2yjhD5E6ESyviF2qgUoH2bsyy6ybm5KTrMJstb6Mbxy3up
memory: 
     quota:     1.552 MiB    used:     3.627 KiB  

net bandwidth: 
     staked:        110.0000 EOS           (total stake delegated from account to self)
     delegated:       0.0000 EOS           (total staked delegated to account from others)
     used:               185 bytes
     available:        20.12 MiB  
     limit:            20.12 MiB  

cpu bandwidth:
     staked:        110.0000 EOS           (total stake delegated from account to self)
     delegated:       0.0000 EOS           (total staked delegated to account from others)
     used:             3.275 ms   
     available:        4.013 sec  
     limit:            4.016 sec  

unstaking tokens:
     time of unstake request:  2018-09-13T10:15:54 (funds will be available in 71.78 hr)
     from net bandwidth:             90.0000 EOS
     from cpu bandwidth:             90.0000 EOS
     total:                         180.0000 EOS

producers:     <not voted>

可以看到各有110EOS的抵押资源

我们使用testnetyy111来赎回抵押给testnetstake的资源:

{
    "code": "eosio",
    "action": "undelegatebw",
    "args": {
        "from": "testnetyy111",
        "receiver": "testnetstake",
        "unstake_net_quantity": "100.0000 EOS",
        "unstake_cpu_quantity": "100.0000 EOS"
    }
}

最后提交交易:

{
    "code": 500,
    "message": "Internal Service Error",
    "error": {
        "code": 3050003,
        "name": "eosio_assert_message_exception",
        "what": "eosio_assert_message assertion failure",
        "details": [
            {
                "message": "assertion failure with message: insufficient staked net bandwidth",
                "file": "wasm_interface.cpp",
                "line_number": 930,
                "method": "eosio_assert"
            },
            {
                "message": "pending console output: ",
                "file": "apply_context.cpp",
                "line_number": 61,
                "method": "exec_one"
            }
        ]
    }
}

可以看到抵押资源不足的错误提示,表明无法将赠送给他人的资源赎回给自己。

6. B自己抵押资源并且赎回

这部分内容就不演示了,通过上面的演示,肯定是可以的,大家可以自己动手试试。

    原文作者:yuyangray
    原文地址: https://www.jianshu.com/p/c64d33d457b6
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞