Commit 1a2f737a authored by shajiaiming's avatar shajiaiming

Merge branch 'feature/airdrop' into 'master'

Feature/airdrop See merge request !85
parents bb2e349a a405b83c
<?php <?php
namespace backend\assets\coinTransfer; namespace backend\assets\airDrop;
use yii\web\AssetBundle; use yii\web\AssetBundle;
use yii\web\View; use yii\web\View;
class IndexAsset extends AssetBundle class IndexAsset extends AssetBundle
{ {
public $sourcePath = '@backend/web/js/coin-transfer'; public $sourcePath = '@backend/web/js/air-drop';
public $js = [ public $js = [
'list.js', 'list.js',
]; ];
......
<?php <?php
/**
* Created by PhpStorm.
* User: shaji
* Date: 2019/4/23
* Time: 10:04
*/
namespace backend\controllers; namespace backend\controllers;
use backend\models\Coin;
use common\service\chain33\Chain33Service; use common\service\chain33\Chain33Service;
use Yii; use Yii;
use common\models\psources\CoinTransfer;
use yii\web\UploadedFile; use yii\web\UploadedFile;
use common\models\psources\CoinAirDropTransfer;
class CoinTransferController extends BaseController class AirDropController extends BaseController
{ {
public $enableCsrfValidation = false; public $enableCsrfValidation = false;
public function actionList() public function actionList()
{ {
$get = Yii::$app->request->get(); $get = Yii::$app->request->get();
$page = $get['page'] ?? 1; $page = $get['page'] ?? 1;
$limit = $get['limit'] ?? 10; $limit = $get['limit'] ?? 10;
if (Yii::$app->request->isAjax) { if (Yii::$app->request->isAjax) {
Yii::$app->response->format = 'json'; Yii::$app->response->format = 'json';
$condition = []; $condition = [];
$data = CoinTransfer::getList($page, $limit, $condition); $data = CoinAirDropTransfer::getList($page, $limit, $condition);
return $data; return $data;
} }
return $this->render('list'); return $this->render('list');
...@@ -42,7 +35,7 @@ class CoinTransferController extends BaseController ...@@ -42,7 +35,7 @@ class CoinTransferController extends BaseController
$file = UploadedFile::getInstanceByName('file'); $file = UploadedFile::getInstanceByName('file');
try { try {
$fd = fopen($file->tempName, 'r'); $fd = fopen($file->tempName, 'r');
$data = []; $data = [];
while (($row = fgetcsv($fd)) !== false) { while (($row = fgetcsv($fd)) !== false) {
$data[] = $row; $data[] = $row;
...@@ -50,12 +43,17 @@ class CoinTransferController extends BaseController ...@@ -50,12 +43,17 @@ class CoinTransferController extends BaseController
if (ctype_digit($data[0][0])) { if (ctype_digit($data[0][0])) {
array_shift($data); array_shift($data);
} }
foreach ($data as $key => &$value){ foreach ($data as $key => &$value) {
$value[0] = trim($value[0]); $value[0] = trim($value[0]);
$value[1] = trim($value[1]); $value[1] = trim($value[1]);
} }
if ($data) { if ($data) {
if (CoinTransfer::loadArray($data)) { $additional = [1, '0', ' ', 0];
foreach ($data as $key => &$val) {
$val = array_merge($val, $additional);
}
if (CoinAirDropTransfer::loadArray($data)) {
return ['code' => 0, 'msg' => 'succeed']; return ['code' => 0, 'msg' => 'succeed'];
} }
} }
...@@ -65,48 +63,27 @@ class CoinTransferController extends BaseController ...@@ -65,48 +63,27 @@ class CoinTransferController extends BaseController
} }
} }
public function actionBatchTransfer() public function actionAdd()
{ {
$model = CoinTransfer::find()->where(['status' => 0])->limit(30)->all(); $this->layout = false;
foreach ($model as $val){ if (Yii::$app->request->isPost) {
$id = $val->id; Yii::$app->response->format = 'json';
$to = $val->address; $post = Yii::$app->request->post();
$amount = $val->amount * 1e8; $model = new CoinAirDropTransfer();
$fee = 100000; $model->setScenario(CoinAirDropTransfer::SCENARIOS_CREATE);
$note = ''; $post['txhash'] = '0';
$execer = 'user.p.tschain.coins'; $post['msg'] = '';
$post['balance'] = 0;
$isExist = CoinTransfer::find()->where(['address' => $val->address, 'status' => 1])->one(); $post['origin'] = CoinAirDropTransfer::ORIGIN_ADD;
if($isExist) continue; if ($model->load($post, '') && $model->save()) {
return ['code' => 0, 'msg' => 'succeed'];
$service = new Chain33Service();
$createRawTransaction = $service->createRawTransaction($to, $amount, $fee, $note, $execer);
if(0 != $createRawTransaction['code']){
continue;
}
$txHex = $createRawTransaction['result'];
$privkey = '72c3879f1f9b523f266a9545b69bd41c0251483a93e21e348e85118afe17a5e2';
$expire = '1m';
$signRawTx = $service->signRawTx($privkey, $txHex, $expire);
if(0 != $signRawTx['code']){
continue;
} }
$sign_str = $signRawTx['result']; $error = $model->errors;
$result = $service->sendTransaction($sign_str); if ($error) {
if(0 != $result['code']){ return ['code' => -1, 'msg' => current($model->firstErrors)];
continue;
} }
} else {
$hash = $result['result']; return $this->render('add');
$current_model = CoinTransfer::findOne($id);
$current_model->hash = $hash;
$current_model->status = 1;
$current_model->save();
echo $val->address."-----".$hash."<br/>";
} }
} }
} }
\ No newline at end of file
<div style="padding: 5px 20px;">
<br>
<form id="addData">
<input name="_csrf" type="hidden" value="<?= Yii::$app->request->getCsrfToken() ?>">
<div class="input-group my-group">
<span class="input-group-addon">打币地址</span>
<input name="to_address" type="text" class="form-control" lay-verify="required">
</div>
<div class="input-group my-group">
<span class="input-group-addon">打币数量</span>
<input name="amount" type="text" class="form-control" lay-verify="required">
</div>
<div class="input-group my-group">
<span class="input-group-addon">币种名称</span>
<input name="coin_name" type="text" class="form-control" lay-verify="required">
</div>
</form>
</div>
\ No newline at end of file
<?php
use backend\assets\airDrop\IndexAsset;
IndexAsset::register($this);
?>
<div class="layui-row">
<button class="layui-btn layui-btn-sm" lay-event="import" id="import">名单导入</button>
<button class="layui-btn layui-btn-sm" lay-event="add" id="add">单笔添加</button>
<!-- <button class="layui-btn layui-btn-sm" lay-event="transfer" id="transfer">批量打币</button>-->
</div>
<div class="layui-row">
<table class="layui-table" id="table1"></table>
</div>
<?php
use backend\assets\coinTransfer\IndexAsset;
IndexAsset::register($this);
?>
<div class="layui-row">
<button class="layui-btn layui-btn-sm" lay-event="implode" id="implode">名单导入</button>
<button class="layui-btn layui-btn-sm" lay-event="transfer" id="transfer">批量打币</button>
</div>
<div class="layui-row">
<div class="layui-col-md8">
<table class="layui-table" id="table1" lay-filter="table1"></table>
</div>
</div>
var table = layui.table;
var upload = layui.upload;
table.render({
elem: "#table1",
url: '/admin/air-drop/list',
page: 1,
limit: 10,
cols: [[
{field:'id', width:100, title: 'ID', sort: true},
{field: 'to_address', width:400, title: '地址'},
{field: 'amount', title: '数量'},
{field: 'coin_name', title: '币种名称'},
{field: 'txhash', width:400,title: 'hash'},
{field: 'msg', width:200, title: '返回结果'},
{field: 'create_time', title: '添加时间'},
{field: 'update_time', title: '处理时间'}
]]
,done: function () {
//$("[data-field='id']").css('display','none');
}
});
$('#add').click(function () {
//打开弹窗
$.get('/admin/air-drop/add', {}, function (str) {
var index = layer.open({
type: 1,
title: '添加数据',
id: 'add-one',
skin: 'layui-layer-lan',
area: ['320px', 'auto'],
content: str,
btn: ['确认', '取消'],
btn1: function () {
$.post('/admin/air-drop/add', $("#addData").serialize(), function (rev) {
layer.msg(rev.msg);
if (rev.code == 0) {
layer.close(index);
table.reload("table1", {});
}
});
}
});
layui.form.render();
});
return false;
});
upload.render({
elem: "#import",
url: "/admin/air-drop/load-file",
accept: 'file',
exts: 'csv',
done: function (res) {
if(res.code == 0){
layer.msg(res.msg);
table.reload('table1', {curr: 1});
}else{
layer.msg(res.msg);
}
},
before: function (obj) { //obj参数包含的信息,跟 choose回调完全一致,可参见上文。
layer.load(); //上传loading
}
});
$('#transfer').click(function () {
//var data = obj.data; //得到所在行所有键值
$.get('/admin/air-drop/batch-transfer', {}, function (rev) {
layer.msg(rev.msg);
if (0==rev.code) {
table.reload('table1', {
page: {
curr: 1
}
});
}
});
});
\ No newline at end of file
<?php <?php
namespace common\models\psources; namespace common\models\psources;
use common\core\BaseActiveRecord; use Yii;
use Yii; use common\core\BaseActiveRecord;
/** class CoinAirDropTransfer extends BaseActiveRecord
* CoinReleaseMember {
* const ORIGIN_IMPORT = 1;
* @property int $id const ORIGIN_ADD = 2;
* @property int $status const ORIGIN_API = 3;
* @property string $address
* @property string $amount public static function getDb()
*/ {
class CoinTransfer extends BaseActiveRecord return Yii::$app->get('p_sources');
{ }
const SCENARIOS_ADD = 'add';
const SCENARIOS_UPDATE = 'update'; public static function tableName()
{
public static function getDb() return '{{%coin_airdrop_transfer}}';
{ }
return Yii::$app->get('p_sources');
} //定义场景
const SCENARIOS_CREATE = 'create';
public static function tableName() const SCENARIOS_UPDATE = 'update';
{
return '{{%coin_transfer}}';
} public function rules() {
return [
public function attributeLabels() [['to_address', 'amount', 'coin_name'], 'required','message'=>"请输入{attribute}"],
{ [['origin', 'txhash', 'msg', 'balance'], 'safe'],
return [ ];
'id' => 'ID', }
'address' => '地址',
'amount' => '数量', /**
'hash' => '交易hash', * @inheritdoc
'status' => '状态', */
]; public function attributeLabels()
} {
return [
public function rules() 'to_address' => '钱包地址',
{ 'amount' => '数量',
return [ 'coin_name' => '币种名称',
[['address', 'amount', 'hash'], 'string'], 'origin' => '来源',
]; 'txhash' => '返回hash',
} 'msg' => '返回结果',
'balance' => '钱包余额',
public function sercians() ];
{ }
return [
self::SCENARIOS_ADD => ['address', 'amount', 'hash', 'status'], public function scenarios() {
self::SCENARIOS_UPDATE => ['address', 'amount', 'hash', 'status'], $scenarios = [
]; self:: SCENARIOS_CREATE => ['to_address', 'amount','coin_name', 'origin'],
} ];
return array_merge( parent:: scenarios(), $scenarios);
/** }
* 批量返回用户
* public static function loadArray(array $data)
* @param integer|array $ids {
* @return array return self::getDb()->createCommand()->batchInsert(self::tableName(),
*/ ['to_address', 'coin_name', 'amount', 'origin', 'txhash', 'msg', 'balance'],
public static function getMemberByRuleIds($ids) $data)->execute();
{ }
return CoinReleaseMember::find()->where(['rule_id' => $ids])->andWhere(['>', 'freeze', 0])->asArray()->all();
} public static function getOrigin()
{
return [
public static function loadArray(array $data) self::ORIGIN_IMPORT => '批量导入',
{ self::ORIGIN_ADD => '单独添加',
return self::getDb()->createCommand()->batchInsert(self::tableName(), self::ORIGIN_API => '接口导入',
['address', 'amount'], ];
$data)->execute(); }
} }
} \ No newline at end of file
<?php
namespace console\controllers;
use Yii;
use yii\console\Controller;
use common\service\chain33\Chain33Service;
use common\models\psources\CoinAirDropTransfer;
class AirDropController extends Controller
{
/**
* 获取游戏状态
*
* @return array
*/
public function actionAutoAirDrop()
{
$model = CoinAirDropTransfer::find()->where(['txhash' => "0", 'msg' => ''])->limit(20)->asArray()->all();
if (empty($model)) {
echo date('Y-m-d H:i:s') . '暂无空投计划' . PHP_EOL;
return 0;
}
foreach ($model as $key => $val) {
go(function () use ($val) {
\Co::sleep(0.5);
$id = $val['id'];
$to = $val['to_address'];
$amount = $val['amount'] * 1e8;
$fee = 100000;
$note = '';
$execer = isset(Yii::$app->params['chain_parallel']['wasm'][$val['coin_name'] . '_wallet']) ? Yii::$app->params['chain_parallel']['wasm'][$val['coin_name'] . '_wallet']['execer'] : null;
$node_params = Yii::$app->params['chain_parallel']['wasm'][$val['coin_name'] . '_wallet'];
$service = new Chain33Service($node_params);
$createRawTransaction = $service->createRawTransaction($to, $amount, $fee, $note, $execer);
if (0 != $createRawTransaction['code']) {
$txhash = '0';
$msg = $createRawTransaction['msg'];
goto doEnd;
}
$txHex = $createRawTransaction['result'];
$privkey = '72c3879f1f9b523f266a9545b69bd41c0251483a93e21e348e85118afe17a5e21';
$expire = '1m';
$signRawTx = $service->signRawTx($privkey, $txHex, $expire);
if (0 != $signRawTx['code']) {
$txhash = '0';
$msg = $signRawTx['msg'];
goto doEnd;
}
$sign_str = $signRawTx['result'];
$result = $service->sendTransaction($sign_str);
if (0 != $result['code']) {
$txhash = '0';
$msg = $result['msg'];
goto doEnd;
}
$txhash = $result['result'];
$msg = 'success';
doEnd :
$currentModel = CoinAirDropTransfer::findOne($id);
$currentModel->txhash = $txhash;
$currentModel->msg = $msg;
$currentModel->save();
});
}
return 0;
}
}
\ No newline at end of file
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