HOME/PHP Bitcoin tutorial based on Mixin Network/

How to trade bitcoin through PHP language

Article Outline
TOC
Collection Outline

How to trade bitcoin through PHP language

null

Solution One: pay to ExinCore API

Exincore provide a commercial trading API on Mixin Network.

You pay USDT to ExinCore, ExinCore transfer Bitcoin to you on the fly with very low fee and fair price. Every transaction is anonymous to public but still can be verified on blockchain explorer. Only you and ExinCore know the details.

ExinCore don't know who you are because ExinCore only know your client's uuid.

Pre-request:

You should have created a bot based on Mixin Network. Create one by reading PHP Bitcoin tutorial.

Install required packages

As you know, we introduce you the mixin-sdk-php in chapter 1, assume it has installed before, let's install uuid, msgpack here.

  composer require ramsey/uuid
  composer require rybakit/msgpack

Deposit USDT or Bitcoin into your Mixin Network account and read balance

ExinCore can exchange between Bitcoin, USDT, EOS, Eth etc. Here show you how to exchange between USDT and Bitcoin, Check the wallet's balance & address before you make order.

  • Check the address & balance, remember it Bitcoin wallet address.
  • Deposit Bitcoin to this Bitcoin wallet address.
  • Check Bitcoin balance after 100 minutes later.

By the way, Bitcoin & USDT 's address are the same.

if ($line == '2') {
  if (($handle = fopen("new_users.csv", "r")) !== FALSE) {
  while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
    $mixinSdk_eachAccountInstance = new MixinSDK(GenerateConfigByCSV($data));
    $asset_info = $mixinSdk_eachAccountInstance->Wallet()->readAsset(BTC_ASSET_ID);
    print_r("Bitcoin wallet address is :".$asset_info["public_key"]."\n");
    print_r("Bitcoin wallet balance is :".$asset_info["balance"]."\n");
  }
    fclose($handle);
  } else print("Create user first\n");
}
if ($line == '3') {
  if (($handle = fopen("new_users.csv", "r")) !== FALSE) {
  while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
    $mixinSdk_eachAccountInstance = new MixinSDK(GenerateConfigByCSV($data));
    $asset_info = $mixinSdk_eachAccountInstance->Wallet()->readAsset(USDT_ASSET_ID);
    print_r("USDT wallet address is :".$asset_info["public_key"]."\n");
    print_r("USDT wallet balance is :".$asset_info["balance"]."\n");
  }
    fclose($handle);
  } else print("Create user first\n");
}

Read market price

How to check the coin's price? You need understand what is the base coin. If you want buy Bitcoin and sell USDT, the USDT is the base coin. If you want buy USDT and sell Bitcoin, the Bitcoin is the base coin.

function getExchangeCoins($base_coin) :string {
  $client = new GuzzleHttp\Client();
  $res = $client->request('GET', 'https://exinone.com/exincore/markets?base_asset='.$base_coin, [
      ]);
  $result = "";
  if ($res->getStatusCode() == "200") {
    // echo $res->getStatusCode() . PHP_EOL;
    $resInfo = json_decode($res->getBody(), true);
    echo "Asset ID | Asset Symbol | Price | Amount | Exchanges" . PHP_EOL;
    $result = "Asset ID | Asset Symbol | Price | Amount | Exchanges" . PHP_EOL;
    foreach ($resInfo["data"] as $key => $coinInfo) {
      echo ($coinInfo["exchange_asset"] ." ".$coinInfo["exchange_asset_symbol"]. "/". $coinInfo["base_asset_symbol"] .
            " ". $coinInfo["price"] ." ". $coinInfo["minimum_amount"] ."-". $coinInfo["maximum_amount"] . " ");
      $result .= $coinInfo["exchange_asset_symbol"]. "/". $coinInfo["base_asset_symbol"] .
                  " ". $coinInfo["price"] ." ". $coinInfo["minimum_amount"] ."-". $coinInfo["maximum_amount"] . " ";
      foreach ($coinInfo["exchanges"] as $key => $exchange) {
        echo $exchange . " ";
        $result .= $exchange . " ";
      }
      echo PHP_EOL;
      $result .= PHP_EOL;
    }
  }
  return $result;
}

Create a memo to prepare order

The chapter two: Echo Bitcoin introduce transfer coins. But you need to let ExinCore know which coin you want to buy. Just write your target asset into memo.

$memo = base64_encode(MessagePack::pack([
                     'A' => Uuid::fromString($_targetAssetID)->getBytes(),
                     ]));

Pay BTC to API gateway with generated memo

