| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199 |
- <?php
- namespace App\Module\Blockchain\Models;
- use App\Module\Transaction\Enums\ACCOUNT_TYPE;
- use Illuminate\Database\Eloquent\Model;
- use Illuminate\Database\Eloquent\Relations\BelongsTo;
- use Illuminate\Support\Facades\DB;
- use Illuminate\Support\Facades\Cache;
- /**
- *
- *
- * field start
- * @property int $id
- * @property string $tx_hash 交易哈希
- * @property string $from_address 发送地址
- * @property string $to_address 接收地址
- * @property float $amount 转账金额
- * @property int $token_type 代币类型:1=bnb, 2=usdt, 3=uraus
- * @property float $gas_price 矿工费单价(GWEI)
- * @property int $gas_used 消耗的Gas数量
- * @property float $gas_fee 矿工费总额(BNB)
- * @property int $status 状态:0=待确认,1=成功,2=失败
- * @property int $block_number 区块号
- * @property \Carbon\Carbon $created_at
- * @property \Carbon\Carbon $updated_at
- * field end
- */
- class BlockchainTransaction extends Model
- {
- protected $table = 'blockchain_transactions';
- // attrlist start
- protected $fillable = [
- 'id',
- 'tx_hash',
- 'from_address',
- 'to_address',
- 'amount',
- 'token_type',
- 'gas_price',
- 'gas_used',
- 'gas_fee',
- 'status',
- 'block_number',
- ];
- // attrlist end
- protected $casts = [
- 'token_type' => ACCOUNT_TYPE::class,
- 'amount' => 'decimal:18',
- 'gas_price' => 'decimal:18',
- 'gas_used' => 'integer',
- 'gas_fee' => 'decimal:18',
- 'status' => 'integer',
- 'block_number' => 'integer'
- ];
- protected static function booted()
- {
- static::creating(function ($transaction) {
- // 自动计算矿工费
- if ($transaction->gas_price && $transaction->gas_used) {
- $transaction->gas_fee = bcdiv(
- bcmul($transaction->gas_price, (string)$transaction->gas_used),
- '1000000000',
- 18
- );
- }
- });
- static::updating(function ($transaction) {
- // 当 gas_price 或 gas_used 更新时重新计算矿工费
- if ($transaction->isDirty('gas_price') || $transaction->isDirty('gas_used')) {
- $transaction->gas_fee = bcdiv(
- bcmul($transaction->gas_price, (string)$transaction->gas_used),
- '1000000000',
- 18
- );
- }
- });
- static::updated(function ($transaction) {
- if ($transaction->isDirty('status')) {
- if ($transaction->status === 1) { // 交易成功
- $transaction->updateWalletBalances();
- }
- }
- });
- }
- public function fromWallet(): BelongsTo
- {
- return $this->belongsTo(BlockchainWallet::class, 'from_address', 'address');
- }
- public function toWallet(): BelongsTo
- {
- return $this->belongsTo(BlockchainWallet::class, 'to_address', 'address');
- }
- public function getStatusLabelAttribute(): string
- {
- return match($this->status) {
- 0 => '待确认',
- 1 => '成功',
- 2 => '失败',
- default => '未知'
- };
- }
- public function updateWalletBalances(): void
- {
- DB::transaction(function () {
- // 更新发送方钱包余额
- $fromWallet = $this->fromWallet;
- if ($fromWallet) {
- $newBalance = bcsub($fromWallet->balance, $this->amount, 18);
- $fromWallet->updateBalance($newBalance);
- }
- // 更新接收方钱包余额
- $toWallet = $this->toWallet;
- if ($toWallet) {
- $newBalance = bcadd($toWallet->balance, $this->amount, 18);
- $toWallet->updateBalance($newBalance);
- }
- });
- }
- public function getConfirmations(): int
- {
- if (!$this->block_number) {
- return 0;
- }
- $latestBlockNumber = Cache::remember('latest_block_number', 60, function () {
- $bscScanService = app(BscScanService::class);
- return $bscScanService->getLatestBlockNumber();
- });
- return max(0, $latestBlockNumber - $this->block_number);
- }
- public function isSafe(): bool
- {
- $requiredConfirmations = config('blockchain.transaction.confirmations', 12);
- return $this->getConfirmations() >= $requiredConfirmations;
- }
- public function getTotalGasFee(): float
- {
- if (!$this->gas_price || !$this->gas_used) {
- return 0;
- }
- return bcmul($this->gas_price, (string)$this->gas_used, 18);
- }
- /**
- * 获取交易的总成本(转账金额 + 矿工费)
- * 仅当代币类型为 BNB 时才包含矿工费
- */
- public function getTotalCost(): string
- {
- if ($this->token_type === ACCOUNT_TYPE::BNB) {
- return bcadd($this->amount, $this->gas_fee, 18);
- }
- return $this->amount;
- }
- /**
- * 获取格式化的矿工费(带单位)
- */
- public function getFormattedGasFeeAttribute(): string
- {
- return $this->gas_fee . ' BNB';
- }
- /**
- * 获取预估的矿工费
- */
- public static function estimateGasFee(ACCOUNT_TYPE $tokenType, float $gasPrice): float
- {
- $gasLimit = $tokenType === ACCOUNT_TYPE::BNB ? 21000 : 65000;
- return bcdiv(bcmul((string)$gasPrice, (string)$gasLimit), '1000000000', 18);
- }
- /**
- * 检查矿工费是否超过最大限制
- */
- public function isGasFeeTooHigh(): bool
- {
- $maxGasPrice = config('blockchain.transaction.max_gas_price', 10); // GWEI
- return bccomp($this->gas_price, (string)$maxGasPrice, 18) === 1;
- }
- }
|