Skip to content

Commit 484faf3

Browse files
committed
course 6
1 parent 4434638 commit 484faf3

File tree

4 files changed

+258
-0
lines changed

4 files changed

+258
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ A Mixin messenger bot will be created in this tutorial. The bot is powered by PH
1010
3. [Create Bitcoin wallet, read balance and send Bitcoin by PHP based on Mixin Network](https://github.com/wenewzhang/mixin_labs-php-bot/blob/master/README3.md)| [Chinese](https://github.com/wenewzhang/mixin_labs-php-bot/blob/master/README3-zhchs.md)
1111
4. [How to trade bitcoin through PHP language: Pay to ExinCore](https://github.com/wenewzhang/mixin_labs-php-bot/blob/master/README4.md) | [Chinese](https://github.com/wenewzhang/mixin_labs-php-bot/blob/master/README4-zhchs.md)
1212
5. [How to trade bitcoin through PHP: List your order on Ocean.One](https://github.com/wenewzhang/mixin_labs-php-bot/blob/master/README5.md) | [Chinese](https://github.com/wenewzhang/mixin_labs-php-bot/blob/master/README5-zhchs.md)
13+
6. [How to trade ERC-20 compliant coins on OceanOne through PHP](https://github.com/wenewzhang/mixin_labs-php-bot/blob/master/README6.md) | [Chinese](https://github.com/wenewzhang/mixin_labs-php-bot/blob/master/README6-zhchs.md)
1314

1415
## Create bot in Mixin messenger and reply message to user
1516
### PHP environment setup:

README5.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,8 @@ Build it and then run it.
212212

213213
- **php bitcoin_wallet.php** run it.
214214

215+
Commands of trade with OceanOne:
216+
215217
- o: Ocean.One Exchange
216218
- q: Exit
217219

README6-zhchs.md

Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
# 在OceanOne上挂单买卖任意ERC20 token
2+
![](https://github.com/wenewzhang/mixin_labs-php-bot/raw/master/Bitcoin_php.jpg)
3+
4+
[上一课](https://github.com/wenewzhang/mixin_labs-java-bot/blob/master/README5.md)中,我们介绍了如何在OceanOne交易比特币。OceanOne支持交易任何Mixin Network上的token,包括所有的ERC20和EOS token,不需要任何手续和费用,直接挂单即可。下面介绍如何将将一个ERC20 token挂上OceanOne交易!
5+
6+
此处我们用一个叫做Benz的[ERC20 token](https://etherscan.io/token/0xc409b5696c5f9612e194a582e14c8cd41ecdbc67)为例。这个token已经被充值进Mixin Network,你可以在[区块链浏览器](https://mixin.one/snapshots/2b9c216c-ef60-398d-a42a-eba1b298581d )看到这个token在Mixin Network内部的总数和交易
7+
### 预备知识:
8+
先将Ben币存入你的钱包,然后使用**getAssets** API读取它的UUID.
9+
10+
### 取得该币的UUID
11+
调用 **getAssets** API 会返回json数据, 如:
12+
13+
- **asset_id** 币的UUID.
14+
- **public_key** 该币的当前钱包的地址.
15+
- **symbol** 币的名称. 如: Benz.
16+
17+
```php
18+
if ($line == 'aw') {
19+
$mixinSdk_eachAccountInstance = GenerateWalletSDKFromCSV();
20+
$asset_info = $mixinSdk_eachAccountInstance->Wallet()->readAssets();
21+
foreach ($asset_info as $key => $asset) {
22+
echo $asset["symbol"] . " " . $asset["asset_id"] ." ". $asset["balance"] .
23+
" ". $asset["public_key"].PHP_EOL;
24+
}
25+
}
26+
```
27+
调用 **getAssets** API的完整输出如下:
28+
```bash
29+
Make your choose:aw
30+
run...
31+
client id is:26b20aa5-40c0-3e00-9de0-666cfb6f2daa
32+
Benz 2b9c216c-ef60-398d-a42a-eba1b298581d 799 0x9A4F6c67444cd6558905ef5B04a4c429b9538A9d
33+
EOS 6cfe566e-4aad-470b-8c9a-2fd35b49c68d 0
34+
CNB 965e5c6e-434c-3fa9-b780-c50f43cd955c 4.72599997 0x9A4F6c67444cd6558905ef5B04a4c429b9538A9d
35+
BTC c6d0c728-2624-429b-8e0d-d9d19b6592fa 0 17z1Rq3VsyvvXvGWiHT8YErjBoFgnhErB8
36+
XIN c94ac88f-4671-3976-b60a-09064f1811e8 0.01 0x9A4F6c67444cd6558905ef5B04a4c429b9538A9d
37+
```
38+
### 限价挂单
39+
- **挂限价买单** 低于或者等于市场价的单.
40+
- **挂限价卖单** 高于或者是等于市场价的单.
41+
42+
OceanOne支持三种基类价格: USDT, XIN, BTC, 即: Benz/USDT, Benz/XIN, Benz/BTC, 这儿示范Benz/USDT.
43+
44+
### 限价挂卖单.
45+
新币挂单后,需要等一分钟左右,等OceanOne来初始化新币的相关数据.
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+
### 限价挂买单.
68+
新币挂单后,需要等一分钟左右,等OceanOne来初始化新币的相关数据.
69+
70+
```php
71+
if ( $ocmd == 'b1') {
72+
$p = readline("Input the Price of XIN/USDT: ");
73+
$a = readline("Input the Amount of USDT: ");
74+
$tMemo = GenerateOrderMemo("B",XIN_ASSET_ID,$p);
75+
echo $tMemo . PHP_EOL;
76+
$mixinSdk_WalletInstance = GenerateWalletSDKFromCSV();
77+
$asset_info = $mixinSdk_WalletInstance->Wallet()->readAsset(USDT_ASSET_ID);
78+
79+
print_r($asset_info);
80+
if ( ((float) $asset_info["balance"] >= 1) && ( (float) $asset_info["balance"] >= (float) $a ) ) {
81+
$transInfos = $mixinSdk_WalletInstance->Wallet()->transfer(USDT_ASSET_ID,OCEANONE_BOT,
82+
$mixinSdk_WalletInstance->getConfig()['default']['pin'],
83+
$a,
84+
$tMemo);
85+
print_r($transInfos);
86+
echo "The Order ID (trace_id) is: " . $transInfos["trace_id"] . PHP_EOL;
87+
} else { echo "Not enough USDT!\n";}
88+
}
89+
```
90+
### 读取币的价格列表
91+
读取币的价格列表,来确认挂单是否成功!
92+
93+
```php
94+
if ( $ocmd == '2') { getOceanOneMarketInfos(ERC20_BENZ,USDT_ASSET_ID);}
95+
function getOceanOneMarketInfos($targetCoin, $baseCoin) {
96+
$client = new GuzzleHttp\Client();
97+
$baseUrl = "https://events.ocean.one/markets/".$targetCoin."-".$baseCoin."/book";
98+
$res = $client->request('GET', $baseUrl, [
99+
]);
100+
if ($res->getStatusCode() == "200") {
101+
// echo $res->getStatusCode() . PHP_EOL;
102+
$resInfo = json_decode($res->getBody(), true);
103+
echo "Side | Price | Amount | Funds" . PHP_EOL;
104+
foreach ($resInfo["data"]["data"]["asks"] as $key => $exchange) {
105+
echo $exchange["side"] . " " . $exchange["price"] . " " . $exchange["amount"] ." " . $exchange["funds"] . PHP_EOL;
106+
}
107+
foreach ($resInfo["data"]["data"]["bids"] as $key => $exchange) {
108+
echo $exchange["side"] . " " . $exchange["price"] . " " . $exchange["amount"] ." " . $exchange["funds"] . PHP_EOL;
109+
}
110+
}
111+
}
112+
```
113+
### ERC20相关的操作指令
114+
115+
Commands list of this source code:
116+
117+
- trb:Transfer ERC20 from Bot to Wallet
118+
- trm:Transfer ERC20 from Wallet to Master
119+
- o: Ocean.One Exchange
120+
121+
Make your choose(eg: q for Exit!):
122+
- x: Orders-Book of ERC20/USDT
123+
- x1: Buy ERC20 pay USDT
124+
- x2: Sell ERC20 get USDT
125+
- c: Cancel the order
126+
- q: Exit
127+
128+
[完整的代码](https://github.com/wenewzhang/mixin_labs-php-bot/blob/master/bitcoin_wallet.php)

README6.md

Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
# How to list any ERC20 token on decentralized market through PHP
2+
![](https://github.com/wenewzhang/mixin_labs-php-bot/raw/master/Bitcoin_php.jpg)
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

Comments
 (0)