主页 > imtoken官网版 > 通过 Ruby 买卖比特币:使用开放式交易所 OceanOne
通过 Ruby 买卖比特币:使用开放式交易所 OceanOne
摘要:Option 2 Pending Order Exchange 基于去中心化交易所,性能一流。 查看比特币或钱包地址余额的完整步骤如下。 比特币的到达时间是一个区块的高度,大约几分钟。
上一章节介绍了Exincore,1秒即可完成资产的市价交易。 如果你想限价买卖,或者买卖一些exincore不支持的资产,你需要OceanOne。
选项 2:Ocean.One Exchange 上的挂单
Ocean.one是一个基于Mixin Network的去中心化交易所,具有一流的性能。
您可以在OceanOne上交易任何资产,只需将您的币转入OceanOne查询虚拟币usdt地址余额怎么查,将交易信息写在交易备忘录中,OceanOne会在市场列出您的交易需求,
交易成功后,目标币种将转入您的 MixinNetwork 账户。 它具有三大特点和优势:
无需注册OceanOne
无需向交易所存币
支持Mixin Network上所有可转让资产,所有ERC20 EOS代币。
预备知识:
首先需要创建一个机器人,方法在教程一中。
安装依赖
我们需要依赖msgpack和mixin-bot,这在第四章已经介绍过了,你应该先安装好了。
将代币存入 Mixin Network查询虚拟币usdt地址余额怎么查,并读取其余额。
下面演示用USDT购买BTC或用BTC购买USDT。 交易前,请检查钱包地址。
完整步骤如下:
查看比特币或USDT余额、钱包地址。 并记下钱包地址。
从第三方交易所或您的冷钱包向上述钱包地址存币。
再次查看币种余额,看是否入账。 (比特币到达时间为5个区块高度,约100分钟)。
比特币和USDT的充值地址是一样的。
if cmd == "aw" assetsInfo = walletAccount.read_assets() p "--------The Wallet Assets List-----------------" assetsInfo["data"].each { |x| puts x["symbol"] + " " + x["balance"] + " " + x["public_key"] + x["account_name"] + " " + x["account_tag"]} p "----------End of Wallet Assets --------------" end
获取Ocean.one的市场价格信息
如何查看Ocean.one市场的价格信息? 您需要知道交易的基础货币是什么。 如果你想买比特币,卖USDT,那么基础货币就是USDT; 如果你想买USDT,卖比特币,那么基础货币就是比特币。
if ocmd == "1" Utils.OceanOneMarketPriceRequest(BTC_ASSET_ID, USDT_ASSET_ID) end def self.OceanOneMarketPriceRequest(asset_id, base_asset_id) full_url = "https://events.ocean.one/markets/" + asset_id + "-" + base_asset_id + "/book" data = HTTP.get(full_url).body body = "" redData = data.readpartial while redData != nil body = body + redData redData = data.readpartial end result = ActiveSupport::JSON.decode(body).with_indifferent_access result["data"]["data"]["asks"].each { |x| puts x["side"] + " " + x["price"] + " " + x["amount"] + " " + x["funds"] } result["data"]["data"]["bids"].each { |x| puts x["side"] + " " + x["price"] + " " + x["amount"] + " " + x["funds"] } end
交易前,创建一个备忘录!
在第二章Ruby比特币开发教程:机器人接受比特币并立即退款给用户,我们已经学会了如何转账,这里介绍如何告诉Ocean.one转账的目的是什么,以及所有的信息放在备忘录中。
方向,“B”或“A”,“B”为买入,“A”为卖出。
目标虚拟资产的 targetAsset UUID。
price 价格,如果操作方向为“B”,则价格为AssetUUID的价格; 若操作方向为“B”,价格为转入Ocean.one币的价格。
def self.GenerateOceanMemo(targetAsset,side,price) memo = Base64.encode64(MessagePack.pack({ "A" => UUID.parse(targetAsset).to_raw, "S" => side, "P" => price, "T" => "L" })) return memo.sub(" ","") end
购买BTC的代码如下:
if ocmd == "b1" p "Input the price of BTC/USDT: " bprice = gets.chomp p "Input the amount of USDT: " amount = gets.chomp memo = Utils.GenerateOceanMemo(BTC_ASSET_ID,"B",bprice) p memo assetsInfo = walletAccount.read_asset(USDT_ASSET_ID) if assetsInfo["data"]["balance"].to_f >= 1 && assetsInfo["data"]["balance"].to_f >= amount.to_f transInfo = walletAccount.create_transfer(walletAccount.encrypt_pin(DEFAULT_PIN), { asset_id: USDT_ASSET_ID, opponent_id: OCEANONE_BOT, amount: amount, trace_id: SecureRandom.uuid, memo: memo }) p transInfo p "The Order id is " + transInfo["data"]["trace_id"] + " It"s needed by cancel-order!" else p "Not enough USDT" end end
卖出 BTC 的例子
将您计划出售的XIN转给Ocean.one(OCEANONE_BOT),并将您计划兑换的目标虚拟资产的UUID放入memo中。
if ocmd == "s1" p "Input the price of BTC/USDT: " bprice = gets.chomp p "Input the amount of BTC: " amount = gets.chomp memo = Utils.GenerateOceanMemo(USDT_ASSET_ID,"A",bprice) p memo assetsInfo = walletAccount.read_asset(BTC_ASSET_ID) if assetsInfo["data"]["balance"].to_f > 0 && assetsInfo["data"]["balance"].to_f >= amount.to_f transInfo = walletAccount.create_transfer(walletAccount.encrypt_pin(DEFAULT_PIN), { asset_id: BTC_ASSET_ID, opponent_id: OCEANONE_BOT, amount: amount, trace_id: SecureRandom.uuid, memo: memo }) p transInfo p "The Order id is " + transInfo["data"]["trace_id"] + " It"s needed by cancel-order!" else p "Not enough BTC" end end
一个成功的挂单如下:
Make your choose(eg: q for Exit!): b1 "Input the price of BTC/USDT: " 7777 "Input the amount of USDT: " 1 "hKFBxBDG0McoJiRCm44N2dGbZZL6oVOhQqFQpDc3NzehVKFM" {"data"=>{"type"=>"transfer", "snapshot_id"=>"10178f11-4e05-4076-b7c9-006e95919a1b", "opponent_id"=>"aaff5bef-42fb-4c9f-90e0-29f69176b7d4", "asset_id"=>"815b0b1a-2764-3736-8faa-42d694fa620a", "amount"=>"-1", "trace_id"=>"89025aab-598f-43e5-834a-2feaa01797ff", "memo"=>"hKFBxBDG0McoJiRCm44N2dGbZZL6oVOhQqFQpDc3NzehVKFM", "created_at"=>"2019-05-27T06:53:07.135709255Z", "counter_user_id"=>"aaff5bef-42fb-4c9f-90e0-29f69176b7d4"}} "The Order id is 89025aab-598f-43e5-834a-2feaa01797ff It"s needed by cancel-order!"
取消挂单
Ocean.one 将 trace_id 视为一个命令。 比如上面的例子,89025aab-598f-43e5-834a-2feaa01797ff就是订单号,我们用它来取消订单。
if ocmd == "c" p "Input the Order ID: " orderid = gets.chomp memo1 = Base64.encode64(MessagePack.pack({ "O" => UUID.parse(orderid).to_raw })) memo = memo1.sub(" ","") assetsInfo = walletAccount.read_asset(CNB_ASSET_ID) if assetsInfo["data"]["balance"].to_f > 0 transInfo = walletAccount.create_transfer(walletAccount.encrypt_pin(DEFAULT_PIN), { asset_id: CNB_ASSET_ID, opponent_id: OCEANONE_BOT, amount: "0.00000001", trace_id: SecureRandom.uuid, memo: memo }) p transInfo else p "Not enough CNB!" end end
通过读取资产余额确认到账
if cmd == "aw" assetsInfo = walletAccount.read_assets() p "--------The Wallet Assets List-----------------" assetsInfo["data"].each { |x| puts x["symbol"] + " " + x["balance"] + " " + x["public_key"] + x["account_name"] + " " + x["account_tag"]} p "----------End of Wallet Assets --------------" end
源代码执行
编译执行,就可以开始交易了。
源代码执行
编译执行,就可以开始交易了。
[x] ruby bitcoin-wallet-ruby.rb 运行。
这段代码执行时的命令列表:
1:获取BTC/USDT订单簿
2:获取XIN/USDT订单簿
3:获取ERC20/USDT订单簿
s1:卖出BTC/USDT
b1:买入BTC/USDT
s2:卖出XIN/USDT
b2:买入XIN/USDT
s3:卖出ERC20/USDT
s3:买入ERC20/USDT
c:取消订单
问:退出
做出您的选择(例如:q 退出!):
完整代码