Skip to content

Commit

Permalink
Merge pull request #340 from OasisDEX/swap-more-to-aave
Browse files Browse the repository at this point in the history
Swap position - To aave
  • Loading branch information
juan-langa authored May 31, 2024
2 parents 966c21a + f4d4758 commit c31894a
Show file tree
Hide file tree
Showing 5 changed files with 180 additions and 44 deletions.
12 changes: 12 additions & 0 deletions src/pages/position/swap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}
}
100 changes: 56 additions & 44 deletions tests/sharedTestSteps/positionManagement.ts
Original file line number Diff line number Diff line change
Expand Up @@ -424,6 +424,9 @@ export const swapPosition = async ({
targetProtocol,
targetPool,
verifyPositions,
existingDpmAndApproval,
upToStep5,
rejectSwap,
}: {
app: App;
forkId: string;
Expand All @@ -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) {
Expand All @@ -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();
Expand All @@ -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,
});
}
}
}
}
};
Original file line number Diff line number Diff line change
@@ -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,
});
})
);
});
6 changes: 6 additions & 0 deletions utils/tx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
});
};

0 comments on commit c31894a

Please sign in to comment.