In the last lesson, we explained how to trade bitcoin in OceanOne. OceanOne supports the exchange of tokens on any Mixin Network, including all ERC20 and EOS tokens, with no formalities and no fees. Here’s how to hang up an ERC20 token for OceanOne transaction! After mastering the ERC20 token, you can buy and sell any token on Ocean.

Here we use an ERC20 token called Benz. This token has been charged to the Mixin Network, and you can see the total number and transactions of this token within the Mixin Network in the blockchain browser

Preliminary knowledge:

Deposit the Ben coin in your wallet and then use the ReadAssets API to read its UUID.

Get the UUID of the coin

Calling the ReadAssets API returns JSON data, such as:

  • UUID asset_id currency.
  • Public_key Current wallet address of the coin.
  • Symbol The name of a currency. Such as: Benz.
if cmd == "aw" {
  priKey, _, sID, userID, _ := GetWalletInfo()
  assets, err := mixin.ReadAssets(userID,sID,priKey)
  iferr ! =nil {
      log.Fatal(err)
  }
  var AssetsInfo map[string]interface{}
  err = json.Unmarshal(assets, &AssetsInfo)
  iferr ! =nil {
      log.Fatal(err)
  }
  // fmt.Println("Data is: ",AssetsInfo["data"].(map[string]interface{})["public_key"])
  for _, v := range (AssetsInfo["data"([]].interface{{}))if v.(map[string]interface{})"symbol"] = ="EOS" {
      fmt.Println(v.(map[string]interface{})"symbol"]."",
                  v.(map[string]interface{})"asset_id"]."",
                  v.(map[string]interface{})"account_name"]."",
                  v.(map[string]interface{})"account_tag"]."",
                  v.(map[string]interface{})"balance"])}else {
      fmt.Println(v.(map[string]interface{})"symbol"]."",
                  v.(map[string]interface{})"asset_id"]."",
                  v.(map[string]interface{})"public_key"]."",
                  v.(map[string]interface{})"balance"])}}}Copy the code

The complete output from a call to the ReadAssets API is as follows:

Make your choose: aw Benz 0 x330860ec473ff366f5bc4339a69f5bffb52d18cb 88.9 EOS eoswithmixin 79dd76cedf8f6af49a8d98216bbde890 0 USDT 16wWhKAjmACvZzkfxkyrVutqfrJ1JQ83aj 1 CNB 16 wwhkajmacvzzkfxkyrvutqfrj1jq83aj 0 0 x330860ec473ff366f5bc4339a69f5bffb52d18cb 0.10999989 BTC XIN 0 x330860ec473ff366f5bc4339a69f5bffb52d18cb 0.01Copy the code

Price are cancelled

  • An order that is below or equal to the market price.
  • Limit order An order higher than or equal to the market price.

OceanOne supports three base price categories: USDT, XIN, BTC, namely: Benz/USDT, Benz/XIN, Benz/BTC, Benz/USDT here.

Limit price hang sell order.

After the new currency is listed, we need to wait for about a minute for OceanOne to initialize the relevant data of the new currency.

if cmd == "s2" {
  fmt.Print("Please input the price of ERC20/USDT: ")
  var pcmd string
  var acmd string
  scanner.Scan()
  pcmd = scanner.Text()
  fmt.Println(pcmd)
  fmt.Print("Please input the amount of ERC20: ")
  scanner.Scan()
  acmd = scanner.Text()
  fmt.Println(acmd)
  omemo := generateOceanOrderMemo(mixin.GetAssetId("USDT"),"A",pcmd)
  priKey, pToken, sID, userID, uPIN := GetWalletInfo()
  balance := ReadAssetBalanceByUUID(ERC20_BENZ,userID,sID,priKey)
  fmt.Println(balance)
  fbalance, _ := strconv.ParseFloat(balance,64)
  abalance, _ := strconv.ParseFloat(acmd,64)
  if fbalance > 0 && fbalance >= abalance {
    fmt.Println(omemo)
    transInfo, _ := mixin.Transfer(OCEANONE_BOT,
                                   acmd,
                                   ERC20_BENZ,
                                   omemo,
                                   messenger.UuidNewV4().String(),
                                   uPIN,pToken,userID,sID,priKey)
    fmt.Println(string(transInfo))
    var jsTransInfo map[string]interface{}
    err := json.Unmarshal([]byte(transInfo), &jsTransInfo)
    iferr ! =nil {
        log.Fatal(err)
    }
    fmt.Println("The Order id is " + jsTransInfo["data"]. (map[string]interface{})"trace_id"]. (string) +
               " it is needed to cancel the order!")}else { fmt.Println("Not enough BenZ!")}}Copy the code

