Skip to content

Commit

Permalink
fix: qa review request, show only one balance (#352)
Browse files Browse the repository at this point in the history
  • Loading branch information
khanti42 authored Sep 11, 2024
1 parent ac41722 commit fdbeeb2
Show file tree
Hide file tree
Showing 8 changed files with 13 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ export default {
const asset = {
address: '0x049d36570d4e46f48e99674bd3fcc84644ddd6b96f7c741b1562b82f9e004dc7',
amount: BigNumber.from('1000000000000000000'),
spendableAmount: BigNumber.from('1000000000000000000'),
chainId: constants.StarknetChainId.SN_SEPOLIA,
decimals: 18,
name: 'Ether',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ export const AmountInputView = ({
const handleMaxClick = () => {
if (inputRef.current && asset.usdPrice) {
const amountStr = ethers.utils
.formatUnits(asset.spendableAmount, asset.decimals)
.formatUnits(asset.amount, asset.decimals)
.toString();
const amountFloat = parseFloat(amountStr);
inputRef.current.value = usdMode
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ export default {
const asset: Erc20TokenBalance = {
address: '0x049d36570d4e46f48e99674bd3fcc84644ddd6b96f7c741b1562b82f9e004dc7',
amount: BigNumber.from('1000000000000000000'),
spendableAmount: BigNumber.from('1000000000000000000'),
chainId: constants.StarknetChainId.SN_SEPOLIA,
decimals: 18,
name: 'Ether',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export const HeaderView = ({ address }: Props) => {
const getUSDValue = () => {
const amountFloat = parseFloat(
ethers.utils.formatUnits(
wallet.erc20TokenBalanceSelected.spendableAmount,
wallet.erc20TokenBalanceSelected.amount,
wallet.erc20TokenBalanceSelected.decimals,
),
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ export const SendModalView = ({ closeModal }: Props) => {
fieldValue,
wallet.erc20TokenBalanceSelected.decimals,
);
const userBalance = wallet.erc20TokenBalanceSelected.spendableAmount;
const userBalance = wallet.erc20TokenBalanceSelected.amount;
if (inputAmount.gt(userBalance)) {
setErrors((prevErrors) => ({
...prevErrors,
Expand Down
6 changes: 2 additions & 4 deletions packages/wallet-ui/src/services/useStarkNetSnap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -712,15 +712,13 @@ export const useStarkNetSnap = () => {
},
});
return {
balanceLatest: BigNumber.from(response.balanceLatest),
balancePending: BigNumber.from(response.balancePending),
balance: BigNumber.from(response.balancePending),
};
} catch (err) {
//eslint-disable-next-line no-console
console.error(err);
return {
balanceLatest: BigNumber.from('0x0'),
balancePending: BigNumber.from('0x0'),
balance: BigNumber.from('0x0'),
};
}
};
Expand Down
4 changes: 1 addition & 3 deletions packages/wallet-ui/src/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ export type Network = Pick<

export interface Erc20TokenBalance extends Types.Erc20Token {
amount: BigNumber;
spendableAmount: BigNumber;
usdPrice?: number;
}
export type TransactionStatusOptions =
Expand Down Expand Up @@ -50,8 +49,7 @@ export type {

// Define the type for your token balances
export interface TokenBalance {
balancePending: BigNumber;
balanceLatest: BigNumber;
balance: BigNumber;
}

export enum FeeToken {
Expand Down
31 changes: 7 additions & 24 deletions packages/wallet-ui/src/utils/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,18 +45,14 @@ export const isValidAddress = (toCheck: string) => {
export const addMissingPropertiesToToken = (
token: Erc20Token,
balance?: string,
balanceSpendable?: string,
usdPrice?: number,
): Erc20TokenBalance => {
// when balance is undefined, use 0
const hexBalance = balance ?? '0x0';
// when balanceSpendable is undefined, we use hexBalance
const hexSpendableBalance = balanceSpendable ?? hexBalance;

return {
...token,
amount: ethers.BigNumber.from(hexBalance),
spendableAmount: ethers.BigNumber.from(hexSpendableBalance),
usdPrice: usdPrice,
};
};
Expand Down Expand Up @@ -87,20 +83,15 @@ export const getHumanReadableAmount = (
};

export const getSpendableTotalBalance = (asset: Erc20TokenBalance): string => {
if (asset.spendableAmount === undefined) {
throw new Error('Spendable amount can not be undefined');
if (asset.amount === undefined) {
throw new Error('Amount can not be undefined');
}
const spendableAmount = getHumanReadableAmount(
const amount = getHumanReadableAmount(
asset,
ethers.utils.formatUnits(asset.spendableAmount, asset.decimals),
ethers.utils.formatUnits(asset.amount, asset.decimals),
);
if (asset.spendableAmount.eq(asset.amount)) {
return `${spendableAmount}`;
}

const totalAmount = getHumanReadableAmount(asset);

return `${spendableAmount} (${totalAmount})`;
return amount;
};

export const getMaxDecimalsReadable = (
Expand Down Expand Up @@ -248,14 +239,6 @@ export function getTokenBalanceWithDetails(
token: Erc20Token,
tokenUSDPrice?: number,
): Erc20TokenBalance {
const { balancePending, balanceLatest } = tokenBalance;
const spendableBalance = balancePending.lt(balanceLatest)
? balancePending
: balanceLatest;
return addMissingPropertiesToToken(
token,
balanceLatest.toString(),
spendableBalance.toString(),
tokenUSDPrice,
);
const { balance } = tokenBalance;
return addMissingPropertiesToToken(token, balance.toString(), tokenUSDPrice);
}

0 comments on commit fdbeeb2

Please sign in to comment.