Transfer Bitcoin(BTC_ASSET_ID) to ExinCore(EXIN_BOT), put you target asset uuid in the memo, otherwise, ExinCore will refund you coin immediately!

const EXIN_BOT        = "61103d28-3ac2-44a2-ae34-bd956070dab1";
const BTC_ASSET_ID    = "c6d0c728-2624-429b-8e0d-d9d19b6592fa";
const EOS_ASSET_ID    = "6cfe566e-4aad-470b-8c9a-2fd35b49c68d";
const USDT_ASSET_ID   = "815b0b1a-2764-3736-8faa-42d694fa620a";
coinExchange(BTC_ASSET_ID,"0.0001",USDT_ASSET_ID);

//...........

function coinExchange($_assetID,$_amount,$_targetAssetID) {
  $mixinSdk = new MixinSDK(require './config.php');
  // print_r();
  $memo = base64_encode(MessagePack::pack([
                       'A' => Uuid::fromString($_targetAssetID)->getBytes(),
                       ]));
  $BotInfo = $mixinSdk->Wallet()->transfer($_assetID,EXIN_BOT,
                                           $mixinSdk->getConfig()['default']['pin'],$_amount,$memo);
  print_r($BotInfo);
}

The ExinCore should transfer the target coin to your bot, meanwhile, put the fee, order id, price etc. information in the memo, unpack the data like below.

  • readUserSnapshots Read snapshots of the user.
    $limit        = 20;
    $offset       = '2019-03-10T01:58:25.362528Z';
    $snapInfo = $mixinSdk_BotInstance->Wallet()->readUserSnapshots($limit, $offset);
    // print_r($networkInfo2);
    foreach ($snapInfo as  $record) {
    // echo $key . PHP_EOL;
    // print_r($record);
    if ($record['amount'] > 0 and $record['memo'] != '') {
      echo "------------MEMO:-coin--exchange--------------" . PHP_EOL;
      echo "memo: " . $record['memo'] . PHP_EOL;
      // print_r($dtPay->memo);
      echo "You Get Coins: ". $record['asset_id']. " " . $record['amount'] . PHP_EOL;
      $memoUnpack = MessagePack::unpack(base64_decode($record['memo']));
      $feeAssetID = Uuid::fromBytes($memoUnpack['FA'])->toString();
      $OrderID    = Uuid::fromBytes($memoUnpack['O'])->toString();
      if ($memoUnpack['C'] == 1000) {
        echo "Successful Exchange:". PHP_EOL;
        echo "Fee asset ID: " . $feeAssetID . " fee is :" . $memoUnpack['F'] . PHP_EOL;
        echo "Order ID: " . $OrderID . " Price is :" . $memoUnpack['P'] . PHP_EOL;
      } else print_r($memoUnpack);
      echo "--------------memo-record end---------------" . PHP_EOL;
    }
    }

If you coin exchange successful, console output like below:

------------MEMO:-coin--exchange--------------
memo: hqFDzQPooVCnMzg3Mi45N6FGqTAuMDAwNzc0NqJGQcQQgVsLGidkNzaPqkLWlPpiCqFUoUahT8QQIbfeL6p5RVOcEP0mLb+t+g==
You Get Coins: 815b0b1a-2764-3736-8faa-42d694fa620a 0.3857508
Successful Exchange:
Fee asset ID: 815b0b1a-2764-3736-8faa-42d694fa620a fee is :0.0007746
Order ID: 21b7de2f-aa79-4553-9c10-fd262dbfadfa Price is :3872.97
--------------memo-record end---------------

Read Bitcoin balance

Check the wallet's balance.

$mixinSdk = new MixinSDK(require './config.php');
$asset_info = $mixinSdk->Wallet()->readAsset(USDT_ASSET_ID);
print_r("USDT wallet balance is :".$asset_info["balance"]."\n");

Source code usage

Execute php call_apis.php to run it.

  • 1: Create user and update PIN
  • 2: Read Bitcoin balance & address
  • 3: Read USDT balance & address
  • 4: Read EOS balance
  • 5: Read EOS address
  • 6: Transfer Bitcoin from bot to new user
  • 7: Transfer Bitcoin from new user to Master
  • 8: Withdraw bot's Bitcoin
  • 9: Withdraw bot's EOS
  • qu: Read market price(USDT)
  • qb: Read market price(BTC)
  • b: Balance of bot (USDT & BTC)
  • s: Read Snapshots
  • tb: Transfer 0.0001 BTC buy USDT
  • tu: Transfer $1 USDT buy BTC
  • q: Exit

Full source code

Solution Two: List your order on Ocean.One exchange