|
@@ -4,13 +4,9 @@
|
|
|
namespace App\Servers;
|
|
|
|
|
|
use App\Models\Broadcast;
|
|
|
-use App\Models\CoinType;
|
|
|
-use App\Models\Config;
|
|
|
-use App\Models\MemberCoins;
|
|
|
-use App\Models\Withdraw;
|
|
|
-use App\Servers\Icon\TronRPC;
|
|
|
+use App\Servers\Icon\BanRPC;
|
|
|
use App\Servers\Icon\Utils;
|
|
|
-use Illuminate\Support\Facades\Log;
|
|
|
+use Web3p\EthereumTx\Transaction;
|
|
|
|
|
|
|
|
|
|
|
@@ -20,15 +16,18 @@ class BroadcastServer
|
|
|
{
|
|
|
|
|
|
public static $type_str=[
|
|
|
- 1=>'BTC充值',
|
|
|
- 2=>'ETH充值',
|
|
|
- 3=>'USDT充值',
|
|
|
- 4=>'BTC提现',
|
|
|
- 5=>'ETH提现',
|
|
|
- 6=>'USDT提现',
|
|
|
- 7=>'ETH手续费',
|
|
|
];
|
|
|
|
|
|
+ static private $server = null;
|
|
|
+
|
|
|
+ * 创建对象
|
|
|
+ * @return BroadcastServer
|
|
|
+ */
|
|
|
+ static function creatServer()
|
|
|
+ {
|
|
|
+ if (empty(self::$server)) self::$server = new BroadcastServer();
|
|
|
+ return self::$server;
|
|
|
+ }
|
|
|
|
|
|
|
|
|
* 创建广播信息
|
|
@@ -59,6 +58,7 @@ class BroadcastServer
|
|
|
'gas_limit' => '',
|
|
|
'to_address' => $to,
|
|
|
'sign' => '',
|
|
|
+ 'success' => '',
|
|
|
'hash' => $status == 5 ? '系统内部转账,直接成功,无需链上广播' : '',
|
|
|
'from_key' => $from_key,
|
|
|
'money' =>round($money,6),
|
|
@@ -66,7 +66,6 @@ class BroadcastServer
|
|
|
'type' => $status_type,
|
|
|
'm_id' => $member_id,
|
|
|
'order_id' => $order_id,
|
|
|
- 'send_money' => $money,
|
|
|
];
|
|
|
|
|
|
$transaction = Broadcast::create($transaction);
|
|
@@ -77,22 +76,30 @@ class BroadcastServer
|
|
|
|
|
|
* 广播查询
|
|
|
*/
|
|
|
- public static function getBroadcast()
|
|
|
+ public function getBroadcast()
|
|
|
{
|
|
|
$list = Broadcast::where('status', '2')->select(['id', 'm_id', 'from', 'to', 'send_money', 'value', 'coin_id', 'type', 'sign', 'money', 'from_key', 'coin_id', 'hash', 'send_money', 'order_id'])->limit(100)->get();
|
|
|
$ret_type=false;
|
|
|
if (empty($list)) return $ret_type;
|
|
|
+ $bnbServer=BanRPC::creatServer();
|
|
|
foreach ($list as $value) {
|
|
|
- $ret = TronRPC::CreationTron()->getTransaction($value->{'hash'});
|
|
|
- if (!empty($ret['ret'][0]['contractRet']) && $ret['ret'][0]['contractRet'] == 'SUCCESS') {
|
|
|
- $update_info['status'] = 4;
|
|
|
- $update_info['end_time'] = time();
|
|
|
- } elseif (!empty($ret['ret'][0]['contractRet']) && $ret['ret'][0]['contractRet'] == 'REVERT') {
|
|
|
- $update_info['status'] = 3;
|
|
|
+ $receipt = $bnbServer->getTransactionReceipt($value['hash']);
|
|
|
+ if (empty($receipt)) {
|
|
|
+
|
|
|
+ continue;
|
|
|
+ } else {
|
|
|
+
|
|
|
+ if ($receipt['status'] != '0x1') {
|
|
|
+ $update_info['error'] = '广播提交失败';
|
|
|
+ $update_info['status'] = 3;
|
|
|
+ } else {
|
|
|
+ $update_info['status'] = 4;
|
|
|
+ $update_info['end_time'] = time();
|
|
|
+ }
|
|
|
+
|
|
|
}
|
|
|
- if(!empty($update_info))Broadcast::where('id',$value->{'id'})->update($update_info);
|
|
|
}
|
|
|
- return $ret_type;
|
|
|
+ return $list->count();
|
|
|
}
|
|
|
|
|
|
|
|
@@ -101,39 +108,117 @@ class BroadcastServer
|
|
|
*/
|
|
|
function sendBroadcast(){
|
|
|
$list = Broadcast::where('status', '1')->where([['end_time','=',0,'or'],['end_time','<=',time()-60,'or']])->select(['id', 'm_id', 'from', 'to', 'nonce', 'value', 'data', 'gas_price', 'gas_limit', 'sign', 'money', 'from_key', 'coin_id', 'depend','type','contract_address','order_id'])->limit(100)->get();
|
|
|
- foreach ($list as $transaction){
|
|
|
-
|
|
|
- $trx_num = TronRPC::CreationTron()->getBalance($transaction['from']);
|
|
|
- if ($trx_num < 5) {
|
|
|
-
|
|
|
- CommonServer::creatServer()->addErrorRecord('系统账户手续不足',$transaction);
|
|
|
- continue;
|
|
|
+ $bnbServer=BanRPC::creatServer();
|
|
|
+ foreach ($list as $value){
|
|
|
+ $bnbNum=$bnbServer->getBalance($value->{'from'});
|
|
|
+ if(env('TEST_SERVE')){
|
|
|
+ $value->{'money'}='0.00001';
|
|
|
}
|
|
|
- $send_data =TronRPC::CreationTron()->createContract($transaction['to'], $transaction['contract_address'], $transaction['money'] * 1, $transaction['from']);
|
|
|
- if(empty($send_data)){
|
|
|
- CommonServer::creatServer()->addErrorRecord('交易信息生成失败',$transaction);
|
|
|
- continue;
|
|
|
+ if(empty($bnbNum) || $bnbNum<($value->{'money'}+0.0004)){
|
|
|
+ $transaction['end_time'] = time();
|
|
|
}else{
|
|
|
- $sign_data = TronRPC::CreationTron()->signTransaction($send_data, PassServer::creatServer()->getSecretKey('sadsadas','1'));
|
|
|
- if (empty($sign_data['signature'][0])) {
|
|
|
- CommonServer::creatServer()->addErrorRecord('签名失败',$sign_data);
|
|
|
- $transaction['status'] = 3;
|
|
|
+
|
|
|
+ $send_num = Broadcast::where('from', $value->{'from'})->where('status', '2')->count('id');
|
|
|
+ if ($send_num > 0) {
|
|
|
+ sleep(1);
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ $transaction = $this->sendBnb($value);
|
|
|
+ if (empty($transaction) || empty($transaction['hash'])) {
|
|
|
+ $transaction = ['status' => 3, 'error' => '广播失败'];
|
|
|
} else {
|
|
|
- $transaction['sign'] = $sign_data['signature'][0];
|
|
|
- $hash_data = TronRPC::CreationTron()->sendRawTransaction($sign_data);
|
|
|
- if (empty($hash_data['result'])) {
|
|
|
- CommonServer::creatServer()->addErrorRecord('广播失败',$hash_data);
|
|
|
- $transaction['status'] = 3;
|
|
|
- } else {
|
|
|
- $transaction['hash'] = $hash_data['txid'];
|
|
|
- $transaction['status'] = 2;
|
|
|
- }
|
|
|
+ $transaction['status'] = 2;
|
|
|
+ }
|
|
|
+ if ($transaction) {
|
|
|
+ Broadcast::where('id', $value->{'id'})->update($transaction);
|
|
|
}
|
|
|
- $transaction->save();
|
|
|
}
|
|
|
}
|
|
|
+ return $list->count();
|
|
|
}
|
|
|
|
|
|
+ function sendBnb($value){
|
|
|
+ $bnbServer=BanRPC::creatServer();
|
|
|
+
|
|
|
+ $transaction['from'] = $value['from'];
|
|
|
+ $transaction['to'] = $value['to'];
|
|
|
+ $transaction['chain_id'] = $bnbServer->chainId;
|
|
|
+ $transaction['value'] = Utils::dec2hex(Utils::fund2int($value['money'] ));
|
|
|
+ $transaction['data'] = '0x0';
|
|
|
+ $nonce = $bnbServer->getTransactionCount($value['from']);
|
|
|
+ $transaction['nonce'] = $nonce;
|
|
|
+ $gas_price = $bnbServer->getGasPrice();
|
|
|
+
|
|
|
+ $transaction['gas_price'] =$gas_price;
|
|
|
+ $gas_limit =$this->getEthGasLimit($transaction);
|
|
|
+ $transaction['gas_limit'] = $gas_limit;
|
|
|
+ if($value->{'type'}==3)$value->{'m_id'}=0;
|
|
|
+ $sign = $this->ethSign($transaction, PassServer::creatServer()->getSecretKey($value->{'from_key'}, $value->{'m_id'}));
|
|
|
+ $transaction['sign'] = $sign;
|
|
|
+ $error='';
|
|
|
+ $hash = $bnbServer->sendRawTransaction($transaction['sign'], $error);
|
|
|
+ $transaction['hash'] = $hash;
|
|
|
+ return $transaction;
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ * @param $transaction
|
|
|
+ * @return string
|
|
|
+ */
|
|
|
+ public function getEthGasLimit($transaction)
|
|
|
+ {
|
|
|
+ $bnbServer=BanRPC::creatServer();
|
|
|
+ $tx = [
|
|
|
+ 'from' => $transaction['from'],
|
|
|
+ 'to' => $transaction['to'],
|
|
|
+ 'nonce' => $transaction['nonce'],
|
|
|
+ 'value' => $transaction['value'],
|
|
|
+ 'data' => $transaction['data'],
|
|
|
+ 'chainId' => $bnbServer->chainId,
|
|
|
+ 'gasPrice' => $transaction['gas_price'],
|
|
|
+ ];
|
|
|
+
|
|
|
+ $gasLimit = $bnbServer->estimateGas($tx);
|
|
|
+ if (!$gasLimit) {
|
|
|
+ $gasLimit = 10 * 10000;
|
|
|
+ }
|
|
|
+ return $gasLimit;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ protected function build($from, $to, $value, $data)
|
|
|
+ {
|
|
|
+ $tx = [];
|
|
|
+ $tx['from'] = $from;
|
|
|
+ $tx['to'] = $to;
|
|
|
+ $tx['value'] = $value;
|
|
|
+ $tx['data'] = $data;
|
|
|
+
|
|
|
+ return $tx;
|
|
|
+ }
|
|
|
+
|
|
|
+ protected function ethSign($transaction, $key)
|
|
|
+ {
|
|
|
+ $bnbServer=BanRPC::creatServer();
|
|
|
+ $tx = [
|
|
|
+ 'from' => $transaction['from'],
|
|
|
+ 'to' => $transaction['to'],
|
|
|
+ 'nonce' => $transaction['nonce'],
|
|
|
+ 'value' => $transaction['value'],
|
|
|
+ 'data' => $transaction['data'],
|
|
|
+ 'chainId' => $bnbServer->chainId,
|
|
|
+ 'gasPrice' => $transaction['gas_price'],
|
|
|
+ 'gasLimit' => $transaction['gas_limit'],
|
|
|
+ ];
|
|
|
+
|
|
|
+
|
|
|
+ $tr = new Transaction($tx);
|
|
|
+ $tr->sign($key);
|
|
|
+ return Utils::fixHex($tr->serialize());
|
|
|
+ }
|
|
|
|
|
|
|
|
|
|