Limit price hang buy.

After the new currency is listed, we need to wait for about a minute for OceanOne to initialize the relevant data of the new currency.

if cmd == "b2" {
  fmt.Print("Please input the price of ERC20/USDT: ")
  var pcmd string
  var acmd string
  scanner.Scan()
  pcmd = scanner.Text()
  fmt.Println(pcmd)
  fmt.Print("Please input the amount of USDT: ")
  scanner.Scan()
  acmd = scanner.Text()
  fmt.Println(acmd)
  omemo := generateOceanOrderMemo(ERC20_BENZ,"B",pcmd)
  priKey, pToken, sID, userID, uPIN := GetWalletInfo()
  balance := ReadAssetBalance("USDT",userID,sID,priKey)
  fmt.Println(balance)
  fbalance, _ := strconv.ParseFloat(balance,64)
  abalance, _ := strconv.ParseFloat(acmd,64)
  if fbalance > 0 && fbalance >= abalance {
    fmt.Println(omemo)
    transInfo, _ := mixin.Transfer(OCEANONE_BOT,
                                   acmd,
                                   mixin.GetAssetId("USDT"),
                                   omemo,
                                   messenger.UuidNewV4().String(),
                                   uPIN,pToken,userID,sID,priKey)
    fmt.Println(string(transInfo))
    var jsTransInfo map[string]interface{}
    err := json.Unmarshal([]byte(transInfo), &jsTransInfo)
    iferr ! =nil {
        log.Fatal(err)
    }
    fmt.Println("The Order id is " + jsTransInfo["data"]. (map[string]interface{})"trace_id"]. (string) +
               " it is needed to cancel the order!")}else { fmt.Println("Not enough USDT!")}}//end of b2
Copy the code

Read the price list of coins

Read the price list of coins to confirm whether the listing is successful!

if cmd == "3" {
   FormatOceanOneMarketPrice(mixin.GetAssetId("BTC"),mixin.GetAssetId("USDT"))}func FormatOceanOneMarketPrice(asset_id string, base_asset string) {
 priceInfo, err := GetOceanOneMarketPrice(asset_id, base_asset)
 iferr ! =nil {
   log.Fatal(err)
 }

 var marketInfo map[string]interface{}
 err = json.Unmarshal([]byte(priceInfo), &marketInfo)
  fmt.Println("Price | Amount | Funds | Side")
 for _, v := range (marketInfo["data"].
                   (map[string]interface{})"data"].
                   (map[string]interface{})"asks"([]].interface{})) {
   fmt.Println(v.(map[string]interface{})"price"],
               v.(map[string]interface{})"amount"],
               v.(map[string]interface{})"funds"],
               v.(map[string]interface{})"side"],)}for _, v := range (marketInfo["data"].
                   (map[string]interface{})"data"].
                   (map[string]interface{})"bids"([]].interface{})) {
   fmt.Println(v.(map[string]interface{})"price"],
               v.(map[string]interface{})"amount"],
               v.(map[string]interface{})"funds"],
               v.(map[string]interface{})"side"],)}}func GetOceanOneMarketPrice(asset_id string, base_asset string) ([]byte, error)  {
	var body []byte
	req, err := http.NewRequest("GET"."https://events.ocean.one/markets/" + asset_id + "-" + base_asset + "/book",
                             bytes.NewReader(body))
	iferr ! =nil {
		return nil, err
	}
	req.Header.Set("Content-Type"."application/json")

	resp, err := httpClient.Do(req)
	iferr ! =nil {
		return nil, err
	}
	defer resp.Body.Close()
 // fmt.Println(resp.Body)
	bt, err := ioutil.ReadAll(resp.Body)
	ifresp.StatusCode ! = http.StatusOK {var resp struct {
			Error Error `json:"error"`
		}
		err = json.Unmarshal(bt, &resp)
		if err == nil {
			err = resp.Error
		}
	}
	return bt, err
}
Copy the code

ERC20 related operation instructions

Commands list of this source code:

  • trb:Transfer ERC20 from Bot to Wallet
  • trm:Transfer ERC20 from Wallet to Master
  • o: Ocean.One Exchange

Make your choose(eg: q for Exit!) :

  • 3: Orders-Book of ERC20/USDT
  • b3: Buy ERC20 pay USDT
  • s3: Sell ERC20 get USDT
  • c: Cancel the order
  • q: Exit

Complete code