|
| 1 | +<?php |
| 2 | + |
| 3 | +require __DIR__ . '/vendor/autoload.php'; |
| 4 | +use ExinOne\MixinSDK\Traits\MixinSDKTrait; |
| 5 | +use ExinOne\MixinSDK\MixinSDK; |
| 6 | +use Ramsey\Uuid\Uuid; |
| 7 | +use Ratchet\RFC6455\Messaging\Frame; |
| 8 | +use MessagePack\MessagePack; |
| 9 | + |
| 10 | +const EXIN_BOT = "61103d28-3ac2-44a2-ae34-bd956070dab1"; |
| 11 | +const MASTER_UUID = "0b4f49dc-8fb4-4539-9a89-fb3afc613747"; |
| 12 | +const BTC_ASSET_ID = "c6d0c728-2624-429b-8e0d-d9d19b6592fa"; |
| 13 | +const EOS_ASSET_ID = "6cfe566e-4aad-470b-8c9a-2fd35b49c68d"; |
| 14 | +const USDT_ASSET_ID = "815b0b1a-2764-3736-8faa-42d694fa620a"; |
| 15 | +$loop = \React\EventLoop\Factory::create(); |
| 16 | +$reactConnector = new \React\Socket\Connector($loop, [ |
| 17 | + 'timeout' => 15 |
| 18 | +]); |
| 19 | +class callTraitClass { |
| 20 | + use MixinSDKTrait; |
| 21 | + public $config; |
| 22 | + public function __construct() |
| 23 | + { |
| 24 | + $config = require(__DIR__.'/config.php'); |
| 25 | + $this->config = $config; |
| 26 | + } |
| 27 | +} |
| 28 | +$callTrait = new callTraitClass(); |
| 29 | +$Token = $callTrait->getToken('GET', '/', ''); |
| 30 | +print_r($callTrait->config['client_id']); |
| 31 | +// $Header = 'Authorization'.'Bearer '.$Token; |
| 32 | +// print($Header); |
| 33 | +$connector = new \Ratchet\Client\Connector($loop,$reactConnector); |
| 34 | +// $connector('ws://127.0.0.1:9000', ['protocol' => 'Mixin-Blaze-1'], ['Origin' => 'http://localhost', |
| 35 | +$connector('wss://blaze.mixin.one', ['protocol' => 'Mixin-Blaze-1'],[ |
| 36 | + 'Authorization' => 'Bearer '.$Token |
| 37 | + ]) |
| 38 | +->then(function(Ratchet\Client\WebSocket $conn) { |
| 39 | + $conn->on('message', function(\Ratchet\RFC6455\Messaging\MessageInterface $msg) use ($conn) { |
| 40 | + $jsMsg = json_decode(gzdecode($msg)); |
| 41 | + // print_r($jsMsg); |
| 42 | + if ($jsMsg->action === 'CREATE_MESSAGE' and property_exists($jsMsg,'data')) { |
| 43 | + echo "\nNeed reply server a receipt!\n"; |
| 44 | + $RspMsg = generateReceipt($jsMsg->data->message_id); |
| 45 | + $msg = new Frame(gzencode(json_encode($RspMsg)),true,Frame::OP_BINARY); |
| 46 | + $conn->send($msg); |
| 47 | + |
| 48 | + if ($jsMsg->data->category === 'PLAIN_TEXT') { |
| 49 | + echo "PLAIN_TEXT:".base64_decode($jsMsg->data->data); |
| 50 | + $isCmd = strtolower(base64_decode($jsMsg->data->data)); |
| 51 | + if ($isCmd ==='?' or $isCmd ==='help') { |
| 52 | + $msgData = sendUsage($jsMsg->data->conversation_id); |
| 53 | + $msg = new Frame(gzencode(json_encode($msgData)),true,Frame::OP_BINARY); |
| 54 | + $conn->send($msg); |
| 55 | + } elseif ($isCmd === '1') { |
| 56 | + // print($callTrait->config['client_id']); |
| 57 | + $msgData = sendAppButtons($jsMsg); |
| 58 | + $msg = new Frame(gzencode(json_encode($msgData)),true,Frame::OP_BINARY); |
| 59 | + $conn->send($msg); |
| 60 | + } elseif ($isCmd === '2') { |
| 61 | + // print($callTrait->config['client_id']); |
| 62 | + $msgData = sendAppCard($jsMsg); |
| 63 | + $msg = new Frame(gzencode(json_encode($msgData)),true,Frame::OP_BINARY); |
| 64 | + $conn->send($msg); |
| 65 | + } elseif ($isCmd === '3') { |
| 66 | + $marketInfo = getExchangeCoins(BTC_ASSET_ID); |
| 67 | + echo $marketInfo; |
| 68 | + $msgData = sendPlainText($jsMsg->data->conversation_id, $marketInfo); |
| 69 | + $msg = new Frame(gzencode(json_encode($msgData)),true,Frame::OP_BINARY); |
| 70 | + $conn->send($msg); |
| 71 | + } elseif ($isCmd === '4') { |
| 72 | + $marketInfo = getExchangeCoins(USDT_ASSET_ID); |
| 73 | + echo $marketInfo; |
| 74 | + $msgData = sendPlainText($jsMsg->data->conversation_id, $marketInfo); |
| 75 | + $msg = new Frame(gzencode(json_encode($msgData)),true,Frame::OP_BINARY); |
| 76 | + $conn->send($msg); |
| 77 | + } elseif ($isCmd === '6') { |
| 78 | + $msgData = sendAppCardBuyUSDTSellBTC($jsMsg); |
| 79 | + $msg = new Frame(gzencode(json_encode($msgData)),true,Frame::OP_BINARY); |
| 80 | + $conn->send($msg); |
| 81 | + } elseif ($isCmd === '7') { |
| 82 | + coinExchange(BTC_ASSET_ID,"0.0001",USDT_ASSET_ID); |
| 83 | + } elseif ($isCmd === '8') { |
| 84 | + $mixinSdk = new MixinSDK(require './config.php'); |
| 85 | + $asset_info = $mixinSdk->Wallet()->readAsset(BTC_ASSET_ID); |
| 86 | + print_r("Bitcoin wallet balance is :".$asset_info["balance"]."\n"); |
| 87 | + $asset_info = $mixinSdk->Wallet()->readAsset(USDT_ASSET_ID); |
| 88 | + print_r("USDT wallet balance is :".$asset_info["balance"]."\n"); |
| 89 | + } else { |
| 90 | + $msgData = sendPlainText($jsMsg->data->conversation_id, |
| 91 | + base64_decode($jsMsg->data->data)); |
| 92 | + $msg = new Frame(gzencode(json_encode($msgData)),true,Frame::OP_BINARY); |
| 93 | + $conn->send($msg); |
| 94 | + } |
| 95 | + } //end of PLAIN_TEXT |
| 96 | + if ($jsMsg->data->category === 'SYSTEM_ACCOUNT_SNAPSHOT') { |
| 97 | + // refundInstant |
| 98 | + echo "user id:".$jsMsg->data->user_id . PHP_EOL; |
| 99 | + $dtPay = json_decode(base64_decode($jsMsg->data->data)); |
| 100 | + if ($dtPay->amount > 0) { |
| 101 | + echo "paid!".$dtPay->asset_id . PHP_EOL; |
| 102 | + if ( $dtPay->opponent_id == MASTER_UUID ) { |
| 103 | + // refundInstant($dtPay->asset_id,$dtPay->amount,$jsMsg->data->user_id); |
| 104 | + } else { |
| 105 | + echo "------------MEMO:-coin--exchange--------------" . PHP_EOL; |
| 106 | + // print_r($dtPay->memo); |
| 107 | + echo "You Get Coins: ". $dtPay->asset_id. " " . $dtPay->amount . PHP_EOL; |
| 108 | + $memoUnpack = MessagePack::unpack(base64_decode($dtPay->memo)); |
| 109 | + $feeAssetID = Uuid::fromBytes($memoUnpack['FA'])->toString(); |
| 110 | + $OrderID = Uuid::fromBytes($memoUnpack['O'])->toString(); |
| 111 | + if ($memoUnpack['C'] == 1000) { |
| 112 | + echo "Successful Exchange:". PHP_EOL; |
| 113 | + echo "Fee asset ID: " . $feeAssetID . " fee is :" . $memoUnpack['F'] . PHP_EOL; |
| 114 | + echo "Order ID: " . $OrderID . " Price is :" . $memoUnpack['P'] . PHP_EOL; |
| 115 | + } else print_r($memoUnpack); |
| 116 | + } |
| 117 | + } |
| 118 | + } //end of SYSTEM_ACCOUNT_SNAPSHOT |
| 119 | + } //end of CREATE_MESSAGE |
| 120 | + |
| 121 | + }); |
| 122 | + |
| 123 | + $conn->on('close', function($code = null, $reason = null) { |
| 124 | + echo "Connection closed ({$code} - {$reason})\n"; |
| 125 | + }); |
| 126 | +/* start listen for the incoming message */ |
| 127 | + $message = [ |
| 128 | + 'id' => Uuid::uuid4()->toString(), |
| 129 | + 'action' => 'LIST_PENDING_MESSAGES', |
| 130 | + ]; |
| 131 | + print_r(json_encode($message)); |
| 132 | + $msg = new Frame(gzencode(json_encode($message)),true,Frame::OP_BINARY); |
| 133 | + $conn->send($msg); |
| 134 | + // $conn->send(gzencode($msg,1,FORCE_DEFLATE)); |
| 135 | +}, function(\Exception $e) use ($loop) { |
| 136 | + echo "Could not connect: {$e->getMessage()}\n"; |
| 137 | + $loop->stop(); |
| 138 | +}); |
| 139 | + |
| 140 | +$loop->run(); |
| 141 | + |
| 142 | + |
| 143 | +function sendUsage($conversation_id):Array { |
| 144 | + $msgHelp = <<<EOF |
| 145 | + Usage: |
| 146 | + ? or help : for help! \n |
| 147 | + 1 : pay by APP_BUTTON_GROUP \n |
| 148 | + 2 : pay by APP_CARD \n |
| 149 | + 3 : ask price of USDT/BTC \n |
| 150 | + 4 : ask price of BTC/USDT \n |
| 151 | + 3x : Buy USDT sell BTC \n |
| 152 | + 4x : Buy BTC sell USDT \n |
| 153 | + 6 : Buy USDT sell BTC Directly \n |
| 154 | + 7 : Bot 0.0001 BTC exchange \n |
| 155 | + 8 : Check BitCoin balance \n |
| 156 | +EOF; |
| 157 | + return sendPlainText($conversation_id,$msgHelp); |
| 158 | +} |
| 159 | + |
| 160 | +function sendPlainText($conversation_id,$msgContent):Array { |
| 161 | + |
| 162 | + $msgParams = [ |
| 163 | + 'conversation_id' => $conversation_id, |
| 164 | + 'category' => 'PLAIN_TEXT', |
| 165 | + 'status' => 'SENT', |
| 166 | + 'message_id' => Uuid::uuid4()->toString(), |
| 167 | + 'data' => base64_encode($msgContent),//base64_encode("hello!"), |
| 168 | + ]; |
| 169 | + $msgPayButton = [ |
| 170 | + 'id' => Uuid::uuid4()->toString(), |
| 171 | + 'action' => 'CREATE_MESSAGE', |
| 172 | + 'params' => $msgParams, |
| 173 | + ]; |
| 174 | + return $msgPayButton; |
| 175 | +} |
| 176 | +function sendAppButtons($jsMsg):Array { |
| 177 | + $client_id = (require "./config.php")['client_id']; |
| 178 | + $payLinkEOS = "https://mixin.one/pay?recipient=". |
| 179 | + $client_id."&asset=". |
| 180 | + "6cfe566e-4aad-470b-8c9a-2fd35b49c68d". |
| 181 | + "&amount=0.0001"."&trace=".Uuid::uuid4()->toString(). |
| 182 | + "&memo="; |
| 183 | + $payLinkBTC = "https://mixin.one/pay?recipient=". |
| 184 | + $client_id."&asset=". |
| 185 | + "c6d0c728-2624-429b-8e0d-d9d19b6592fa". |
| 186 | + "&amount=0.0001"."&trace=".Uuid::uuid4()->toString(). |
| 187 | + "&memo="; |
| 188 | + $msgData = [[ |
| 189 | + 'label' => "Pay 0.001 EOS", |
| 190 | + 'color' => "#FFABAB", |
| 191 | + 'action' => $payLinkEOS, |
| 192 | + ],[ |
| 193 | + 'label' => "Pay 0.0001 BTC", |
| 194 | + 'color' => "#00EEFF", |
| 195 | + 'action' => $payLinkBTC, |
| 196 | + ], |
| 197 | + ]; |
| 198 | + $msgParams = [ |
| 199 | + 'conversation_id' => $jsMsg->data->conversation_id,// $callTrait->config[client_id], |
| 200 | + // 'recipient_id' => $jsMsg->data->user_id, |
| 201 | + 'category' => 'APP_BUTTON_GROUP',//'PLAIN_TEXT', |
| 202 | + 'status' => 'SENT', |
| 203 | + 'message_id' => Uuid::uuid4()->toString(), |
| 204 | + 'data' => base64_encode(json_encode($msgData)),//base64_encode("hello!"), |
| 205 | + ]; |
| 206 | + $msgPayButtons = [ |
| 207 | + 'id' => Uuid::uuid4()->toString(), |
| 208 | + 'action' => 'CREATE_MESSAGE', |
| 209 | + 'params' => $msgParams, |
| 210 | + ]; |
| 211 | + return $msgPayButtons; |
| 212 | +} |
| 213 | + |
| 214 | +function sendAppCard($jsMsg):Array |
| 215 | +{ |
| 216 | + $client_id = (require "./config.php")['client_id']; |
| 217 | + $payLink = "https://mixin.one/pay?recipient=". |
| 218 | + $client_id."&asset=". |
| 219 | + "6cfe566e-4aad-470b-8c9a-2fd35b49c68d". |
| 220 | + "&amount=0.0001"."&trace=".Uuid::uuid4()->toString(). |
| 221 | + "&memo="; |
| 222 | + $msgData = [ |
| 223 | + 'icon_url' => "https://mixin.one/assets/98b586edb270556d1972112bd7985e9e.png", |
| 224 | + 'title' => "Pay 0.001 EOS", |
| 225 | + 'description' => "pay", |
| 226 | + 'action' => $payLink, |
| 227 | + ]; |
| 228 | + $msgParams = [ |
| 229 | + 'conversation_id' => $jsMsg->data->conversation_id,// $callTrait->config[client_id], |
| 230 | + // 'recipient_id' => $jsMsg->data->user_id, |
| 231 | + 'category' => 'APP_CARD',//'PLAIN_TEXT', |
| 232 | + 'status' => 'SENT', |
| 233 | + 'message_id' => Uuid::uuid4()->toString(), |
| 234 | + 'data' => base64_encode(json_encode($msgData)),//base64_encode("hello!"), |
| 235 | + ]; |
| 236 | + $msgPayButton = [ |
| 237 | + 'id' => Uuid::uuid4()->toString(), |
| 238 | + 'action' => 'CREATE_MESSAGE', |
| 239 | + 'params' => $msgParams, |
| 240 | + ]; |
| 241 | + return $msgPayButton; |
| 242 | +} |
| 243 | + |
| 244 | +function sendAppCardBuyUSDTSellBTC($jsMsg):Array |
| 245 | +{ |
| 246 | + $client_id = (require "./config.php")['client_id']; |
| 247 | + $memo = base64_encode(MessagePack::pack([ |
| 248 | + 'A' => Uuid::fromString('815b0b1a-2764-3736-8faa-42d694fa620a')->getBytes(), |
| 249 | + ])); |
| 250 | + $payLink = "https://mixin.one/pay?recipient=". |
| 251 | + EXIN_BOT."&asset=". |
| 252 | + "c6d0c728-2624-429b-8e0d-d9d19b6592fa". |
| 253 | + "&amount=0.0001"."&trace=".Uuid::uuid4()->toString(). |
| 254 | + "&memo=".$memo; |
| 255 | + $msgData = [ |
| 256 | + 'icon_url' => "https://mixin.one/assets/98b586edb270556d1972112bd7985e9e.png", |
| 257 | + 'title' => "Pay 0.0001 BTC", |
| 258 | + 'description' => "pay", |
| 259 | + 'action' => $payLink, |
| 260 | + ]; |
| 261 | + $msgParams = [ |
| 262 | + 'conversation_id' => $jsMsg->data->conversation_id,// $callTrait->config[client_id], |
| 263 | + // 'recipient_id' => $jsMsg->data->user_id, |
| 264 | + 'category' => 'APP_CARD',//'PLAIN_TEXT', |
| 265 | + 'status' => 'SENT', |
| 266 | + 'message_id' => Uuid::uuid4()->toString(), |
| 267 | + 'data' => base64_encode(json_encode($msgData)),//base64_encode("hello!"), |
| 268 | + ]; |
| 269 | + $msgPayButton = [ |
| 270 | + 'id' => Uuid::uuid4()->toString(), |
| 271 | + 'action' => 'CREATE_MESSAGE', |
| 272 | + 'params' => $msgParams, |
| 273 | + ]; |
| 274 | + return $msgPayButton; |
| 275 | +} |
| 276 | + |
| 277 | +function transfer() { |
| 278 | + $mixinSdk = new MixinSDK(require './config.php'); |
| 279 | + print_r($mixinSdk->getConfig()); |
| 280 | +} |
| 281 | + |
| 282 | +function generateReceipt($msgID):Array { |
| 283 | + $IncomingMsg = ["message_id" => $msgID, "status" => "READ"]; |
| 284 | + $RspMsg = ["id" => Uuid::uuid4()->toString(), "action" => "ACKNOWLEDGE_MESSAGE_RECEIPT", |
| 285 | + "params" => $IncomingMsg]; |
| 286 | + return $RspMsg; |
| 287 | +} |
| 288 | + |
| 289 | +function refundInstant($_assetID,$_amount,$_opponent_id) { |
| 290 | + $mixinSdk = new MixinSDK(require './config.php'); |
| 291 | + // print_r(); |
| 292 | + $BotInfo = $mixinSdk->Wallet()->transfer($_assetID,$_opponent_id, |
| 293 | + $mixinSdk->getConfig()['default']['pin'],$_amount); |
| 294 | + print_r($BotInfo); |
| 295 | +} |
| 296 | + |
| 297 | +function coinExchange($_assetID,$_amount,$_targetAssetID) { |
| 298 | + $mixinSdk = new MixinSDK(require './config.php'); |
| 299 | + // print_r(); |
| 300 | + $memo = base64_encode(MessagePack::pack([ |
| 301 | + 'A' => Uuid::fromString($_targetAssetID)->getBytes(), |
| 302 | + ])); |
| 303 | + $BotInfo = $mixinSdk->Wallet()->transfer($_assetID,EXIN_BOT, |
| 304 | + $mixinSdk->getConfig()['default']['pin'],$_amount,$memo); |
| 305 | + print_r($BotInfo); |
| 306 | +} |
| 307 | + |
| 308 | +function getExchangeCoins($base_coin) :string { |
| 309 | + $client = new GuzzleHttp\Client(); |
| 310 | + $res = $client->request('GET', 'https://exinone.com/exincore/markets?base_asset='.$base_coin, [ |
| 311 | + ]); |
| 312 | + $result = ""; |
| 313 | + if ($res->getStatusCode() == "200") { |
| 314 | + // echo $res->getStatusCode() . PHP_EOL; |
| 315 | + $resInfo = json_decode($res->getBody(), true); |
| 316 | + echo "Asset ID | Asset Symbol | Price | Amount | Exchanges" . PHP_EOL; |
| 317 | + $result = "Asset ID | Asset Symbol | Price | Amount | Exchanges" . PHP_EOL; |
| 318 | + foreach ($resInfo["data"] as $key => $coinInfo) { |
| 319 | + echo ($coinInfo["exchange_asset"] ." ".$coinInfo["exchange_asset_symbol"]. "/". $coinInfo["base_asset_symbol"] . |
| 320 | + " ". $coinInfo["price"] ." ". $coinInfo["minimum_amount"] ."-". $coinInfo["maximum_amount"] . " "); |
| 321 | + $result .= $coinInfo["exchange_asset_symbol"]. "/". $coinInfo["base_asset_symbol"] . |
| 322 | + " ". $coinInfo["price"] ." ". $coinInfo["minimum_amount"] ."-". $coinInfo["maximum_amount"] . " "; |
| 323 | + foreach ($coinInfo["exchanges"] as $key => $exchange) { |
| 324 | + echo $exchange . " "; |
| 325 | + $result .= $exchange . " "; |
| 326 | + } |
| 327 | + echo PHP_EOL; |
| 328 | + $result .= PHP_EOL; |
| 329 | + } |
| 330 | + } |
| 331 | + return $result; |
| 332 | +} |
0 commit comments