Commit e6bf216b authored by shajiaiming's avatar shajiaiming

优化

parent da76d9eb
......@@ -2,16 +2,22 @@
namespace api\controllers;
use Yii;
use api\base\BaseController;
use common\models\psources\AirDrop;
use common\models\psources\AirDropApplyRecord;
use common\models\psources\CoinAirDropTrade;
use common\service\chain33\Chain33Service;
use Yii;
use common\models\psources\CoinAirDropTrade;
use common\models\psources\AirDropApplyRecord;
class AirDropController extends BaseController
{
/**
* 参于空投申请/空投申请列表
* @param identifier 树莓派编号
* @param miner_address 矿工地址
* @param apply_ids
* @return array
*/
public function actionApply()
{
if (Yii::$app->request->isPost) {
......@@ -36,6 +42,7 @@ class AirDropController extends BaseController
$data = Yii::$app->request->get();
$identifier = $data['identifier'] ?? '';
$miner_address = $data['miner_address'] ?? '';
$apply_ids = isset($data['apply_ids']) ? $data['apply_ids'] : '';
if (false == $identifier || false == $miner_address) {
$this->code = -1;
$this->msg = 'Validation failed.';
......@@ -51,6 +58,11 @@ class AirDropController extends BaseController
$query = AirDropApplyRecord::find()
->select('id, reach, bonus_token, draw_status, create_time')
->where(['apply_id' => $model['id']]);
if (!empty($apply_ids)) {
$apply_ids = rtrim($apply_ids, ',');
$apply_id_arr = explode(',', $apply_ids);
$query->andWhere(['in', 'id', $apply_id_arr]);
}
$items = $query->offset(($page - 1) * $size)->orderBy('id desc')->limit($size)->all();
$countQuery = clone $query;
......@@ -58,7 +70,8 @@ class AirDropController extends BaseController
$this->data = [
'items' => $items,
'total' => $total
'total' => $total,
'income' => 134
];
}
......
......@@ -55,4 +55,9 @@ class AirDropApplyRecord extends CommonActiveRecord
'finish_time' => '更新时间'
];
}
public function getApply()
{
return $this->hasOne(AirDrop::className(), ['id' => 'apply_id'])->one();
}
}
\ No newline at end of file
<?php
namespace common\models\psources;
use Yii;
use yii\db\Expression;
class AirDropApplyTransferRecord extends CommonActiveRecord
{
//定义场景
const SCENARIOS_CREATE = 'create';
public static function getDb()
{
return Yii::$app->get('p_sources');
}
public static function tableName()
{
return '{{%wallet_airdrop_apply_transfer_record}}';
}
public function rules()
{
return [
[['apply_record_id', 'create_result', 'sign_result', 'send_result'], 'safe'],
[['apply_record_id'], 'integer'],
];
}
public function scenarios()
{
$scenarios = [
self:: SCENARIOS_CREATE => ['apply_record_id', 'create_result', 'sign_result', 'send_result'],
];
return array_merge(parent:: scenarios(), $scenarios);
}
public function attributeLabels()
{
return [
'apply_record_id' => '申请记录id',
'create_result' => '构造失败原因',
'sign_result' => '签名失败原因',
'send_result' => '发送失败原因',
'create_time' => '创建时间',
];
}
public function getRecord()
{
return $this->hasOne(AirDropApplyRecord::className(), ['id' => 'apply_record_id'])->all();
}
}
\ No newline at end of file
......@@ -203,7 +203,7 @@ class Chain33Service
return $this->send($params, 'Chain33.CreateRawTransaction');
}
public function createRawTransaction($to, $amount, $fee, $note, $execer)
public function createRawTransaction($to, $amount, $fee, $note, $execer, $tokenSymbol = '')
{
$params = [
"to" => $to,
......@@ -211,7 +211,6 @@ class Chain33Service
"fee" => $fee,
"note" => $note,
"execer" => $execer,
];
return $this->send($params, 'Chain33.CreateRawTransaction');
}
......@@ -259,7 +258,7 @@ class Chain33Service
public function getBalance($address, $execer = "")
{
$params = [
'addresses' => $address,
'addresses' => [$address],
'execer' => $execer,
];
return $this->send($params, 'Chain33.GetBalance');
......
......@@ -2,14 +2,20 @@
namespace console\controllers;
use common\models\psources\AirDrop;
use Yii;
use yii\console\Controller;
use common\models\psources\AirDrop;
use common\service\chain33\Chain33Service;
use common\models\psources\AirDropApplyRecord;
use common\models\psources\CoinAirDropTransfer;
use common\models\psources\AirDropApplyTransferRecord;
class AirDropController extends Controller
{
/**
* 获取矿工地址的对应的冷钱包地址
* @return
*/
public function actionMinerSourceList()
{
$model = AirDrop::find()->where(['wallet_address' => ''])->asArray()->all();
......@@ -18,24 +24,121 @@ class AirDropController extends Controller
}
$service = new Chain33Service();
foreach ($model as $key => $val) {
$playload = [
'data' => $val['miner_address']
];
$result = $service->chainQuery('ticket', 'MinerSourceList', $playload);
if (0 != $result['code']) {
continue;
}
$wallet_address = isset($result['result']['datas'][0]) ? $result['result']['datas'][0] : '';
if (empty($wallet_address)) {
continue;
}
$current_model = AirDrop::findOne($val['id']);
$current_model->wallet_address = $wallet_address;
$current_model->save();
go(function () use ($val, $service) {
\Co::sleep(0.5);
$playload = [
'data' => $val['miner_address']
];
$result = $service->chainQuery('ticket', 'MinerSourceList', $playload);
if (0 == $result['code']) {
$wallet_address = isset($result['result']['datas'][0]) ? $result['result']['datas'][0] : '';
if (!empty($wallet_address)) {
$current_model = AirDrop::findOne($val['id']);
$current_model->wallet_address = $wallet_address;
$current_model->save();
}
}
});
}
}
/**
* 查询地址余额
* 通过矿主地址查询是否有冻结资金,有BTY代表达标
* @return
*/
public function actionReach()
{
$begin = date("Y-m-d", time()) . " 00:00:00";
$end = date("Y-m-d", time()) . " 23:59:59";
$record = AirDropApplyRecord::find()
->where(['reach' => AirDropApplyRecord::REACH_NO])
->andWhere(['between', 'create_time', $begin, $end])
->all();
if (empty($record)) return 0;
$service = new Chain33Service();
$execer = 'coins';
foreach ($record as $val) {
go(function () use ($val, $service, $execer) {
\Co::sleep(0.5);
$result = $service->getBalance($val->apply->wallet_address, $execer);
if (0 == $result['code']) {
$frozen = $result['result']['frozen'] ?? 0;
if ($frozen > 0) {
$val->reach = AirDropApplyRecord::REACH_YES;
$val->save();
}
}
});
}
}
/**
* 领取符合条件的记录
* @return
*/
public function actionDraw()
{
$record = AirDropApplyRecord::find()->where(['reach' => AirDropApplyRecord::REACH_YES, 'draw_status' => AirDropApplyRecord::STATUS_DRAWING])->all();
if (empty($record)) {
return 0;
}
foreach ($record as $val) {
go(function () use ($val) {
\Co::sleep(0.5);
$amount = 1;
$fee = 1;
$note = '';
$execer = 'token';
$isToken = true;
$tokenSymbol = 'CCNY';
$val->draw_status = AirDropApplyRecord::STATUS_DRAW_SUEEESS;
$service = new Chain33Service();
$createTokenRawTransaction = $service->createTokenRawTransaction($val->apply->wallet_address, $amount, $isToken, $tokenSymbol, $fee, $note, $execer);
if (0 != $createTokenRawTransaction['code']) {
$val->draw_status = AirDropApplyRecord::STATUS_DRAW_FAIL;
$transfer_record['create_result'] = $createTokenRawTransaction['msg'];
goto doEnd;
}
$privkey = '72c3879f1f9b523f266a9545b69bd41c0251483a93e21e348e85118afe17a5e2';
$expire = '1m';
$signRawTx = $service->signRawTx($privkey, $createTokenRawTransaction['result'], $expire);
if (0 != $signRawTx['code']) {
$val->draw_status = AirDropApplyRecord::STATUS_DRAW_FAIL;
$transfer_record['sign_result'] = $signRawTx['msg'];
goto doEnd;
}
$sendTransaction = $service->sendTransaction($signRawTx['result']);
if (0 != $sendTransaction['code']) {
$val->draw_status = AirDropApplyRecord::STATUS_DRAW_FAIL;
$transfer_record['send_result'] = $sendTransaction['msg'];
goto doEnd;
}
doEnd :
if ($val->draw_status == AirDropApplyRecord::STATUS_DRAW_FAIL) {
$transfer_record_model = new AirDropApplyTransferRecord();
$transfer_record['apply_record_id'] = $val->id;
$transfer_record_model->load($transfer_record, '');
$transfer_record_model->save();
}
$val->save();
});
}
return 0;
}
/**
* 获取游戏状态
*
* @return array
......
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