Commit d6b36b90 authored by shajiaiming's avatar shajiaiming

Merge branch 'feature/airdrop' into develop

parents cce9b4e3 ef58e2e3
...@@ -2,14 +2,13 @@ ...@@ -2,14 +2,13 @@
namespace api\controllers; namespace api\controllers;
use common\models\psources\AirDropRulePool;
use Yii; use Yii;
use api\base\BaseController; use api\base\BaseController;
use common\models\psources\AirDrop; use common\models\psources\AirDrop;
use common\service\chain33\Chain33Service; use common\service\chain33\Chain33Service;
use common\models\psources\AirDropRulePool;
use common\models\psources\CoinAirDropTrade; use common\models\psources\CoinAirDropTrade;
use common\models\psources\AirDropApplyRecord; use common\models\psources\AirDropApplyRecord;
use yii\db\Exception;
class AirDropController extends BaseController class AirDropController extends BaseController
{ {
...@@ -25,7 +24,6 @@ class AirDropController extends BaseController ...@@ -25,7 +24,6 @@ class AirDropController extends BaseController
if (Yii::$app->request->isPost) { if (Yii::$app->request->isPost) {
$data = Yii::$app->request->post(); $data = Yii::$app->request->post();
$identifier = $data['identifier'] ?? null; $identifier = $data['identifier'] ?? null;
if (false == $identifier) { if (false == $identifier) {
$this->code = -1; $this->code = -1;
...@@ -38,45 +36,16 @@ class AirDropController extends BaseController ...@@ -38,45 +36,16 @@ class AirDropController extends BaseController
$this->msg = 'Validation failed.'; $this->msg = 'Validation failed.';
goto doEnd; goto doEnd;
} }
$time = $exist->rule->duration - 1;
$create_time = date('Y-m-d'); $model = new AirDrop();
$finish_time = date('Y-m-d', strtotime("+$time day")); $model->setScenario(AirDrop::SCENARIOS_CREATE);
$model->load($data, '');
$transaction = Yii::$app->db->beginTransaction(); if (!$model->validate()) {
try {
$model = new AirDrop();
$model->setScenario(AirDrop::SCENARIOS_CREATE);
$model->load($data, '');
if (!$model->validate()) {
$this->code = -1;
$this->msg = $model->errors;
goto doEnd;
}
$model->save();
$apply_id = $model->id;
$expiry_date = $this->getDatesFromRange($create_time, $finish_time);
$apply_record_model = new AirDropApplyRecord();
foreach ($expiry_date as $val) {
$apply_record_model->setIsNewRecord(true);
$apply_record_model->apply_id = $apply_id;
$apply_record_model->reach = AirDropApplyRecord::REACH_NO;
$apply_record_model->amount = $exist->rule->amount;
$apply_record_model->token = $exist->rule->token;
$apply_record_model->draw_status = AirDropApplyRecord::STATUS_UNDRAW;
$apply_record_model->create_time = $val;
$apply_record_model->save() && $apply_record_model->id = 0;;
}
$transaction->commit();
} catch (Exception $e) {
$transaction->rollback();
$this->code = -1; $this->code = -1;
$this->msg = $e->getMessage(); $this->msg = $model->errors;
goto doEnd; goto doEnd;
} }
$model->save();
if (empty($this->msg)) {
$this->msg = 'Validation failed.';
}
$redis = Yii::$app->redis_app; $redis = Yii::$app->redis_app;
$redis->hmset('airdrop:' . $data['identifier'], 'reach', 0, 'draw_success', 0, 'income', 0, 'un_draw', 0); $redis->hmset('airdrop:' . $data['identifier'], 'reach', 0, 'draw_success', 0, 'income', 0, 'un_draw', 0);
...@@ -104,7 +73,7 @@ class AirDropController extends BaseController ...@@ -104,7 +73,7 @@ class AirDropController extends BaseController
} }
$query = AirDropApplyRecord::find() $query = AirDropApplyRecord::find()
->select('id, reach, amount, token, create_time') ->select('id, reach, amount, token,draw_status, create_time')
->where(['apply_id' => $model['id']]); ->where(['apply_id' => $model['id']]);
if (!empty($apply_ids)) { if (!empty($apply_ids)) {
$apply_ids = rtrim($apply_ids, ','); $apply_ids = rtrim($apply_ids, ',');
...@@ -146,8 +115,8 @@ class AirDropController extends BaseController ...@@ -146,8 +115,8 @@ class AirDropController extends BaseController
} }
$this->data = AirDropApplyRecord::find() $this->data = AirDropApplyRecord::find()
->where(['apply_id' => $model['id'], 'reach' => AirDropApplyRecord::REACH_YES]) ->where(['apply_id' => $model['id'], 'reach' => AirDropApplyRecord::REACH_YES, 'draw_status' => AirDropApplyRecord::STATUS_DRAW_SUEEESS])
->sum('bonus_token'); ->sum('amount');
if (empty($this->data)) { if (empty($this->data)) {
$this->data = 0; $this->data = 0;
goto doEnd; goto doEnd;
...@@ -238,7 +207,6 @@ class AirDropController extends BaseController ...@@ -238,7 +207,6 @@ class AirDropController extends BaseController
return ['code' => $this->code, 'msg' => $this->msg, 'data' => $this->data]; return ['code' => $this->code, 'msg' => $this->msg, 'data' => $this->data];
} }
public function actionGameTrade() public function actionGameTrade()
{ {
$coins_address = Yii::$app->request->post('coins_address', ''); $coins_address = Yii::$app->request->post('coins_address', '');
...@@ -358,27 +326,4 @@ class AirDropController extends BaseController ...@@ -358,27 +326,4 @@ class AirDropController extends BaseController
} }
return ['code' => 1, 'msg' => 'ok']; return ['code' => 1, 'msg' => 'ok'];
} }
function getDatesFromRange($start, $end, $format = 'Y-m-d')
{
// Declare an empty array
$array = array();
// Variable that store the date interval
// of period 1 day
$interval = new \DateInterval('P1D');
$realEnd = new \DateTime($end);
$realEnd->add($interval);
$period = new \DatePeriod(new \DateTime($start), $interval, $realEnd);
// Use loop to store date into array
foreach ($period as $date) {
$array[] = $date->format($format);
}
// Return the array elements
return $array;
}
} }
\ No newline at end of file
...@@ -144,8 +144,6 @@ class WalletController extends BaseController ...@@ -144,8 +144,6 @@ class WalletController extends BaseController
$coin_model = Coin::find()->select('name, treaty')->where(['name' => strtoupper($symbol), 'platform' => $platform->platform])->one(); $coin_model = Coin::find()->select('name, treaty')->where(['name' => strtoupper($symbol), 'platform' => $platform->platform])->one();
$switch = false; $switch = false;
if (empty($coin_model)) { if (empty($coin_model)) {
$execer = $platform->exer; $execer = $platform->exer;
...@@ -164,12 +162,11 @@ class WalletController extends BaseController ...@@ -164,12 +162,11 @@ class WalletController extends BaseController
} }
$service = new Chain33Service($node); $service = new Chain33Service($node);
$address[] = $token;
if (false == $switch) { if (false == $switch) {
$result = $service->getTokenBalance($address, $execer, $symbol); $result = $service->getTokenBalance($token, $execer, $symbol);
} else { } else {
$result = $service->getBalance($address, $execer = ''); $result = $service->getBalance($token, $execer = '');
} }
if (0 !== $result['code']) { if (0 !== $result['code']) {
...@@ -200,7 +197,6 @@ class WalletController extends BaseController ...@@ -200,7 +197,6 @@ class WalletController extends BaseController
goto doEnd; goto doEnd;
} }
$model = new CoinIssueTransfer(); $model = new CoinIssueTransfer();
$data['txhex'] = $txhex; $data['txhex'] = $txhex;
$data['issue_coin_id'] = (int)$issue_coin_id; $data['issue_coin_id'] = (int)$issue_coin_id;
......
...@@ -16,7 +16,7 @@ class Tools ...@@ -16,7 +16,7 @@ class Tools
{ {
$chars = '1234567890abcdefghijklmnopqrstuvwxyz'; $chars = '1234567890abcdefghijklmnopqrstuvwxyz';
switch($type) { switch ($type) {
case 'number': case 'number':
$startIndex = 0; $startIndex = 0;
$endIndex = 9; $endIndex = 9;
...@@ -32,7 +32,7 @@ class Tools ...@@ -32,7 +32,7 @@ class Tools
} }
$randomNumber = ''; $randomNumber = '';
for($i = 0; $i<$count; $i++) { for ($i = 0; $i < $count; $i++) {
$randomNumber .= substr($chars, rand($startIndex, $endIndex), 1); $randomNumber .= substr($chars, rand($startIndex, $endIndex), 1);
} }
return $randomNumber; return $randomNumber;
...@@ -50,15 +50,15 @@ class Tools ...@@ -50,15 +50,15 @@ class Tools
return $filename; return $filename;
switch ($type) { switch ($type) {
case 'case': case 'case':
return $filename?Url::to('@resDomain/' . $type . '/' . $filename):''; return $filename ? Url::to('@resDomain/' . $type . '/' . $filename) : '';
case 'video': case 'video':
return $filename?Url::to('@resDomain/' . $type . '/' . $filename):''; return $filename ? Url::to('@resDomain/' . $type . '/' . $filename) : '';
case 'banner': case 'banner':
return $filename?Url::to('@resDomain/' . $type . '/' . $filename):''; return $filename ? Url::to('@resDomain/' . $type . '/' . $filename) : '';
case 'weixin': case 'weixin':
return $filename?Url::to('@resDomain/' . $type . '/' . $filename):''; return $filename ? Url::to('@resDomain/' . $type . '/' . $filename) : '';
case 'public': case 'public':
return $filename?Url::to('@resDomain/' . $type . '/' . $filename):''; return $filename ? Url::to('@resDomain/' . $type . '/' . $filename) : '';
default: default:
return ''; return '';
} }
...@@ -78,10 +78,10 @@ class Tools ...@@ -78,10 +78,10 @@ class Tools
public static function isAvailableDate($start, $end) public static function isAvailableDate($start, $end)
{ {
$current = date('Y-m-d'); $current = date('Y-m-d');
$start = $start?date('Y-m-d', strtotime($start)):$current; $start = $start ? date('Y-m-d', strtotime($start)) : $current;
$end = $end?date('Y-m-d', strtotime($end)):$current; $end = $end ? date('Y-m-d', strtotime($end)) : $current;
if($start <= $current && $end >= $current) { if ($start <= $current && $end >= $current) {
return true; return true;
} else { } else {
return false; return false;
...@@ -106,9 +106,9 @@ class Tools ...@@ -106,9 +106,9 @@ class Tools
*/ */
public static function getPost(array $values, $key = null) public static function getPost(array $values, $key = null)
{ {
if(Yii::$app->request->isPost) { if (Yii::$app->request->isPost) {
$data = Yii::$app->request->post(); $data = Yii::$app->request->post();
if($key !== null) { if ($key !== null) {
$data[$key] = ArrayHelper::merge($data[$key], $values); $data[$key] = ArrayHelper::merge($data[$key], $values);
} else { } else {
$data = ArrayHelper::merge($data, $values); $data = ArrayHelper::merge($data, $values);
...@@ -127,27 +127,21 @@ class Tools ...@@ -127,27 +127,21 @@ class Tools
$result = ''; $result = '';
$string = html_entity_decode(trim(strip_tags($string)), ENT_QUOTES, 'UTF-8'); $string = html_entity_decode(trim(strip_tags($string)), ENT_QUOTES, 'UTF-8');
$strlen = strlen($string); $strlen = strlen($string);
for ($i = 0; (($i < $strlen) && ($length > 0)); $i++) for ($i = 0; (($i < $strlen) && ($length > 0)); $i++) {
{ if ($number = strpos(str_pad(decbin(ord(substr($string, $i, 1))), 8, '0', STR_PAD_LEFT), '0')) {
if ($number = strpos(str_pad(decbin(ord(substr($string, $i, 1))), 8, '0', STR_PAD_LEFT), '0')) if ($length < 1.0) {
{
if ($length < 1.0)
{
break; break;
} }
$result .= substr($string, $i, $number); $result .= substr($string, $i, $number);
$length -= 1.0; $length -= 1.0;
$i += $number - 1; $i += $number - 1;
} } else {
else
{
$result .= substr($string, $i, 1); $result .= substr($string, $i, 1);
$length -= 0.5; $length -= 0.5;
} }
} }
$result = htmlspecialchars($result, ENT_QUOTES, 'UTF-8'); $result = htmlspecialchars($result, ENT_QUOTES, 'UTF-8');
if ($i < $strlen) if ($i < $strlen) {
{
$result .= $etc; $result .= $etc;
} }
return $result; return $result;
...@@ -162,4 +156,27 @@ class Tools ...@@ -162,4 +156,27 @@ class Tools
array_multisort($keysValue, $sort, $array); array_multisort($keysValue, $sort, $array);
return $array; return $array;
} }
public static function getDatesFromRange($start, $end, $format = 'Y-m-d')
{
// Declare an empty array
$array = array();
// Variable that store the date interval
// of period 1 day
$interval = new \DateInterval('P1D');
$realEnd = new \DateTime($end);
$realEnd->add($interval);
$period = new \DatePeriod(new \DateTime($start), $interval, $realEnd);
// Use loop to store date into array
foreach ($period as $date) {
$array[] = $date->format($format);
}
// Return the array elements
return $array;
}
} }
\ No newline at end of file
...@@ -7,6 +7,9 @@ use yii\db\Expression; ...@@ -7,6 +7,9 @@ use yii\db\Expression;
class AirDrop extends CommonActiveRecord class AirDrop extends CommonActiveRecord
{ {
const REACH_INIT_NO = 0; //未达标
const REACH_INIT_YES = 1; //已达标
//定义场景 //定义场景
const SCENARIOS_CREATE = 'create'; const SCENARIOS_CREATE = 'create';
const SCENARIOS_UPDATE = 'update'; const SCENARIOS_UPDATE = 'update';
...@@ -25,8 +28,8 @@ class AirDrop extends CommonActiveRecord ...@@ -25,8 +28,8 @@ class AirDrop extends CommonActiveRecord
{ {
return [ return [
[['identifier', 'wallet_address', 'miner_address'], 'required'], [['identifier', 'wallet_address', 'miner_address'], 'required'],
[['reach_times'], 'integer'], [['reach_times', 'reach_init'], 'integer'],
[['create_time', 'finish_time'], 'safe'], [['reach_times', 'reach_init', 'create_time', 'finish_time'], 'safe'],
[['identifier'], 'string', 'length' => [5, 50]], [['identifier'], 'string', 'length' => [5, 50]],
[['wallet_address', 'miner_address'], 'string', 'length' => [10, 50]], [['wallet_address', 'miner_address'], 'string', 'length' => [10, 50]],
['identifier', 'unique', 'message' => '树莓派编号已存在'], ['identifier', 'unique', 'message' => '树莓派编号已存在'],
...@@ -37,7 +40,7 @@ class AirDrop extends CommonActiveRecord ...@@ -37,7 +40,7 @@ class AirDrop extends CommonActiveRecord
public function scenarios() public function scenarios()
{ {
$scenarios = [ $scenarios = [
self:: SCENARIOS_CREATE => ['identifier', 'miner_address', 'reach_times', 'create_time'], self:: SCENARIOS_CREATE => ['identifier', 'miner_address', 'reach_init', 'reach_times', 'create_time'],
self:: SCENARIOS_UPDATE => ['wallet_address'], self:: SCENARIOS_UPDATE => ['wallet_address'],
]; ];
return array_merge(parent:: scenarios(), $scenarios); return array_merge(parent:: scenarios(), $scenarios);
...@@ -49,6 +52,7 @@ class AirDrop extends CommonActiveRecord ...@@ -49,6 +52,7 @@ class AirDrop extends CommonActiveRecord
'identifier' => '树莓派编号', 'identifier' => '树莓派编号',
'wallet_address' => '矿主地址', 'wallet_address' => '矿主地址',
'miner_address' => '矿工地址', 'miner_address' => '矿工地址',
'reach_init' => '达标初始化',
'reach_times' => '达标次数', 'reach_times' => '达标次数',
'create_time' => '开始时间', 'create_time' => '开始时间',
'finish_time' => '结束时间' 'finish_time' => '结束时间'
......
...@@ -2,6 +2,8 @@ ...@@ -2,6 +2,8 @@
namespace console\controllers; namespace console\controllers;
use common\components\Tools;
use common\models\psources\AirDropRulePool;
use Yii; use Yii;
use yii\console\Controller; use yii\console\Controller;
use common\models\psources\AirDrop; use common\models\psources\AirDrop;
...@@ -9,6 +11,7 @@ use common\service\chain33\Chain33Service; ...@@ -9,6 +11,7 @@ use common\service\chain33\Chain33Service;
use common\models\psources\AirDropApplyRecord; use common\models\psources\AirDropApplyRecord;
use common\models\psources\CoinAirDropTransfer; use common\models\psources\CoinAirDropTransfer;
use common\models\psources\AirDropApplyTransferRecord; use common\models\psources\AirDropApplyTransferRecord;
use yii\db\Exception;
class AirDropController extends Controller class AirDropController extends Controller
{ {
...@@ -40,14 +43,14 @@ class AirDropController extends Controller ...@@ -40,14 +43,14 @@ class AirDropController extends Controller
$draw_success = AirDropApplyRecord::find() $draw_success = AirDropApplyRecord::find()
->where(['apply_id' => $val['id'], 'draw_status' => AirDropApplyRecord::STATUS_DRAW_SUEEESS]) ->where(['apply_id' => $val['id'], 'draw_status' => AirDropApplyRecord::STATUS_DRAW_SUEEESS])
->andWhere(['<', 'create_time', $expiry_date]) ->andWhere(['<', 'create_time', $expiry_date])
->sum('bonus_token'); ->sum('amount');
$draw_success = empty($draw_success) ? 0 : (int)$draw_success; $draw_success = empty($draw_success) ? 0 : (int)$draw_success;
//总收益 //总收益
$income = AirDropApplyRecord::find() $income = AirDropApplyRecord::find()
->where(['apply_id' => $val['id'], 'reach' => AirDropApplyRecord::REACH_YES]) ->where(['apply_id' => $val['id'], 'reach' => AirDropApplyRecord::REACH_YES])
->andWhere(['<', 'create_time', $expiry_date]) ->andWhere(['<', 'create_time', $expiry_date])
->sum('bonus_token'); ->sum('amount');
$income = empty($income) ? 0 : (int)$income; $income = empty($income) ? 0 : (int)$income;
//未领取 //未领取
...@@ -91,6 +94,57 @@ class AirDropController extends Controller ...@@ -91,6 +94,57 @@ class AirDropController extends Controller
} }
/** /**
* 达标初始化
* 通过矿主地址查询是否有余额,有则生成记录
* @return
*/
public function actionReachInit()
{
$applies = AirDrop::find()->where(['reach_init' => AirDrop::REACH_INIT_NO])->all();
if (empty($apply)) return 0;
$service = new Chain33Service();
$execer = 'ticket';
foreach ($applies as $apply) {
$exist = AirDropRulePool::find()->where(['identifier' => $apply->identifier])->one();
if (false == $exist) continue;
$result = $service->getBalance($apply->wallet_address, $execer);
if (0 != $result['code']) continue;
$balance = $result['result'][0]['balance'] ?? 0;
if ($balance == 0) continue;
$transaction = Yii::$app->db->beginTransaction();
try {
$apply->reach_init = AirDrop::REACH_INIT_YES;
$apply->save();
$time = $exist->rule->duration - 1;
$create_time = date('Y-m-d');
$finish_time = date('Y-m-d', strtotime("+$time day"));
$expiry_date = Tools::getDatesFromRange($create_time, $finish_time);
$apply_record_model = new AirDropApplyRecord();
foreach ($expiry_date as $key => $val) {
$apply_record_model->setIsNewRecord(true);
$apply_record_model->apply_id = $apply->id;
if (0 == $key) {
$apply_record_model->reach = AirDropApplyRecord::REACH_YES;
} else {
$apply_record_model->reach = AirDropApplyRecord::REACH_NO;
}
$apply_record_model->amount = $exist->rule->amount;
$apply_record_model->token = $exist->rule->token;
$apply_record_model->draw_status = AirDropApplyRecord::STATUS_UNDRAW;
$apply_record_model->create_time = $val;
$apply_record_model->save() && $apply_record_model->id = 0;;
}
$transaction->commit();
} catch (Exception $e) {
$transaction->rollback();
$msg = $e->getMessage();
echo date('Y-m-d H:i:s') . $msg . PHP_EOL;
}
}
}
/**
* 查询地址余额 * 查询地址余额
* 通过矿主地址查询是否有冻结资金,有BTY代表达标 * 通过矿主地址查询是否有冻结资金,有BTY代表达标
* @return * @return
...@@ -113,8 +167,8 @@ class AirDropController extends Controller ...@@ -113,8 +167,8 @@ class AirDropController extends Controller
\Co::sleep(0.5); \Co::sleep(0.5);
$result = $service->getBalance($val->apply->wallet_address, $execer); $result = $service->getBalance($val->apply->wallet_address, $execer);
if (0 == $result['code']) { if (0 == $result['code']) {
$frozen = $result['result'][0]['frozen'] ?? 0; $balance = $result['result'][0]['balance'] ?? 0;
if ($frozen > 0) { if ($balance > 0) {
$val->reach = AirDropApplyRecord::REACH_YES; $val->reach = AirDropApplyRecord::REACH_YES;
$val->save(); $val->save();
} }
...@@ -136,7 +190,7 @@ class AirDropController extends Controller ...@@ -136,7 +190,7 @@ class AirDropController extends Controller
foreach ($record as $val) { foreach ($record as $val) {
go(function () use ($val) { go(function () use ($val) {
\Co::sleep(0.5); \Co::sleep(0.5);
$amount = $val->amount; $amount = $val->amount * 1e8;
$fee = 1; $fee = 1;
$note = ''; $note = '';
$execer = 'token'; $execer = 'token';
...@@ -154,7 +208,7 @@ class AirDropController extends Controller ...@@ -154,7 +208,7 @@ class AirDropController extends Controller
goto doEnd; goto doEnd;
} }
$privkey = '72c3879f1f9b523f266a9545b69bd41c0251483a93e21e348e85118afe17a5e2'; $privkey = '4ab1b1253e8984acf95797eed52ad9d8247ee96bd9bb49bb08a742d2c4477c0b';
$expire = '1m'; $expire = '1m';
$signRawTx = $service->signRawTx($privkey, $createTokenRawTransaction['result'], $expire); $signRawTx = $service->signRawTx($privkey, $createTokenRawTransaction['result'], $expire);
......
...@@ -91,11 +91,17 @@ class GameBetController extends Controller ...@@ -91,11 +91,17 @@ class GameBetController extends Controller
echo date('Y-m-d H:i:s') . '无节点' . PHP_EOL; echo date('Y-m-d H:i:s') . '无节点' . PHP_EOL;
return 0; return 0;
} }
foreach ($nodes as $key => $node) { foreach ($nodes as $key => $node) {
$service = new Chain33Service($node); $service = new Chain33Service($node);
$result = $service->getLastHeader(); $result = $service->getLastHeader();
$height = $result['result']['height']; $height = $result['result']['height'];
$main_info = $service->getBlock2MainInfo($height - 36, $height); $param = [
'start' => $height - 36,
'end' => $height,
'isDetail' => false
];
$main_info = $service->chainQuery('paracross', 'GetBlock2MainInfo', $param);
if (!isset($main_info['result']['items'])) continue; if (!isset($main_info['result']['items'])) continue;
$items = $main_info['result']['items']; $items = $main_info['result']['items'];
$items = $this->arraySort($items, 'mainHeight'); $items = $this->arraySort($items, 'mainHeight');
......
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