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

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

cover

上一課中,我們介紹瞭如何在OceanOne交易比特幣。OceanOne支持交易任何Mixin Network上的token,包括所有的ERC20和EOS token,不需要任何手續和費用,直接掛單即可。下面介紹如何將將一個ERC20 token掛上OceanOne交易。掌握了ERC20的代幣買賣之後,你就可以用同樣的方法買賣任何EOS以及其他Mixin Network上的token

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

預備知識:

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

取得該幣的UUID

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

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

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.

限價掛賣單.

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

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!");
}

讀取幣的價格列表

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

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;
}

ERC20相關的操作指令

Commands list of this source code:

  • teb:Transfer ERC20 from Bot to Wallet
  • tem:Transfer ERC20 from Wallet to Master
  • o: Ocean.One Exchange

Make your choose:

  • 1: Fetch XIN/USDT orders
  • s1: Sell XIN/USDT
  • b1: Buy XIN/USDT
  • 2: Fetch ERC20(Benz)/USDT orders
  • s2: Sell Benz/USDT
  • b2: Buy Benz/USDT
  • c: Cancel Order
  • q: Exit

完整的代碼

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章