Skip to content

Commit

Permalink
fix: modified logic for checking the native token's remaining balance (
Browse files Browse the repository at this point in the history
…#1429)

* fix: modified balance checking logic

* fix: refactoring

* fix: refactoring
  • Loading branch information
impelcrypto authored Dec 30, 2024
1 parent 7cb4b0e commit f8dcfe1
Showing 1 changed file with 22 additions and 19 deletions.
41 changes: 22 additions & 19 deletions src/v2/services/implementations/AssetsService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,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';
Expand All @@ -29,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
Expand All @@ -38,23 +39,25 @@ export class AssetsService implements IAssetsService {
}

public async transferNativeAsset(param: ParamAssetTransfer): Promise<void> {
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 requiredBalance = ethers.utils.parseEther(String(REQUIRED_MINIMUM_BALANCE)).toBigInt();
const isBalanceEnough = BigInt(useableBalance) - BigInt(param.amount) > requiredBalance;

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<void> {
Expand All @@ -79,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,
});
Expand All @@ -100,15 +103,15 @@ export class AssetsService implements IAssetsService {
}

public async evmWithdraw({ amount, senderAddress }: ParamEvmWithdraw): Promise<void> {
const transaction = await this.AssetsRepository.getEvmWithdrawCall({ amount, senderAddress });
const transaction = await this.assetsRepository.getEvmWithdrawCall({ amount, senderAddress });
await this.wallet.signAndSend({
extrinsic: transaction,
senderAddress,
});
}

public async unlockVestingTokens(senderAddress: string): Promise<void> {
const transaction = await this.AssetsRepository.getVestCall();
const transaction = await this.assetsRepository.getVestCall();
await this.wallet.signAndSend({
extrinsic: transaction,
senderAddress,
Expand Down

0 comments on commit f8dcfe1

Please sign in to comment.