Commit 60f0b8c2 authored by shajiaiming's avatar shajiaiming

fix

parent 37ee074c
...@@ -50,14 +50,6 @@ class IssueCoinController extends BaseController ...@@ -50,14 +50,6 @@ class IssueCoinController extends BaseController
goto doEnd; goto doEnd;
} }
$pre_create_tx = isset($result['pre_create_tx']) ? $result['pre_create_tx'] : '';
$pre_send_transaction = isset($result['pre_send_transaction']) ? $result['pre_send_transaction'] : '';
if (false == $pre_create_tx || false == $pre_send_transaction) {
$msg = '预签名错误';
$code = -1;
goto doEnd;
}
$result = [ $result = [
'name' => isset($result['name']) ? $result['name'] : '', 'name' => isset($result['name']) ? $result['name'] : '',
'symbol' => isset($result['symbol']) ? strtoupper($result['symbol']) : '', 'symbol' => isset($result['symbol']) ? strtoupper($result['symbol']) : '',
...@@ -80,20 +72,6 @@ class IssueCoinController extends BaseController ...@@ -80,20 +72,6 @@ class IssueCoinController extends BaseController
$code = 0; $code = 0;
$data = $model->getPrimaryKey(); $data = $model->getPrimaryKey();
$params = [
'pre_create_tx' => $pre_create_tx,
'pre_send_transaction' => $pre_send_transaction,
'pre_query_transaction' => 'standby',
'finish_tx' => 'standby',
'finish_send_transaction' => 'standby',
'finish_query_transaction' => 'standby',
'issue_coin_id' => $data,
];
$transfer_model = new CoinIssueChainRecord();
$transfer_model->setScenario(CoinIssueChainRecord::SCENARIOS_PRE_CREATE);
$transfer_model->load($params,'');
$transfer_model->save();
doEnd : doEnd :
return ['code' => $code, 'msg' => $msg, 'data' => $data]; return ['code' => $code, 'msg' => $msg, 'data' => $data];
} }
...@@ -175,14 +153,69 @@ class IssueCoinController extends BaseController ...@@ -175,14 +153,69 @@ class IssueCoinController extends BaseController
} }
/** /**
* 撤消申请 * 申请撤消/确认
* @param integer id
* @param string pre_create_tx
* @param string pre_send_transaction
* @return array
*/
public function actionSendTransfer()
{
$params = Yii::$app->request->post();
$id = isset($params['id']) ? (int)$params['id'] : 0;
$pre_create_tx = isset($params['pre_create_tx']) ? $params['pre_create_tx'] : '';
$pre_send_transaction = isset($params['pre_send_transaction']) ? $params['pre_send_transaction'] : '';
if (false == $id || false == $pre_create_tx || false == $pre_send_transaction) {
$msg = '缺少必要的参数';
$code = -1;
goto doEnd;
}
$model = CoinIssueChainRecord::find()->where(['issue_coin_id' => $id])->one();
if (true == $model) {
$msg = '交易记录已存在';
$code = -1;
goto doEnd;
}
$params = [
'pre_create_tx' => $pre_create_tx,
'pre_send_transaction' => $pre_send_transaction,
'pre_query_transaction' => 'standby',
'finish_tx' => 'standby',
'finish_send_transaction' => 'standby',
'finish_query_transaction' => 'standby',
'issue_coin_id' => $id,
];
$transfer_model = new CoinIssueChainRecord();
$transfer_model->setScenario(CoinIssueChainRecord::SCENARIOS_PRE_CREATE);
$transfer_model->load($params, '');
if (!$transfer_model->save()) {
$msg = current($transfer_model->firstErrors);
$code = -1;
goto doEnd;
}
$code = 0;
$msg = 'success';
doEnd :
return ['code' => $code, 'msg' => $msg];
}
/**
* 申请撤消/确认
* @param integer id * @param integer id
* @param integer status
* @return array * @return array
*/ */
public function actionVerify() public function actionVerify()
{ {
$id = Yii::$app->request->post('id', ''); $params = Yii::$app->request->post();
if (false == $id) { $id = isset($params['id']) ? (int)$params['id'] : 0;
$status = isset($params['status']) ? (int)$params['status'] : 0;
if (false == $id || false == $status) {
$msg = '缺少必要的参数'; $msg = '缺少必要的参数';
$code = -1; $code = -1;
goto doEnd; goto doEnd;
...@@ -195,6 +228,13 @@ class IssueCoinController extends BaseController ...@@ -195,6 +228,13 @@ class IssueCoinController extends BaseController
goto doEnd; goto doEnd;
} }
$record_model = CoinIssueChainRecord::find()->where(['issue_coin_id' => $id])->one();
if (false == $record_model) {
$msg = '交易记录不存在';
$code = -1;
goto doEnd;
}
if ($model->status > CoinIssueCoin::UN_AUDIT) { if ($model->status > CoinIssueCoin::UN_AUDIT) {
$msg = '不允许取消的发行'; $msg = '不允许取消的发行';
$code = -1; $code = -1;
...@@ -202,7 +242,7 @@ class IssueCoinController extends BaseController ...@@ -202,7 +242,7 @@ class IssueCoinController extends BaseController
} }
$data = [ $data = [
'status' => CoinIssueCoin::CANCEL_ISSUE, 'status' => $status,
]; ];
$model->setScenario(CoinIssueCoin::SCENARIOS_CANCEL); $model->setScenario(CoinIssueCoin::SCENARIOS_CANCEL);
$model->load($data, ''); $model->load($data, '');
...@@ -212,10 +252,117 @@ class IssueCoinController extends BaseController ...@@ -212,10 +252,117 @@ class IssueCoinController extends BaseController
goto doEnd; goto doEnd;
} }
if ($status == CoinIssueCoin::ALLOWED_ISSUE) {
$record_model->pre_query_transaction = 'success';
$record_model->update();
}
$code = 0; $code = 0;
$msg = 'success'; $msg = 'success';
doEnd : doEnd :
return ['code' => $code, 'msg' => $msg]; return ['code' => $code, 'msg' => $msg];
} }
/**
* 手续费划转
* @return
*/
public function actionPayCharge()
{
$issue_coin_model = CoinIssueCoin::find()
->select('id')->where(['charge_pay' => CoinIssueCoin::ALLOW_PAY])->asArray()->all();
if (false == $issue_coin_model) {
echo date('Y-m-d H:i:s') . '暂无手续费划转任务' . PHP_EOL;
return 0;
}
$issue_transfer = CoinIssueTransfer::find()
->where(['unissue' => 1])
->andWhere(['in', 'issue_coin_id', ArrayHelper::getColumn($issue_coin_model, 'id')])
->asArray()
->all();
if (false == $issue_transfer) {
echo date('Y-m-d H:i:s') . '暂无手续费划转任务' . PHP_EOL;
return 0;
}
$node = Yii::$app->params['chain_nodes']['STO'];
$service = new Chain33Service($node);
foreach ($issue_transfer as $val) {
$result = $service->sendTransaction($val['txhex']);
if (0 == $result['code']) {
$send_result = $result['result'];
$currentModel = CoinIssueTransfer::findOne($val['id']);
$currentModel->send_result = $send_result;
$currentModel->msg = 0;
$currentModel->save();
} else {
$currentModel = CoinIssueTransfer::findOne($val['id']);
$currentModel->msg = $result['msg'];
$currentModel->save();
}
}
echo date('Y-m-d H:i:s') . '手续费划转任务完成' . PHP_EOL;
return 0;
}
/**
* 手续费划转状态确认
* @return
*/
public function actionPayChargeStatus()
{
$unissue = -1;
$model = CoinIssueTransfer::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;
}
$node = \Yii::$app->params['chain_parallel']['primary'];
$service = new Chain33Service($node);
foreach ($model as $val) {
$send_result = $val['send_result'];
$result = $service->QueryTransaction($send_result);
if (isset($result['result']['actionName']) && 'unknown' == $result['result']['actionName']) {
$query_result = 'success';
$msg = 'success';
goto doEnd;
} else if (isset($result['result']['receipt']['ty']) && 2 == $result['result']['receipt']['ty']) {
$query_result = 'success';
$msg = 'success';
goto doEnd;
} else {
if (isset($result['result']['receipt']['logs'])) {
foreach ($result['result']['receipt']['logs'] as $log) {
if (isset($log['tyName']) && 'LogErr' == $log['tyName']) {
$msg = $log['tyName'];
break;
}
}
} else {
$msg = $result['msg'];
}
$query_result = $result['code'];
goto doEnd;
}
doEnd :
if ('success' == $msg) {
$unissue = 0;
$coin_issue_coin = CoinIssueCoin::find()->where(['id' => $val['issue_coin_id']])->one();
$coin_issue_coin->charge_pay = CoinIssueCoin::SUCCESS_PAY;
$coin_issue_coin->save();
}
$currentModel = CoinIssueTransfer::findOne($val['id']);
$currentModel->query_result = $query_result;
if ($unissue > -1) {
$currentModel->unissue = $unissue;
}
$currentModel->msg = $msg;
$currentModel->save();
}
}
} }
\ No newline at end of file
...@@ -35,4 +35,9 @@ class CoinIssueChainRecord extends CommonActiveRecord ...@@ -35,4 +35,9 @@ class CoinIssueChainRecord extends CommonActiveRecord
]; ];
return array_merge(parent:: scenarios(), $scenarios); return array_merge(parent:: scenarios(), $scenarios);
} }
public function getCoin()
{
return $this->hasOne(CoinIssueCoin::className(), ['id' => 'issue_coin_id']);
}
} }
\ No newline at end of file
...@@ -20,6 +20,7 @@ class CoinIssueCoin extends CommonActiveRecord ...@@ -20,6 +20,7 @@ class CoinIssueCoin extends CommonActiveRecord
const SUCCESS_ISSUE = 3; const SUCCESS_ISSUE = 3;
const FAIL_ISSUE = 4; const FAIL_ISSUE = 4;
const CANCEL_ISSUE = 5; const CANCEL_ISSUE = 5;
const ALLOWED_ISSUE = 6;
//定义场景 //定义场景
const SCENARIOS_CREATE = 'create'; const SCENARIOS_CREATE = 'create';
...@@ -146,6 +147,6 @@ class CoinIssueCoin extends CommonActiveRecord ...@@ -146,6 +147,6 @@ class CoinIssueCoin extends CommonActiveRecord
public function getTransfer() public function getTransfer()
{ {
return $this->hasOne(CoinIssueChainTransfer::className(), ['issue_coin_id' => 'id']); return $this->hasOne(CoinIssueChainRecord::className(), ['issue_coin_id' => 'id']);
} }
} }
\ No newline at end of file
<?php
namespace common\models\psources;
use Yii;
use common\core\BaseActiveRecord;
class CoinIssueTransfer extends BaseActiveRecord
{
public static function getDb()
{
return Yii::$app->get('p_sources');
}
public static function tableName()
{
return '{{%coin_issue_transfer}}';
}
//定义场景
const SCENARIOS_CREATE = 'create';
public function rules()
{
return [
[['txhex', 'issue_coin_id'], 'required'],
[['send_result', 'query_result', 'msg'], 'safe']
];
}
public function scenarios()
{
$scenarios = [
self:: SCENARIOS_CREATE => ['txhex', 'issue_coin_id', 'send_result', 'query_result', 'msg'],
];
return array_merge(parent:: scenarios(), $scenarios);
}
}
...@@ -2,15 +2,46 @@ ...@@ -2,15 +2,46 @@
namespace console\controllers; namespace console\controllers;
use common\models\psources\CoinIssueChainRecord;
use common\service\chain33\Chain33Service;
use Yii; use Yii;
use yii\console\Controller; use yii\console\Controller;
use yii\helpers\ArrayHelper;
use common\models\psources\Coin;
use common\models\psources\CoinIssueChainRecord;
use common\models\psources\CoinIssueCoin;
use common\models\psources\CoinIssueRecord;
use common\models\psources\CoinIssueTransfer;
use common\service\chain33\Chain33Service;
class IssueChainTransferController extends Controller class IssueChainTransferController extends Controller
{ {
public function actionSendTransaction($step) /**
* 自动发币
* @param $step 1 生成预创建token 的交易 2 生成完成创建token 的交易
* @param $type 0 自动发布 1 人工发布
* @return
*/
public function actionSendTransaction($step, $type)
{ {
if (0 == $type) {
$status = CoinIssueCoin::UN_AUDIT;
$manual_review = Yii::$app->redis->get('issue_chain_manual_review');
if ('close' == $manual_review) {
return 0;
}
}
if (1 == $type) {
$status = CoinIssueCoin::ALLOW_ISSUE;
}
$issue_coin_model = CoinIssueCoin::find()->select('id')->where(['status' => $status])->asArray()->all();
if (false == $issue_coin_model) {
echo date('Y-m-d H:i:s') . '暂无执行任务' . PHP_EOL;
return 0;
}
$issue_coin_ids = ArrayHelper::getColumn($issue_coin_model, 'id');
if (1 == $step) { if (1 == $step) {
$columns = ['id', 'pre_create_tx', 'issue_coin_id']; $columns = ['id', 'pre_create_tx', 'issue_coin_id'];
$condition = [ $condition = [
...@@ -23,12 +54,13 @@ class IssueChainTransferController extends Controller ...@@ -23,12 +54,13 @@ class IssueChainTransferController extends Controller
$columns = ['id', 'finish_tx', 'issue_coin_id']; $columns = ['id', 'finish_tx', 'issue_coin_id'];
$condition = [ $condition = [
'pre_query_transaction' => 'success', 'pre_query_transaction' => 'success',
'finish_tx' => 'standby',
'finish_send_transaction' => 'standby', 'finish_send_transaction' => 'standby',
'finish_query_transaction' => 'standby' 'finish_query_transaction' => 'standby'
]; ];
} }
$model = CoinIssueChainRecord::find()->select($columns)->where($condition)->all(); $model = CoinIssueChainRecord::find()->select($columns)->where($condition)->andWhere(['in', 'issue_coin_id', $issue_coin_ids])->all();
if (false == $model) { if (false == $model) {
echo date('Y-m-d H:i:s') . ' STEP: ' . $step . '暂无交易计划' . PHP_EOL; echo date('Y-m-d H:i:s') . ' STEP: ' . $step . '暂无交易计划' . PHP_EOL;
...@@ -38,35 +70,69 @@ class IssueChainTransferController extends Controller ...@@ -38,35 +70,69 @@ class IssueChainTransferController extends Controller
$node = Yii::$app->params['chain_nodes']['STO']; $node = Yii::$app->params['chain_nodes']['STO'];
$chain_service = new Chain33Service($node); $chain_service = new Chain33Service($node);
foreach ($model as $val) { foreach ($model as $val) {
$result = $chain_service->sendTransaction($val->pre_create_tx); //执行1.2构造
$result = $chain_service->createRawTokenFinishTx($val->coin->symbol, $val->coin->owner);
if (null != $result['error']) {
$status = CoinIssueCoin::FAIL_ISSUE;
$data = [
'status' => CoinIssueCoin::FAIL_ISSUE,
'finish_query_transaction' => $result['msg']
];
goto doEnd;
}
$txHex = $result['result'];
$privkey = '72c3879f1f9b523f266a9545b69bd41c0251483a93e21e348e85118afe17a5e21';
#$privkey = $val->coin->chain->private_key;
$expire = '1m';
//执行1.2签名
$signRawTx = $chain_service->signRawTx($privkey, $txHex, $expire);
if (0 != $signRawTx['code']) {
$status = CoinIssueCoin::FAIL_ISSUE;
$data = [
'finish_query_transaction' => $signRawTx['msg']
];
goto doEnd;
}
//执行1.2交易
$result = $chain_service->sendTransaction($signRawTx['result']);
if (0 != $result['code']) { if (0 != $result['code']) {
$params = [ $status = CoinIssueCoin::FAIL_ISSUE;
'pre_query_transaction' => $result['msg'] $data = [
'finish_tx' => $signRawTx['result'],
'finish_query_transaction' => $result['msg']
]; ];
goto doEnd; goto doEnd;
} }
$params = [ $status = CoinIssueCoin::SUCCESS_ISSUE;
'pre_send_transaction' => $result['result'] $data = [
'finish_tx' => $signRawTx['result'],
'finish_send_transaction' => $result['result']
]; ];
doEnd : doEnd :
CoinIssueChainRecord::updateAll($data, [
$current_record = CoinIssueChainRecord::findOne($val->id); 'id' => $val->id,
$current_record->load($params, ''); ]);
$current_record->update(); //交易失败
echo json_encode($current_record->errors);exit; if ($status == CoinIssueCoin::FAIL_ISSUE) {
CoinIssueCoin::updateAll(['status' => $status], [
'id' => $val->coin->id,
]);
} }
}
echo date('Y-m-d H:i:s') . '执行完成' . PHP_EOL;
return 0;
} }
public function actionQueryTransaction($step) public function actionQueryTransaction($step)
{ {
if (1 == $step) { if (1 == $step) {
$model = CoinIssueChainRecord::find()->where(['<>' , 'pre_send_transaction', 'standby'])->all(); $model = CoinIssueChainRecord::find()->where(['<>', 'pre_send_transaction', 'standby'])->all();
} }
if (2 == $step) { if (2 == $step) {
$model = CoinIssueChainRecord::find()->where(['<>' , 'pre_send_transaction', 'standby'])->andWhere(['<>', 'finish_send_transaction', 'standby'])->all(); $model = CoinIssueChainRecord::find()->where(['pre_send_transaction' => 'success'])->andWhere(['<>', 'finish_send_transaction', 'standby'])->all();
} }
if (false == $model) { if (false == $model) {
...@@ -84,30 +150,98 @@ class IssueChainTransferController extends Controller ...@@ -84,30 +150,98 @@ class IssueChainTransferController extends Controller
$result = $service->QueryTransaction($val->finish_send_transaction); $result = $service->QueryTransaction($val->finish_send_transaction);
} }
if (isset($result['result']['actionName']) && 'unknown' == $result['result']['actionName']) { if (isset($result['result']['actionName']) && 'unknown' == $result['result']['actionName']) {
$query_result = 'success'; $status = CoinIssueCoin::SUCCESS_ISSUE;
$msg = 'success'; $data = [
'finish_query_transaction' => 'success'
];
goto doEnd; goto doEnd;
} else if (isset($result['result']['receipt']['ty']) && 2 == $result['result']['receipt']['ty']) { } else if (isset($result['result']['receipt']['ty']) && 2 == $result['result']['receipt']['ty']) {
$query_result = 'success'; $status = CoinIssueCoin::SUCCESS_ISSUE;
$msg = 'success'; $data = [
'finish_query_transaction' => 'success'
];
goto doEnd; goto doEnd;
} else { } else {
$status = CoinIssueCoin::FAIL_ISSUE;
if (isset($result['result']['receipt']['logs'])) { if (isset($result['result']['receipt']['logs'])) {
foreach ($result['result']['receipt']['logs'] as $log) { foreach ($result['result']['receipt']['logs'] as $log) {
if (isset($log['tyName']) && 'LogErr' == $log['tyName']) { if (isset($log['tyName']) && 'LogErr' == $log['tyName']) {
$msg = $log['tyName']; $data = [
'finish_query_transaction' => $log['tyName']
];
break; break;
} }
} }
} else { } else {
$msg = $result['msg']; $data = [
'finish_query_transaction' => $result['msg']
];
} }
$query_result = $result['code'];
goto doEnd; goto doEnd;
} }
doEnd : doEnd :
CoinIssueChainRecord::updateAll($data, [
'id' => $val->id,
]);
//查询后,交易失败
if ($status == CoinIssueCoin::FAIL_ISSUE) {
CoinIssueCoin::updateAll(['status' => $status], [
'id' => $val->coin->id,
]);
}
//查询后,交易成功
if ($status = CoinIssueCoin::SUCCESS_ISSUE) {
CoinIssueCoin::updateAll(['status' => $status, 'charge_pay' => CoinIssueCoin::SUCCESS_PAY], [
'id' => $val->coin->id,
]);
$params = [
'name' => $val->coin->name,
'symbol' => $val->coin->symbol,
'introduction' => $val->coin->introduction,
'total' => (int)$val->coin->total,
'price' => isset($val->coin->platform->issue_charge) ? (int)$val->coin->platform->issue_charge : 0,
'category' => (int)$val->coin->category,
'owner' => $val->coin->owner,
'platform_id' => $val->coin->platform_id,
'platform' => $val->coin->chain->platform
];
$this->syncRecord($params);
$this->syncCoin($params);
}
}
echo date('Y-m-d H:i:s') . '查询完成,同步完成' . PHP_EOL;
return 0;
}
public function syncCoin($params = [])
{
$model_coin = Coin::find()->where(['name' => $params['name'], 'platform' => $params['platform']])->one();
if (false == $model_coin) {
$model = new Coin();
$model->name = $params['symbol'];
$model->sid = $params['name'];
$model->platform = $params['platform'];
$model->chain = 'BTY';
$model->treaty = 1;
$model->save();
}
} }
public function syncRecord($params = [])
{
$coin_issue_record = new CoinIssueRecord();
$coin_issue_record->platform_id = $params['platform_id'];
$coin_issue_record->total = $params['total'];
$coin_issue_record->save();
}
public function syncTransfer($id)
{
$issue_transfer = CoinIssueTransfer::find()->where(['id' => $id])->one();
$issue_transfer->unissue = 0;
$issue_transfer->save();
} }
} }
\ 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