Scheme 1: Exchange coins through ExinCore API

Exincore provides a Mixin Network-based coin exchange API.

You can pay USDT to ExinCore, and ExinCore will transfer the bitcoin you buy to you at the lowest price and the most favorable transaction fee. Every coin transaction is anonymous and can be verified on the blockchain. The details of the transaction are only known to you and ExinCore!

ExinCore doesn’t know who you are, it just knows your UUID.

Preliminary knowledge:

You first need to create a robot, the method in tutorial 1.

Installing dependency packages

As we explained in tutorial 1, we need to rely on mixin-SDK-Go, which you should have installed first. Here we will install uUID and MSgPack.

  go get -u github.com/vmihailenco/msgpack
  go get -u github.com/satori/go.uuid
Copy the code

Charge coins into the Mixin Network and read out its balance.

ExinCore can do BTC, USDT, EOS, ETH, etc. If you buy BTC with USDT or buy USDT with BTC, check the wallet address first! The complete steps are as follows:

  • Check bitcoin or USDT balance, wallet address. And write down the address of the wallet.
  • From a third party exchange or your cold wallet, charge the coins to the above wallet address.
  • Check the balance of currency again to see whether the account is accounted. (Bitcoin’s arrival time is about 100 minutes at a height of five blocks).

Note that bitcoin has the same address as USDT.

if cmd == "2" {
  userInfo, userID := ReadAssetInfo("BTC")
  fmt.Println("User ID ",userID, "'s BTC Address is: ",
             userInfo["data"]. (map[string]interface{})"public_key"])
  fmt.Println("Balance is: ",
             userInfo["data"]. (map[string]interface{})"balance"])}if cmd == "3" {
  userInfo, userID := ReadAssetInfo("USDT")
  fmt.Println("User ID ",userID, "'s USDT Address is: ",
             userInfo["data"]. (map[string]interface{})"public_key"])
  fmt.Println("Balance is: ",
             userInfo["data"]. (map[string]interface{})"balance"])}Copy the code

Check ExinCore market for price information

How about the price information of ExinCore market? If you want to buy bitcoin and sell USDT, the base currency is USDT. If you want to buy USDT and sell Bitcoin, then the base currency is bitcoin.

if cmd == "6" {
  priceInfo, err := GetMarketPrice(mixin.GetAssetId("USDT"))
  iferr ! =nil {
    log.Fatal(err)
  }

  var marketInfo map[string]interface{}
  err = json.Unmarshal([]byte(priceInfo), &marketInfo)
  fmt.Println("Asset | price | min | max | exchanges")
  for _, v := range (marketInfo["data"]. (map[string]interface{})) {
    fmt.Println(v.(map[string]interface{})"exchange_asset_symbol"]."/",
                v.(map[string]interface{})"base_asset_symbol"],
                v.(map[string]interface{})"price"],
                v.(map[string]interface{})"minimum_amount"],
                v.(map[string]interface{})"maximum_amount"],
                v.(map[string]interface{})"exchanges"],)}}func GetMarketPrice(asset_id string) ([]byte, error)  {
	var body []byte
	req, err := http.NewRequest("GET"."https://exinone.com/exincore/markets?base_asset="+asset_id, 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

Before trading, create a Memo!

In chapter 2, the Mixin Network-based PHP Bitcoin development tutorial: The robot accepts bitcoin and immediately returns it to the user. We have learned to return bitcoin to the user. Here, in addition to giving ExinCore payment coins, we also tell him what coins we want to buy, and store the coins in the Memo.

packUuid, _ := uuid.FromString(mixin.GetAssetId("BTC"))
pack, _ := msgpack.Marshal(OrderAction{A: packUuid})
memo := base64.StdEncoding.EncodeToString(pack)
// fmt.Println(memo)
Copy the code

The complete process of coin exchange

When transferring coins to ExinCore, write the memo into the coins you want to buy, otherwise, ExinCore will directly refund the coins to you! If you want to sell bitcoin and buy USDT, the call is as follows:

EXIN_BOT        = "61103d28-3ac2-44a2-ae34-bd956070dab1";

packUuid, _ := uuid.FromString(mixin.GetAssetId("USDT"))
pack, _ := msgpack.Marshal(OrderAction{A: packUuid})
memo := base64.StdEncoding.EncodeToString(pack)
// fmt.Println(memo)
priKey, pToken, sID, userID, uPIN := GetWalletInfo()
bt, err := mixin.Transfer(EXIN_BOT,"0.0001",mixin.GetAssetId("BTC"),memo,
                         messenger.UuidNewV4().String(),
                         uPIN,pToken,userID,sID,priKey)
iferr ! =nil {
        log.Fatal(err)
}
fmt.Println(string(bt))
Copy the code

If you want to sell USDT and buy bitcoin, the call is as follows:

packUuid, _ := uuid.FromString(mixin.GetAssetId("BTC"))
pack, _ := msgpack.Marshal(OrderAction{A: packUuid})
memo := base64.StdEncoding.EncodeToString(pack)
// fmt.Println(memo)
priKey, pToken, sID, userID, uPIN := GetWalletInfo()
bt, err := mixin.Transfer(EXIN_BOT,"0.0001",mixin.GetAssetId("USDT"),memo,
                         messenger.UuidNewV4().String(),
                         uPIN,pToken,userID,sID,priKey)
iferr ! =nil {
        log.Fatal(err)
}
fmt.Println(string(bt))
Copy the code

After the transaction is completed, Exincore will transfer the currency you need to your account. Also, Exincore will record the transaction price, transaction fee and other information in memo. You just need to press the following way to unlock it!

  • “ReadUserSnapshots” reads transaction records in your wallet.

Copy the code

A successful transaction is as follows:

------------MEMO:-coin--exchange-------------- memo: hqFDzQPooVCnMzg3Mi45N6FGqTAuMDAwNzc0NqJGQcQQgVsLGidkNzaPqkLWlPpiCqFUoUahT8QQIbfeL6p5RVOcEP0mLb+t+g== You Get Coins: 815B0B1A-2764-3736-8FAA-42D694FA620A 0.3857508 815B0B1A-2764-3736-8FAA-42D694FA620A fee IS :0.0007746 Order ID: 21 b7de2f - aa79-4553-9 c10 - fd262dbfadfa Price is: 3872.97 -- -- -- -- -- -- -- -- -- -- -- -- -- -- memo record end -- -- -- -- -- -- -- -- -- -- -- -- -- -- --Copy the code

Read the balance of coins

By reading the balance of the currency, to confirm the transaction situation!

if cmd == "2" {
  userInfo, userID := ReadAssetInfo("BTC")
  fmt.Println("User ID ",userID, "'s BTC Address is: ",
             userInfo["data"]. (map[string]interface{})"public_key"])
  fmt.Println("Balance is: ",
             userInfo["data"]. (map[string]interface{})"balance"])}if cmd == "3" {
  userInfo, userID := ReadAssetInfo("USDT")
  fmt.Println("User ID ",userID, "'s USDT Address is: ",
             userInfo["data"]. (map[string]interface{})"public_key"])
  fmt.Println("Balance is: ",
             userInfo["data"]. (map[string]interface{})"balance"])}Copy the code

Source code execution

To start trading, run coin_exchange.go.

  • 1: Create Wallet
  • 2: Read Bitcoin balance & Address
  • 3: Read USDT balance & Address
  • 4: Read EOS balance & address
  • 5: pay 0.0001 BTC buy USDT
  • 6: Read ExinCore Price(USDT)
  • 7: Read ExinCore Price(BTC)
  • 8: pay 1 USDT buy BTC
  • q: Exit Make your choose:

The complete code