在上一课中,我们介绍了如何在 OceanOne 交易比特币。OceanOne 支持交易任何 Mixin Network 上的 token,包括所有的 ERC20 和 EOS token,不需要任何手续和费用,直接挂单即可。下面介绍如何将将一个 ERC20 token 挂上 OceanOne 交易!在掌握了 ERC20 token 之后,就可以把任何 token 在 Ocean 上买卖。
此处我们用一个叫做 Benz 的 ERC20 token 为例。这个 token 已经被充值进 Mixin Network,你可以在区块链浏览器看到这个 token 在 Mixin Network 内部的总数和交易
预备知识:
先将 Benz 币存入你的钱包,然后使用 getAssets API 读取它的 UUID.
取得该币的 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 的完整输出如下:
"--------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.
限价挂卖单.
新币挂单后, 需要等一分钟左右,等 OceanOne 来初始化新币的相关数据.
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 来初始化新币的相关数据.
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:
- 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