Skip to content

Commit

Permalink
remove max label on form
Browse files Browse the repository at this point in the history
  • Loading branch information
fishonamos committed Sep 10, 2024
1 parent 1765a8e commit 776d2d6
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 42 deletions.
64 changes: 22 additions & 42 deletions src/components/Deposit.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,6 @@ import { useAtomValue } from 'jotai';
import mixpanel from 'mixpanel-browser';
import { useMemo, useState } from 'react';
import { ProviderInterface } from 'starknet';
import { BigNumber } from '@ethersproject/bignumber';
import { constants, uint256 } from 'starknet';

import LoadingWrap from './LoadingWrap';
import TxButton from './TxButton';

Expand Down Expand Up @@ -99,8 +96,7 @@ export default function Deposit(props: DepositProps) {
// const { balance, isLoading, isError } = useERC20Balance(selectedMarket);
const maxAmount: MyNumber = useMemo(() => {
if (props.buttonText === 'Redeem') {
// For withdrawals, use a large value (but not the maximum uint256)
return MyNumber.fromEther('1000000000000000000000000000', selectedMarket.decimals);
return balance; // Use the actual balance for Redeem
}

const currentTVl = tvlInfo.data?.amount || MyNumber.fromZero();
Expand All @@ -112,21 +108,20 @@ export default function Deposit(props: DepositProps) {
);
let reducedBalance = balance;

if (props.buttonText === 'Deposit') {
if (selectedMarket.name === 'STRK') {
reducedBalance = balance.subtract(
MyNumber.fromEther('1.5', selectedMarket.decimals),
);
} else if (selectedMarket.name === 'ETH') {
reducedBalance = balance.subtract(
MyNumber.fromEther('0.001', selectedMarket.decimals),
);
}
if (selectedMarket.name === 'STRK') {
reducedBalance = balance.subtract(
MyNumber.fromEther('1.5', selectedMarket.decimals),
);
} else if (selectedMarket.name === 'ETH') {
reducedBalance = balance.subtract(
MyNumber.fromEther('0.001', selectedMarket.decimals),
);
}

console.log('Deposit:: reducedBalance2', reducedBalance.toEtherStr());
const min = MyNumber.min(reducedBalance, adjustedMaxAllowed);
return MyNumber.max(min, MyNumber.fromEther('0', selectedMarket.decimals));
}, [balance, props.strategy, selectedMarket, props.buttonText]);
}, [balance, props.strategy, selectedMarket, props.buttonText, tvlInfo]);

function BalanceComponent(props: {
token: TokenInfo;
Expand Down Expand Up @@ -172,21 +167,16 @@ export default function Deposit(props: DepositProps) {
color: 'color_50p',
}}
onClick={() => {
let maxValue;
if (props.buttonText === 'Redeem') {
maxValue = balance; // Use actual balance for max withdrawal
} else {
maxValue = maxAmount;
}
setAmount(maxValue);
setRawAmount(maxValue.toEtherStr());
const maxAmountToSet = props.buttonText === 'Redeem' ? balance : maxAmount;
setAmount(maxAmountToSet);
setRawAmount(maxAmountToSet.toEtherStr());
mixpanel.track('Chose max amount', {
strategyId: props.strategy.id,
strategyName: props.strategy.name,
buttonText: props.buttonText,
amount: maxValue.toEtherStr(),
amount: maxAmountToSet.toEtherStr(),
token: selectedMarket.name,
maxAmount: maxValue.toEtherStr(),
maxAmount: maxAmount.toEtherStr(),
address,
});
}}
Expand Down Expand Up @@ -262,11 +252,7 @@ export default function Deposit(props: DepositProps) {
{/* add min max validations and show err */}
<NumberInput
min={0}
max={
props.buttonText === 'Redeem'
? undefined
: parseFloat(maxAmount.toEtherStr())
}
max={parseFloat(maxAmount.toEtherStr())}
step={parseFloat(selectedMarket.stepAmount.toEtherStr())}
color={'white'}
bg={'bg'}
Expand Down Expand Up @@ -310,17 +296,11 @@ export default function Deposit(props: DepositProps) {
Require amount {'>'} 0
</Text>
)}
{props.buttonText === 'Deposit' &&
amount.compare(maxAmount.toEtherStr(), 'gt') && (
<Text
marginTop="2px"
marginLeft={'7px'}
color="red"
fontSize={'13px'}
>
Amount to be less than {maxAmount.toEtherToFixedDecimals(2)}
</Text>
)}
{amount.compare(maxAmount.toEtherStr(), 'gt') && (
<Text marginTop="2px" marginLeft={'7px'} color="red" fontSize={'13px'}>
Amount to be less than or equal to {maxAmount.toEtherToFixedDecimals(2)}
</Text>
)}

<Center marginTop={'10px'}>
<TxButton
Expand Down
26 changes: 26 additions & 0 deletions src/utils/MyNumber.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,23 @@ export default class MyNumber {
}

toEtherStr() {
if (this.isMaxUint256()) {
return 'MAX';
}
return ethers.formatUnits(this.bigNumber.toFixed(), this.decimals);
}

toFixedStr(decimals: number) {
if (this.isMaxUint256()) {
return 'MAX';
}
return Number(this.toEtherStr()).toFixed(decimals);
}

toEtherToFixedDecimals(decimals: number) {
if (this.isMaxUint256()) {
return 'MAX';
}
// rounding down
return (
Math.floor(parseFloat(this.toEtherStr()) * 10 ** decimals) /
Expand All @@ -51,6 +60,10 @@ export default class MyNumber {
return this.bigNumber.eq('0');
}

eq(other: MyNumber) {
return this.bigNumber.eq(other.bigNumber);
}

/**
*
* @param amountEther in token terms without decimal e.g. 1 for 1 STRK
Expand All @@ -59,6 +72,12 @@ export default class MyNumber {
* @dev Add more commands as needed
*/
compare(amountEther: string, command: 'gte' | 'gt' | 'lt') {
if (this.isMaxUint256()) {
return command === 'lt'; // MAX is never less than any value
}
if (amountEther === 'MAX') {
return this.bigNumber[command](MyNumber.MAX_UINT256.bigNumber);
}
const fullNum = new BigNumber(
ethers.parseUnits(amountEther, this.decimals).toString(),
);
Expand Down Expand Up @@ -107,6 +126,13 @@ export default class MyNumber {
);
}

// Use a hardcoded value for MAX_UINT256
static MAX_UINT256 = new MyNumber('115792089237316195423570985008687907853269984665640564039457584007913129639935', 0);

isMaxUint256() {
return this.eq(MyNumber.MAX_UINT256);
}

[customInspectSymbol](depth: any, inspectOptions: any, inspect: any) {
return JSON.stringify({ raw: this.toString(), decimals: this.decimals });
}
Expand Down

0 comments on commit 776d2d6

Please sign in to comment.