Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

dev-getBalance-change-priority #935

Open
wants to merge 2 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions Models/Balance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ export type Gas = {
}

export type BalanceProvider = {
getBalance: ({ network, token, address }: BalanceProps) => Promise<Balance | undefined | null> | Balance | undefined | void,
getNetworkBalances: (props: NetworkBalancesProps) => Promise<Balance[] | undefined> | Balance[] | undefined | void,
getGas?: (props: GasProps) => Promise<Gas[] | undefined> | undefined | void,
getBalance: ({ network, token, address }: BalanceProps) => Promise<Balance | undefined | null> | Balance | undefined,
getNetworkBalances: (props: NetworkBalancesProps) => Promise<Balance[] | undefined> | Balance[] | undefined,
getGas?: (props: GasProps) => Promise<Gas[] | undefined> | undefined,
supportedNetworks: string[],
}
62 changes: 37 additions & 25 deletions components/Input/dynamic/MinMax.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ import useBalance from "../../../hooks/useBalance";
import { useFee } from "../../../context/feeContext";
import { useBalancesState } from "../../../context/balances";
import { useQueryState } from "../../../context/query";
import { Token } from "../../../Models/Network";
import { QueryParams } from "../../../Models/QueryParams";
import { Balance } from "../../../Models/Balance";

const MinMax = ({ onAddressGet }: { onAddressGet: (address: string) => void }) => {

Expand All @@ -21,7 +24,7 @@ const MinMax = ({ onAddressGet }: { onAddressGet: (address: string) => void }) =
return from && getProvider(from)
}, [from, getProvider])

const { fetchNetworkBalances, fetchGas } = useBalance()
const { fetchBalance, fetchGas } = useBalance()

const wallet = provider?.getConnectedWallet()

Expand All @@ -33,37 +36,18 @@ const MinMax = ({ onAddressGet }: { onAddressGet: (address: string) => void }) =
const walletBalance = wallet && balances[wallet.address]?.find(b => b?.network === from?.name && b?.token === fromCurrency?.symbol)
const native_currency = from?.token

let maxAllowedAmount: number | null = maxAmountFromApi || 0
if (query.balances && fromCurrency) {
try {
const balancesFromQueries = new URL(window.location.href.replaceAll('&quot;', '"')).searchParams.get('balances');
const parsedBalances = balancesFromQueries && JSON.parse(balancesFromQueries)
let balancesTyped = parsedBalances
if (balancesTyped && balancesTyped[fromCurrency.symbol] && balancesTyped[fromCurrency.symbol] > Number(minAllowedAmount)) {
maxAllowedAmount = Math.min(maxAllowedAmount, balancesTyped[fromCurrency.symbol]);
}
}
// in case the query parameter had bad formatting just ignoe
catch { }
} else if (walletBalance && (walletBalance.amount >= Number(minAllowedAmount) && walletBalance.amount <= Number(maxAmountFromApi))) {
if (((native_currency?.symbol === fromCurrency?.symbol) || !native_currency) && ((walletBalance.amount - gasAmount) >= Number(minAllowedAmount) && (walletBalance.amount - gasAmount) <= Number(maxAmountFromApi))) {
maxAllowedAmount = walletBalance.amount - gasAmount
}
else maxAllowedAmount = walletBalance.amount
}
else {
maxAllowedAmount = Number(maxAmountFromApi) || 0
}
const maxAllowedAmount = calculateMaxAmount(maxAmountFromApi, query, fromCurrency, minAllowedAmount, walletBalance, gasAmount, native_currency)

const handleSetMaxAmount = useCallback(async () => {
setFieldValue('amount', maxAllowedAmount);
from && fetchNetworkBalances(from);
const balance = from && native_currency && await fetchBalance(from, native_currency);
const maxAmount = calculateMaxAmount(maxAmountFromApi, query, fromCurrency, minAllowedAmount, balance, gasAmount, native_currency)
setFieldValue('amount', maxAmount);

from &&
fromCurrency &&
amount && fetchGas(from, fromCurrency, destination_address || "");

}, [from, fromCurrency, destination_address, maxAllowedAmount])
}, [from, fromCurrency, destination_address, minAllowedAmount, fetchBalance, fetchGas, setFieldValue, amount, query, gasAmount, native_currency])

useEffect(() => {
wallet?.address && onAddressGet(wallet.address)
Expand All @@ -85,4 +69,32 @@ const MinMax = ({ onAddressGet }: { onAddressGet: (address: string) => void }) =
)
}


const calculateMaxAmount = (maxAmountFromApi: number | undefined, query: QueryParams, fromCurrency: Token | undefined, minAllowedAmount: number | undefined, walletBalance: Balance | undefined | null, gasAmount: number, native_currency: Token | undefined) => {
let maxAllowedAmount: number | null = maxAmountFromApi || 0

if (query.balances && fromCurrency) {
try {
const balancesFromQueries = new URL(window.location.href.replaceAll('&quot;', '"')).searchParams.get('balances');
const parsedBalances = balancesFromQueries && JSON.parse(balancesFromQueries)
let balancesTyped = parsedBalances
if (balancesTyped && balancesTyped[fromCurrency.symbol] && balancesTyped[fromCurrency.symbol] > Number(minAllowedAmount)) {
maxAllowedAmount = Math.min(maxAllowedAmount, balancesTyped[fromCurrency.symbol]);
}
}
// in case the query parameter had bad formatting just ignoe
catch { }
} else if (walletBalance && (walletBalance.amount >= Number(minAllowedAmount) && walletBalance.amount <= Number(maxAmountFromApi))) {
if (((native_currency?.symbol === fromCurrency?.symbol) || !native_currency) && ((walletBalance.amount - gasAmount) >= Number(minAllowedAmount) && (walletBalance.amount - gasAmount) <= Number(maxAmountFromApi))) {
maxAllowedAmount = walletBalance.amount - gasAmount
}
else maxAllowedAmount = walletBalance.amount
}
else {
maxAllowedAmount = Number(maxAmountFromApi) || 0
}

return maxAllowedAmount
}

export default MinMax
6 changes: 4 additions & 2 deletions hooks/useBalance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,10 +83,12 @@ export default function useBalanceProvider() {
network: network,
address: address,
token
}) || []
})

setAllBalances((data) => ({ ...data, [address]: filteredBalances?.concat(balance) }))
setAllBalances((data) => ({ ...data, [address]: filteredBalances?.concat(balance || []) }))
setIsBalanceLoading(false)

return balance
}
}

Expand Down