Commit 290d40bc authored by shajiaiming's avatar shajiaiming

Merge branch 'feature/issue_coin' into 'master'

Feature/issue coin See merge request !268
parents a6f957c0 c71f9826
......@@ -4,6 +4,7 @@ namespace api\controllers;
use common\components\ErrorMessage;
use common\models\psources\CoinIssueRevokeRecord;
use common\models\psources\CoinIssueAttachment;
use common\service\chain33\Chain33Service;
use Yii;
use yii\data\Pagination;
......@@ -42,7 +43,7 @@ class IssueCoinController extends BaseController
goto doEnd;
}
$result = [
$params = [
'name' => isset($result['name']) ? $result['name'] : '',
'symbol' => isset($result['symbol']) ? $result['symbol'] : '',
'total' => isset($result['total']) ? $result['total'] : '',
......@@ -54,10 +55,24 @@ class IssueCoinController extends BaseController
'chain_id' => $chain_id,
'charge_unit' => isset($result['charge_unit']) ? $result['charge_unit'] : '',
'charge' => $coin_platform->issue_charge,
'token_type' => isset($result['token_type']) ? $result['token_type'] : 0,
'nickname' => isset($result['nickname']) ? $result['nickname'] : ''
];
$model = new CoinIssueCoin();
$model->setScenario(CoinIssueCoin::SCENARIOS_CREATE);
$model->load($result, '');
if (0 == $params['token_type']) {
$model->setScenario(CoinIssueCoin::SCENARIOS_CREATE);
}
if (in_array($params['token_type'], [1, 2])) {
$model->setScenario(CoinIssueCoin::SCENARIOS_NO_CHARGE);
}
if (in_array($params['token_type'], [3, 4])) {
$params['template'] = isset($result['template']) ? $result['template'] : [];
$model->setScenario(CoinIssueCoin::SCENARIOS_VERIFY_MALL);
}
$model->load($params, '');
if (!$model->validate()) {
$msg = $model->errors;
$code = -1;
......@@ -95,9 +110,6 @@ class IssueCoinController extends BaseController
goto doEnd;
}
$model = new CoinIssueCoin();
$model->setScenario(CoinIssueCoin::SCENARIOS_CREATE);
if (!Yii::$app->request->isPost) {
$msg = '错误的请求方式';
$code = -1;
......@@ -118,8 +130,7 @@ class IssueCoinController extends BaseController
$code = -1;
goto doEnd;
}
$result = [
$params = [
'name' => isset($result['name']) ? $result['name'] : '',
'symbol' => isset($result['symbol']) ? strtoupper($result['symbol']) : '',
'total' => isset($result['total']) ? $result['total'] : '',
......@@ -127,12 +138,29 @@ class IssueCoinController extends BaseController
'introduction' => isset($result['introduction']) ? $result['introduction'] : '',
'category' => isset($result['category']) ? $result['category'] : 0,
'type' => isset($result['type']) ? $result['type'] : 0,
'platform_id' => $platform_id,
'platform_id' => (int)$platform_id,
'chain_id' => $chain_id,
'charge_unit' => isset($result['charge_unit']) ? $result['charge_unit'] : '',
'charge' => (CoinIssueCoin::TYPE_NO == $result['type']) ? $coin_platform->issue_charge : 0,
'charge' => isset($result['type']) ? ((CoinIssueCoin::TYPE_NO == $result['type']) ? $coin_platform->issue_charge : 0) : 0,
'token_type' => isset($result['token_type']) ? $result['token_type'] : 0,
'nickname' => isset($result['nickname']) ? $result['nickname'] : ''
];
$model = new CoinIssueCoin();
if (0 == $params['token_type']) {
$model->setScenario(CoinIssueCoin::SCENARIOS_CREATE);
}
if (in_array($params['token_type'], [1, 2])) {
$model->setScenario(CoinIssueCoin::SCENARIOS_NO_CHARGE);
}
if (in_array($params['token_type'], [3, 4])) {
$model->setScenario(CoinIssueCoin::SCENARIOS_CREATE_MALL);
}
$model->load($result, '');
if (!$model->save()) {
$msg = $model->errors;
$code = -1;
......@@ -141,6 +169,13 @@ class IssueCoinController extends BaseController
$msg = 'ok';
$code = 0;
$data = $model->getPrimaryKey();
if (0 != $params['token_type']) {
$template = new CoinIssueAttachment();
$template->issue_coin_id = $data;
$template->origin = isset($result['origin']) ? $result['origin'] : '';
$template->template = isset($result['template']) ? $result['template'] : '{}';
$template->save();
}
$params = [
'pre_create_tx' => $pre_create_tx,
......@@ -183,7 +218,7 @@ class IssueCoinController extends BaseController
$size = \Yii::$app->request->get('size', 5);
$query = CoinIssueCoin::find()
->select('id, name, symbol, type, total, create_time, status, chain_id')
->select('id, name, symbol, type, total, create_time, status, chain_id, token_type')
->where(['owner' => $address])
->andWhere(['in', 'status', [CoinIssueCoin::STATUS_PEDDING, CoinIssueCoin::STATUS_CANCEL_SUCCESS, CoinIssueCoin::STATUS_CANCEL_FAILED, CoinIssueCoin::STATUS_CONFIRM, CoinIssueCoin::STATUS_ALLOW, CoinIssueCoin::STATUS_REFUSE, CoinIssueCoin::STATUS_SUCCESS, CoinIssueCoin::STATUS_FAILED]])
->orderBy('create_time desc');
......@@ -193,7 +228,9 @@ class IssueCoinController extends BaseController
$pages = new Pagination(['totalCount' => $countQuery->count(), 'pageSize' => $size]);
foreach ($models as &$val) {
$val->chain_id = $val->chain->platform;
$val->total = (int)$val->total * 1e4;
if (0 == $val->token_type) {
$val->total = (int)$val->total * 1e4;
}
}
$data = [
'list' => $models,
......@@ -231,9 +268,13 @@ class IssueCoinController extends BaseController
$code = -1;
goto doEnd;
}
$data->total = (int)$data->total * 1e4;
if (0 == $data->token_type) {
$data->total = (int)$data->total * 1e4;
}
$data->chain_name = $data->chain->platform;
$data->issue_charge = rtrim(sprintf('%.3f', floatval($data->charge)), '0');
$data->template = isset($data->templated->template) ? $data->templated->template : null;
$data->origin = isset($data->templated->origin) ? $data->templated->origin : null;
$code = 0;
$msg = 'success';
if (CoinIssueCoin::STATUS_FAILED == $data->status) {
......@@ -251,7 +292,6 @@ class IssueCoinController extends BaseController
$msg = '发行失败。失败原因:' . $data->transfer->finish_query_transaction;
}
if (CoinIssueCoin::STATUS_CANCEL_FAILED == $data->status) {
$code = -1;
$msg = '撤消失败。失败原因:' . ErrorMessage::getMessage($data->revoke->revoke_query_transaction);
......@@ -461,4 +501,54 @@ class IssueCoinController extends BaseController
doEnd :
return ['code' => $code, 'msg' => $msg];
}
public function actionSymbolValidate()
{
$code = 0;
$msg = 'success';
$symbol = Yii::$app->request->post('symbol', '');
$token_type = Yii::$app->request->post('token_type', '');
if (false == $symbol || false == $token_type) {
$msg = '缺少必要的参数';
$code = -1;
$data = null;
goto doEnd;
}
$model = CoinIssueCoin::find()
->where(['like', 'symbol', $symbol . '%', false])
->andWhere(['platform_id' => 35])
->andWhere(['token_type' => (int)$token_type])
->orderBy('id desc')
->asArray()
->one();
if (empty($model)) {
$data = $symbol;
goto doEnd;
}
if ($symbol == $model['symbol']) {
$data = $model['symbol'] . '01';
goto doEnd;
}
$prefix = (explode($symbol, $model['symbol']))[1];
$prefix++;
if ($prefix < 10) {
$data = $symbol . '0' . $prefix;
goto doEnd;
}
$data = $symbol . $prefix;
// $suffix = substr($data, -1);
// if (is_numeric($suffix)) {
// $prefix = substr($data, 0, -1);
// $data = $prefix . ($suffix + 1);
// } else {
// $data = $data . 1;
// }
doEnd :
return ['code' => $code, 'msg' => $msg, 'data' => $data];
}
}
\ No newline at end of file
<?php
namespace common\models\psources;
use Yii;
use yii\db\Expression;
class CoinIssueAttachment extends CommonActiveRecord
{
public static function getDb()
{
return Yii::$app->get('p_sources');
}
public static function tableName()
{
return '{{%coin_issue_attachment}}';
}
public function getTransfer()
{
return $this->hasOne(CoinIssueCoin::className(), ['issue_coin_id' => 'id']);
}
}
\ No newline at end of file
......@@ -32,6 +32,9 @@ class CoinIssueCoin extends CommonActiveRecord
const SCENARIOS_CREATE = 'create';
const SCENARIOS_UPDATE = 'update';
const SCENARIOS_CANCEL = 'cancel';
const SCENARIOS_NO_CHARGE = 'no_charge';
const SCENARIOS_CREATE_MALL = 'create_mall';
const SCENARIOS_VERIFY_MALL = 'verify_mall';
public static function getDb()
{
......@@ -46,11 +49,14 @@ class CoinIssueCoin extends CommonActiveRecord
public function rules()
{
return [
[['name', 'symbol', 'total', 'owner', 'introduction', 'category', 'type', 'platform_id', 'chain_id', 'charge_unit', 'charge'], 'required'],
[['name', 'symbol', 'total', 'owner', 'introduction', 'category', 'type', 'platform_id', 'chain_id', 'charge_unit', 'charge', 'template'], 'required'],
[['total', 'category', 'type', 'platform_id', 'chain_id'], 'integer'],
[['introduction', 'charge_unit'], 'string', 'length' => [1, 20]],
['symbol', 'string', 'length' => [1, 6]],
['name', 'string', 'length' => [1, 20]],
[['introduction', 'charge_unit'], 'string', 'length' => [1, 100]],
['symbol', 'string', 'length' => [6, 128]],
['name', 'string', 'length' => [3, 50]],
['nickname', 'string', 'length' => [0, 10]],
[['token_type', 'nickname'], 'safe'],
['template', 'verfiyTemplate'],
#['status', 'in', 'range' => [1, 2, 0]],
['name', 'verfiyName'],
['symbol', 'verfiySymbol'],
......@@ -61,13 +67,32 @@ class CoinIssueCoin extends CommonActiveRecord
public function scenarios()
{
$scenarios = [
self:: SCENARIOS_CREATE => ['name', 'symbol', 'total', 'owner', 'introduction', 'category', 'type', 'platform_id', 'chain_id', 'charge_unit', 'charge'],
self:: SCENARIOS_CREATE => ['name', 'symbol', 'total', 'owner', 'introduction', 'category', 'type', 'platform_id', 'chain_id', 'charge_unit', 'charge', 'token_type', 'nickname'],
self:: SCENARIOS_CREATE_MALL => ['name', 'symbol', 'total', 'owner', 'introduction', 'category', 'type', 'platform_id', 'chain_id', 'token_type', 'nickname'],
self:: SCENARIOS_VERIFY_MALL => ['name', 'symbol', 'total', 'owner', 'introduction', 'category', 'type', 'platform_id', 'chain_id', 'token_type', 'nickname', 'template'],
self:: SCENARIOS_NO_CHARGE => ['name', 'symbol', 'total', 'owner', 'introduction', 'category', 'type', 'platform_id', 'chain_id', 'token_type', 'nickname'],
self:: SCENARIOS_UPDATE => ['status'],
self:: SCENARIOS_CANCEL => ['status'],
];
return array_merge(parent:: scenarios(), $scenarios);
}
public function verfiyTemplate($attribute, $params)
{
if (3 == $this->token_type) {
if (empty($this->template)) {
$this->addError($attribute, '缺少必要的模板参数');
return false;
}
foreach ($this->template as $key => $val) {
if (empty($val)) {
$this->addError($attribute, $key . '不能为空');
return false;
}
}
}
}
public function verfiyName($attribute, $params)
{
//增发
......@@ -98,8 +123,8 @@ class CoinIssueCoin extends CommonActiveRecord
public function verfiySymbol($attribute, $params)
{
if (!preg_match('/^[A-Z]+$/', $this->symbol)) {
$this->addError($attribute, 'Token简称必须大写');
if (!preg_match('/^[a-zA-Z0-9]+$/u', $this->symbol)) {
$this->addError($attribute, 'Token简称必须包含字母和数字');
return false;
}
......@@ -155,12 +180,13 @@ class CoinIssueCoin extends CommonActiveRecord
'msg' => '失败原因',
'status' => '状态',
'charge_unit' => '手续费',
'template' => '模板',
];
}
public function attributes()
{
return array_merge(parent::attributes(), ['issue_charge', 'url', 'chain_name']);
return array_merge(parent::attributes(), ['issue_charge', 'url', 'chain_name', 'template', 'origin']);
}
public function getChain()
......@@ -182,4 +208,9 @@ class CoinIssueCoin extends CommonActiveRecord
{
return $this->hasOne(CoinIssueRevokeRecord::className(), ['issue_coin_id' => 'id']);
}
public function getTemplated()
{
return $this->hasOne(CoinIssueAttachment::className(), ['issue_coin_id' => 'id'])->select('template, origin');
}
}
\ No newline at end of file
......@@ -256,6 +256,7 @@ class IssueChainTransferController extends Controller
if (CoinIssueCoin::TYPE_YES == $val->coin->type) {
$params = [
'name' => $val->coin->name,
'nickname' => $val->nickname,
'symbol' => $val->coin->symbol,
'introduction' => $val->coin->introduction,
'total' => (int)$val->coin->total,
......@@ -277,6 +278,7 @@ class IssueChainTransferController extends Controller
if (CoinIssueCoin::TYPE_NO == $val->coin->type) {
$params = [
'name' => $val->coin->name,
'nickname' => $val->nickname,
'symbol' => $val->coin->symbol,
'introduction' => $val->coin->introduction,
'total' => (int)$val->coin->total,
......@@ -374,7 +376,7 @@ class IssueChainTransferController extends Controller
$model = new Coin();
$model->name = $params['symbol'];
$model->sid = $params['name'];
$model->nickname = ['ja' => '', 'en-US' => '', 'zh-CN' => ''];
$model->nickname = ['ja' => '', 'en-US' => '', 'zh-CN' => $params['nickname']];
$model->platform = $params['platform'];
$model->publish_count = $params['total'] * 1e4;
$model->chain = 'BTY';
......
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