用Ruby在去中心化交易所OceanOne上掛單買賣任意ERC20 token

上一課中,咱們介紹瞭如何在OceanOne交易比特幣。OceanOne支持交易任何Mixin Network上的token,包括全部的ERC20和EOS token,不須要任何手續和費用,直接掛單便可。下面介紹如何將將一個ERC20 token掛上OceanOne交易!在掌握了ERC20 token以後,就能夠把任何token在Ocean上買賣。git

此處咱們用一個叫作Benz的ERC20 token爲例。這個token已經被充值進Mixin Network,你能夠在區塊鏈瀏覽器看到這個token在Mixin Network內部的總數和交易github

預備知識:

先將Benz幣存入你的錢包,而後使用getAssets API讀取它的UUID.json

取得該幣的UUID

調用 getAssets API 會返回json數據, 如:瀏覽器

  • asset_id 幣的UUID.
  • public_key 該幣的當前錢包的地址.
  • symbol 幣的名稱. 如: Benz.
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

調用 read_assets API的完整輸出以下:ruby

"--------The Wallet Assets List-----------------"
Benz 10.03 0x822664c2EFb27E2Eb4c4286f421B4BF6FB943fC6
ETH 0 0x822664c2EFb27E2Eb4c4286f421B4BF6FB943fC6
EOS 0 eoswithmixin b0adfae2f8828d15e11cb1fbe23d6096
USDT 1 1KB4RbV5W4MNybpjcJjULKNVXubfR5MJqA
CNB 0.99999995 0x822664c2EFb27E2Eb4c4286f421B4BF6FB943fC6
BTC 0 1KB4RbV5W4MNybpjcJjULKNVXubfR5MJqA
"----------End of Wallet Assets --------------"
-------------------------------------------------------------------------

限價掛單

  • 掛限價買單 低於或者等於市場價的單.
  • 掛限價賣單 高於或者是等於市場價的單.

OceanOne支持三種基類價格: USDT, XIN, BTC, 即: Benz/USDT, Benz/XIN, Benz/BTC, 這兒示範Benz/USDT.bash

限價掛賣單.

新幣掛單後,須要等一分鐘左右,等OceanOne來初始化新幣的相關數據.dom

if ocmd == "s3"
  p "Input the price of ERC/USDT: "
  bprice = gets.chomp
  p "Input the amount of ERC20_BENZ: "
  amount = gets.chomp
  memo = Utils.GenerateOceanMemo(USDT_ASSET_ID,"A",bprice)
  p memo
  assetsInfo = walletAccount.read_asset(ERC20_BENZ)
  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: ERC20_BENZ,
                                        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 ERC20_BENZ"
  end
end

限價掛買單.

新幣掛單後,須要等一分鐘左右,等OceanOne來初始化新幣的相關數據.ide

if ocmd == "b3"
  p "Input the price of ERC20/USDT: "
  bprice = gets.chomp
  p "Input the amount of USDT: "
  amount = gets.chomp
  memo = Utils.GenerateOceanMemo(ERC20_BENZ,"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

讀取幣的價格列表

讀取幣的價格列表,來確認掛單是否成功!區塊鏈

if ocmd == "3"
  Utils.OceanOneMarketPriceRequest(ERC20_BENZ, 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

ERC20相關的操做指令

Commands list of this source code:ui

  • 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!):

完整的代碼

個人博客即將同步至騰訊雲+社區,邀請你們一同入駐:https://cloud.tencent.com/developer/support-plan?invite_code=cw4h96k6poj4
相關文章
相關標籤/搜索