上一章介紹了Exincore,你能夠1秒完成資產的市價買賣。若是你想限訂價格買賣,或者買賣一些exincore不支持的資產,你須要OceanOne。git
Ocean.one是基於Mixin Network的去中心化交易所,它性能一流。 你能夠在OceanOne上交易任何資產,只須要將你的幣轉給OceanOne, 將交易信息寫在交易的memo裏,OceanOne會在市場裏列出你的交易需求, 交易成功後,會將目標幣轉入到你的MixinNetwork賬上,它有三大特色與優點:github
你先須要建立一個機器人, 方法在 教程一.ruby
咱們須要依賴 msgpack and mixin-bot ,第四章 已經作過介紹, 你應該先安裝過它了.bash
此處演示用 USDT購買BTC 或者 用BTC購買USDT。交易前,先檢查一下錢包地址。 完整的步驟以下:dom
比特幣與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市場的價格信息呢?你要先了解你交易的基礎幣是什麼,若是你想買比特幣,賣出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,咱們給它轉賬的目的是什麼,信息所有放在memo裏.學習
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
複製代碼
轉打算出售的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
複製代碼
編譯執行,便可開始交易了.
編譯執行,便可開始交易了.
本代碼執行時的命令列表: