Skip to content

Commit

Permalink
Merge pull request #302 from MVPWorkshop/298-show-balance-for-the-tok…
Browse files Browse the repository at this point in the history
…en-we-swap-into

refactor: add balances for assets we want swap if have it in wallet
  • Loading branch information
ra79sta authored Oct 27, 2023
2 parents bf758db + c1dda20 commit 8da14a1
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 24 deletions.
2 changes: 1 addition & 1 deletion src/components/molecule/TokenAmountInput/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ const TokenAmountInput = ({
</div>
<div className="flex w-full justify-end pr-1 text-medium text-gray-200">
Balance:{" "}
{tokenId && tokenText
{tokenId && tokenText && Number(tokenBalance) !== 0
? formatDecimalsFromToken(Number(tokenBalance?.replace(/[, ]/g, "")), tokenDecimals as string)
: tokenBalance || 0}
</div>
Expand Down
20 changes: 8 additions & 12 deletions src/components/organism/SwapSelectTokenModal/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ interface SwapSelectTokenModalProps {
title: string;
tokensData: TokenProps[];
selected: TokenProps;
isWalletTokens: boolean;
onClose: () => void;
onSelect: (tokenData: TokenProps) => void;
}
Expand All @@ -27,7 +26,6 @@ const SwapSelectTokenModal: FC<SwapSelectTokenModalProps> = ({
title,
tokensData,
selected,
isWalletTokens,
onClose,
onSelect,
}) => {
Expand Down Expand Up @@ -85,16 +83,14 @@ const SwapSelectTokenModal: FC<SwapSelectTokenModalProps> = ({
</div>
</div>
<div className="flex gap-1">
{isWalletTokens && (
<div className="text-[12px] group-hover:text-white">
{item.tokenId
? formatDecimalsFromToken(
Number(item.tokenAsset.balance.replace(/[, ]/g, "")),
item.assetTokenMetadata.decimals
)
: item.tokenAsset.balance}
</div>
)}
<div className="text-[12px] group-hover:text-white">
{item.tokenId && item.tokenAsset.balance !== 0
? formatDecimalsFromToken(
Number(item.tokenAsset.balance.replace(/[, ]/g, "")),
item.assetTokenMetadata.decimals
)
: item.tokenAsset.balance}
</div>
{item.tokenId === selected.tokenId ? <CheckIcon /> : null}
</div>
</div>
Expand Down
22 changes: 15 additions & 7 deletions src/components/organism/SwapTokens/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -659,9 +659,16 @@ const SwapTokens = () => {
const poolLiquidTokens: any = [nativeToken]
.concat(poolsTokenMetadata)
?.filter((item: any) => item.tokenId !== selectedTokens.tokenA?.tokenId);

setAvailablePoolTokenB(poolLiquidTokens);

if (tokenBalances !== null) {
for (const item of poolLiquidTokens) {
for (const walletAsset of tokenBalances.assets) {
if (item.tokenId === walletAsset.tokenId) {
item.tokenAsset.balance = walletAsset.tokenAsset.balance;
}
}
}
setAvailablePoolTokenB(poolLiquidTokens);
}
return poolLiquidTokens;
};

Expand Down Expand Up @@ -923,18 +930,21 @@ const SwapTokens = () => {
tokenValue={selectedTokenAValue?.tokenValue}
onClick={() => fillTokenPairsAndOpenModal(TokenSelection.TokenA)}
onSetTokenValue={(value) => tokenAValue(value.toString())}
disabled={!selectedAccount || swapLoading || !tokenBalances?.assets}
disabled={!selectedAccount || swapLoading || !tokenBalances?.assets || poolsTokenMetadata.length === 0}
assetLoading={assetLoading}
/>

<TokenAmountInput
tokenText={selectedTokens.tokenB?.tokenSymbol}
tokenBalance={selectedTokens.tokenB?.tokenBalance}
tokenId={selectedTokens.tokenB?.tokenId}
tokenDecimals={selectedTokens.tokenB?.decimals}
labelText={t("tokenAmountInput.youReceive")}
tokenIcon={<DotToken />}
tokenValue={selectedTokenBValue?.tokenValue}
onClick={() => fillTokenPairsAndOpenModal(TokenSelection.TokenB)}
onSetTokenValue={(value) => tokenBValue(value.toString())}
disabled={!selectedAccount || swapLoading || !tokenBalances?.assets}
disabled={!selectedAccount || swapLoading || !tokenBalances?.assets || poolsTokenMetadata.length === 0}
assetLoading={assetLoading}
/>

Expand Down Expand Up @@ -1005,7 +1015,6 @@ const SwapTokens = () => {
onSwapSelectModal(tokenData);
}}
selected={selectedTokens.tokenA}
isWalletTokens={true}
/>

<SwapSelectTokenModal
Expand All @@ -1018,7 +1027,6 @@ const SwapTokens = () => {
onSwapSelectModal(tokenData);
}}
selected={selectedTokens.tokenB}
isWalletTokens={false}
/>

<Button
Expand Down
16 changes: 13 additions & 3 deletions src/services/polkadotWalletServices/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@ import type { AnyJson } from "@polkadot/types/types/codec";
import dotAcpToast from "../../app/util/toast";
import { Dispatch } from "react";
import { WalletAction } from "../../store/wallet/interface";
import { PoolAction } from "../../store/pools/interface";
import { ActionType } from "../../app/types/enum";
import "@polkadot/api-augment";
import { TokenBalanceData } from "../../app/types";
import { getWalletBySource, getWallets } from "@talismn/connect-wallets";
import type { Wallet, WalletAccount } from "@talismn/connect-wallets";
import LocalStorage from "../../app/util/localStorage";
import { formatDecimalsFromToken } from "../../app/util/helper";
import { getAllLiquidityPoolsTokensMetadata } from "../poolServices";

export const setupPolkadotApi = async () => {
const wsProvider = new WsProvider(import.meta.env.VITE_NETWORK_RPC_URL);
Expand Down Expand Up @@ -97,10 +99,17 @@ export const getSupportedWallets = () => {
return supportedWallets;
};

export const setTokenBalance = async (dispatch: Dispatch<WalletAction>, api: any, selectedAccount: WalletAccount) => {
export const setTokenBalance = async (
dispatch: Dispatch<WalletAction | PoolAction>,
api: any,
selectedAccount: WalletAccount
) => {
if (api) {
dispatch({ type: ActionType.SET_ASSET_LOADING, payload: true });
try {
const poolsTokenMetadata = await getAllLiquidityPoolsTokensMetadata(api);
dispatch({ type: ActionType.SET_POOLS_TOKEN_METADATA, payload: poolsTokenMetadata });

const walletTokens: any = await getWalletTokensBalance(api, selectedAccount?.address);

dispatch({ type: ActionType.SET_TOKEN_BALANCES, payload: walletTokens });
Expand Down Expand Up @@ -227,15 +236,16 @@ export const setTokenBalanceAfterAssetsSwapUpdate = async (
return updatedTokensInfo;
};

export const handleDisconnect = (dispatch: Dispatch<WalletAction>) => {
export const handleDisconnect = (dispatch: Dispatch<WalletAction | PoolAction>) => {
LocalStorage.remove("wallet-connected");
dispatch({ type: ActionType.SET_ACCOUNTS, payload: [] });
dispatch({ type: ActionType.SET_SELECTED_ACCOUNT, payload: {} as WalletAccount });
dispatch({ type: ActionType.SET_TOKEN_BALANCES, payload: {} as TokenBalanceData });
dispatch({ type: ActionType.SET_POOLS_TOKEN_METADATA, payload: [] });
};

export const connectWalletAndFetchBalance = async (
dispatch: Dispatch<WalletAction>,
dispatch: Dispatch<WalletAction | PoolAction>,
api: any,
account: WalletAccount
) => {
Expand Down
2 changes: 1 addition & 1 deletion src/services/swapServices/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -382,8 +382,8 @@ export const swapAssetForAssetExactOut = async (

const result = api.tx.assetConversion.swapTokensForExactTokens(
[firstArg, secondArg, thirdArg],
assetTokenAValue,
assetTokenBValue,
assetTokenAValue,
account.address,
false
);
Expand Down

1 comment on commit 8da14a1

@vercel
Copy link

@vercel vercel bot commented on 8da14a1 Oct 27, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.