用 Ruby 買賣Bitcoin: 閃兌交易所 Exincore

方案一: 經過ExinCore API進行幣幣交易

Exincore 提供了基於Mixin Network的幣幣交易API.git

你能夠支付USDT給ExinCore, ExinCore會以最低的價格,最優惠的交易費將你購買的比特幣轉給你, 每一幣交易都是匿名的,而且能夠在區塊鏈上進行驗證,交易的細節只有你與ExinCore知道!github

ExinCore 也不知道你是誰,它只知道你的UUID.ruby

預備知識:

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

安裝依賴包

正如教程一里咱們介紹過的, 咱們須要依賴 mixin-bot, 你應該先安裝過它了, 這兒咱們再安裝 easy-uuid, msgpack 兩個軟件包.dom

gem install msgpack
 gem install easy-uuid
複製代碼

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

ExinCore能夠進行BTC, USDT, EOS, ETH 等等交易, 這兒演示若是用 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
複製代碼

查詢ExinCore市場的價格信息

若是來查詢ExinCore市場的價格信息呢?你要先了解你交易的基礎幣是什麼,若是你想買比特幣,賣出USDT,那麼基礎貨幣就是USDT;若是你想買USDT,賣出比特幣,那麼基礎貨幣就是比特幣.ui

if cmd == "8"
  Utils.ExinCoreMarketPriceRequest(USDT_ASSET_ID)
end
if cmd == "9"
  Utils.ExinCoreMarketPriceRequest(BTC_ASSET_ID)
end

def self.ExinCoreMarketPriceRequest(asset_id)
  full_url = "https://exinone.com/exincore/markets?base_asset=" + asset_id
  p full_url
  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"].each { |x|
        puts x["exchange_asset_symbol"] +  "/" +  x["base_asset_symbol"] + " " +
             x["price"] + " " + x["minimum_amount"] + " " + x["maximum_amount"] +
             " " + x["exchanges"][0]
      }
end
複製代碼

交易前,建立一個Memo!

在第二章裏,基於Mixin Network的Ruby比特幣開發教程: 機器人接受比特幣並當即退還用戶, 咱們學習過退還用戶比特幣,在這裏,咱們除了給ExinCore支付幣外,還要告訴他咱們想購買的幣是什麼,即將想購買的幣存到memo裏。url

memo1 = Base64.encode64(MessagePack.pack({
'A' => UUID.parse(USDT_ASSET_ID).to_raw
}))
memo = memo1.sub("\n","")
複製代碼

幣幣交易的完整流程

轉幣給ExinCore時,將memo寫入你但願購買的幣, 這個memo別錯了,不然,ExinCore須要人工操做來退幣! 若是你想賣出比特幣買入USDT,調用方式以下:spa

const EXIN_BOT        = "61103d28-3ac2-44a2-ae34-bd956070dab1";
const BTC_ASSET_ID    = "c6d0c728-2624-429b-8e0d-d9d19b6592fa";
const EOS_ASSET_ID    = "6cfe566e-4aad-470b-8c9a-2fd35b49c68d";
const USDT_ASSET_ID   = "815b0b1a-2764-3736-8faa-42d694fa620a";
if cmd == "5"
  memo1 = Base64.encode64(MessagePack.pack({
  'A' => UUID.parse(USDT_ASSET_ID).to_raw
  }))
  memo = memo1.sub("\n","")
  p memo
  transInfo = walletAccount.create_transfer(walletAccount.encrypt_pin(DEFAULT_PIN),
                                    {
                                      asset_id: BTC_ASSET_ID,
                                      opponent_id: EXIN_BOT,
                                      amount: "0.0001",
                                      trace_id: SecureRandom.uuid,
                                      memo: memo
                                    })
   p transInfo
end
複製代碼

若是你想賣出USDT買入比特幣,調用方式以下:

if cmd == "6"
  memo1 = Base64.encode64(MessagePack.pack({
  'A' => UUID.parse(BTC_ASSET_ID).to_raw
  }))
  memo = memo1.sub("\n","")
  p memo
  transInfo = walletAccount.create_transfer(walletAccount.encrypt_pin(DEFAULT_PIN),
                                    {
                                      asset_id: USDT_ASSET_ID,
                                      opponent_id: EXIN_BOT,
                                      amount: "1",
                                      trace_id: SecureRandom.uuid,
                                      memo: memo
                                    })
   p transInfo
end
複製代碼

交易完成後,Exincore會將你須要的幣轉到你的賬上,一樣,會在memo裏,記錄成交價格,交易費用等信息!你只須要按下面的方式解開便可!

  • read_snapshots 讀取錢包的交易記錄。
if cmd == "7"
  if wallet_userid == "5e4ad097-21e8-3f6b-98f7-9dc74dd99f77"
    dt = "2019-05-23T09:48:04.582099Z"
  else
    puts "Input the snapshots datetime: "
    dt = gets.chomp
  end
  p CGI.escape(dt)
  snaps = walletAccount.read_snapshots({
                                      limit: "10",
                                      offset: CGI.escape(dt),
                                      asset: BTC_ASSET_ID,
                                      order: "ASC"
                                      })
  snaps["data"].each { |x|
                      if x["amount"].to_f > 0
                        if x["data"] != nil
                          uData = MessagePack.unpack(Base64.decode64(x["data"]))
                          p uData
                          p "The code is " + uData["C"].to_s + " Price is " + uData["P"] + " Fee is " +
                            uData["F"]
                          p "Fee is Asset UUID is " + UUID.parse(uData["FA"]).to_s
                          p "Order id is " + UUID.parse(uData["O"]).to_s
                        end
                      end
                      }
end
複製代碼

一次成功的交易以下:

Make your choose(eg: q for Exit!):
7
"2019-05-23T09%3A48%3A04.582099Z"
{"C"=>1000, "P"=>"7562.91", "F"=>"0.000000264", "FA"=>"\xC6\xD0\xC7(&$B\x9B\x8E\r\xD9\xD1\x9Be\x92\xFA", "T"=>"R", "O"=>"\xC4\x00*\a\xF1\xB1C9\xBC\xCE%\xC3C\x88^R"}
"The code is 1000 Price is 7562.91 Fee is 0.000000264"
"Fee is Asset UUID is c6d0c728-2624-429b-8e0d-d9d19b6592fa"
"Order id is c4002a07-f1b1-4339-bcce-25c343885e52"
-------------------------------------------------------------------------
複製代碼

讀取幣的餘額

經過讀取幣的餘額,來確認交易狀況!

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: Create Bitcoin Wallet and update PIN
  • 2: Read Bitcoin balance & address
  • 3: Read USDT balance & address
  • 4: Read EOS balance & address
  • tbb:Transfer BTC from Bot to Wallet
  • tbm:Transfer BTC from Wallet to Master
  • teb:Transfer EOS from Bot to Wallet
  • tem:Transfer EOS from Wallet to Master
  • tub:Transfer USDT from Bot to Wallet
  • tum:Transfer USDT from Wallet to Master
  • tcb:Transfer CNB from Bot to Wallet
  • tcm:Transfer CNB from Wallet to Master
  • 5: Pay 0.0001 BTC to ExinCore buy USDT
  • 6: Pay $1 USDT to ExinCore buy BTC
  • 7: Read Snapshots
  • 8: Fetch market price(USDT)
  • 9: Fetch market price(BTC)
  • ab: Read Bot Assets
  • aw: Read Wallet Assets
  • q: Exit Make your choose(eg: q for Exit!):

完整代碼

Solution Two: List your order on Ocean.One exchange

相關文章
相關標籤/搜索