diff --git a/tests/sharedTestSteps/positionManagement.ts b/tests/sharedTestSteps/positionManagement.ts index 2d180ba..643b363 100644 --- a/tests/sharedTestSteps/positionManagement.ts +++ b/tests/sharedTestSteps/positionManagement.ts @@ -104,21 +104,25 @@ export const adjustRisk = async ({ }; export const close = async ({ - app, forkId, + app, + positionType, closeTo, collateralToken, debtToken, tokenAmountAfterClosing, }: { - app: App; forkId: string; + app: App; + positionType?: 'Multiply' | 'Borrow' | 'Earn'; closeTo: 'collateral' | 'debt'; collateralToken: string; debtToken: string; tokenAmountAfterClosing: string; }) => { - await app.position.manage.openManageOptions({ currentLabel: 'Adjust' }); + await app.position.manage.openManageOptions({ + currentLabel: positionType === 'Borrow' ? collateralToken : 'Adjust', + }); await app.position.manage.select('Close position'); if (closeTo === 'debt') { await app.position.manage.closeTo(debtToken); @@ -149,14 +153,21 @@ export const close = async ({ }); await app.position.overview.shouldHaveLoanToValue('0.00'); await app.position.overview.shouldHaveNetValue({ value: '0.00' }); - await app.position.overview.shouldHaveBuyingPower('0.00'); - await app.position.overview.shouldHaveExposure({ token: collateralToken, amount: '0.00' }); await app.position.overview.shouldHaveDebt({ amount: '0.00', token: debtToken, protocol: 'Ajna', }); - await app.position.overview.shouldHaveMultiple('1.00'); + + if (positionType === 'Borrow') { + await app.position.overview.shouldHaveCollateralDeposited({ amount: '0.00', token: 'RETH' }); + await app.position.overview.shouldHaveAvailableToWithdraw({ token: 'RETH', amount: '0.00' }); + await app.position.overview.shouldHaveAvailableToBorrow({ token: 'ETH', amount: '0.00' }); + } else { + await app.position.overview.shouldHaveExposure({ token: collateralToken, amount: '0.00' }); + await app.position.overview.shouldHaveBuyingPower('0.00'); + await app.position.overview.shouldHaveMultiple('1.00'); + } }; export const depositAndBorrow = async ({ @@ -215,7 +226,6 @@ export const withdrawAndPayBack = async ({ expectedCollateralDeposited: { token: string; amount: string }; expectedDebt: { token: string; amount: string }; }) => { - await app.position.manage.withdrawCollateral(); await app.position.manage.withdraw(withdraw); await app.position.manage.payback(payback); diff --git a/tests/withWallet/ajna/ethereum/ajnaBorrowEthereum.spec.ts b/tests/withWallet/ajna/ethereum/ajnaBorrowEthereum.spec.ts index 2222d18..fc20a7d 100644 --- a/tests/withWallet/ajna/ethereum/ajnaBorrowEthereum.spec.ts +++ b/tests/withWallet/ajna/ethereum/ajnaBorrowEthereum.spec.ts @@ -1,12 +1,16 @@ import { BrowserContext, test } from '@playwright/test'; import { metamaskSetUp } from 'utils/setup'; import { resetState } from '@synthetixio/synpress/commands/synpress'; -import * as metamask from '@synthetixio/synpress/commands/metamask'; import * as tenderly from 'utils/tenderly'; import { setup } from 'utils/setup'; import { extremelyLongTestTimeout, positionTimeout, veryLongTestTimeout } from 'utils/config'; import { App } from 'src/app'; -import { depositAndBorrow, openPosition } from 'tests/sharedTestSteps/positionManagement'; +import { + close, + depositAndBorrow, + openPosition, + withdrawAndPayBack, +} from 'tests/sharedTestSteps/positionManagement'; let context: BrowserContext; let app: App; @@ -91,33 +95,17 @@ test.describe('Ajna Ethereum Borrow - Wallet connected', async () => { test.setTimeout(veryLongTestTimeout); await app.position.manage.withdrawCollateral(); - await app.position.manage.withdraw({ token: 'RETH', amount: '10' }); - await app.position.manage.payback({ token: 'ETH', amount: '5' }); - await app.position.setup.confirm(); - - // ============================================================ - - // UI sometimes gets stuck after confirming position update - // - 'Reload' added to avoid flakines - await app.position.setup.confirm(); - await test.step('Metamask: ConfirmPermissionToSpend', async () => { - await metamask.confirmPermissionToSpend(); - }); - await app.position.setup.shouldShowUpdatingPosition(); - await app.page.reload(); - - // ============================================================ - - await app.position.overview.shouldHaveCollateralDeposited({ - amount: '25.00', - token: 'RETH', - timeout: positionTimeout, - }); - await app.position.overview.shouldHaveDebt({ - amount: '20.[0-9]{2}([0-9]{1,2})?', - token: 'ETH', - protocol: 'Ajna', + await withdrawAndPayBack({ + app, + forkId, + withdraw: { token: 'RETH', amount: '10' }, + payback: { token: 'ETH', amount: '5' }, + expectedCollateralDeposited: { + amount: '25.00', + token: 'RETH', + }, + expectedDebt: { amount: '20.[0-9]{2}([0-9]{1,2})?', token: 'ETH' }, }); }); @@ -132,33 +120,16 @@ test.describe('Ajna Ethereum Borrow - Wallet connected', async () => { await app.position.manage.openManageOptions({ currentLabel: 'RETH' }); await app.position.manage.select('Manage debt'); - await app.position.manage.borrow({ token: 'ETH', amount: '15' }); - await app.position.manage.deposit({ token: 'RETH', amount: '20' }); - - await app.position.setup.confirm(); - - // ============================================================ - - // UI sometimes gets stuck after confirming position update - // - 'Reload' added to avoid flakines - await app.position.setup.confirm(); - await test.step('Metamask: ConfirmPermissionToSpend', async () => { - await metamask.confirmPermissionToSpend(); - }); - await app.position.setup.shouldShowUpdatingPosition(); - await app.page.reload(); - - // ============================================================ - - await app.position.overview.shouldHaveCollateralDeposited({ - amount: '45.00', - token: 'RETH', - timeout: positionTimeout, - }); - await app.position.overview.shouldHaveDebt({ - amount: '35.[0-9]{2}([0-9]{1,2})?', - token: 'ETH', - protocol: 'Ajna', + await depositAndBorrow({ + app, + forkId, + borrow: { token: 'ETH', amount: '15' }, + deposit: { token: 'RETH', amount: '20' }, + expectedCollateralDeposited: { + amount: '45.00', + token: 'RETH', + }, + expectedDebt: { amount: '35.[0-9]{2}([0-9]{1,2})?', token: 'ETH' }, }); }); @@ -174,33 +145,17 @@ test.describe('Ajna Ethereum Borrow - Wallet connected', async () => { await app.position.manage.select('Manage debt'); await app.position.manage.payBackDebt(); - await app.position.manage.payback({ token: 'ETH', amount: '20' }); - await app.position.manage.withdraw({ token: 'RETH', amount: '15' }); - - await app.position.setup.confirm(); - - // ============================================================ - // UI sometimes gets stuck after confirming position update - // - 'Reload' added to avoid flakines - await app.position.setup.confirm(); - await test.step('Metamask: ConfirmPermissionToSpend', async () => { - await metamask.confirmPermissionToSpend(); - }); - await app.position.setup.shouldShowUpdatingPosition(); - await app.page.reload(); - - // ============================================================ - - await app.position.overview.shouldHaveCollateralDeposited({ - amount: '30.00', - token: 'RETH', - timeout: positionTimeout, - }); - await app.position.overview.shouldHaveDebt({ - amount: '15.[0-9]{2}([0-9]{1,2})?', - token: 'ETH', - protocol: 'Ajna', + await withdrawAndPayBack({ + app, + forkId, + payback: { token: 'ETH', amount: '20' }, + withdraw: { token: 'RETH', amount: '15' }, + expectedCollateralDeposited: { + amount: '30.00', + token: 'RETH', + }, + expectedDebt: { amount: '15.[0-9]{2}([0-9]{1,2})?', token: 'ETH' }, }); }); @@ -212,42 +167,15 @@ test.describe('Ajna Ethereum Borrow - Wallet connected', async () => { test.setTimeout(veryLongTestTimeout); - await app.position.manage.openManageOptions({ currentLabel: 'RETH' }); - await app.position.manage.select('Close position'); - await app.position.manage.shouldHaveTokenAmountAfterClosing({ - token: 'RETH', - amount: '[0-9]{1,2}.[0-9]{1,2}', - }); - - await app.position.setup.confirm(); - - // ============================================================ - - // UI sometimes gets stuck after confirming position update - // - 'Reload' added to avoid flakines - await app.position.setup.confirm(); - await test.step('Metamask: ConfirmPermissionToSpend', async () => { - await metamask.confirmPermissionToSpend(); - }); - await app.position.setup.shouldShowUpdatingPosition(); - await app.page.reload(); - - // ============================================================ - - await app.position.overview.shouldHaveLiquidationPrice({ - price: '0.00', - timeout: positionTimeout, - }); - await app.position.overview.shouldHaveLoanToValue('0.00'); - await app.position.overview.shouldHaveCollateralDeposited({ amount: '0.00', token: 'RETH' }); - await app.position.overview.shouldHaveDebt({ - amount: '0.00', - token: 'ETH', - protocol: 'Ajna', + await close({ + app, + forkId, + positionType: 'Borrow', + closeTo: 'collateral', + collateralToken: 'RETH', + debtToken: 'ETH', + tokenAmountAfterClosing: '[0-9]{1,2}.[0-9]{1,2}', }); - await app.position.overview.shouldHaveNetValue({ value: '0.00' }); - await app.position.overview.shouldHaveAvailableToWithdraw({ token: 'RETH', amount: '0.00' }); - await app.position.overview.shouldHaveAvailableToBorrow({ token: 'ETH', amount: '0.00' }); }); test('It should allow to simulate an Ajna Ethereum Borrow position before opening it', async () => {