Commit 17e24d61 authored by shajiaiming's avatar shajiaiming

撤消发行

parent 668b57b5
......@@ -2,6 +2,7 @@
namespace api\controllers;
use common\models\psources\CoinIssueRevokeRecord;
use common\service\chain33\Chain33Service;
use Yii;
use yii\data\Pagination;
......@@ -257,6 +258,10 @@ class IssueCoinController extends BaseController
$msg = '发行失败。失败原因:' . $data->transfer->finish_query_transaction;
}
}
if (CoinIssueCoin::STATUS_CANCEL_FAILED == $data->status) {
$code = -1;
$msg = '撤消失败。失败原因:' . $data->revoke->revoke_query_transaction;
}
doEnd :
return ['code' => $code, 'msg' => $msg, 'data' => $data];
......@@ -325,9 +330,8 @@ class IssueCoinController extends BaseController
}
/**
* 申请撤消/确认
* 确认申请
* @param integer id
* @param integer status
* @return array
*/
public function actionVerify()
......@@ -344,19 +348,12 @@ class IssueCoinController extends BaseController
$params = Yii::$app->request->post();
$id = isset($params['id']) ? (int)$params['id'] : 0;
$status = isset($params['status']) ? (int)$params['status'] : 0;
if (false == $id || false == $status) {
if (false == $id) {
$msg = '缺少必要的参数';
$code = -1;
goto doEnd;
}
if (!in_array($status, [CoinIssueCoin::STATUS_CANCEL, CoinIssueCoin::STATUS_CONFIRM])) {
$msg = '错误的状态值';
$code = -1;
goto doEnd;
}
$model = CoinIssueCoin::findOne($id);
if (false == $model) {
$msg = '不存在的记录';
......@@ -364,20 +361,10 @@ class IssueCoinController extends BaseController
goto doEnd;
}
if (CoinIssueCoin::STATUS_CONFIRM == $status) {
if (CoinIssueCoin::STATUS_PEDDING != $model->status) {
$msg = '该状态下禁止发行';
$code = -1;
goto doEnd;
}
}
if (CoinIssueCoin::STATUS_CANCEL == $status) {
if ('success' != $model->transfer->pre_query_transaction) {
$msg = '该状态下禁止撤消';
$code = -1;
goto doEnd;
}
if (CoinIssueCoin::STATUS_PEDDING != $model->status) {
$msg = '该状态下禁止发行';
$code = -1;
goto doEnd;
}
$record_model = CoinIssueChainRecord::find()->where(['issue_coin_id' => $id])->one();
......@@ -388,7 +375,7 @@ class IssueCoinController extends BaseController
}
$data = [
'status' => $status,
'status' => CoinIssueCoin::STATUS_CONFIRM,
];
$model->setScenario(CoinIssueCoin::SCENARIOS_CANCEL);
$model->load($data, '');
......@@ -398,11 +385,88 @@ class IssueCoinController extends BaseController
goto doEnd;
}
if ($status == CoinIssueCoin::STATUS_CONFIRM) {
$record_model->pre_query_transaction = 'success';
$record_model->update();
$record_model->pre_query_transaction = 'success';
$record_model->update();
$code = 0;
$msg = 'success';
doEnd :
return ['code' => $code, 'msg' => $msg];
}
/**
* 申请撤消
* @param integer id
* @param string revoke_tx
* @param string revoke_send_transaction
* @return array
*/
public function actionRevoke()
{
$data = null;
$header = Yii::$app->request->headers;
$platform_id = $header['FZM-PLATFORM-ID'] ?? null;
if (empty($platform_id)) {
$msg = '缺少必要的参数';
$code = -1;
goto doEnd;
}
$params = Yii::$app->request->post();
$id = isset($params['id']) ? (int)$params['id'] : 0;
$revoke_tx = isset($params['revoke_tx']) ? $params['revoke_tx'] : '';
$revoke_send_transaction = isset($params['revoke_send_transaction']) ? $params['revoke_send_transaction'] : '';
if (false == $revoke_tx || false == $revoke_send_transaction || false == $id) {
$msg = '缺少必要的参数';
$code = -1;
goto doEnd;
}
$issue_coin = CoinIssueCoin::findOne($id);
if (false == $issue_coin) {
$msg = '不存在的记录';
$code = -1;
goto doEnd;
}
if ($issue_coin->status > CoinIssueCoin::STATUS_PEDDING) {
$msg = '该状态下禁止发行';
$code = -1;
goto doEnd;
}
$isExist = CoinIssueRevokeRecord::find()->where(['issue_coin_id' => $id])->count();
if (0 != $isExist) {
$msg = '撤消记录已存在';
$code = -1;
goto doEnd;
}
$issue_coin_data = [
'status' => CoinIssueCoin::STATUS_CANCEL,
];
$issue_coin->setScenario(CoinIssueCoin::SCENARIOS_CANCEL);
$issue_coin->load($issue_coin_data, '');
if (!$issue_coin->save()) {
$msg = current($issue_coin->firstErrors);
$code = -1;
goto doEnd;
}
$issue_revoke_record = new CoinIssueRevokeRecord();
$issue_revoke_data = [
'issue_coin_id' => $id,
'revoke_tx' => $revoke_tx,
'revoke_send_transaction' => $revoke_send_transaction,
'revoke_query_transaction' => 'standby',
];
$issue_revoke_record->setScenario(CoinIssueRevokeRecord::SCENARIOS_CREATE);
$issue_revoke_record->load($issue_revoke_data, '');
$issue_revoke_record->save();
$code = 0;
$msg = 'success';
......
......@@ -26,6 +26,7 @@ class CoinIssueCoin extends CommonActiveRecord
const TYPE_YES = 1; //是增发
const ISSUE_TOKEN = 'issue_token';
const REVOKE_TOKEN = 'revoke_token';
//定义场景
const SCENARIOS_CREATE = 'create';
......@@ -164,4 +165,8 @@ class CoinIssueCoin extends CommonActiveRecord
{
return $this->hasOne(CoinIssueChainRecord::className(), ['issue_coin_id' => 'id']);
}
public function getRevoke()
{
return $this->hasOne(CoinIssueRevokeRecord::className(), ['issue_coin_id' => 'id']);
}
}
\ No newline at end of file
<?php
namespace common\models\psources;
use Yii;
class CoinIssueRevokeRecord extends CommonActiveRecord
{
//定义场景
const SCENARIOS_CREATE = 'create';
public static function getDb()
{
return Yii::$app->get('p_sources');
}
public static function tableName()
{
return '{{%coins_issue_revoke_record}}';
}
public function rules()
{
return [
[['revoke_tx', 'revoke_send_transaction', 'issue_coin_id'], 'required'],
[['issue_coin_id'], 'integer'],
];
}
public function scenarios()
{
$scenarios = [
self:: SCENARIOS_CREATE => ['revoke_tx', 'revoke_send_transaction', 'issue_coin_id', 'revoke_query_transaction'],
];
return array_merge(parent:: scenarios(), $scenarios);
}
public function getCoin()
{
return $this->hasOne(CoinIssueCoin::className(), ['id' => 'issue_coin_id']);
}
}
\ No newline at end of file
......@@ -2,6 +2,7 @@
namespace console\controllers;
use common\models\psources\CoinIssueRevokeRecord;
use Yii;
use yii\console\Controller;
use yii\helpers\ArrayHelper;
......@@ -274,55 +275,68 @@ class IssueChainTransferController extends Controller
public function actionCancel()
{
$redis = Yii::$app->redis;
$issue_coin_model = CoinIssueCoin::find()->where(['status' => CoinIssueCoin::STATUS_CANCEL])->all();
if (false == $issue_coin_model) {
echo date('Y-m-d H:i:s') . '暂无需要撤消的发行' . PHP_EOL;
return 0;
}
$node = Yii::$app->params['chain_nodes']['STO'];
$chain_service = new Chain33Service($node);
$service = new Chain33Service($node);
$current_time = time();
foreach ($issue_coin_model as $val) {
$data = [];
//执行1.3构造
$result = $chain_service->CreateRawTokenRevokeTx($val->symbol, $val->owner);
if (null != $result['error']) {
$status = CoinIssueCoin::STATUS_CANCEL_FAILED;
$result = $service->QueryTransaction($val->revoke->revoke_send_transaction);
if (isset($result['result']['actionName']) && 'unknown' == $result['result']['actionName']) {
$data = [
'pre_query_transaction' => 'fail'
'revoke_query_transaction' => 'success'
];
$status = CoinIssueCoin::STATUS_CANCEL_SUCCESS;
$redis->hdel(CoinIssueCoin::REVOKE_TOKEN, $val->id);
goto doEnd;
}
$txHex = $result['result'];
$privkey = '8ac19c0b8858ccd6ed34e2bce0f11be2fc696e658d0b98fb1d3ef85ec5a3992c';
$expire = '1m';
//执行1.3签名
$signRawTx = $chain_service->signRawTx($privkey, $txHex, $expire);
if (0 != $signRawTx['code']) {
$status = CoinIssueCoin::STATUS_CANCEL_FAILED;
} else if (isset($result['result']['receipt']['ty']) && 2 == $result['result']['receipt']['ty']) {
$data = [
'pre_query_transaction' => 'fail'
'revoke_query_transaction' => 'success'
];
$status = CoinIssueCoin::STATUS_CANCEL_SUCCESS;
$redis->hdel(CoinIssueCoin::REVOKE_TOKEN, $val->id);
goto doEnd;
}
//执行1.3交易
$result = $chain_service->sendTransaction($signRawTx['result']);
if (0 != $result['code']) {
} else {
$status = CoinIssueCoin::STATUS_CANCEL_FAILED;
$data = [
'pre_query_transaction' => 'fail'
];
if (isset($result['result']['receipt']['logs'])) {
foreach ($result['result']['receipt']['logs'] as $log) {
if (isset($log['tyName']) && 'LogErr' == $log['tyName']) {
$data = [
'revoke_query_transaction' => $log['log']
];
break;
}
}
} else {
$data = [
'revoke_query_transaction' => $result['msg']
];
}
$cache_error_time = $redis->hget(CoinIssueCoin::REVOKE_TOKEN, $val->id);
if (false == $cache_error_time) {
$redis->hmset(CoinIssueCoin::REVOKE_TOKEN, $val->id, $current_time);
continue;
}
if (($current_time - $cache_error_time) < 30) {
continue;
}
$redis->hdel(CoinIssueCoin::REVOKE_TOKEN, $val->id);
goto doEnd;
}
$status = CoinIssueCoin::STATUS_CANCEL_SUCCESS;
$data = [
'pre_query_transaction' => $result['result']
];
doEnd :
CoinIssueCoin::updateAll(['status' => $status], [
'id' => $val->id,
]);
CoinIssueChainRecord::updateAll($data, [
CoinIssueRevokeRecord::updateAll($data, [
'issue_coin_id' => $val->id,
]);
}
......
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