Skip to content

Commit

Permalink
fix: commit button doesn't show with offers on Amoy when price > 0 an… (
Browse files Browse the repository at this point in the history
  • Loading branch information
levalleux-ludo committed Apr 16, 2024
1 parent 9c2585e commit c73e925
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 34 deletions.
21 changes: 16 additions & 5 deletions src/lib/utils/hooks/defaultTokenList/useTokenBalances.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,20 @@ import { useProvider } from "../connection/connection";
type UseTokensBalancesProps = {
address: string | undefined | null;
chainId: number | undefined | null;
tokens?: SDKToken[] | undefined | null;
};
// TODO: Token should be exported by core-sdk / react-kit
type SDKToken = {
symbol: string;
name: string;
address: string;
decimals: string;
};

export const useTokenBalances = ({
address,
chainId
chainId,
tokens
}: UseTokensBalancesProps) => {
const {
config: {
Expand All @@ -20,13 +30,14 @@ export const useTokenBalances = ({
const coreSDK = useCoreSDK();
const provider = useProvider();
return useQuery(
["use-token-balances", address, chainId, defaultTokens],
["use-token-balances", address, chainId, tokens, defaultTokens],
async () => {
if (!address || !chainId || !defaultTokens?.length) {
tokens = tokens || defaultTokens;
if (!address || !chainId || !tokens?.length) {
return;
}

const promises = defaultTokens.map((token) => {
const promises = tokens.map((token) => {
const isNative = token.address === ethers.constants.AddressZero;
if (isNative) {
return provider?.getBalance(address);
Expand All @@ -37,7 +48,7 @@ export const useTokenBalances = ({
});
});
const balances = await Promise.all(promises);
const tokenBalance = defaultTokens.map((token, index) => {
const tokenBalance = tokens.map((token, index) => {
const balance = balances[index]?.toString();
return {
...token,
Expand Down
46 changes: 17 additions & 29 deletions src/lib/utils/hooks/useCurrencyBalance.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,7 @@
import { Interface } from "@ethersproject/abi";
import { Currency, CurrencyAmount, Token } from "@uniswap/sdk-core";
import ERC20ABI from "abis/erc20.json";
import { Erc20Interface } from "abis/types/Erc20";
import JSBI from "jsbi";
import {
useMultipleContractSingleData,
useSingleContractMultipleData
} from "lib/utils/hooks/multicall";
import { useTokenBalances as useTokenBalancesSDK } from "lib/utils/hooks/defaultTokenList/useTokenBalances";
import { useSingleContractMultipleData } from "lib/utils/hooks/multicall";
import { useInterfaceMulticall } from "lib/utils/hooks/useContract";
import { useMemo } from "react";

Expand Down Expand Up @@ -60,9 +55,6 @@ export function useNativeCurrencyBalances(
);
}

const ERC20Interface = new Interface(ERC20ABI) as Erc20Interface;
const tokenBalancesGasRequirement = { gasRequired: 185_000 };

/**
* Returns a map of token addresses to their eventually consistent token balances for a single account.
*/
Expand All @@ -82,25 +74,21 @@ export function useTokenBalancesWithLoadingIndicator(
) ?? [],
[chainId, tokens]
);
const validatedTokenAddresses = useMemo(
() => validatedTokens.map((vt) => vt.address),
[validatedTokens]
);

const balances = useMultipleContractSingleData(
const balances = useTokenBalancesSDK({
chainId,
0, // not used
validatedTokenAddresses,
ERC20Interface,
"balanceOf",
useMemo(() => [address], [address]),
tokenBalancesGasRequirement
);

const anyLoading: boolean = useMemo(
() => balances.some((callState) => callState.loading),
[balances]
);
address,
tokens: validatedTokens.map((t) => {
return {
symbol: t.symbol || "",
name: t.name || "",
address: t.address,
decimals: t.decimals.toString()
};
})
});

const anyLoading: boolean = useMemo(() => balances.isLoading, [balances]);

const result = useMemo(
() =>
Expand All @@ -109,8 +97,8 @@ export function useTokenBalancesWithLoadingIndicator(
? validatedTokens.reduce<{
[tokenAddress: string]: CurrencyAmount<Token> | undefined;
}>((memo, token, i) => {
const value = balances?.[i]?.result?.[0];
const amount = value ? JSBI.BigInt(value.toString()) : undefined;
const value = balances?.data?.[i]?.balance;
const amount = value ? JSBI.BigInt(value) : undefined;
if (amount) {
memo[token.address] = CurrencyAmount.fromRawAmount(
token,
Expand Down

0 comments on commit c73e925

Please sign in to comment.