Commit 3794327b authored by shajiaiming's avatar shajiaiming

Merge branch 'feature/cross_chain' into 'master'

Feature/cross chain See merge request !102
parents e71c922b 2dfd4b10
<?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;
$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'] = (false == $post['isWithdraw']) ? 0 : 1;
$data['address'] = $post['address'];
$data['transfer_number'] = date('YmdHis') . self::getrandnums();
$redis = Yii::$app->redis;
if ($redis->exists($post['address'] . '_' . $data['is_with_draw'])) {
$msg = '存在未完成的交易!';
goto doEnd;
}
$redis->set($post['address'] . '_' . $data['is_with_draw'], $post['address'] . '_' . $data['is_with_draw']);
// $isExist = CoinCrossChain::find()
// ->where(['address' => $post['address'], 'is_with_draw' => $data['is_with_draw']])
// ->andWhere(['<>', 'query_result', 'success'])
// ->asArray()->all();
//
// if ($isExist) {
// $msg = '存在未完成的交易!';
// goto doEnd;
// }
foreach ($post['txs'] as $key => $val) {
$model = new CoinCrossChain();
$data['txhex'] = $val['tx'];
$data['transfer_url'] = $val['url'];
$model->load($data, '');
$model->save();
}
$code = 0;
$msg = 'success';
doEnd :
return ['code' => $code, 'msg' => $msg];
}
public function actionTransferStatus()
{
$code = -1;
$msg = 'fail';
$data = null;
$step = 0;
$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(3)->asArray()->all();
if (empty($model)) {
$msg = '数据不存在!';
goto doEnd;
}
sort($model);
foreach ($model as $key => $val) {
//未交易
if ('0' == $val['send_result'] && '0' == $val['query_result'] && '0' == $val['msg']) {
$step = $key + 1;
$code = 0;
$msg = '第' . ($key + 1) . '笔交易尚未执行';
goto doEnd;
}
//交易报错
if ('0' == $val['send_result'] && '0' == $val['query_result'] && true == $val['msg']) {
$step = $key + 1;
$code = -1;
$msg = $val['msg'];
goto doEnd;
}
//交易成功
if (true == $val['send_result'] && '0' == $val['query_result'] && '0' == $val['msg']) {
$step = $key + 1;
$code = 0;
$msg = $val['send_result'];
goto doEnd;
}
//交易成功,查询失败
if (true == $val['send_result'] && 'fail' == $val['query_result']) {
$step = $key + 1;
$code = -1;
$msg = $val['msg'];
goto doEnd;
}
//交易成功,查询成功
if (true == $val['send_result'] && 'success' == $val['query_result'] && 'success' == $val['msg']) {
if (2 == $key){
$step = (2 == $key) ? 4 : ($key + 2);
$code = 0;
$msg = $val['send_result'];
goto doEnd;
}
continue;
}
}
doEnd :
$data = [
'step' => $step
];
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 WITHDRAW = 0;
const RECHARGE = 1;
public static function getDb()
{
return Yii::$app->get('p_sources');
}
public static function tableName()
{
return '{{%coin_cross_chain_transfer}}';
}
//定义场景
const SCENARIOS_CREATE = 'create';
public function rules()
{
return [
[['is_with_draw', 'address', 'txhex', 'transfer_url'], 'required'],
['transfer_number', 'safe']
];
}
public function scenarios()
{
$scenarios = [
self:: SCENARIOS_CREATE => ['is_with_draw', 'address', 'txhex', 'transfer_url', 'transfer_number'],
];
return array_merge(parent:: scenarios(), $scenarios);
}
}
......@@ -234,6 +234,14 @@ class Chain33Service
return $this->send($params, 'Chain33.SendTransaction');
}
public function QueryTransaction($data)
{
$params = [
'hash' => $data
];
return $this->send($params, 'Chain33.QueryTransaction');
}
public function getBalance($address, $execer)
{
$params = [
......
<?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(['send_result' => "0"])->limit(20)->groupBy('transfer_number')->orderBy('id')->asArray()->all();
if (empty($model)) {
echo date('Y-m-d H:i:s') . '暂无跨链交易计划' . PHP_EOL;
return 0;
}
foreach ($model as $val) {
$isExist = CoinCrossChain::find()
->where(['transfer_number' => $val['transfer_number']])
->andWhere(['<', 'id', (int)$val['id']])
->orderBy('id desc')
->asArray()
->one();
//上一步发送成功,未查询
if ('success' != $isExist['query_result'] && !empty($isExist)) {
continue;
}
go(function () use ($val) {
\Co::sleep(0.5);
$node_params = $val['transfer_url'];
$node_params = explode(':', $node_params);
$node_params = [
'scheme' => $node_params[0],
'host' => str_replace('//', '', $node_params[1]),
'port' => $node_params[2]
];
$service = new Chain33Service($node_params);
$sign_str = $val['txhex'];
$result = $service->sendTransaction($sign_str);
if (0 != $result['code']) {
$msg = $result['msg'];
goto doEnd;
}
$send_result = $result['result'];
doEnd :
$currentModel = CoinCrossChain::findOne($val['id']);
if (isset($msg)) {
$currentModel->msg = $msg;
}
if (isset($send_result)) {
$currentModel->send_result = $send_result;
}
$currentModel->save();
});
}
echo date('Y-m-d H:i:s') . '跨链交易成功' . PHP_EOL;
return 0;
}
public function actionQueryTransaction()
{
$model = CoinCrossChain::find()->where(['<>', 'send_result', '0'])->andWhere(['msg' => '0'])->limit(20)->asArray()->all();
if (empty($model)) {
echo date('Y-m-d H:i:s') . '暂无跨链交易计划' . PHP_EOL;
return 0;
}
foreach ($model as $val) {
$node_params = $val['transfer_url'];
$node_params = explode(':', $node_params);
$node_params = [
'scheme' => $node_params[0],
'host' => str_replace('//', '', $node_params[1]),
'port' => $node_params[2]
];
$service = new Chain33Service($node_params);
$send_result = $val['send_result'];
$result = $service->QueryTransaction($send_result);
if (-1 == $result['code']) {
$msg = $result['msg'];
$query_result = $result['code'];
goto doEnd;
// echo date('Y-m-d H:i:s') . 'ID:' . $val['id'] . '查询错误' . PHP_EOL;
// continue;
}
if (isset($result['result']['actionName']) && 'unknown' == $result['result']['actionName']) {
$query_result = 'success';
$msg = 'success';
} else if (isset($result['result']['receipt']['ty']) && 2 == $result['result']['receipt']['ty']) {
$query_result = 'success';
$msg = 'success';
} else {
$query_result = 'fail';
foreach ($result['result']['receipt']['logs'] as $log) {
if (is_array($log['log'])) continue;
$msg = isset($log['log']) ? $log['log'] : '查询错误';
}
}
doEnd :
$currentModel = CoinCrossChain::findOne($val['id']);
$currentModel->query_result = $query_result;
$currentModel->msg = $msg;
$currentModel->save();
$count = CoinCrossChain::find()->where(['transfer_number' => $val['transfer_number']])->andWhere(['query_result' => 'success'])->count();
if (3 == $count) {
$redis = Yii::$app->redis;
$key = $val['address'] . '_' . $val['is_with_draw'];
if ($redis->exists($key)) {
$redis->del($key);
}
}
}
echo date('Y-m-d H:i:s') . '查询完毕' . PHP_EOL;
return 0;
}
}
\ No newline at end of file
#!/bin/bash
while true; do php /var/www/html/token_pwallet/yii cross-chain/auto-transfer >> /var/www/html/auto-transfer.log 2>&1; sleep 15; done &
\ No newline at end of file
#!/bin/bash
while true; do php /var/www/html/token_pwallet/yii cross-chain/query-transaction >> /var/www/html/query-transaction.log 2>&1; sleep 20; done &
\ 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