From b7c300c28cc5d163083dcdac5b72a84237f2b65c Mon Sep 17 00:00:00 2001 From: impelcrypto Date: Thu, 19 Dec 2024 15:58:18 +0800 Subject: [PATCH 1/3] fix: modified balance checking logic --- .../services/implementations/AssetsService.ts | 36 +++++++++++-------- 1 file changed, 21 insertions(+), 15 deletions(-) diff --git a/src/v2/services/implementations/AssetsService.ts b/src/v2/services/implementations/AssetsService.ts index 5df7ca018..ba5c6e1c3 100644 --- a/src/v2/services/implementations/AssetsService.ts +++ b/src/v2/services/implementations/AssetsService.ts @@ -1,5 +1,6 @@ import { ethers } from 'ethers'; import { inject, injectable } from 'inversify'; +import { is } from 'quasar'; import { endpointKey } from 'src/config/chainEndpoints'; import { LOCAL_STORAGE } from 'src/config/localStorage'; import { handleCheckProviderChainId } from 'src/config/web3'; @@ -10,6 +11,7 @@ import { REQUIRED_MINIMUM_BALANCE_ETH, } from 'src/modules/toast/index'; import { astarNativeTokenErcAddr } from 'src/modules/xcm'; +import { idAstarNativeToken } from 'src/modules/xcm/tokens'; import { container } from 'src/v2/common'; import { IEventAggregator } from 'src/v2/messaging'; import { IAssetsRepository } from 'src/v2/repositories/IAssetsRepository'; @@ -38,23 +40,27 @@ export class AssetsService implements IAssetsService { } public async transferNativeAsset(param: ParamAssetTransfer): Promise { - const useableBalance = await this.AssetsRepository.getNativeBalance(param.senderAddress); - const isBalanceEnough = - Number(ethers.utils.formatEther(useableBalance)) - - Number(ethers.utils.formatEther(param.amount)) > - REQUIRED_MINIMUM_BALANCE; + const isNativeToken = param.assetId === idAstarNativeToken; + // Memo: Check if the native token's remaining balance is enough to pay the transaction fee + if (isNativeToken) { + const useableBalance = await this.AssetsRepository.getNativeBalance(param.senderAddress); + const isBalanceEnough = + Number(ethers.utils.formatEther(useableBalance)) - + Number(ethers.utils.formatEther(param.amount)) > + REQUIRED_MINIMUM_BALANCE; - if (isBalanceEnough) { - const transaction = await this.AssetsRepository.getNativeTransferCall(param); - const hash = await this.wallet.signAndSend({ - extrinsic: transaction, - senderAddress: param.senderAddress, - successMessage: param.successMessage, - }); - param.finalizedCallback(String(hash)); - } else { - throw new Error(AlertMsg.MINIMUM_BALANCE); + if (!isBalanceEnough) { + throw new Error(AlertMsg.MINIMUM_BALANCE); + } } + + const transaction = await this.AssetsRepository.getNativeTransferCall(param); + const hash = await this.wallet.signAndSend({ + extrinsic: transaction, + senderAddress: param.senderAddress, + successMessage: param.successMessage, + }); + param.finalizedCallback(String(hash)); } public async transferEvmAsset(param: ParamEvmTransfer): Promise { From 7c28c9ae426439da8bfcd62deddb4e947afd40ba Mon Sep 17 00:00:00 2001 From: impelcrypto Date: Thu, 19 Dec 2024 16:00:24 +0800 Subject: [PATCH 2/3] fix: refactoring --- src/v2/services/implementations/AssetsService.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/src/v2/services/implementations/AssetsService.ts b/src/v2/services/implementations/AssetsService.ts index ba5c6e1c3..20d1f3ea5 100644 --- a/src/v2/services/implementations/AssetsService.ts +++ b/src/v2/services/implementations/AssetsService.ts @@ -1,6 +1,5 @@ import { ethers } from 'ethers'; import { inject, injectable } from 'inversify'; -import { is } from 'quasar'; import { endpointKey } from 'src/config/chainEndpoints'; import { LOCAL_STORAGE } from 'src/config/localStorage'; import { handleCheckProviderChainId } from 'src/config/web3'; From a60c78c9ebd58c736b53cd18e81b81967df0d974 Mon Sep 17 00:00:00 2001 From: impelcrypto Date: Thu, 19 Dec 2024 18:24:33 +0800 Subject: [PATCH 3/3] fix: refactoring --- .../services/implementations/AssetsService.ts | 18 ++++++++---------- 1 file changed, 8 insertions(+), 10 deletions(-) diff --git a/src/v2/services/implementations/AssetsService.ts b/src/v2/services/implementations/AssetsService.ts index 20d1f3ea5..f7d5da6c5 100644 --- a/src/v2/services/implementations/AssetsService.ts +++ b/src/v2/services/implementations/AssetsService.ts @@ -30,7 +30,7 @@ export class AssetsService implements IAssetsService { constructor( @inject(Symbols.AssetsRepository) - private AssetsRepository: IAssetsRepository, + private assetsRepository: IAssetsRepository, @inject(Symbols.WalletFactory) walletFactory: () => IWalletService, @inject(Symbols.CurrentWallet) private currentWallet: string, @inject(Symbols.EventAggregator) readonly eventAggregator: IEventAggregator @@ -42,18 +42,16 @@ export class AssetsService implements IAssetsService { const isNativeToken = param.assetId === idAstarNativeToken; // Memo: Check if the native token's remaining balance is enough to pay the transaction fee if (isNativeToken) { - const useableBalance = await this.AssetsRepository.getNativeBalance(param.senderAddress); - const isBalanceEnough = - Number(ethers.utils.formatEther(useableBalance)) - - Number(ethers.utils.formatEther(param.amount)) > - REQUIRED_MINIMUM_BALANCE; + const useableBalance = await this.assetsRepository.getNativeBalance(param.senderAddress); + const requiredBalance = ethers.utils.parseEther(String(REQUIRED_MINIMUM_BALANCE)).toBigInt(); + const isBalanceEnough = BigInt(useableBalance) - BigInt(param.amount) > requiredBalance; if (!isBalanceEnough) { throw new Error(AlertMsg.MINIMUM_BALANCE); } } - const transaction = await this.AssetsRepository.getNativeTransferCall(param); + const transaction = await this.assetsRepository.getNativeTransferCall(param); const hash = await this.wallet.signAndSend({ extrinsic: transaction, senderAddress: param.senderAddress, @@ -84,7 +82,7 @@ export class AssetsService implements IAssetsService { const isBalanceEnough = useableBalance - amount > minBal; if (isBalanceEnough) { - const rawTx = await this.AssetsRepository.getEvmTransferData({ + const rawTx = await this.assetsRepository.getEvmTransferData({ param, web3, }); @@ -105,7 +103,7 @@ export class AssetsService implements IAssetsService { } public async evmWithdraw({ amount, senderAddress }: ParamEvmWithdraw): Promise { - const transaction = await this.AssetsRepository.getEvmWithdrawCall({ amount, senderAddress }); + const transaction = await this.assetsRepository.getEvmWithdrawCall({ amount, senderAddress }); await this.wallet.signAndSend({ extrinsic: transaction, senderAddress, @@ -113,7 +111,7 @@ export class AssetsService implements IAssetsService { } public async unlockVestingTokens(senderAddress: string): Promise { - const transaction = await this.AssetsRepository.getVestCall(); + const transaction = await this.assetsRepository.getVestCall(); await this.wallet.signAndSend({ extrinsic: transaction, senderAddress,