在上一課中,咱們介紹瞭如何在OceanOne交易比特幣。OceanOne支持交易任何Mixin Network上的token,包括全部的ERC20和EOS token,不須要任何手續和費用,直接掛單便可。下面介紹如何將將一個ERC20 token掛上OceanOne交易。掌握了ERC20的代幣買賣以後,你就能夠用一樣的方法買賣任何EOS以及其餘Mixin Network上的tokengit
此處咱們用一個叫作Benz的ERC20 token爲例。這個token已經被充值進Mixin Network,你能夠在區塊鏈瀏覽器看到這個token在Mixin Network內部的總數和交易github
先將Ben幣存入你的錢包,而後使用getAssets API讀取它的UUID.json
調用 getAssets API 會返回json數據, 如:瀏覽器
if (cmd == "aw" ) { // Console.WriteLine(mixinApi.VerifyPIN(USRCONFIG.PinCode.ToString()).ToString()); MixinApi mixinApiNewUser = GetWalletSDK(); var assets = mixinApiNewUser.ReadAssets(); string wuuid = GetWalletUUID(); Console.WriteLine("Current wallet uuid is " + wuuid); foreach (Asset asset in assets) { if (asset.symbol == "EOS") { Console.WriteLine(asset.symbol + " Public Address is: " + asset.account_name + " " + asset.account_tag + " Balance is: " + asset.balance); } else Console.WriteLine(asset.symbol + " Public Address is: " + asset.public_key + " Balance is: " + asset.balance); Console.WriteLine(); } }
調用 getAssets API的完整輸出以下:bash
Make your choose: aw Current wallet uuid is 85d5609d-d93b-3c96-96f6-58357c5d99eb Benz Public Address is: 0x5fD0F147830a186545e6020F58fEc0c4B39065D4 Balance is: 1 EOS Public Address is: eoswithmixin 30399f17622cb2bfc57efd3393144bf9 Balance is: 0 USDT Public Address is: 1JvQ98N5Y8TvDbXr8eA8DHsNZqEuGbzzng Balance is: 1 BTC Public Address is: 1JvQ98N5Y8TvDbXr8eA8DHsNZqEuGbzzng Balance is: 0 XIN Public Address is: 0x5fD0F147830a186545e6020F58fEc0c4B39065D4 Balance is: 0.01
OceanOne支持三種基類價格: USDT, XIN, BTC, 即: Benz/USDT, Benz/XIN, Benz/BTC, 這兒示範Benz/USDT.async
新幣掛單後,須要等一分鐘左右,等OceanOne來初始化新幣的相關數據.ide
public static string ERC20_BENZ = "2b9c216c-ef60-398d-a42a-eba1b298581d"; if ( cmdo == "s2") { Console.WriteLine("Please input the price of ERC20/USDT: "); var pinput = Console.ReadLine(); Console.WriteLine("Please input the amount of ERC20: "); var ainput = Console.ReadLine(); string memo = GenerateOrderMemo("A",USRCONFIG.ASSET_ID_USDT,pinput); Console.WriteLine(memo); // Console.WriteLine(Convert.ToBase64String(stream3.ToArray())); MixinApi mixinApiNewUser = GetWalletSDK(); var assets = mixinApiNewUser.ReadAsset(USRCONFIG.ERC20_BENZ); float balance = float.Parse(assets.balance); float amount = float.Parse(ainput); if ( ( balance >= 0 ) && ( balance >= amount ) ) { Transfer reqInfo = mixinApiNewUser.Transfer(USRCONFIG.ERC20_BENZ, USRCONFIG.OCEANONE_BOT, ainput, GetWalletPinCode(), System.Guid.NewGuid().ToString(), memo); Console.WriteLine(reqInfo); Console.WriteLine("Order id is " + reqInfo.trace_id); } else Console.WriteLine("Not enough ERC20_BENZ!"); }
新幣掛單後,須要等一分鐘左右,等OceanOne來初始化新幣的相關數據.區塊鏈
if ( cmdo == "b2") { Console.WriteLine("Please input the price of ERC20_BENZ/USDT: "); var pinput = Console.ReadLine(); Console.WriteLine("Please input the amount of USDT: "); var ainput = Console.ReadLine(); string memo = GenerateOrderMemo("B",USRCONFIG.ERC20_BENZ,pinput); Console.WriteLine(memo); MixinApi mixinApiNewUser = GetWalletSDK(); var assets = mixinApiNewUser.ReadAsset(USRCONFIG.ASSET_ID_USDT); Console.WriteLine(assets.balance); float balance = float.Parse(assets.balance); float amount = float.Parse(ainput); if ( ( balance >= 1.0 ) && ( balance >= amount ) ) { Transfer reqInfo = mixinApiNewUser.Transfer(USRCONFIG.ASSET_ID_USDT, USRCONFIG.OCEANONE_BOT, ainput, GetWalletPinCode(), System.Guid.NewGuid().ToString(), memo); Console.WriteLine(reqInfo); Console.WriteLine("Order id is " + reqInfo.trace_id); } else Console.WriteLine("Not enough USDT!"); }
讀取幣的價格列表,來確認掛單是否成功!ui
if (cmdo == "2") { string jsonData = FetchOceanMarketPrice(USRCONFIG.ERC20_BENZ,USRCONFIG.ASSET_ID_USDT); // string jsonData = FetchMarketPrice("c6d0c728-2624-429b-8e0d-d9d19b6592fa"); var marketObj = JsonConvert.DeserializeObject<MarketInfoOcean>(jsonData); Console.WriteLine("--Price--Amount---Funds---Side----"); foreach (order value in marketObj.data.data.asks) { Console.WriteLine(value.price + " " + value.amount + " " + value.funds + " " + value.side); } foreach (order value in marketObj.data.data.bids) { Console.WriteLine(value.price + " " + value.amount + " " + value.funds + " " + value.side); } } public static string FetchOceanMarketPrice(string asset_id, string base_asset) { return FetchOceanMarketPriceAsync(asset_id,base_asset).Result; } public static async Task<string> FetchOceanMarketPriceAsync(string asset_id, string base_asset) { HttpClient client = new HttpClient(); string baseUrl = "https://events.ocean.one/markets/" + asset_id + "-" + base_asset + "/book"; try { HttpResponseMessage response = await client.GetAsync(baseUrl); response.EnsureSuccessStatusCode(); string responseBody = await response.Content.ReadAsStringAsync(); // Above three lines can be replaced with new helper method below // string responseBody = await client.GetStringAsync(uri); Console.WriteLine(responseBody); return responseBody; } catch(HttpRequestException e) { Console.WriteLine("\nException Caught!"); Console.WriteLine("Message :{0} ",e.Message); } return null; }
Commands list of this source code:this
Make your choose: