create account

为了回答朋友一个小问题,重学python-bitshares by oflyhigh

View this thread on: hive.blogpeakd.comecency.com
· @oflyhigh ·
$44.93
为了回答朋友一个小问题,重学python-bitshares
今天微信上一个朋友问我一个python-bitshares查看订单ID的问题,让我有些晕,好久没摸python-bitshares的库了,当然也不知道如果解决他的问题,要想解决,最好的办法就是自己装一下python-bitshares,然后测试一下。

![image.png](https://images.hive.blog/DQmadvzsxgMsHkRaw6Ng5JdTPq5ewNDWj5zwYc8anuHMFXR/image.png)
(图源 :[pixabay](https://pixabay.com/))

# 安装

于是装了一下python-bitshares:
>`pip3 install bitshares`

由于是本地电脑,安装要去远程抓取一些依赖库,所以那是一个超级慢啊,好在慢是慢,安装还是很顺利的。

详细依赖和安装信息可以参考[这里](https://github.com/bitshares/python-bitshares#installation)

# 问题

安装之后,来看我朋友的问题,他想通过`account.openorders`获取用户订单,以及对应的订单ID,那么我来试试:
>`from pprint import pprint`
>`from bitshares.account import Account`
>`account = Account("xxxx")`
>`pprint(account.openorders)`

结果输出如下内容:
>![image.png](https://images.hive.blog/DQmVwqBYSLS8em3eLL6evzkTCZVQrtRxf1nXHDgBqkQBhgU/image.png)

毫无疑问,这是一条订单信息,然而却真的找不到订单ID。而订单ID可以用于取消订单等操作,在自动化交易中是非常重要的。

# uptick

可是我记得使用uptick是可以获得订单ID的啊,来试试uptick:
>`uptick openorders xxxx`

返回信息如下:
>![image.png](https://images.hive.blog/DQmapy5ZP9HLrYLQ1ZUgZCdmW73gfwLNXHHm6WWwZXkMF4x/image.png)

# 解决

既然uptick能获取订单ID,而uptick又是依赖于python-bitshares,那么说明python-bitshares来获取也应该没问题,来看一下uptick中相应的代码:
>![image.png](https://images.hive.blog/DQmVqQCdJa1FPVPtZ4rfkFfsAV3jhas7sncJc1crdjmoawY/image.png)

也就是说它使用`o["id"]`来获取订单的ID,那么这个问题其实就很简单了,我们可以直接读openorders的条目,然后显示自己需要的内容比如订单ID等,在之前的代码后增加如下代码就可以得到每条订单的ID了:
>![image.png](https://images.hive.blog/DQmY5GL11QKr2DyamJxT3EtR2LTyAqRWVTwJtnqHDTCaGvx/image.png)

至此,问题解决。

# 为何之前的方式行不通?

告诉我朋友如何操作后,他又问我,为啥之前的方式不显示ID呢?其实这个问题说起来很简单,就是order是一组数据,这组数据显示成什么样子,是库的代码控制的。

所以要看Order类如何处理数据的显示,相应的内容在`price.py`文件中,`class Order(Price)`类下,`__repr__()`中,我瞄了一眼,果然是没处理Order ID,或许是觉得没有必要吧,哈哈哈。

# 更进一步

那么更进一步的问题是,`account.openorders`中的订单数据是从哪里来的呢?

看一下Account类(`class Account(GrapheneAccount)`)的`openorders`属性:
>![image.png](https://images.hive.blog/DQmX3wLLk7PMZ1zPLFxvYn9Zmjkxs648fYSukJoFaM31QK5/image.png)

发现它是遍历了`Account`的`limit_orders`。那么这个`limit_orders`的数据又是哪里来的呢?看了一下源代码,不难发现,其实是通过`get_full_accounts`调用读入了包括`limit_orders`在内的所有数据。

比如:
>`curl --data '{"jsonrpc": "2.0", "method": "call", "params": ["database", "get_full_accounts", [["xxxx"], 1]], "id": 1}'  https://api.bts.ai`

返回信息中就可以找到`limit_orders`数据啦:
>![image.png](https://images.hive.blog/DQmdZsatSo2zEGV3PcCKPqPQUGg35ckXaMKiJpFZNKdhNNa/image.png)

至此总算搞清楚来龙去脉啦。

# 相关链接

* https://github.com/bitshares/python-bitshares
* https://github.com/bitshares/uptick
* https://github.com/xeroc/python-graphenelib
* https://dev.bitshares.works/en/master/api/blockchain_api/database.html#get-full-accounts
👍  , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , and 140 others
properties (23)
authoroflyhigh
permlinkhbgdh-python-bitshares
categoryhive-105017
json_metadata{"tags":["hive-105017","cn","bitshares","bts","python","cn-programming","life","blog"],"image":["https://images.hive.blog/DQmadvzsxgMsHkRaw6Ng5JdTPq5ewNDWj5zwYc8anuHMFXR/image.png","https://images.hive.blog/DQmVwqBYSLS8em3eLL6evzkTCZVQrtRxf1nXHDgBqkQBhgU/image.png","https://images.hive.blog/DQmapy5ZP9HLrYLQ1ZUgZCdmW73gfwLNXHHm6WWwZXkMF4x/image.png","https://images.hive.blog/DQmVqQCdJa1FPVPtZ4rfkFfsAV3jhas7sncJc1crdjmoawY/image.png","https://images.hive.blog/DQmY5GL11QKr2DyamJxT3EtR2LTyAqRWVTwJtnqHDTCaGvx/image.png","https://images.hive.blog/DQmX3wLLk7PMZ1zPLFxvYn9Zmjkxs648fYSukJoFaM31QK5/image.png","https://images.hive.blog/DQmdZsatSo2zEGV3PcCKPqPQUGg35ckXaMKiJpFZNKdhNNa/image.png"],"links":["https://pixabay.com/","https://github.com/bitshares/python-bitshares#installation","https://github.com/bitshares/python-bitshares","https://github.com/bitshares/uptick","https://github.com/xeroc/python-graphenelib","https://dev.bitshares.works/en/master/api/blockchain_api/database.html#get-full-accounts"],"app":"hiveblog/0.1","format":"markdown"}
created2020-07-30 04:14:18
last_update2020-07-30 04:14:18
depth0
children2
last_payout2020-08-06 04:14:18
cashout_time1969-12-31 23:59:59
total_payout_value24.480 HBD
curator_payout_value20.445 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length2,486
author_reputation6,381,148,360,887,252
root_title为了回答朋友一个小问题,重学python-bitshares
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id98,793,203
net_rshares116,760,191,879,219
author_curate_reward""
vote details (204)
@chitty ·
I have picked your post for my daily hive voting initiative, Keep it up and Hive On!!
properties (22)
authorchitty
permlinkre-hbgdh-python-bitshares-20200801t000455
categoryhive-105017
json_metadata""
created2020-08-01 00:04:57
last_update2020-08-01 00:04:57
depth1
children1
last_payout2020-08-08 00:04:57
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length86
author_reputation86,901,300,608,582
root_title为了回答朋友一个小问题,重学python-bitshares
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id98,826,942
net_rshares0
@oflyhigh ·
Thank you very much.
properties (22)
authoroflyhigh
permlinkqeftmw
categoryhive-105017
json_metadata{"app":"hiveblog/0.1"}
created2020-08-02 13:04:12
last_update2020-08-02 13:04:12
depth2
children0
last_payout2020-08-09 13:04:12
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length20
author_reputation6,381,148,360,887,252
root_title为了回答朋友一个小问题,重学python-bitshares
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id98,852,523
net_rshares0