Skip to content

Commit

Permalink
Merge branch 'dev' into fix/telegram_api
Browse files Browse the repository at this point in the history
  • Loading branch information
tuul-wq authored Oct 4, 2024
2 parents 3ea6860 + b8ed2e1 commit cbd3f7f
Show file tree
Hide file tree
Showing 131 changed files with 4,970 additions and 4,286 deletions.
72 changes: 24 additions & 48 deletions app/common/_temp_hooks/useAmountLogic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,21 @@ import { useEffect, useState } from 'react';

import { BN, BN_ZERO } from '@polkadot/util';

import { TransactionType, useExtrinsic } from '@/common/extrinsicService';
import { useQueryService } from '@/common/queryService/QueryService';
import { type IBalance, type ITransfer } from '@/shared/api/types';
import { toPreciseBalance } from '@/shared/helpers';
import { useOrml } from '@/shared/hooks';
import { useAssetHub } from '@/shared/hooks/useAssetHub';
import type { Asset, Balance, OrmlAsset, StatemineAsset } from '@/types/substrate';
import { type Asset, type Balance } from '@/types/substrate';

type AmountLogicParams = {
chainId: ChainId;
services: {
transferService: ITransfer;
balanceService: IBalance;
};
asset: Asset;
balance?: Balance;
isGift: boolean;
};

export const useAmountLogic = ({ chainId, asset, balance, isGift }: AmountLogicParams) => {
const { getAssetHubFee } = useAssetHub();
const { getOrmlFee } = useOrml();
const { getTransactionFee } = useExtrinsic();
const { getExistentialDeposit } = useQueryService();

export const useAmountLogic = ({ services, asset, balance, isGift }: AmountLogicParams) => {
const [fee, setFee] = useState(BN_ZERO);
const [amount, setAmount] = useState(BN_ZERO);
const [deposit, setDeposit] = useState(BN_ZERO);
Expand All @@ -33,24 +28,29 @@ export const useAmountLogic = ({ chainId, asset, balance, isGift }: AmountLogicP
const [isTransferAll, setIsTransferAll] = useState(false);
const [isAmountValid, setIsAmountValid] = useState(true);

const { balanceService, transferService } = services;

useEffect(() => {
if (amount.isZero()) return;

setPending(true);
getTransferDetails(chainId, asset, amount)
.then(({ fee, deposit }) => {
const feeParams = { amount, transferAll: isTransferAll };

Promise.all([
isGift ? transferService.getGiftTransferFee(feeParams) : transferService.getTransferFee(feeParams),
balanceService.getExistentialDeposit(),
])
.then(([fee, deposit]) => {
setFee(fee);
setDeposit(deposit);
})
.finally(() => {
setPending(false);
});
}, [amount]);
.finally(() => setPending(false));
}, [amount, isTransferAll]);

useEffect(() => {
if (!asset) return;

getMaxAmount(chainId, asset, balance?.transferable)
getMaxAmount(balance?.transferable)
.then(setMaxAmount)
.finally(() => setIsMaxPending(false));
}, []);
Expand All @@ -64,43 +64,19 @@ export const useAmountLogic = ({ chainId, asset, balance, isGift }: AmountLogicP
setIsAmountValid(!amount.isZero() && isUnderMax && (isTransferAll || isOverDeposit));
}, [isTransferAll, maxAmount, amount, deposit, isTouched]);

const getFeeAmount = (chainId: ChainId, asset: Asset, transferAmount: BN): Promise<BN> => {
const FEE_BY_TYPE: Record<Asset['type'], () => Promise<BN>> = {
// TODO: refactor to a better TX service
native: async () => {
const fee = await getTransactionFee(chainId, TransactionType.TRANSFER, transferAmount);

return isGift ? fee.muln(2) : fee;
},
statemine: () => getAssetHubFee(chainId, asset as StatemineAsset, transferAmount, isGift),
orml: () => getOrmlFee(chainId, asset as OrmlAsset, transferAmount, isGift),
};

return FEE_BY_TYPE[asset.type]();
};

const getMaxAmount = async (chainId: ChainId, asset: Asset, transferable = BN_ZERO): Promise<BN> => {
const fee = await getFeeAmount(chainId, asset, transferable);
const getMaxAmount = async (transferable = BN_ZERO): Promise<BN> => {
const feeParams = { amount: transferable, transferAll: true };
const fee = isGift
? await transferService.getGiftTransferFee(feeParams)
: await transferService.getTransferFee(feeParams);

return BN.max(transferable.sub(fee), BN_ZERO);
};

const getTransferDetails = async (
chainId: ChainId,
asset: Asset,
transferAmount: BN,
): Promise<{ fee: BN; deposit: BN }> => {
const deposit = await getExistentialDeposit(chainId, asset);
const fee = await getFeeAmount(chainId, asset, transferAmount);

return { fee, deposit };
};

const getIsAccountToBeReaped = (): boolean => {
if (amount.isZero() || fee.isZero() || !isTouched || isTransferAll) return false;

// We don't add fee to the amount because maxAmount is already subtracted by fee
// Look into getMaxAmount for details
return maxAmount.sub(amount).lt(deposit);
};

Expand Down
67 changes: 0 additions & 67 deletions app/common/extrinsicService/ExtrinsicBuilder.ts

This file was deleted.

51 changes: 0 additions & 51 deletions app/common/extrinsicService/ExtrinsicProvider.tsx

This file was deleted.

52 changes: 0 additions & 52 deletions app/common/extrinsicService/ExtrinsicService.ts

This file was deleted.

5 changes: 0 additions & 5 deletions app/common/extrinsicService/index.ts

This file was deleted.

57 changes: 0 additions & 57 deletions app/common/extrinsicService/types/index.ts

This file was deleted.

Loading

0 comments on commit cbd3f7f

Please sign in to comment.