python-bitshares 边学边记 (六) / Dex类
python-bitshares·@oflyhigh·
0.000 HBDpython-bitshares 边学边记 (六) / Dex类
在之前的几篇文章中,我们简单介绍了如何安装python-bitshares 、python-bitshares的钱包相关操作、BitShares类以及Account类、Market类。 详情可以参考文末的参考链接。 https://steemitimages.com/DQmab7QPFcG6Q4U9Z1FMThkdVcDneSY5xR2bPxVWryEZfeX/image.png (图源 :pixabay) 这节我们来继续学习python-bitshares 。 --- # Dex 类查询功能 #### 创建实例 我们可以使用以下代码创建Dex实例 `from bitshares.dex import Dex` `dex = Dex()` #### 手续费 以下代码可以用与显示bitshares区块链上各类操作的手续费 `from pprint import pprint` `from bitshares.dex import Dex` `dex = Dex()` `pprint(dex.returnFees())` 返回的数据比较多,就不一一贴出来了。 如果是经常交易,我们可能会比较关注以下几项费用:  从上图可知,创建订单和取消订单都非常便宜,而撮合订单完全免费。 由此可见bitshares真的是良心交易所 #### 抵押债仓 在学习Account类时,我们知道可以用如下代码查看指定用户的抵押债仓 `account = Account("xxxxx")` `pprint(account.callpositions)` 其实,account.callpositions的实现如下:  调用的是Dex类的方法 当然,我们可以直接使用Dex类 `from pprint import pprint` `from bitshares.dex import Dex` `dex = Dex()` `pprint(dex.list_debt_positions("aaa"))`  结果如上,和调用Account类callpositions属性,完全一致。 # Dex 类借钱 & 平仓等 #### 抵押(借入) 我们可以使用如下代码借入CNY或者其它智能货币资产 `from bitshares.dex import Dex` `from bitshares.amount import Amount` `dex = Dex()` `dex.borrow(amount = Amount(1, 'CNY'), collateral_ratio=2, account='test2018')`  但是显而易见抵押率计算得不对  ***注:可能与账户是否借过钱等条件有关,具体原因需要阅读代码*** 在测试过程中,曾出现过如下错误: >bitsharesapi.exceptions.UnhandledRPCError: Assert Exception: enable_black_swan: Black swan was detected during a margin update which is not allowed to trigger a blackswan ***注:我尝试用网页借款后,这个错误无法重现,可能和新账户有关,需要核实一下*** #### 平仓(还钱) 我们可以使用如下代码平掉CNY的抵押债仓 `from bitshares.dex import Dex` `from bitshares.amount import Amount` `dex = Dex()` `dex.close_debt_position(symbol='CNY', account='test2018')`   可见我借入和平仓操作都很成功 #### 其它操作 除了借入和平仓以外,我们还可以通过Dex类调整债仓或者调整抵押率 `def adjust_debt(self, delta, new_collateral_ratio=None, account=None)` `def adjust_collateral_ratio(self, symbol, target_collateral_ratio, account=None)` 写的多了有点晕,怕写错了误导大家,就不多说了,需要的自己去看代码。 # 总结 Dex类主要提供了手续费、抵押债仓查询以及借钱、平仓、调整仓位、调整抵押率等操作。 但是在抵押率计算上似乎有一些问题,所以如果你需要在你的应用中使用Dex类,你应该先测试并核实是否存在问题后再将其应用到代码中。 ***文中信息仅供参考,始终文中代码造成损失概不负责!*** # 参考信息 * [https://github.com/xeroc/python-bitshares](https://github.com/xeroc/python-bitshares) * [python-bitshares 边学边记 (一) : 简介与安装](https://steemit.com/python-bitshares/@oflyhigh/python-bitshares) * [python-bitshares 边学边记 (二) : 钱包操作](https://steemit.com/python-bitshares/@oflyhigh/3ab1oc-python-bitshares) * [python-bitshares 边学边记 (三) / BitShares类](https://steemit.com/python-bitshares/@oflyhigh/python-bitshares-bitshares) * [python-bitshares 边学边记 (四) / Account类](https://steemit.com/python-bitshares/@oflyhigh/python-bitshares-account) * [python-bitshares 边学边记 (五) / Market类](https://steemit.com/python-bitshares/@oflyhigh/python-bitshares-market)
👍 eval, smb14, damarth, auntigormint, liangfengyouren, abraomarcos, ikonik, ms8988, ivysrono, fessikay, tiffanyrej, lemminon, jincheng, cqf, technologynepal, wangwenjing, greatness96, speeding, framethinker, itikna09, wylo, shellycai, michaelwilshaw, monkeyplayfire, exec, aabb, drwom, steemtruth, jackhircus, xiaohui, nietaasyqil, deanliu, laoyao, devilwsy, mark-waser, digital-wisdom, jwaser, herpetologyguy, morgan.waser, bwaser, ellepdub, ethical-ai, strong-ai, davidjkelley, technoprogressiv, handyman, mrliga, bxt, sanzo, revelim, chann, feelapi, xiaokongcom, cryptohustler, blackbunny, janiceting, travelgirl, oflyhigh, chinadaily, abetterworld, raili, weavingwords, mrtv2, heyeshuang, ribalinux, bearpaw, fastiduos, saury, rebecca80, vi1son, sweethoney, profitgenerator, freedom-fighter, raku, midnightoil, khalilad, dragon40, vadimlasca, somebody, lingfei, tensaix2j, archiles, ericsim, ericsim1991, ericsim1989, yyyy, ethansteem, ebejammin, shenchensucc, blockchainbilly, angela.ghkh, candiru, joythewanderer, jademont, exprmnt, trafalgar, austinsandersco, nanosesame, jkkim, lalala, newhope, helene, dgorbunov, fractalnode, chessmasterhex, redes, asterix87, resteeming, davaowhenyo, happychau123, allenshayzar, ravenousappetite, stacee, nitro.live, that1consultant, vfxness, btsabc, jiangchen, walkinharmony, mandagoi, shaffrimohd, deanyeong, ace108, chandravibha, mabed1, magicmonk, kimminyoung, ygern, ealfskillz, idx, abit, micayla, brendashockley, dennisphillips, cryptoninja, mangoanddaddy, nurhasib, simivalleyjeff53, yukoww, semlinker, yingpingzhang, haiqal, almostpeter, tamaramariadavey, shirlam, sylvia1997, rapp, gaysex, anower, dapeng, zinia, bellevich, gmsan, cherrypick, seanwang, daudio, dfilm, cm-steem, dvoice, xiaoshancun, twinkledrop, rea, englishtchrivy, pharesim, bue,