Files
cycle_api/app/Service/Wallet/PlatformWithdrawService.php
2024-03-26 20:07:19 +08:00

131 lines
5.8 KiB
PHP

<?php
namespace App\Service\Wallet;
use App\Bean\Queue\Wallet\QueueWalletPlatformTransactionBean;
use App\Bean\Queue\Wallet\QueueWalletPlatformWithdrawTransferBean;
use App\Exceptions\QueueException;
use App\Models\Wallet\Platform\WalletPlatformBalanceTransactionModel;
use App\Models\Wallet\Platform\WalletPlatformBindModel;
use App\Models\Wallet\Wallet\WalletAddressModel;
use App\Models\Wallet\Wallet\WalletCurrencyModel;
use App\Tools\Logs;
use App\Tools\Times;
use Illuminate\Support\Facades\DB;
use Sdk\Wallet\TokenAddress;
use Sdk\Wallet\UsdtTrc20;
class PlatformWithdrawService
{
function withdrawTransferConsumer(QueueWalletPlatformWithdrawTransferBean $bean): void
{
//获取账变
$oWalletPlatformBalanceTransactionModel = new WalletPlatformBalanceTransactionModel();
$resModel = $oWalletPlatformBalanceTransactionModel->findItem($bean->getTransactionId());
if (!$resModel) throw new QueueException('账变不存在');
if($resModel->status != WalletPlatformBalanceTransactionModel::STATUS_WAIT) throw new QueueException('账变状态不正确');
//更新账变状态
$updateItem = [
'id' => $bean->getTransactionId(),
'status' => WalletPlatformBalanceTransactionModel::STATUS_PROCESSING,
'updated_at' => Times::getNowDateTime(),
];
$res = $oWalletPlatformBalanceTransactionModel->updateItem($updateItem);
if (!$res) throw new QueueException('更新账变状态失败');
$currency_code = $resModel->currency_code;
//提现
try {
//根据币种调用不同的提现方法
switch ($currency_code) {
case WalletCurrencyModel::CODE_USDT_TRC20: //提现USDT
$txId = $this->withdrawUsdtTrc20($resModel);
break;
default:
throw new QueueException('不支持的币种');
}
Logs::SuccLog(__FUNCTION__, $bean->toArrayNotNull());
}catch (\Exception $e){
//更新账变状态
$updateItem = [
'id' => $bean->getTransactionId(),
'status' => WalletPlatformBalanceTransactionModel::STATUS_FAIL,
'updated_at' => Times::getNowDateTime(),
];
$oWalletPlatformBalanceTransactionModel->updateItem($updateItem);
Logs::ErrLog(__FUNCTION__ . ' ' . 'rollBack', $e, $bean->toArrayNotNull());
throw new QueueException($e->getMessage());
}
}
function withdrawUsdtTrc20($model): string
{
try {
DB::beginTransaction();
$oWalletAddressModel = new WalletAddressModel();
//获取出金地址
$resFromModel = $oWalletAddressModel->findWithdrawWallet($model->currency_code, $model->amount);
if(!$resFromModel) throw new QueueException('提现地址不足');
$from_wallet_address_id = $resFromModel->id;
$from_wallet_address = $resFromModel->address_base58;
//获取入金地址
$to_wallet_address = $model->to_wallet_address;
if(!empty($to_wallet_address)) throw new QueueException('平台钱包id错误');
$fromAddress = new TokenAddress($resFromModel->address_base58, $oWalletAddressModel->decodePrivateKey($resFromModel->private_key));
$toAddress = new TokenAddress($to_wallet_address);
$amount = abs($model->amount);
//调用sdk发起提现
$sdk = new UsdtTrc20();
$oTransferTransactionBean = $sdk->getHandler()->trc20Transfer($fromAddress,$toAddress, $amount);
if(!$oTransferTransactionBean) throw new QueueException('提现sdk发起失败');
if(empty($oTransferTransactionBean->getTxID())) throw new QueueException('提现sdk发起失败');
//存储交易id
$updateItem = [
'id' => $model->id,
'block_transaction_id' => $oTransferTransactionBean->getTxID(),
'from_wallet_address_id' => $from_wallet_address_id,
'from_wallet_address' => $from_wallet_address,
'updated_at' => Times::getNowDateTime(),
];
$oWalletPlatformBalanceTransactionModel = new WalletPlatformBalanceTransactionModel();
$res = $oWalletPlatformBalanceTransactionModel->updateItem($updateItem);
if(!$res) throw new QueueException('更新账变失败');
DB::commit();
return $oTransferTransactionBean->getTxID();
}catch (\Exception $e){
DB::rollBack();
throw new QueueException($e->getMessage());
}
}
function walletPlatformTransactionConsumer(QueueWalletPlatformTransactionBean $bean): void
{
//根据txid查找平台和用户
$oWalletPlatformBindModel = new WalletPlatformBindModel();
$resBindModel = $oWalletPlatformBindModel->findItem($bean->getWalletId());
if (!$resBindModel) throw new QueueException('钱包不存在');
$oWalletPlatformBalanceTransactionModel = new WalletPlatformBalanceTransactionModel();
$resModel = $oWalletPlatformBalanceTransactionModel->findByBlockTransactionId($bean->getBlockTransactionId(), WalletPlatformBalanceTransactionModel::TYPE_WITHDRAW_DEC);
if (!$resModel) throw new QueueException('账变不存在');
if($resModel->status != WalletPlatformBalanceTransactionModel::STATUS_PROCESSING) throw new QueueException('账变状态不正确');
$res = $oWalletPlatformBalanceTransactionModel->typeWithdrawSecondCallback(
$resModel->id,
WalletPlatformBalanceTransactionModel::STATUS_SUCCESS,
null,
$bean->getWalletTransactionId(),
);
if (!$res) throw new QueueException('typeWithdrawSecondCallback fail');
}
}