Commit da0f0201 authored by shajiaiming's avatar shajiaiming

游戏空投

parent 3a260f2c
<?php
namespace api\controllers;
use api\base\BaseController;
use common\models\psources\CoinAirDropTrade;
use common\service\chain33\Chain33Service;
use Yii;
class AirDropController extends BaseController
{
public function actionGameTrade()
{
$coins_address = Yii::$app->request->post('coins_address', '');
if(empty($coins_address)){
return ['code' => -1, 'msg' => '参数不能为空'];
}
$coinTokenTransfer = CoinAirDropTrade::find()->where(['coins_address' => $coins_address, 'type' => CoinAirDropTrade::TYPE_GAME])->one();
if($coinTokenTransfer){
return ['code' => -1, 'data' => null, 'msg' => 'record already exists'];
}
$fee = 100000;
$amount = CoinAirDropTrade::AMOUNT_GAME * 1e8;
$execer = 'coins';
$note = '';
$service = new Chain33Service();
$createRawTransaction = $service->createRawTransaction($coins_address, $amount, $fee, $note, $execer);
if(0 != $createRawTransaction['code']){
$msg = $createRawTransaction['msg'];
$code = -1;
goto doEnd;
}
$txHex = $createRawTransaction['result'];
$privkey = '72c3879f1f9b523f266a9545b69bd41c0251483a93e21e348e85118afe17a5e2';
$expire = '1m';
$signRawTx = $service->signRawTx($privkey, $txHex, $expire);
if(0 != $signRawTx['code']){
$msg = $signRawTx['msg'];
$code = -1;
goto doEnd;
}
$sign_str = $signRawTx['result'];
$result = $service->sendTransaction($sign_str);
if(0 != $result['code']){
$msg = $result['msg'];
$code = -1;
goto doEnd;
}
$code = 1;
$msg = $result['result'];
doEnd :
$airDropModel = new CoinAirDropTrade();
$airDropModel->coins_address = $coins_address;
$airDropModel->amount = CoinAirDropTrade::AMOUNT_GAME;
$airDropModel->msg = $msg;
$airDropModel->type = CoinAirDropTrade::TYPE_GAME;
$airDropModel->save();
return ['code' => $code, 'msg' => $msg];
}
}
\ No newline at end of file
<?php
namespace common\models\psources;
use Yii;
use common\core\BaseActiveRecord;
class CoinAirDropTrade extends BaseActiveRecord
{
const TYPE_GAME = 1;
const AMOUNT_GAME = 0.2;
public static function getDb()
{
return Yii::$app->get('p_sources');
}
public static function tableName()
{
return '{{%coin_airdrop_trade}}';
}
//定义场景
const SCENARIOS_CREATE = 'create';
const SCENARIOS_UPDATE = 'update';
public function rules() {
return [
[['coins_address','msg', 'amount', 'type'], 'required'],
];
}
public function scenarios() {
$scenarios = [
self:: SCENARIOS_CREATE => ['coins_address','msg', 'amount', 'type'],
];
return array_merge( parent:: scenarios(), $scenarios);
}
}
\ 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