Skip to content

Commit

Permalink
refactor(tangle-dapp): Get rid of Liquifier remenants
Browse files Browse the repository at this point in the history
  • Loading branch information
yurixander committed Oct 3, 2024
1 parent 3f064fa commit 081cdbb
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 98 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -78,16 +78,11 @@ const COLUMNS = [
COLUMN_HELPER.accessor('unlockId', {
header: () => <HeaderCell title="Unlock ID" className="justify-start" />,
cell: (props) => {
const canSelect =
props.row.original.type === 'liquifierUnlockNft'
? props.row.original.progress === 1
: true;

return (
<div className="flex items-center justify-start gap-2">
<CheckBox
isChecked={props.row.getIsSelected()}
isDisabled={!props.row.getCanSelect() || !canSelect}
isDisabled={!props.row.getCanSelect()}
onChange={props.row.getToggleSelectedHandler()}
wrapperClassName="pt-0.5 flex items-center justify-center"
/>
Expand Down Expand Up @@ -137,11 +132,7 @@ const COLUMNS = [
header: () => <HeaderCell title="Amount" className="justify-center" />,
cell: (props) => {
const unstakeRequest = props.row.original;

const tokenSymbol =
unstakeRequest.type === 'parachainUnstakeRequest'
? unstakeRequest.currency.toUpperCase()
: unstakeRequest.symbol;
const tokenSymbol = unstakeRequest.currency.toUpperCase();

return (
<TokenAmountCell
Expand Down Expand Up @@ -255,9 +246,7 @@ const UnstakeRequestsTable: FC = () => {

// If the remaining time unit is undefined, it means that the
// request has completed its unlocking period.
return request.type === 'parachainUnstakeRequest'
? request.progress === undefined
: request.progress === 1;
return request.progress === undefined;
});
}, [selectedRowsUnlockIds, rows]);

Expand Down
86 changes: 2 additions & 84 deletions apps/tangle-dapp/data/liquidStaking/useLsExchangeRate.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,14 @@
import { BN } from '@polkadot/util';
import { TANGLE_RESTAKING_PARACHAIN_LOCAL_DEV_NETWORK } from '@webb-tools/webb-ui-components/constants/networks';
import { useCallback, useEffect, useMemo, useState } from 'react';
import { erc20Abi } from 'viem';
import { useCallback, useMemo, useState } from 'react';

import LIQUIFIER_ADAPTER_ABI from '../../constants/liquidStaking/liquifierAdapterAbi';
import LIQUIFIER_TG_TOKEN_ABI from '../../constants/liquidStaking/liquifierTgTokenAbi';
import {
LsNetworkId,
LsParachainCurrencyKey,
} from '../../constants/liquidStaking/types';
import useApiRx from '../../hooks/useApiRx';
import calculateBnRatio from '../../utils/calculateBnRatio';
import getLsProtocolDef from '../../utils/liquidStaking/getLsProtocolDef';
import useContractRead from '../evm/useContractRead';
import { ContractReadOptions } from '../evm/useContractReadOnce';
import { useLsStore } from './useLsStore';
import usePolling from './usePolling';

Expand Down Expand Up @@ -83,72 +78,10 @@ const useLsExchangeRate = (type: ExchangeRateType) => {
return computeExchangeRate(type, tokenPoolAmount, lstTotalIssuance);
}, [lstTotalIssuance, tokenPoolAmount, type]);

const getTgTokenTotalSupplyOptions = useCallback((): ContractReadOptions<
typeof LIQUIFIER_TG_TOKEN_ABI,
'totalSupply'
> | null => {
if (protocol.networkId !== LsNetworkId.ETHEREUM_MAINNET_LIQUIFIER) {
return null;
}

return {
address: protocol.erc20TokenAddress,
functionName: 'totalSupply',
args: [],
};
}, [protocol]);

const getLiquifierTotalSharesOptions = useCallback((): ContractReadOptions<
typeof LIQUIFIER_ADAPTER_ABI,
'totalShares'
> | null => {
if (protocol.networkId !== LsNetworkId.ETHEREUM_MAINNET_LIQUIFIER) {
return null;
}

return {
address: protocol.liquifierContractAddress,
functionName: 'totalShares',
args: [],
};
}, [protocol]);

const {
value: tgTokenTotalSupply,
setIsPaused: setIsTgTokenTotalSupplyPaused,
} = useContractRead(erc20Abi, getTgTokenTotalSupplyOptions);

const {
value: liquifierTotalShares,
setIsPaused: setIsLiquifierTotalSharesPaused,
} = useContractRead(LIQUIFIER_ADAPTER_ABI, getLiquifierTotalSharesOptions);

const fetchLiquifierExchangeRate = useCallback(async () => {
// Propagate error or loading states.
if (typeof tgTokenTotalSupply !== 'bigint') {
return tgTokenTotalSupply;
} else if (typeof liquifierTotalShares !== 'bigint') {
return liquifierTotalShares;
}

const tgTokenTotalSupplyBn = new BN(tgTokenTotalSupply.toString());
const liquifierTotalSharesBn = new BN(liquifierTotalShares.toString());

return computeExchangeRate(
type,
tgTokenTotalSupplyBn,
liquifierTotalSharesBn,
);
}, [liquifierTotalShares, tgTokenTotalSupply, type]);

const fetch = useCallback(async () => {
let promise: Promise<number | Error | null>;

switch (selectedNetworkId) {
case LsNetworkId.ETHEREUM_MAINNET_LIQUIFIER:
promise = fetchLiquifierExchangeRate();

break;
case LsNetworkId.TANGLE_RESTAKING_PARACHAIN:
promise = parachainExchangeRate;

Expand All @@ -170,22 +103,7 @@ const useLsExchangeRate = (type: ExchangeRateType) => {
}

setExchangeRate(newExchangeRate);
}, [fetchLiquifierExchangeRate, parachainExchangeRate, selectedNetworkId]);

// Pause or resume ERC20-based exchange rate fetching based
// on whether the requested protocol is a parachain or an ERC20 token.
// This helps prevent unnecessary requests.
useEffect(() => {
const isPaused =
protocol.networkId === LsNetworkId.TANGLE_RESTAKING_PARACHAIN;

setIsTgTokenTotalSupplyPaused(isPaused);
setIsLiquifierTotalSharesPaused(isPaused);
}, [
protocol.networkId,
setIsLiquifierTotalSharesPaused,
setIsTgTokenTotalSupplyPaused,
]);
}, [parachainExchangeRate, selectedNetworkId]);

const isRefreshing = usePolling({ effect: fetch });

Expand Down

0 comments on commit 081cdbb

Please sign in to comment.