|
| 1 | +# How to list any ERC20 token on decentralized market through PHP |
| 2 | + |
| 3 | + |
| 4 | +OceanOne is introduced in [last chapter](https://github.com/wenewzhang/mixin_labs-php-bot/blob/master/README5.md), you can trade Bitcoin. All kinds of crypto asset on Mixin Network can be listed on OceanOne.All ERC20 token and EOS token can be listed. Following example will show you how to list a ERC20 token. |
| 5 | + |
| 6 | +There is a [ERC20 token](https://etherscan.io/token/0xc409b5696c5f9612e194a582e14c8cd41ecdbc67) called Benz. It is deposited into Mixin Network. You can search all transaction history from [Mixin Network browser](https://mixin.one/snapshots/2b9c216c-ef60-398d-a42a-eba1b298581d ) |
| 7 | + |
| 8 | +### Pre-request: |
| 9 | +Deposit some coin to your wallet, and then use **getAssets** API fetch the asset UUID which Mixin Network gave it. |
| 10 | + |
| 11 | +### Get the ERC-20 compliant coin UUID |
| 12 | +The **getAssets** API return json data, for example: |
| 13 | + |
| 14 | +- **asset_id** UUID of this coin |
| 15 | +- **public_key** The wallet address for this coin |
| 16 | +- **symbol** Coin name, Eg: Benz. |
| 17 | + |
| 18 | +```php |
| 19 | +if ($line == 'aw') { |
| 20 | + $mixinSdk_eachAccountInstance = GenerateWalletSDKFromCSV(); |
| 21 | + $asset_info = $mixinSdk_eachAccountInstance->Wallet()->readAssets(); |
| 22 | + foreach ($asset_info as $key => $asset) { |
| 23 | + echo $asset["symbol"] . " " . $asset["asset_id"] ." ". $asset["balance"] . |
| 24 | + " ". $asset["public_key"].PHP_EOL; |
| 25 | + } |
| 26 | +} |
| 27 | +``` |
| 28 | +The detail information of **getAssets** is output like below: |
| 29 | +```bash |
| 30 | +Make your choose:aw |
| 31 | +run... |
| 32 | +client id is:26b20aa5-40c0-3e00-9de0-666cfb6f2daa |
| 33 | +Benz 2b9c216c-ef60-398d-a42a-eba1b298581d 799 0x9A4F6c67444cd6558905ef5B04a4c429b9538A9d |
| 34 | +EOS 6cfe566e-4aad-470b-8c9a-2fd35b49c68d 0 |
| 35 | +CNB 965e5c6e-434c-3fa9-b780-c50f43cd955c 4.72599997 0x9A4F6c67444cd6558905ef5B04a4c429b9538A9d |
| 36 | +BTC c6d0c728-2624-429b-8e0d-d9d19b6592fa 0 17z1Rq3VsyvvXvGWiHT8YErjBoFgnhErB8 |
| 37 | +XIN c94ac88f-4671-3976-b60a-09064f1811e8 0.01 0x9A4F6c67444cd6558905ef5B04a4c429b9538A9d |
| 38 | +``` |
| 39 | +### Make the limit order |
| 40 | +- **Limit Order to Buy** at or below the market. |
| 41 | +- **Limit Order to Sell** at or above the market. |
| 42 | + |
| 43 | +OceanOne support three base coin: USDT, XIN, BTC, that mean you can sell or buy it between USDT, XIN, BTC, so, you have there order: Benz/USDT, Benz/XIN, Benz/BTC, here show you how to make the sell order with USDT. |
| 44 | + |
| 45 | +### Make the limit order to sell. |
| 46 | + |
| 47 | +```php |
| 48 | +if ( $ocmd == 's1') { |
| 49 | + $p = readline("Input the Price of XIN/USDT: "); |
| 50 | + $a = readline("Input the Amount of XIN: "); |
| 51 | + $tMemo = GenerateOrderMemo("A",USDT_ASSET_ID,$p); |
| 52 | + echo $tMemo . PHP_EOL; |
| 53 | + $mixinSdk_WalletInstance = GenerateWalletSDKFromCSV(); |
| 54 | + $asset_info = $mixinSdk_WalletInstance->Wallet()->readAsset(XIN_ASSET_ID); |
| 55 | + print_r($asset_info); |
| 56 | + if ( (float) $asset_info["balance"] >= (float) $a ) { |
| 57 | + $transInfos = $mixinSdk_WalletInstance->Wallet()->transfer(XIN_ASSET_ID,OCEANONE_BOT, |
| 58 | + $mixinSdk_WalletInstance->getConfig()['default']['pin'], |
| 59 | + $a, |
| 60 | + $tMemo); |
| 61 | + print_r($transInfos); |
| 62 | + echo "The Order ID (trace_id) is: " . $transInfos["trace_id"] . PHP_EOL; |
| 63 | + } else { echo "Not enough XIN!\n";} |
| 64 | +} |
| 65 | +``` |
| 66 | + |
| 67 | +### Make the limit order to buy. |
| 68 | +After the order commit, wait 1 minute to let the OceanOne exchange initialize it. |
| 69 | +```php |
| 70 | +if ( $ocmd == 'b1') { |
| 71 | + $p = readline("Input the Price of XIN/USDT: "); |
| 72 | + $a = readline("Input the Amount of USDT: "); |
| 73 | + $tMemo = GenerateOrderMemo("B",XIN_ASSET_ID,$p); |
| 74 | + echo $tMemo . PHP_EOL; |
| 75 | + $mixinSdk_WalletInstance = GenerateWalletSDKFromCSV(); |
| 76 | + $asset_info = $mixinSdk_WalletInstance->Wallet()->readAsset(USDT_ASSET_ID); |
| 77 | + |
| 78 | + print_r($asset_info); |
| 79 | + if ( ((float) $asset_info["balance"] >= 1) && ( (float) $asset_info["balance"] >= (float) $a ) ) { |
| 80 | + $transInfos = $mixinSdk_WalletInstance->Wallet()->transfer(USDT_ASSET_ID,OCEANONE_BOT, |
| 81 | + $mixinSdk_WalletInstance->getConfig()['default']['pin'], |
| 82 | + $a, |
| 83 | + $tMemo); |
| 84 | + print_r($transInfos); |
| 85 | + echo "The Order ID (trace_id) is: " . $transInfos["trace_id"] . PHP_EOL; |
| 86 | + } else { echo "Not enough USDT!\n";} |
| 87 | +} |
| 88 | +``` |
| 89 | +### Read orders book from Ocean.one |
| 90 | +Now, check the orders-book. |
| 91 | + |
| 92 | +```php |
| 93 | + if ( $ocmd == '2') { getOceanOneMarketInfos(ERC20_BENZ,USDT_ASSET_ID);} |
| 94 | + function getOceanOneMarketInfos($targetCoin, $baseCoin) { |
| 95 | + $client = new GuzzleHttp\Client(); |
| 96 | + $baseUrl = "https://events.ocean.one/markets/".$targetCoin."-".$baseCoin."/book"; |
| 97 | + $res = $client->request('GET', $baseUrl, [ |
| 98 | + ]); |
| 99 | + if ($res->getStatusCode() == "200") { |
| 100 | + // echo $res->getStatusCode() . PHP_EOL; |
| 101 | + $resInfo = json_decode($res->getBody(), true); |
| 102 | + echo "Side | Price | Amount | Funds" . PHP_EOL; |
| 103 | + foreach ($resInfo["data"]["data"]["asks"] as $key => $exchange) { |
| 104 | + echo $exchange["side"] . " " . $exchange["price"] . " " . $exchange["amount"] ." " . $exchange["funds"] . PHP_EOL; |
| 105 | + } |
| 106 | + foreach ($resInfo["data"]["data"]["bids"] as $key => $exchange) { |
| 107 | + echo $exchange["side"] . " " . $exchange["price"] . " " . $exchange["amount"] ." " . $exchange["funds"] . PHP_EOL; |
| 108 | + } |
| 109 | + } |
| 110 | + } |
| 111 | +``` |
| 112 | +### Command of make orders |
| 113 | + |
| 114 | +Commands list of this source code: |
| 115 | + |
| 116 | +- trb:Transfer ERC20 from Bot to Wallet |
| 117 | +- trm:Transfer ERC20 from Wallet to Master |
| 118 | +- o: Ocean.One Exchange |
| 119 | + |
| 120 | +Make your choose(eg: q for Exit!): |
| 121 | +- x: Orders-Book of ERC20/USDT |
| 122 | +- x1: Buy ERC20 pay USDT |
| 123 | +- x2: Sell ERC20 get USDT |
| 124 | +- c: Cancel the order |
| 125 | +- q: Exit |
| 126 | + |
| 127 | +[Full source code](https://github.com/wenewzhang/mixin_labs-php-bot/blob/master/bitcoin_wallet.php) |
0 commit comments