Commit b71d7bf7 authored by shajiaiming's avatar shajiaiming

跨链交易

parent e71c922b
<?php
namespace api\controllers;
use api\base\BaseController;
use common\models\psources\CoinAirDropTrade;
use common\models\psources\CoinCrossChain;
use common\service\chain33\Chain33Service;
use Yii;
use yii\db\Exception;
class CrossChainController extends BaseController
{
public function actionTransfer()
{
$code = -1;
$msg = 'fail';
$request = Yii::$app->request;
if (!$request->isPost) {
$msg = '请求错误!';
goto doEnd;
}
$post = $request->post();
if (3 != count($post['txs'])) {
$msg = '交易笔数错误!';
goto doEnd;
}
$data['is_with_draw'] = $post['isWithdraw'];
$data['address'] = $post['address'];
$data['transfer_number'] = date('YmdHis') . self::getrandnums();
$model = CoinCrossChain::find()->where(['address' => $post['address'], 'current_step' => CoinCrossChain::STEP_DEFAULT, 'is_with_draw' => CoinCrossChain::RECHARGE])->one();
if ($model) {
$msg = '存在未完成的交易!';
goto doEnd;
}
$model = new CoinCrossChain();
foreach ($post['txs'] as $key => $val) {
if (1 == $val['step']) {
$seq = 'first';
} else if (2 == $val['step']) {
$seq = 'second';
} else if (3 == $val['step']) {
$seq = 'thrid';
} else {
}
$data['txhex_' . $seq] = $val['tx'];
$data['transfer_url_' . $seq] = $val['url'];
}
$model->setScenario(CoinCrossChain::SCENARIOS_CREATE);
if ($model->load($data, '') && $model->save()) {
$msg = 'success';
$code = 0;
} else {
$msg = current($model->firstErrors);
}
doEnd :
return ['code' => $code, 'msg' => $msg];
}
public function actionTransferStatus()
{
$code = -1;
$msg = 'fail';
$data = null;
$request = Yii::$app->request;
if (!$request->isGet) {
$msg = '请求错误!';
goto doEnd;
}
$address = $request->get('address', '');
$is_with_draw = $request->get('isWithdraw', '');
if (empty($address)) {
$msg = '参数错误!';
goto doEnd;
}
$model = CoinCrossChain::find()->where(['address' => $address, 'is_with_draw' => $is_with_draw])->orderBy("id desc")->limit(1)->asArray()->one();
if (empty($model)) {
$msg = '数据不存在!';
goto doEnd;
}
if (false == $model['txhash'] && false == $model['msg']) {
$data = [
'step' => $model['current_step']
];
$code = 0;
$msg = '交易尚未执行';
goto doEnd;
}
if (false == $model['txhash'] && false != $model['msg']) {
$data = [
'step' => $model['current_step']
];
$msg = $model['msg'];
goto doEnd;
}
if (true == $model['txhash'] && false == $model['msg']) {
$data = [
'step' => $model['current_step']
];
$code = 0;
$msg = $model['txhash'];
goto doEnd;
}
doEnd :
return ['code' => $code, 'msg' => $msg, 'data' => $data];
}
public static function getrandnums()
{
$arr = array();
while (count($arr) < 6) {
$arr[] = rand(0, 9);
$arr = array_unique($arr);
}
return implode("", $arr);
}
}
\ No newline at end of file
<?php
namespace common\models\psources;
use Yii;
use common\core\BaseActiveRecord;
class CoinCrossChain extends BaseActiveRecord
{
const STEP_DEFAULT = 0;
const STEP_FIRST = 1;
const STEP_SECOND = 2;
const STEP_THRID = 3;
const WITHDRAW = 0;
const RECHARGE = 1;
public static function getDb()
{
return Yii::$app->get('p_sources');
}
public static function tableName()
{
return '{{%coin_cross_chain}}';
}
//定义场景
const SCENARIOS_CREATE = 'create';
public function rules()
{
return [
[['is_with_draw', 'address', 'txhex_first', 'transfer_url_first', 'txhex_second', 'transfer_url_second', 'txhex_thrid', 'transfer_url_thrid'], 'required'],
['transfer_number', 'safe']
];
}
public function scenarios()
{
$scenarios = [
self:: SCENARIOS_CREATE => ['is_with_draw', 'address', 'txhex_first', 'transfer_url_first', 'txhex_second', 'transfer_url_second', 'txhex_thrid', 'transfer_url_thrid', 'transfer_number'],
];
return array_merge(parent:: scenarios(), $scenarios);
}
}
<?php
namespace console\controllers;
use common\models\psources\CoinCrossChain;
use Yii;
use yii\console\Controller;
use common\service\chain33\Chain33Service;
class CrossChainController extends Controller
{
/**
* 自动交易
*
* @return array
*/
public function actionAutoTransfer()
{
$model = CoinCrossChain::find()->where(['txhash' => "0"])->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);
if (1 == $val['step']) {
$seq = 'first';
} else if (2 == $val['step']) {
$seq = 'second';
} else if (3 == $val['step']) {
$seq = 'thrid';
} else {
}
$node_params = $val['transfer_url_' . $seq];
$node_params = Yii::$app->params['chain_parallel']['wasm'][$val['coin_name'] . '_wallet'];
$service = new Chain33Service($node_params);
$txHex = $val['txhex_' . $seq];
$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