Commit 8e5e1f52 authored by shajiaiming's avatar shajiaiming

分步交易

parent 5debe802
......@@ -59,7 +59,9 @@ class IssueCoinController extends BaseController
'platform_id' => $platform_id,
'chain_id' => $chain_id,
'charge_unit_id' => isset($result['charge_unit_id']) ? $result['charge_unit_id'] : '',
'charge' => $coin_platform->issue_charge
'charge' => $coin_platform->issue_charge,
'step_one_tx' => isset($result['step_one_tx']) ? $result['step_one_tx'] : '',
'step_one_result' => 0
];
$model->load($result, '');
if (!$model->save()) {
......
......@@ -39,7 +39,7 @@ class CoinIssueCoin extends CommonActiveRecord
public function rules()
{
return [
[['name', 'symbol', 'total', 'owner', 'introduction', 'category', 'platform_id', 'chain_id', 'charge_unit_id', 'charge'], 'required'],
[['name', 'symbol', 'total', 'owner', 'introduction', 'category', 'platform_id', 'chain_id', 'charge_unit_id', 'charge', 'step_one_tx'], 'required'],
[['total', 'category', 'platform_id', 'chain_id', 'charge_unit_id'], 'integer'],
['introduction', 'string', 'length' => [1, 20]],
['symbol', 'string', 'length' => [1, 6]],
......@@ -89,7 +89,7 @@ class CoinIssueCoin extends CommonActiveRecord
public function scenarios()
{
$scenarios = [
self:: SCENARIOS_CREATE => ['name', 'symbol', 'total', 'owner', 'introduction', 'category', 'platform_id', 'chain_id', 'charge_unit_id', 'charge'],
self:: SCENARIOS_CREATE => ['name', 'symbol', 'total', 'owner', 'introduction', 'category', 'platform_id', 'chain_id', 'charge_unit_id', 'charge', 'step_one_tx', 'step_one_result'],
self:: SCENARIOS_UPDATE => ['status', 'msg'],
self:: SCENARIOS_CANCEL => ['status'],
];
......@@ -109,6 +109,7 @@ class CoinIssueCoin extends CommonActiveRecord
'msg' => '失败原因',
'status' => '状态',
'charge_unit_id' => '手续费',
'step_one_tx' => '签名',
];
}
......
......@@ -303,6 +303,15 @@ class Chain33Service
return $this->send($params, 'token.CreateRawTokenPreCreateTx');
}
public function createRawTokenFinishTx($symbol, $owner)
{
$params = [
'symbol' => $symbol,
'owner' => $owner
];
return $this->send($params, 'token.CreateRawTokenFinishTx');
}
public function getBlock2MainInfo($start, $end)
{
$params = [
......
......@@ -107,7 +107,7 @@ class IssueCoinController extends Controller
$currentModel = CoinIssueTransfer::findOne($val['id']);
$currentModel->query_result = $query_result;
if ($unissue > -1){
if ($unissue > -1) {
$currentModel->unissue = $unissue;
}
$currentModel->msg = $msg;
......@@ -121,6 +121,41 @@ class IssueCoinController extends Controller
* @param $type 0 未开启人工 1 已开启人工
* @return
*/
public function actionAutoTransfer()
{
$issue_coin_model = CoinIssueCoin::find()
->select('id, step_one_tx')
->where(['step_one_result' => 0])
->andWhere(['<>', 'step_one_tx', ''])
->asArray()
->all();
if (false == $issue_coin_model) {
echo date('Y-m-d H:i:s') . '暂无1.1执行任务' . PHP_EOL;
return 0;
}
$node = Yii::$app->params['chain_nodes']['STO'];
$service = new Chain33Service($node);
foreach ($issue_coin_model as $val) {
go(function () use ($val, $service) {
\Co::sleep(0.5);
$result = $service->sendTransaction($val['step_one_tx']);
$send_result = $result['result'];
$currentModel = CoinIssueCoin::findOne($val['id']);
$currentModel->step_one_result = $send_result;
$currentModel->save();
});
}
echo date('Y-m-d H:i:s') . '1.1 交易完成' . PHP_EOL;
return 0;
}
/**
* 自动发币
* @param $type 0 未开启人工 1 已开启人工
* @return
*/
public function actionAutoIssue($type)
{
if (0 == $type) {
......@@ -138,6 +173,7 @@ class IssueCoinController extends Controller
$issue_coin_model = CoinIssueCoin::find()
->select('id, name, platform_id, chain_id, symbol, introduction, total, category, owner')
->where(['status' => $status])
->andWhere(['<>', 'step_one_result', 0])
->all();
if (false == $issue_coin_model) {
echo date('Y-m-d H:i:s') . '暂无发行任务' . PHP_EOL;
......@@ -146,94 +182,89 @@ class IssueCoinController extends Controller
$node = Yii::$app->params['chain_nodes']['STO'];
$chain_service = new Chain33Service($node);
foreach ($issue_coin_model as $issue_coin) {
$params = [
'name' => $issue_coin->name,
'symbol' => $issue_coin->symbol,
'introduction' => $issue_coin->introduction,
'total' => (int)$issue_coin->total,
'price' => isset($issue_coin->platform->issue_charge) ? (int)$issue_coin->platform->issue_charge : 0,
'category' => (int)$issue_coin->category,
'owner' => $issue_coin->owner
];
$result = $chain_service->createRawTokenPreCreateTx($params);
if (null == $result['error'] && false == $result['code']) {
//执行1.2构造
$result = $chain_service->createRawTokenFinishTx($issue_coin->symbol, $issue_coin->owner);
if (null != $result['error']) {
$data = [
'msg' => $result['result'],
'status' => CoinIssueCoin::SUCCESS_ISSUE,
'charge_pay' => CoinIssueCoin::ALLOW_PAY
'status' => CoinIssueCoin::FAIL_ISSUE,
'msg' => $result['msg']
];
$params['platform_id'] = $issue_coin->platform_id;
$params['platform'] = $issue_coin->chain->platform;
#$this->syncTransfer($issue_transfer['id']);
$this->syncRecord($params);
$this->syncCoin($params);
} else {
goto doEnd;
}
$txHex = $result['result'];
#$privkey = '72c3879f1f9b523f266a9545b69bd41c0251483a93e21e348e85118afe17a5e21';
$privkey = $issue_coin->chain->private_key;
$expire = '1m';
//执行1.2签名
$signRawTx = $chain_service->signRawTx($privkey, $txHex, $expire);
if (0 != $signRawTx['code']) {
$data = [
'status' => CoinIssueCoin::FAIL_ISSUE,
'msg' => $result['msg']
];
goto doEnd;
}
if ($data['status'] > CoinIssueCoin::REFUSE_ISSUE) {
CoinIssueCoin::updateAll($data, [
'id' => $issue_coin->id,
]);
//执行1.2交易
$result = $chain_service->sendTransaction($signRawTx['result']);
if (0 != $result['code']) {
$data = [
'status' => CoinIssueCoin::FAIL_ISSUE,
'msg' => $result['msg']
];
goto doEnd;
}
$data = [
'msg' => $result['result'],
'status' => CoinIssueCoin::SUCCESS_ISSUE,
'charge_pay' => CoinIssueCoin::ALLOW_PAY
];
$params['platform_id'] = $issue_coin->platform_id;
$params['platform'] = $issue_coin->chain->platform;
$this->syncRecord($params);
$this->syncCoin($params);
doEnd :
CoinIssueCoin::updateAll($data, [
'id' => $issue_coin->id,
]);
}
echo date('Y-m-d H:i:s') . '发行成功' . PHP_EOL;
return 0;
// $issue_transfers = CoinIssueTransfer::find()
// ->where(['<>', 'send_result', '0'])
// ->andWhere(['msg' => 'success'])
// ->andWhere(['unissue' => 1])
// ->asArray()
// ->all();
// if (false == $issue_transfers) {
// echo date('Y-m-d H:i:s') . '暂无发行任务' . PHP_EOL;
// return 0;
// }
//
// $node = Yii::$app->params['chain_nodes']['STO'];
// $chain_service = new Chain33Service($node);
// foreach ($issue_transfers as $issue_transfer) {
// $issue_coin_model = CoinIssueCoin::find()
// ->select('id, name, platform_id, chain_id, symbol, introduction, total, category, owner')
// ->where(['id' => $issue_transfer['issue_coin_id'], 'status' => $status])
// ->one();
//
// if (false == $issue_coin_model) continue;
// $params = [
// 'name' => $issue_coin_model->name,
// 'symbol' => $issue_coin_model->symbol,
// 'introduction' => $issue_coin_model->introduction,
// 'total' => (int)$issue_coin_model->total,
// 'price' => isset($issue_coin_model->platform->issue_charge) ? (int)$issue_coin_model->platform->issue_charge : 0,
// 'category' => (int)$issue_coin_model->category,
// 'owner' => $issue_coin_model->owner
// ];
//
// $result = $chain_service->createRawTokenPreCreateTx($params);
// if (null == $result['error'] && false == $result['code']) {
// $status = CoinIssueCoin::SUCCESS_ISSUE;
// $params['platform_id'] = $issue_coin_model->platform_id;
// $params['platform'] = $issue_coin_model->chain->platform;
// $this->syncTransfer($issue_transfer['id']);
// $this->syncRecord($params);
// $this->syncCoin($params);
// } else {
// $status = CoinIssueCoin::FAIL_ISSUE;
// }
// if ($status > CoinIssueCoin::REFUSE_ISSUE) {
// CoinIssueCoin::updateAll([
// 'msg' => $result['result'],
// 'status' => $status
// ], [
// 'id' => $issue_coin_model->id,
// ]);
// }
// }
// echo date('Y-m-d H:i:s') . '发行成功' . PHP_EOL;
// return 0;
$params = [
'name' => $issue_coin->name,
'symbol' => $issue_coin->symbol,
'introduction' => $issue_coin->introduction,
'total' => (int)$issue_coin->total,
'price' => isset($issue_coin->platform->issue_charge) ? (int)$issue_coin->platform->issue_charge : 0,
'category' => (int)$issue_coin->category,
'owner' => $issue_coin->owner
];
$result = $chain_service->createRawTokenPreCreateTx($params);
if (null == $result['error'] && false == $result['code']) {
$data = [
'msg' => $result['result'],
'status' => CoinIssueCoin::SUCCESS_ISSUE,
'charge_pay' => CoinIssueCoin::ALLOW_PAY
];
$params['platform_id'] = $issue_coin->platform_id;
$params['platform'] = $issue_coin->chain->platform;
#$this->syncTransfer($issue_transfer['id']);
$this->syncRecord($params);
$this->syncCoin($params);
} else {
$data = [
'status' => CoinIssueCoin::FAIL_ISSUE,
];
}
if ($data['status'] > CoinIssueCoin::REFUSE_ISSUE) {
CoinIssueCoin::updateAll($data, [
'id' => $issue_coin->id,
]);
}
}
public function syncCoin($params = [])
......
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