Skip to content

Commit

Permalink
Merge pull request #2043 from zeitgeistpm/tr-trade-box-slider
Browse files Browse the repository at this point in the history
Handle 0 balances better in trade form
  • Loading branch information
robhyrk authored Dec 10, 2023
2 parents 440330e + 3db9974 commit c4670e1
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 4 deletions.
12 changes: 9 additions & 3 deletions components/trade-form/BuyForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,13 @@ const BuyForm = ({
const subscription = watch((value, { name, type }) => {
const changedByUser = type != null;

if (!changedByUser || !maxSpendableBalance || !maxAmountIn) return;
if (
!changedByUser ||
!maxSpendableBalance ||
maxSpendableBalance.eq(0) ||
!maxAmountIn
)
return;

if (name === "percentage") {
const max = maxSpendableBalance.greaterThan(maxAmountIn)
Expand Down Expand Up @@ -255,9 +261,9 @@ const BuyForm = ({
},
validate: (value) => {
if (value > (maxSpendableBalance?.div(ZTG).toNumber() ?? 0)) {
return `Insufficient balance. Current balance: ${maxSpendableBalance
return `Insufficient balance (${maxSpendableBalance
?.div(ZTG)
.toFixed(3)}`;
.toFixed(3)}${baseSymbol})`;
} else if (value <= 0) {
return "Value cannot be zero or less";
} else if (maxAmountIn?.div(ZTG)?.lessThanOrEqualTo(value)) {
Expand Down
8 changes: 7 additions & 1 deletion components/trade-form/SellForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,13 @@ const SellForm = ({
const subscription = watch((value, { name, type }) => {
const changedByUser = type != null;

if (!changedByUser || !selectedAssetBalance || !maxAmountIn) return;
if (
!changedByUser ||
!selectedAssetBalance ||
selectedAssetBalance.eq(0) ||
!maxAmountIn
)
return;

if (name === "percentage") {
const max = selectedAssetBalance.greaterThan(maxAmountIn)
Expand Down

0 comments on commit c4670e1

Please sign in to comment.