From 2aa592cdab5618203a8fd358e8fd44c9025f4dcf Mon Sep 17 00:00:00 2001 From: Oskar Date: Thu, 25 Apr 2024 07:55:00 +0200 Subject: [PATCH] Fix repaying with whole wallet balance of an asset --- .../dialogs/repay/RepayDialog.test-e2e.ts | 47 +++++++++++++++++++ .../repay/logic/getRepayInFullOptions.ts | 16 +++++-- .../app/src/pages/Dashboard.PageObject.ts | 7 +++ 3 files changed, 66 insertions(+), 4 deletions(-) diff --git a/packages/app/src/features/dialogs/repay/RepayDialog.test-e2e.ts b/packages/app/src/features/dialogs/repay/RepayDialog.test-e2e.ts index e6a48de4d..884653568 100644 --- a/packages/app/src/features/dialogs/repay/RepayDialog.test-e2e.ts +++ b/packages/app/src/features/dialogs/repay/RepayDialog.test-e2e.ts @@ -220,6 +220,53 @@ test.describe('Repay dialog', () => { }) }) + test.describe('Position when borrowed asset was not in user wallet before', () => { + const initialDeposits = { + wstETH: 1000, + } as const + const daiToBorrow = 1_000_000 + + test.beforeEach(async ({ page }) => { + await setup(page, fork, { + initialPage: 'easyBorrow', + account: { + type: 'connected', + assetBalances: { wstETH: 10_000 }, + }, + }) + + const borrowPage = new BorrowPageObject(page) + await borrowPage.depositAssetsActions(initialDeposits, daiToBorrow) + await borrowPage.viewInDashboardAction() + + const dashboardPage = new DashboardPageObject(page) + await dashboardPage.expectAssetToBeInDepositTable('wstETH') + }) + + test('can repay using whole wallet balance of an asset', async ({ page }) => { + const repay = { + asset: 'DAI', + amount: daiToBorrow, + } as const + + const dashboardPage = new DashboardPageObject(page) + + await dashboardPage.clickRepayButtonAction(repay.asset) + + const repayDialog = new DialogPageObject(page, headerRegExp) + await repayDialog.clickMaxAmountAction() + const actionsContainer = new ActionsPageObject(repayDialog.locatePanelByHeader('Actions')) + await actionsContainer.acceptAllActionsAction(2) + await repayDialog.expectSuccessPage([repay], fork) + + await screenshot(repayDialog.getDialog(), 'repay-dialog-whole-balance-dai-success') + + await repayDialog.viewInDashboardAction() + + await dashboardPage.expectNonZeroAmountInBorrowTable(repay.asset) + }) + }) + test.describe('Position with multiple borrowed assets', () => { const initialDeposits = { wstETH: initialBalances.wstETH, // deposit whole balance diff --git a/packages/app/src/features/dialogs/repay/logic/getRepayInFullOptions.ts b/packages/app/src/features/dialogs/repay/logic/getRepayInFullOptions.ts index d46695507..348148db5 100644 --- a/packages/app/src/features/dialogs/repay/logic/getRepayInFullOptions.ts +++ b/packages/app/src/features/dialogs/repay/logic/getRepayInFullOptions.ts @@ -5,7 +5,7 @@ import { MarketInfo } from '@/domain/market-info/marketInfo' import { NormalizedUnitNumber } from '@/domain/types/NumericValues' import { WalletInfo } from '@/domain/wallet/useWalletInfo' -import { AssetInputSchema, isMaxValue } from '../../common/logic/form' +import { AssetInputSchema } from '../../common/logic/form' interface UseRepayInFullOptionsResult { repayInFull: boolean @@ -30,12 +30,20 @@ export function getRepayInFullOptions( }, }) - const repayInFull = isMaxRepay(form, maxRepayValue) + const repayInFull = isFullRepay(form, position.borrowBalance, maxRepayValue) return { repayInFull, maxRepayValue } } -function isMaxRepay(form: UseFormReturn, maxValue: NormalizedUnitNumber): boolean { +function isFullRepay( + form: UseFormReturn, + debt: NormalizedUnitNumber, + maxRepayValue: NormalizedUnitNumber, +): boolean { const { value } = form.getValues() - return isMaxValue(value, maxValue) + const normalizedInputValue = NormalizedUnitNumber(value === '' ? '0' : value) + if (normalizedInputValue.lt(debt)) { + return false + } + return normalizedInputValue.eq(maxRepayValue) } diff --git a/packages/app/src/pages/Dashboard.PageObject.ts b/packages/app/src/pages/Dashboard.PageObject.ts index b2b938658..6d4100d6f 100644 --- a/packages/app/src/pages/Dashboard.PageObject.ts +++ b/packages/app/src/pages/Dashboard.PageObject.ts @@ -187,6 +187,13 @@ export class DashboardPageObject extends BasePageObject { table.getByRole('row').filter({ has: this.page.getByRole('cell', { name: asset, exact: true }) }), ).toBeVisible() } + + async expectNonZeroAmountInBorrowTable(asset: string): Promise { + const table = this.locatePanelByHeader('Borrow') + const row = table.getByRole('row').filter({ has: this.page.getByRole('cell', { name: asset, exact: true }) }) + const amount = row.getByRole('cell').nth(2) + await expect(amount).not.toHaveText('—') + } // #endregion }