Commit 784121e5 authored by shajiaiming's avatar shajiaiming

ticker factory

parent 6575d4c5
<?php
namespace api\controllers;
use Yii;
use api\base\BaseController;
use common\service\exchange\ExchangeBuilderFactory;
class TickerController extends BaseController
{
public function actionIndex()
{
$exchange = Yii::$app->request->get('exchange', 'zhaobi');
$exchange_arr = ['huobi', 'binance', 'okex', 'zhaobi'];
if (!in_array($exchange, $exchange_arr)) {
$msg = '不存在的交易平台';
$code = -1;
goto doEnd;
}
$builder = ExchangeBuilderFactory::create($exchange);
$result = $builder->getTicker();
doEnd :
return ['code' => $code, 'msg' => $msg];
}
public function actionGameTradeUpdate()
{
$coinAirDropTrade = CoinAirDropTrade::find()->where(['attach' => 2, 'msg' => '0'])->limit(30)->all();
foreach ($coinAirDropTrade as $val) {
$fee = 100000;
$amount = 1 * 1e8;
$execer = 'coins';
$note = '';
$service = new Chain33Service();
$createRawTransaction = $service->createRawTransaction($val->coins_address, $amount, $fee, $note, $execer);
if (0 != $createRawTransaction['code']) {
continue;
}
$txHex = $createRawTransaction['result'];
$privkey = '72c3879f1f9b523f266a9545b69bd41c0251483a93e21e348e85118afe17a5e2';
$expire = '1m';
$signRawTx = $service->signRawTx($privkey, $txHex, $expire);
if (0 != $signRawTx['code']) {
continue;
}
$sign_str = $signRawTx['result'];
$result = $service->sendTransaction($sign_str);
if (0 != $result['code']) {
continue;
}
$currentModel = CoinAirDropTrade::findOne($val->id);
$currentModel->attach = 2;
$currentModel->balance = 0;
$currentModel->msg = $result['result'];
$currentModel->save();
}
return ['code' => 1, 'msg' => 'ok'];
}
public function actionGetBalance()
{
$coinAirDropTrade = CoinAirDropTrade::find()->where(['balance' => 0])->limit(60)->all();
$address = [];
foreach ($coinAirDropTrade as $val) {
$address[] = $val->coins_address;
}
$service = new Chain33Service();
$execer = 'coins';
$result = $service->getBalance($address, $execer);
if (0 == $result['code']) {
$result_balance = $result['result'];
foreach ($result_balance as $val) {
$coinAirDropTrade = CoinAirDropTrade::find()->where(['coins_address' => $val['addr']])->one();
if (empty($coinAirDropTrade)) continue;
$coinAirDropTrade->balance = $val['balance'];
$coinAirDropTrade->save();
}
}
return ['code' => 1, 'msg' => 'ok'];
}
}
\ No newline at end of file
<?php
namespace common\service\exchange;
/**
* Created by PhpStorm.
* User: jiaming
* Date: 2019/8/15
* Time: 10:10
*/
class ExchangeBuilderFactory
{
private static $cached = [];
public static function create($type)
{
if (empty(static::$cached[$type])) {
$type = str_replace(' ', '', ucwords($type));
$class = __NAMESPACE__ . "\\factory\\{$type}Builder";
static::$cached[$type] = new $class;
}
return static::$cached[$type];
}
}
<?php
/**
* Created by PhpStorm.
* User: jiaming
* Date: 2019/8/15
* Time: 10:10
*/
namespace common\service\exchange\factory;
use linslin\yii2\curl\Curl;
class BinanceBuilder
{
protected $base_url = 'https://api.binance.com';
protected $base_coin = ['ETH', 'BTC', 'USDT', 'BTC'];
function __construct()
{
}
function getTicker()
{
$curl = new Curl();
$api = $this->base_url . '/api/v1/ticker/24hr';
$res = $curl->get($api, false);
if (is_array($res)) {
foreach ($res as $val) {
foreach ($this->base_coin as $k => $coin) {
$explode_arr = explode($coin, $val['symbol']);
if (2 == count($explode_arr) && empty($explode_arr[1])) {
$ticker[$val['symbol']]['symbol'] = $explode_arr[0] . '/' . $coin;
$ticker[$val['symbol']]['close'] = (float)sprintf("%0.4f", $val['lastPrice']);
$ticker[$val['symbol']]['change'] = (float)sprintf("%0.4f", $val['priceChangePercent']);
$ticker[$val['symbol']]['high'] = (float)sprintf("%0.4f", $val['highPrice']);
$ticker[$val['symbol']]['low'] = (float)sprintf("%0.4f", $val['lowPrice']);
$ticker[$val['symbol']]['vol'] = (float)sprintf("%0.4f", $val['volume']);
}
}
}
}
echo json_encode($ticker);exit;
}
}
\ No newline at end of file
<?php
namespace common\service\exchange\factory;
/**
* Created by PhpStorm.
* User: jiaming
* Date: 2019/8/15
* Time: 10:10
*/
class HuobiBuilder
{
}
\ No newline at end of file
<?php
namespace common\service\exchange\factory;
/**
* Created by PhpStorm.
* User: jiaming
* Date: 2019/8/15
* Time: 10:10
*/
class OkexBuilder
{
}
\ No newline at end of file
<?php
namespace common\service\exchange\factory;
/**
* Created by PhpStorm.
* User: jiaming
* Date: 2019/8/15
* Time: 10:10
*/
class Zhaobi
{
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment