Commit d1cfb63f authored by shajiaiming's avatar shajiaiming

Merge branch 'master' into feature/optimize

parents 283d4301 74c5abaa
<?php
namespace api\controllers;
use Yii;
use yii\data\Pagination;
use api\base\BaseController;
use common\models\psources\CoinCTocTransfer;
class OrderController extends BaseController
{
public function actionIndex()
{
$code = -1;
$request = Yii::$app->request;
if (!$request->isPost) {
$msg = '请求错误!';
goto doEnd;
}
$post = $request->post();
$data['is_sell'] = (false == $post['isSell']) ? 0 : 1;
$data['type'] = $post['type'];
$data['address'] = $post['address'];
$data['token_name'] = strtoupper($post['token_name']);
$data['market_name'] = strtoupper($post['market_name']);
$data['price'] = $post['price'];
$data['amount'] = $post['amount'];
$data['transfer_number'] = date('YmdHis') . self::getrandnums();
$redis = Yii::$app->redis_app;
$key = 'C2C_' . $data['is_sell'] . '_' . str_replace('/', '', strtoupper($post['token_name'])) . '_' . str_replace('/', '', strtoupper($post['market_name']));
if ($redis->hexists($key, $data['address'])) {
$msg = '尚存在一笔未完成的交易!';
goto doEnd;
}
if (!$redis->exists($key)) {
$redis->hmset($key, $data['address'], time());
}
foreach ($post['txs'] as $key => $val) {
$model = new CoinCTocTransfer();
$data['txhex'] = $val['tx'];
$data['transfer_url'] = $val['url'];
$data['step'] = $val['step'];
$data['consensus'] = (false == $val['consensus']) ? 0 : 1;
$model->load($data, '');
$model->save();
}
$code = 0;
$msg = 'success';
doEnd :
return ['code' => $code, 'msg' => $msg];
}
public function actionOrderList()
{
$address = Yii::$app->request->get('address', '');
$is_sell = Yii::$app->request->get('isSell', -1);
$type = Yii::$app->request->get('type', '');
$page = Yii::$app->request->get('page', 1);
if (empty($address)) {
$msg = '请求参数错误';
$code = -1;
$data = null;
goto doEnd;
}
$query = CoinCTocTransfer::find()
->where('address= :address', [':address' => $address])
->orderBy('create_time desc')
->groupBy('address, is_sell');
if ($is_sell > -1) {
$query->andWhere('is_sell= :is_sell', [':is_sell' => $is_sell]);
}
if (false != $type) {
$query->andWhere('type= :type', [':type' => $type]);
}
$data = $query->offset(($page - 1) * 20)->limit(20)->asArray()->all();
$countQuery = clone $query;
$pages = new Pagination(['totalCount' => $countQuery->count(), 'pageSize' => 20]);
$data = [
'list' => $data,
'page' => [
'pageCount' => $pages->pageCount,
'pageSize' => 20,
'currentPage' => $page,
]
];
$code = 1;
$msg = 'success';
doEnd :
return ['code' => $code, 'msg' => $msg, 'data' => $data];
}
public function actionOrderStatus()
{
$address = Yii::$app->request->get('address', '');
$token_name = Yii::$app->request->get('token_name', '');
$market_name = Yii::$app->request->get('market_name', '');
$is_sell = Yii::$app->request->get('isSell', -1);
$type = Yii::$app->request->get('type', '');
if (false == $address || false == $token_name || false == $market_name || false == $type || -1 == $is_sell) {
$msg = '请求参数错误';
$code = -1;
$data = null;
goto doEnd;
}
$latest_model = CoinCTocTransfer::find()->select('transfer_number')
->where(['is_sell' => (int)$is_sell])
->andWhere(['type' => (int)$type])
->andWhere(['address' => $address])
->andWhere(['token_name' => $token_name])
->andWhere(['market_name' => $market_name])
->orderBy('transfer_number desc')
->asArray()
->one();
if (false == $latest_model) {
$msg = '订单不存在';
$code = -1;
$step = 0;
goto doEnd;
}
$orders_info = CoinCTocTransfer::find()->where(['transfer_number' => $latest_model['transfer_number']])->orderBy('id desc')->asArray()->all();
sort($orders_info);
foreach ($orders_info as $key => $val) {
//未交易
if ('0' == $val['send_result'] && '0' == $val['query_result'] && '0' == $val['msg']) {
$step = $key + 1;
$code = 0;
$msg = '第' . ($key + 1) . '笔交易尚未执行';
goto doEnd;
}
//交易报错
if ('0' == $val['send_result'] && '0' == $val['query_result'] && true == $val['msg']) {
$step = $key + 1;
$code = -1;
$msg = $val['msg'];
goto doEnd;
}
//交易成功
if (true == $val['send_result'] && '0' == $val['query_result'] && '0' == $val['msg']) {
$step = $key + 1;
$code = 0;
$msg = $val['send_result'];
goto doEnd;
}
//交易成功,查询失败
if (true == $val['send_result'] && 'success' != $val['query_result']) {
$step = $key + 1;
$code = -1;
$msg = $val['msg'];
goto doEnd;
}
//交易成功,查询成功
if (true == $val['send_result'] && 'success' == $val['query_result'] && 'success' == $val['msg']) {
if ((count($orders_info) - 1) == $key) {
$step = ((count($orders_info) - 1) == $key) ? 99 : ($key + 2);
$code = 0;
$msg = $val['send_result'];
goto doEnd;
}
continue;
}
}
doEnd :
$data = [
'step' => $step,
];
return ['code' => $code, 'msg' => $msg, 'data' => $data];
}
public static function getrandnums()
{
$arr = array();
while (count($arr) < 6) {
$arr[] = rand(0, 9);
$arr = array_unique($arr);
}
return implode("", $arr);
}
}
\ No newline at end of file
......@@ -6,27 +6,90 @@ use api\base\BaseController;
use common\models\psources\CoinAirDropTrade;
use common\models\psources\CoinSupportedSymbol;
use common\service\chain33\Chain33Service;
use linslin\yii2\curl\Curl;
use Yii;
use yii\helpers\ArrayHelper;
class SupportedSymbolController extends BaseController
{
public function actionSearch()
public function actionIndex()
{
$platform_withhold_id = Yii::$app->request->get('id', '');
if (empty($platform_withhold_id)) {
$platform_id = Yii::$app->request->get('platform_id', '');
if (empty($platform_id)) {
$msg = '参数不能为空';
$code = -1;
$data = null;
goto doEnd;
}
$coinSupportedSymbol = CoinSupportedSymbol::find()->select('symbol')->where(['platform_withhold_id' => $platform_withhold_id])->asArray()->all();
$data = array_column($coinSupportedSymbol, 'symbol');
$parentSupportedSymbol = CoinSupportedSymbol::find()->where(['platform_id' => $platform_id])->groupBy('currency')->all();
$market = [];
foreach ($parentSupportedSymbol as $val) {
$coin_name[] = $val->coinInfo->name;
}
$curl = new Curl();
$data = [
"names" => $coin_name
];
$params = json_encode($data);
$curl->setHeader('Content-Type', 'application/json');
$curl->setRawPostData($params);
$res = $curl->post(\Yii::$app->params['biqianbao'].'/interface/coin/coin-index', true);
$res = json_decode($res, true);
$res = array_column($res['data'],NULL,'id');;
foreach ($parentSupportedSymbol as &$val) {
$temp = [];
$temp['id'] = (int)$val->currency;
$temp['name'] = $val->coinInfo->name;
$temp['icon'] = $val->coinInfo->icon;
$temp['isToken'] = (1 == $val->coinInfo->treaty) ? true : false;
$temp['isParacross'] = ('BTY' == strtoupper($val->coinInfo->platform)) ? false : true;
$temp['url'] = 'http://www.huifen.com';
$temp['platform'] = $val->coinInfo->platform;
$temp['rmb'] = isset($res[$val->currency]['rmb']) ? $res[$val->currency]['rmb'] : 0.00;
$temp['usd'] = isset($res[$val->currency]['usd']) ? $res[$val->currency]['usd'] : 0.00;
array_push($market, $temp);
}
$coin_name = [];
foreach ($market as &$val) {
$childSupportedSymbol = CoinSupportedSymbol::find()->where(['platform_id' => $platform_id, 'currency' => $val['id']])->all();
$tokens = [];
foreach ($childSupportedSymbol as $child) {
$coin_name[] = $child->baseCurrencyInfo->name;
}
$data_child = [
"names" => $coin_name
];
$params = json_encode($data_child);
$curl = new Curl();
$curl->setHeader('Content-Type', 'application/json');
$curl->setRawPostData($params);
$res = $curl->post(\Yii::$app->params['biqianbao'].'/interface/coin/coin-index', true);
$res = json_decode($res, true);
$res = array_column($res['data'],NULL,'id');;
foreach ($childSupportedSymbol as $child) {
$temp = [];
$temp['id'] = (int)$child->base_currency;
$temp['name'] = $child->baseCurrencyInfo->name;
$temp['icon'] = $child->baseCurrencyInfo->icon;
$temp['isToken'] = (1 == $child->baseCurrencyInfo->treaty) ? true : false;
$temp['isParacross'] = ('BTY' == strtoupper($child->baseCurrencyInfo->platform)) ? false : true;
$temp['url'] = 'http://www.huifen.com';
$temp['platform'] = $child->baseCurrencyInfo->platform;
$temp['rmb'] = isset($res[$child->base_currency]['rmb']) ? $res[$child->base_currency]['rmb'] : 0.00;
$temp['usd'] = isset($res[$child->base_currency]['usd']) ? $res[$child->base_currency]['usd'] : 0.00;
array_push($tokens, $temp);
}
$val['tokens'] = $tokens;
}
$code = 0;
$msg = 'success';
doEnd :
return ['code' => $code, 'msg' => $msg, 'data' => $data];
return ['code' => $code, 'msg' => $msg, 'data' => $market];
}
}
\ No newline at end of file
......@@ -113,8 +113,7 @@ class WalletController extends BaseController
}
$coin_model = Coin::find()->select('name, treaty')->where(['name' => strtoupper($symbol), 'platform' => $platform->platform])->one();
$service = new Chain33Service($node);
$address[] = $token;
$switch = false;
......@@ -129,10 +128,18 @@ class WalletController extends BaseController
}
}
if ('BTY' == strtoupper($symbol)) {
$switch = true;
$node = \Yii::$app->params['chain_parallel']['primary'];
}
$service = new Chain33Service($node);
$address[] = $token;
if (false == $switch) {
$result = $service->getTokenBalance($address, $execer, $symbol);
} else {
$result = $service->getBalance($address, $execer);
$result = $service->getBalance($address, $execer = '');
}
if (0 !== $result['code']) {
......
......@@ -3,6 +3,7 @@
namespace backend\controllers;
use backend\models\coin\CoinSupportedSymbolForm;
use common\models\psources\Coin;
use common\models\psources\CoinSupportedSymbol;
use Yii;
......@@ -11,7 +12,7 @@ class CoinSupportedSymbolController extends BaseController
{
public function actionList()
{
$platform_withhold_id = Yii::$app->request->get('id');
$platform_id = Yii::$app->request->get('id');
if (Yii::$app->request->isAjax) {
Yii::$app->response->format = 'json';
$request = Yii::$app->request;
......@@ -20,47 +21,45 @@ class CoinSupportedSymbolController extends BaseController
$id = $request->get('id', '');
$where = [];
if ($id) {
$where[] = ['platform_withhold_id' => $id];
$where[] = ['platform_id' => $id];
}
$data = CoinSupportedSymbol::getList($page, $limit, $where);
$data['code'] = 0;
foreach ($data['data'] as &$val) {
$val->currency = $val->coinInfo->name;
$val->base_currency = $val->baseCurrencyInfo->name;
}
Yii::$app->response->format = 'json';
return $data;
}
return $this->render('index', ['platform_withhold_id' => $platform_withhold_id]);
return $this->render('index', ['platform_id' => $platform_id]);
}
public function actionAdd()
{
$this->layout = false;
$model = new CoinSupportedSymbolForm();
$model->scenario = 'add';
if (Yii::$app->request->isPost) {
Yii::$app->response->format = 'json';
$data = Yii::$app->request->post();
if ($model->load($data, '') && $model->validate()) {
$currency = Coin::find()->select('id')->where(['name' => $data['currency']])->asArray()->one();
$base_currency = Coin::find()->select('id')->where(['name' => $data['base_currency']])->asArray()->one();
$coinSupportedSymbol = new CoinSupportedSymbol();
$coinSupportedSymbol->platform_withhold_id = $data['platform_withhold_id'];
$coinSupportedSymbol->symbol = $data['symbol'];
$coinSupportedSymbol->platform_id = $data['platform_id'];
$coinSupportedSymbol->currency = $currency['id'];
$coinSupportedSymbol->base_currency = $base_currency['id'];
$result = $coinSupportedSymbol->save();
if ($result === true) {
$this->success('添加成功', '/admin/coin-supported-symbol/list?id=' . $data['platform_withhold_id']);
}
}
//表单验证失败
$errors = $model->errors;
if ($errors) {
foreach ($errors as $key => $item) {
$errors = $item[0];
break;
if ($result != true) {
return ['code' => -1, 'msg' => current($model->firstErrors)];
}
} elseif (isset($result) && $result['code'] != 0) {
$errors = $result['message'];
}
$this->error($errors, Yii::$app->request->getReferrer());
return ['code' => 0, 'msg' => 'succeed'];
}
$platform_withhold_id = Yii::$app->request->get('platform_withhold_id');
return $this->render('add', ['model' => $model, 'platform_withhold_id' => $platform_withhold_id]);
$platform_id = Yii::$app->request->get('platform_id');
return $this->render('add', ['model' => $model, 'platform_id' => $platform_id]);
}
public function actionDelete()
......
......@@ -2,13 +2,15 @@
namespace backend\models\coin;
use common\models\psources\Coin;
use yii\base\Model;
class CoinSupportedSymbolForm extends Model
{
public $id;
public $platform_withhold_id;
public $symbol;
public $platform_id;
public $currency;
public $base_currency;
public function formName()
{
......@@ -18,34 +20,26 @@ class CoinSupportedSymbolForm extends Model
public function rules()
{
return [
[['platform_withhold_id', 'symbol'], 'required', 'on' => 'add'],
[['id', 'platform_withhold_id', 'symbol'], 'required', 'on' => 'update'],
[['platform_id', 'currency', 'base_currency'], 'required', 'on' => 'add'],
[['id', 'platform_id', 'currency', 'base_currency'], 'required', 'on' => 'update'],
[['currency', 'base_currency'], 'isExist']
];
}
public function attributeLabels()
public function scenarios()
{
return [
'id' => 'ID',
'platform_withhold_id' => '对应链',
'symbol' => '货币对',
'add' => ['platform_id', 'currency', 'base_currency'],
'update' => ['id', 'platform_id', 'currency', 'base_currency'],
];
}
public function scenarios()
public function isExist($attribute, $params)
{
return [
'add' => [
'platform_withhold_id',
'symbol'
],
'update' => [
'id',
'platform_withhold_id',
'symbol'
],
];
$coin_model = Coin::find()->where(['name' => $this->currency])->one();
if (false == $coin_model) {
$this->addError($attribute, '币种不存在');
return false;
}
}
}
\ No newline at end of file
<h4>添加货币对</h4>
<style>
.layui-form-label {
width: 100px;
}
</style>
<div class="layui-row" style="padding: 5px;">
<div class="layui-col-md6">
<form class="layui-form" method="post" action="">
<input name="_csrf" type="hidden" value="<?= Yii::$app->request->getCsrfToken() ?>">
<div class="layui-inline">
<label class="layui-form-label">货币对</label>
<div class="layui-input-block">
<input class="layui-input" name="symbol" lay-verify="required">
</div>
</div>
<div class="layui-form-item">
<button class="layui-btn">提交</button>
</div>
<input class="layui-input" name="platform_withhold_id" type="hidden" value="<?= $platform_withhold_id ?>"
lay-verify="required">
</form>
</div>
</div>
<div style="padding: 5px 20px;">
<br>
<form id="addData">
<input name="_csrf" type="hidden" value="<?= Yii::$app->request->getCsrfToken() ?>">
<div class="input-group my-group">
<span class="input-group-addon">交易货币</span>
<input name="currency" type="text" class="form-control" lay-verify="required">
</div>
<div class="input-group my-group">
<span class="input-group-addon">基础货币</span>
<input name="base_currency" type="text" class="form-control" lay-verify="required">
</div>
<input class="layui-input" name="platform_id" type="hidden" value="<?= $platform_id ?>"
lay-verify="required">
</form>
</div>
<?php
/**
* Created by PhpStorm.
* User: rlgyzhcn
* Date: 18-5-31
* Time: 下午6:23
*/
?>
<style>
.layui-form-label {
width: 100px;
}
</style>
<div class="layui-row" style="padding: 5px;">
<div class="layui-col-md12">
<form class="layui-form" method="post" action="" id="walletEdit">
<input name="_csrf" type="hidden" value="<?= Yii::$app->request->getCsrfToken() ?>">
<input name="id" type="hidden" value="<?= $model->id ?>">
<div class="layui-inline">
<label class="layui-form-label">简称</label>
<div class="layui-input-block">
<input class="layui-input" name="name" value="<?= $model->name ?>" lay-verify="required">
</div>
</div>
<div class="layui-form-item">
<label class="layui-form-label">下载地址</label>
<div class="layui-input-block">
<input class="layui-input" name="download_url" value="<?= $model->download_url ?>">
</div>
</div>
<div class="layui-form-item">
<label class="layui-form-label">介绍</label>
<div class="layui-input-block">
<textarea class="layui-textarea" name="introduce"><?= $model->introduce?></textarea>
</div>
</div>
</form>
</div>
</div>
<script>
var laydate = layui.laydate;
laydate.render({
elem: "#time1"
});
//图片上传
var uploader = layui.upload;
$_csrf = $("input[name='_csrf']").val();
uploader.render({
elem: "#upload1",
url: '/admin/coin/upload',
data: {_csrf: $_csrf},
done: function (res) {
console.log(res.data.src);
$("input[name='icon']").val(res.data.src);
$("#icon1").attr('src', res.data.src);
},
error: function (res) {
}
});
//form render
var form = layui.form;
form.render();
</script>
......@@ -16,15 +16,13 @@ IndexAsset::register($this);
padding: 0px;
}
</style>
<h4>货币列表</h4>
<h4>货币列表</h4>
<div class="layui-row">
<div class="layui-col-md1">
<a href="/admin/coin-supported-symbol/add?platform_withhold_id=<?= $platform_withhold_id ?>"">
<button class="layui-btn layui-btn-default" id="add">添加货币对</button>
</a>
<button class="layui-btn layui-btn-sm" lay-event="add" id="add">单笔添加</button>
</div>
</div>
<input id="platform_withhold_id" type="hidden" value="<?= $platform_withhold_id ?>">
<input id="platform_id" type="hidden" value="<?= $platform_id ?>">
<div class="layui-row">
<table class="layui-table" id="table1" lay-filter="table1"></table>
</div>
......
......@@ -44,5 +44,6 @@ IndexAsset::register($this);
<script type="text/html" id="operationTpl">
<a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>
<a class="layui-btn layui-btn-xs" lay-event="delete">删除</a>
<a class="layui-btn layui-btn-primary layui-btn-xs" href="/admin/coin-supported-coin/list?id={{d.id}}" >支持发行货币种类</a>
<a class="layui-btn layui-btn-primary layui-btn-xs" href="/admin/coin-supported-symbol/list?id={{d.id}}">C2C支持货币对</a>
<a class="layui-btn layui-btn-primary layui-btn-xs" href="/admin/coin-supported-coin/list?id={{d.id}}">支持发行货币种类</a>
</script>
......@@ -5,16 +5,33 @@ var table = layui.table;
var form = layui.form;
var layer = layui.layer;
form.render();
var platform_withhold_id = $("#platform_withhold_id").val();
var platform_id = $("#platform_id").val();
var tableIns = table.render({
elem: "#table1",
url: '/admin/coin-supported-symbol/list?id=' + platform_withhold_id,
url: '/admin/coin-supported-symbol/list?id=' + platform_id,
limit: 10,
page: 1,
loading: true,
cols: [[
{field: 'id', title: 'ID'},
{field: 'symbol', title: '货币对'},
{
field: 'currency',
title: '交易货币',
//templet: function (data) {
//var name = JSON.parse(data.name);
//console.log(typeof (data.name), name.zh);
//return name.zh
//}
},
{
field: 'base_currency',
title: '基础货币',
//templet: function (data) {
//var name = JSON.parse(data.name);
//console.log(typeof (data.name), name.zh);
//return name.zh
//}
},
{field: 'create_time', title: '创建时间'},
{field: 'id', title: '操作', templet: '#operationTpl'}
]],
......@@ -30,7 +47,7 @@ form.on('submit(form1)', function (data) {
table.on('tool(table1)', function (obj) {
var data = obj.data;
if (obj.event == 'delete') {
layer.confirm('真的要删除' + data.symbol + '吗?', {icon: 3, title: '删除'}, function (index) {
layer.confirm('真的要删除' + data.coin_name + '吗?', {icon: 3, title: '删除'}, function (index) {
layer.close(index);
//向服务端发送删除指令
$.get('/admin/coin-supported-symbol/delete?id=' + obj.data.id, function (data, status) {
......@@ -41,4 +58,32 @@ table.on('tool(table1)', function (obj) {
});
});
}
});
$('#add').click(function () {
//打开弹窗
$.get('/admin/coin-supported-symbol/add?platform_id=' + platform_id, {}, function (str) {
var index = layer.open({
type: 1,
title: '添加数据',
id: 'add-one',
skin: 'layui-layer-lan',
area: ['320px', 'auto'],
content: str,
btn: ['确认', '取消'],
btn1: function () {
$.post('/admin/coin-supported-symbol/add', $("#addData").serialize(), function (rev) {
layer.msg(rev.msg);
//if (rev.code == 0) {
layer.close(index);
table.reload("table1", {});
//}
});
}
});
layui.form.render();
});
return false;
});
\ No newline at end of file
<?php
namespace common\models\psources;
use Yii;
use common\core\BaseActiveRecord;
class CoinCTocTransfer extends BaseActiveRecord
{
const TYPE_SELL = 1; //卖单
const TYPE_BUY = 0; //买单
const CONSENSUE_YES = 1; //需要
const CONSENSUE_NO = 0; //不需要
const TYPE_ORDER = 1; //挂单
const TYPE_TRANSFER = 2; //交易
const TYPE_REVOKE = 3; //撤消
const C2C_ORDERING = 'C2C_ORDERING';
const C2C_ORDER_FAILED = 'C2C_ORDER_FAILED';
public static function getDb()
{
return Yii::$app->get('p_sources');
}
public static function tableName()
{
return '{{%coin_ctoc_transfer}}';
}
//定义场景
const SCENARIOS_CREATE = 'create';
public function rules()
{
return [
[['is_sell', 'type', 'address', 'token_name', 'market_name', 'price', 'amount', 'step', 'txhex', 'transfer_url', 'transfer_number', 'consensus'], 'required'],
[['send_result', 'query_result', 'msg'], 'safe']
];
}
public function scenarios()
{
$scenarios = [
self:: SCENARIOS_CREATE => ['is_sell', 'type', 'address', 'token_name', 'market_name', 'price', 'amount', 'step', 'txhex', 'transfer_url', 'transfer_number', 'consensus'],
];
return array_merge(parent:: scenarios(), $scenarios);
}
}
......@@ -48,7 +48,7 @@ class CoinIssueCoin extends CommonActiveRecord
return [
[['name', 'symbol', 'total', 'owner', 'introduction', 'category', 'type', 'platform_id', 'chain_id', 'charge_unit', 'charge'], 'required'],
[['total', 'category', 'type', 'platform_id', 'chain_id'], 'integer'],
[['introduction','charge_unit'], 'string', 'length' => [1, 20]],
[['introduction', 'charge_unit'], 'string', 'length' => [1, 20]],
['symbol', 'string', 'length' => [1, 6]],
['name', 'string', 'length' => [1, 20]],
#['status', 'in', 'range' => [1, 2, 0]],
......@@ -86,7 +86,7 @@ class CoinIssueCoin extends CommonActiveRecord
//非增发
if (CoinIssueCoin::TYPE_NO == $this->type) {
$model = CoinIssueCoin::find()
->where(['name' => $this->name, 'platform_id' => $this->platform_id, 'status' => CoinIssueCoin::STATUS_SUCCESS])
->where(['name' => $this->name, 'platform_id' => $this->platform_id])
->andWhere(['<>', 'status', CoinIssueCoin::STATUS_FAILED])
->orderBy('id desc')->one();
if ($model) {
......@@ -116,7 +116,7 @@ class CoinIssueCoin extends CommonActiveRecord
}
if (CoinIssueCoin::TYPE_NO == $this->type) {
$model = CoinIssueCoin::find()
->where(['symbol' => $this->symbol, 'platform_id' => $this->platform_id, 'status' => CoinIssueCoin::STATUS_SUCCESS])
->where(['symbol' => $this->symbol, 'platform_id' => $this->platform_id])
->andWhere(['<>', 'status', CoinIssueCoin::STATUS_FAILED])
->orderBy('id desc')->one();
if ($model) {
......
......@@ -16,9 +16,9 @@ class CoinSupportedSymbol extends BaseActiveRecord
$query = $query->andWhere($item);
}
$count = $query->count();
$data = $query->offset(($page - 1) * 10)->limit($limit)->asArray()->all();
$data = $query->offset(($page - 1) * 10)->limit($limit)->all();
return ['count' => $count, 'data' => $data];
return ['count' => $count, 'data' => $data, 'code' => 0];
}
public function addOne($params)
......@@ -36,4 +36,20 @@ class CoinSupportedSymbol extends BaseActiveRecord
return ['code' => $exception->getCode(), 'message' => $exception->getMessage()];
}
}
// public function attributes()
// {
// return array_merge(parent::attributes(), ['code']);
// }
public function getCoinInfo()
{
return $this->hasOne(Coin::class, ['id' => 'currency']);
}
public function getBaseCurrencyInfo()
{
return $this->hasOne(Coin::class, ['id' => 'base_currency']);
}
}
......@@ -256,7 +256,7 @@ class Chain33Service
return $this->send($params, 'Chain33.Query');
}
public function getBalance($address, $execer)
public function getBalance($address, $execer = "")
{
$params = [
'addresses' => $address,
......
<?php
namespace console\controllers;
use common\models\psources\CoinCrossChain;
use common\models\psources\CoinCTocTransfer;
use Yii;
use yii\console\Controller;
use common\service\chain33\Chain33Service;
class OrderController extends Controller
{
/**
* 自动交易
*
* @return array
*/
public function actionAutoOrder($is_sell, $type)
{
$redis = Yii::$app->redis_app;
$model = CoinCTocTransfer::find()->where(['send_result' => "0", 'is_sell' => (int)$is_sell, 'type' => (int)$type, 'query_result' => '0'])->asArray()->all();
if (empty($model)) {
echo date('Y-m-d H:i:s') . '暂无交易计划' . PHP_EOL;
return 0;
}
$current_time = time();
foreach ($model as $val) {
if ($val['step'] > 1) {
$isExist = CoinCTocTransfer::find()
->where(['transfer_number' => $val['transfer_number']])
->andWhere(['<', 'id', (int)$val['id']])
->orderBy('id desc')
->asArray()
->one();
//上一步发送成功,未查询
if ('success' != $isExist['query_result'] && !empty($isExist)) {
continue;
}
}
$key = $val['address'] . '_' . $val['is_sell'] . '_' . $val['token_name'] . '_' . $val['market_name'];
$cache_transfering_time = $redis->hget(CoinCTocTransfer::C2C_ORDERING, $key);
if (true == $cache_transfering_time) {
continue;
}
$redis->hmset(CoinCTocTransfer::C2C_ORDERING, $key, $current_time);
go(function () use ($val, $redis, $key) {
\Co::sleep(0.5);
$transfer_url = $val['transfer_url'];
$transfer_url = explode(':', $transfer_url);
$node_params = [
'scheme' => $transfer_url[0],
'host' => str_replace('//', '', $transfer_url[1]),
'port' => isset($transfer_url[2]) ? $transfer_url[2] : '',
];
$service = new Chain33Service($node_params);
$sign_str = $val['txhex'];
$result = $service->sendTransaction($sign_str);
if (0 == $result['code']) {
$send_result = $result['result'];
$currentModel = CoinCTocTransfer::findOne($val['id']);
$currentModel->send_result = $send_result;
$currentModel->msg = 0;
$currentModel->save();
$redis->hdel(CoinCTocTransfer::C2C_ORDERING, $key);
} else {
$currentModel = CoinCTocTransfer::findOne($val['id']);
$currentModel->msg = $result['msg'];
$currentModel->save();
}
});
}
echo date('Y-m-d H:i:s') . '交易成功' . PHP_EOL;
return 0;
}
public function actionOrderStatus()
{
$redis = Yii::$app->redis_app;
$model = CoinCTocTransfer::find()->where(['<>', 'send_result', '0'])->andWhere(['msg' => '0'])->asArray()->all();
if (empty($model)) {
echo date('Y-m-d H:i:s') . '暂无跨链交易计划' . PHP_EOL;
return 0;
}
//正式环境查询共识高度
//3步交易情况下需要做高度判断,2步交易无须判断
$node_params = [
'scheme' => 'https',
'host' => 'jiedian1.bityuan.com',
'port' => 8801
];
//测试环境查询共识高度
//2步交易情况下需要做高度判断
// $node_params = [
// 'scheme' => 'http',
// 'host' => '172.16.100.77',
// 'port' => 8801
// ];
$service = new Chain33Service($node_params);
$result = $service->getHeight();
$consensHeight = $result['result']['consensHeight'];
$current_time = time();
foreach ($model as $val) {
$key = $val['address'] . '_' . $val['is_sell'] . '_' . str_replace('/', '', strtoupper($val['token_name'])) . '_' . str_replace('/', '', strtoupper($val['market_name']));
$node_params = $val['transfer_url'];
$node_params = explode(':', $node_params);
$node_params = [
'scheme' => $node_params[0],
'host' => str_replace('//', '', $node_params[1]),
'port' => isset($node_params[2]) ? $node_params[2] : ''
];
$send_result = $val['send_result'];
$switch = true;
$result = $this->queryTransaction($node_params, $send_result);
if (isset($result['result']['actionName']) && 'unknown' == $result['result']['actionName']) {
$redis->hdel(CoinCTocTransfer::C2C_ORDER_FAILED, $key);
$query_result = 'success';
$msg = 'success';
$height = isset($result['result']['height']) ? $result['result']['height'] : 0;
goto doEnd;
} else if (isset($result['result']['receipt']['ty']) && 2 == $result['result']['receipt']['ty']) {
$redis->hdel(CoinCTocTransfer::C2C_ORDER_FAILED, $key);
$query_result = 'success';
$msg = 'success';
$height = isset($result['result']['height']) ? $result['result']['height'] : 0;
goto doEnd;
} else {
$cache_error_time = $redis->hget(CoinCTocTransfer::C2C_ORDER_FAILED, $key);
if (false == $cache_error_time) {
$redis->hmset(CoinCTocTransfer::C2C_ORDER_FAILED, $key, $current_time);
continue;
}
if (($current_time - $cache_error_time) < 60) {
continue;
}
$redis->hdel(CoinCTocTransfer::C2C_ORDER_FAILED, $key);
if (-1 == $result['code']) {
$msg = $result['msg'];
$query_result = $result['code'];
$switch = false;
goto doEnd;
} else {
$query_result = 'fail';
foreach ($result['result']['receipt']['logs'] as $log) {
if (is_array($log['log'])) continue;
$msg = isset($log['log']) ? $log['log'] : '查询错误';
}
$switch = false;
goto doEnd;
}
}
doEnd :
//3步交易情况下需要做高度判断,2步交易注释IF判断
if (CoinCTocTransfer::CONSENSUE_YES == $val['consensus'] && true == $switch && isset($height) && ($consensHeight < $height)) {
continue;
}
$currentModel = CoinCTocTransfer::findOne($val['id']);
$currentModel->query_result = $query_result;
$currentModel->msg = $msg;
$currentModel->save();
$model_c2c = CoinCTocTransfer::find()->where(['transfer_number' => $val['transfer_number']])->asArray()->all();
$clear = true;
foreach ($model_c2c as $record) {
if ('success' != $record['query_result']) {
$clear = false;
}
}
if ($clear) {
$key = 'C2C_' . $val['is_sell'] . '_' . str_replace('/', '', strtoupper($val['token_name'])) . '_' . str_replace('/', '', strtoupper($val['market_name']));
$redis->hdel($key, $val['address']);
}
}
echo date('Y-m-d H:i:s') . '查询完毕' . PHP_EOL;
return 0;
}
protected function queryTransaction($node_params, $send_result)
{
static $result = [];
$service = new Chain33Service($node_params);
$result = $service->QueryTransaction($send_result);
if (isset($result['result']['receipt']) && is_array($result['result']['receipt']['logs'])) {
foreach ($result['result']['receipt']['logs'] as $log) {
if (isset($log['tyName']) && 'logerr' == strtolower($log['tyName'])) {
return $result;
}
}
}
if (isset($result['result']['tx']['next'])) {
$this->queryTransaction($node_params, $result['result']['tx']['next']);
}
return $result;
}
}
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