Skip to content

Commit

Permalink
Fix repaying with whole wallet balance of an asset
Browse files Browse the repository at this point in the history
  • Loading branch information
Oskar committed Apr 25, 2024
1 parent b374f29 commit 2aa592c
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 4 deletions.
47 changes: 47 additions & 0 deletions packages/app/src/features/dialogs/repay/RepayDialog.test-e2e.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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<AssetInputSchema>, maxValue: NormalizedUnitNumber): boolean {
function isFullRepay(
form: UseFormReturn<AssetInputSchema>,
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)
}
7 changes: 7 additions & 0 deletions packages/app/src/pages/Dashboard.PageObject.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<void> {
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
}

Expand Down

0 comments on commit 2aa592c

Please sign in to comment.