From f4d4758277e6b261bc1b7d1350ce1625f27d31ea Mon Sep 17 00:00:00 2001 From: juan-langa Date: Fri, 31 May 2024 10:12:12 +0200 Subject: [PATCH] Swap position - To aave --- src/pages/position/swap.ts | 12 ++ tests/sharedTestSteps/positionManagement.ts | 100 +++++++++-------- .../multEthDaiToMultSdaiEth.spec.ts | 106 ++++++++++++++++++ ... => multWstethUsdcToMultWstethDai.spec.ts} | 0 utils/tx.ts | 6 + 5 files changed, 180 insertions(+), 44 deletions(-) create mode 100644 tests/withWallet/maker/swap/makerToAave/multEthDaiToMultSdaiEth.spec.ts rename tests/withWallet/morphoBlue/swap/morphoToSpark/{multWstethUsdcXToMultWstethDai.spec.ts => multWstethUsdcToMultWstethDai.spec.ts} (100%) diff --git a/src/pages/position/swap.ts b/src/pages/position/swap.ts index 638f406..9fac3dc 100644 --- a/src/pages/position/swap.ts +++ b/src/pages/position/swap.ts @@ -48,4 +48,16 @@ export class Swap { async confirm() { await this.refinanceLocator.getByRole('button', { name: 'Confirm' }).click(); } + + @step + async confirmOrRetry() { + const confirm = this.page.getByRole('button', { name: 'Confirm', exact: true }); + const retry = this.page.getByRole('button', { name: 'Retry' }); + + if (await confirm.isVisible()) { + await confirm.click(); + } else if (await retry.isVisible()) { + await retry.click(); + } + } } diff --git a/tests/sharedTestSteps/positionManagement.ts b/tests/sharedTestSteps/positionManagement.ts index 3ab1ccc..a367cf7 100644 --- a/tests/sharedTestSteps/positionManagement.ts +++ b/tests/sharedTestSteps/positionManagement.ts @@ -424,6 +424,9 @@ export const swapPosition = async ({ targetProtocol, targetPool, verifyPositions, + existingDpmAndApproval, + upToStep5, + rejectSwap, }: { app: App; forkId: string; @@ -436,6 +439,9 @@ export const swapPosition = async ({ originalPosition?: { type: 'Borrow' | 'Multiply'; collateralToken: string; debtToken?: string }; targetPosition?: { exposure?: ActionData; debt?: ActionData }; }; + existingDpmAndApproval?: boolean; + upToStep5?: boolean; + rejectSwap?: boolean; }) => { let originalPositionPage: string; if (verifyPositions?.originalPosition) { @@ -456,7 +462,7 @@ export const swapPosition = async ({ .byPairPool(`${targetPool.colToken}/${targetPool.debtToken}`) .open(); - if (originalProtocol === 'Maker') { + if (originalProtocol === 'Maker' && !existingDpmAndApproval) { // Smart DeFi Acount creation randomly fails - Retry until it's created. await expect(async () => { await app.position.setup.createSmartDeFiAccount(); @@ -477,53 +483,59 @@ export const swapPosition = async ({ await app.position.setup.continue(); } - await app.position.swap.shouldHaveMaxTransactionCost('.[0-9]{1,2}'); - await app.position.swap.confirm(); + if (!upToStep5) { + await app.position.swap.shouldHaveMaxTransactionCost('.[0-9]{1,2}'); + await app.position.swap.confirmOrRetry(); - await test.step('Confirm automation setup', async () => { - await expect(async () => { - await tx.confirmAndVerifySuccess({ metamaskAction: 'confirmPermissionToSpend', forkId }); - await app.position.setup.goToPositionShouldBeVisible(); - }).toPass(); - }); + if (rejectSwap) { + await tx.rejectPermissionToSpend(); + } else { + await test.step('Confirm automation setup', async () => { + await expect(async () => { + await tx.confirmAndVerifySuccess({ metamaskAction: 'confirmPermissionToSpend', forkId }); + await app.position.setup.goToPositionShouldBeVisible(); + }).toPass(); + }); - await app.position.setup.goToPosition(); + await app.position.setup.goToPosition(); - if (verifyPositions?.targetPosition) { - // Verify new target position - await app.position.manage.shouldBeVisible(`Manage your ${targetProtocol}`); - await app.position.overview.shouldHaveExposure(verifyPositions.targetPosition.exposure); - await app.position.overview.shouldHaveDebt(verifyPositions.targetPosition.debt); - } + if (verifyPositions?.targetPosition) { + // Verify new target position + await app.position.manage.shouldBeVisible(`Manage your ${targetProtocol}`); + await app.position.overview.shouldHaveExposure(verifyPositions.targetPosition.exposure); + await app.position.overview.shouldHaveDebt(verifyPositions.targetPosition.debt); + } - if (verifyPositions?.originalPosition) { - // Verify that original position is now empty - await app.page.goto(originalPositionPage); - - await app.position.manage.shouldBeVisible('Manage your'); - await app.position.overview.shouldHaveLiquidationPrice({ price: '0.00' }); - // TO BE UPDATE for all protocols - // await app.position.overview.shouldHaveVaultDaiDebt('0.0000'); - // if (verifyPositions.originalPosition.type === 'Multiply') { - // await app.position.overview.shouldHaveNetValue({ value: '\\$(<)?0.0[0-1]' }); - // // TO BE UPDATE for all protocols - // // await app.position.overview.shouldHaveTotalCollateral({ - // // amount: '0.00', - // // token: verifyPositions.originalPosition.collateralToken, - // // }); - // } - if (verifyPositions.originalPosition.type === 'Borrow') { - await app.position.overview.shouldHaveLiquidationPrice({ price: '0.00' }); - await app.position.overview.shouldHaveCollateralizationRatio('0.00'); - await app.position.overview.shouldHaveCollateralLocked('0.00'); - await app.position.overview.shouldHaveAvailableToWithdraw({ - amount: '0.00000', - token: verifyPositions.originalPosition.collateralToken, - }); - await app.position.overview.shouldHaveAvailableToGenerate({ - amount: '0.0000', - token: verifyPositions.originalPosition.debtToken, - }); + if (verifyPositions?.originalPosition) { + // Verify that original position is now empty + await app.page.goto(originalPositionPage); + + await app.position.manage.shouldBeVisible('Manage your'); + await app.position.overview.shouldHaveLiquidationPrice({ price: '0.00' }); + // TO BE UPDATE for all protocols + // await app.position.overview.shouldHaveVaultDaiDebt('0.0000'); + // if (verifyPositions.originalPosition.type === 'Multiply') { + // await app.position.overview.shouldHaveNetValue({ value: '\\$(<)?0.0[0-1]' }); + // // TO BE UPDATE for all protocols + // // await app.position.overview.shouldHaveTotalCollateral({ + // // amount: '0.00', + // // token: verifyPositions.originalPosition.collateralToken, + // // }); + // } + if (verifyPositions.originalPosition.type === 'Borrow') { + await app.position.overview.shouldHaveLiquidationPrice({ price: '0.00' }); + await app.position.overview.shouldHaveCollateralizationRatio('0.00'); + await app.position.overview.shouldHaveCollateralLocked('0.00'); + await app.position.overview.shouldHaveAvailableToWithdraw({ + amount: '0.00000', + token: verifyPositions.originalPosition.collateralToken, + }); + await app.position.overview.shouldHaveAvailableToGenerate({ + amount: '0.0000', + token: verifyPositions.originalPosition.debtToken, + }); + } + } } } }; diff --git a/tests/withWallet/maker/swap/makerToAave/multEthDaiToMultSdaiEth.spec.ts b/tests/withWallet/maker/swap/makerToAave/multEthDaiToMultSdaiEth.spec.ts new file mode 100644 index 0000000..f8a45e2 --- /dev/null +++ b/tests/withWallet/maker/swap/makerToAave/multEthDaiToMultSdaiEth.spec.ts @@ -0,0 +1,106 @@ +import { BrowserContext, test } from '@playwright/test'; +import { resetState } from '@synthetixio/synpress/commands/synpress'; +import { metamaskSetUp } from 'utils/setup'; +import * as tenderly from 'utils/tenderly'; +import { setup } from 'utils/setup'; +import { extremelyLongTestTimeout } from 'utils/config'; +import { App } from 'src/app'; +import { openMakerPosition, swapPosition } from 'tests/sharedTestSteps/positionManagement'; + +let context: BrowserContext; +let app: App; +let forkId: string; + +test.describe.configure({ mode: 'serial' }); + +test.describe('Maker Multiply - Swap to Aave V3', async () => { + test.afterAll(async () => { + await tenderly.deleteFork(forkId); + + await app.page.close(); + + await context.close(); + + await resetState(); + }); + + test.use({ + viewport: { width: 1400, height: 720 }, + }); + + // Create a Maker position as part of the Swap tests setup + test('Test setup - Open Maker Mutiply position and start Swap process', async () => { + test.info().annotations.push({ + type: 'Test case', + description: '11788, 11790', + }); + + test.setTimeout(extremelyLongTestTimeout); + + await test.step('Test setup', async () => { + ({ context } = await metamaskSetUp({ network: 'mainnet' })); + let page = await context.newPage(); + app = new App(page); + + ({ forkId } = await setup({ + app, + network: 'mainnet', + extraFeaturesFlags: 'MakerTenderly:true EnableRefinance:true', + })); + }); + + await app.page.goto('/vaults/open-multiply/ETH-C'); + + // Depositing collateral too quickly after loading page returns wrong simulation results + await app.position.overview.waitForComponentToBeStable({ positionType: 'Maker' }); + + await openMakerPosition({ + app, + forkId, + deposit: { token: 'ETH', amount: '10' }, + }); + + await app.page.waitForTimeout(3000); + + await swapPosition({ + app, + forkId, + reason: 'Change direction of my position', + originalProtocol: 'Maker', + targetProtocol: 'Aave V3', + targetPool: { colToken: 'SDAI', debtToken: 'ETH' }, + upToStep5: true, + }); + }); + + ( + [ + { colToken: 'ETH', debtToken: 'DAI' }, + { colToken: 'SDAI', debtToken: 'ETH' }, + ] as const + ).forEach((targetPool) => + test(`It should swap a Maker Multiply position (ETH/DAI) to Aave V3 Multiply (${targetPool.colToken}/${targetPool.debtToken})`, async () => { + test.info().annotations.push({ + type: 'Test case', + description: 'xxx', + }); + + test.setTimeout(extremelyLongTestTimeout); + + // Wait an reload to avoid flakiness + await app.page.waitForTimeout(1000); + await app.page.reload(); + + await swapPosition({ + app, + forkId, + reason: 'Switch to higher max Loan To Value', + originalProtocol: 'Maker', + targetProtocol: 'Aave V3', + targetPool: { colToken: targetPool.colToken, debtToken: targetPool.debtToken }, + existingDpmAndApproval: true, + rejectSwap: true, + }); + }) + ); +}); diff --git a/tests/withWallet/morphoBlue/swap/morphoToSpark/multWstethUsdcXToMultWstethDai.spec.ts b/tests/withWallet/morphoBlue/swap/morphoToSpark/multWstethUsdcToMultWstethDai.spec.ts similarity index 100% rename from tests/withWallet/morphoBlue/swap/morphoToSpark/multWstethUsdcXToMultWstethDai.spec.ts rename to tests/withWallet/morphoBlue/swap/morphoToSpark/multWstethUsdcToMultWstethDai.spec.ts diff --git a/utils/tx.ts b/utils/tx.ts index 86898f4..477a74c 100644 --- a/utils/tx.ts +++ b/utils/tx.ts @@ -23,3 +23,9 @@ export const confirmAndVerifySuccess = async ({ // Verify last tx success await tenderly.verifyTxReceiptStatusSuccess(forkId); }; + +export const rejectPermissionToSpend = async () => { + await test.step(`Metamask: rejectPermissionToSpend`, async () => { + await metamask.rejectPermissionToSpend(); + }); +};