經過 Ruby 買賣Bitcoin:使用開放交易所OceanOne

上一章介紹了Exincore,你能夠1秒完成資產的市價買賣。若是你想限訂價格買賣,或者買賣一些exincore不支持的資產,你須要OceanOne。git

方案二: 掛單Ocean.One交易所

Ocean.one是基於Mixin Network的去中心化交易所,它性能一流。 你能夠在OceanOne上交易任何資產,只須要將你的幣轉給OceanOne, 將交易信息寫在交易的memo裏,OceanOne會在市場裏列出你的交易需求, 交易成功後,會將目標幣轉入到你的MixinNetwork賬上,它有三大特色與優點:github

  • 不須要在OceanOne註冊
  • 不須要存幣到交易所
  • 支持全部Mixin Network上可以轉帳的資產,全部的ERC20 EOS代幣。

預備知識:

你先須要建立一個機器人, 方法在 教程一.ruby

安裝依賴包

咱們須要依賴 msgpack and mixin-bot ,第四章 已經作過介紹, 你應該先安裝過它了.bash

充幣到 Mixin Network, 並讀出它的餘額.

此處演示用 USDT購買BTC 或者 用BTC購買USDT。交易前,先檢查一下錢包地址。 完整的步驟以下:dom

  • 檢查比特幣或USDT的餘額,錢包地址。並記下錢包地址。
  • 從第三方交易所或者你的冷錢包中,將幣充到上述錢包地址。
  • 再檢查一下幣的餘額,看到賬與否。(比特幣的到賬時間是5個區塊的高度,約100分鐘)。

比特幣與USDT的充值地址是同樣的。ide

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
複製代碼

交易前,建立一個Memo!

在第二章裏,Ruby比特幣開發教程: 機器人接受比特幣並當即退還用戶, 咱們學習過轉賬,這兒咱們介紹如何告訴Ocean.one,咱們給它轉賬的目的是什麼,信息所有放在memo裏.學習

  • side 方向,"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("\n","")
end
複製代碼

買入BTC的代碼以下:ui

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.url

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("\n","")
  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
複製代碼

源代碼執行

編譯執行,便可開始交易了.

源代碼執行

編譯執行,便可開始交易了.

  • ruby bitcoin-wallet-ruby.rb 運行.

本代碼執行時的命令列表:

  • 1: Fetch BTC/USDT Order Book
  • 2: Fetch XIN/USDT Order Book
  • 3: Fetch ERC20/USDT Order Book
  • s1: Sell BTC/USDT
  • b1: Buy BTC/USDT
  • s2: Sell XIN/USDT
  • b2: Buy XIN/USDT
  • s3: Sell ERC20/USDT
  • s3: Buy ERC20/USDT
  • c: Cancel the order
  • q: Exit Make your choose(eg: q for Exit!):

完整代碼

相關文章
相關標籤/搜索