Skip to content

Commit

Permalink
Merge pull request #210 from lidofinance/add-wrap-unwrap-tests
Browse files Browse the repository at this point in the history
Tests for wrap/unwrap transactions
  • Loading branch information
alx-khramov authored Jan 30, 2025
2 parents 240b699 + e39b91f commit 621536a
Show file tree
Hide file tree
Showing 13 changed files with 431 additions and 41 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ on:

jobs:
test:
runs-on: ubuntu-22.04
runs-on: ubuntu-latest
environment: test
env:
TEST_BRANCH: ${{ github.event.inputs.branch }}
Expand Down
1 change: 1 addition & 0 deletions playwright-tests/pages/components/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ export * from './wallet-list-modal';
export * from './wallet-modal';
export * from './toast';
export * from './stake-block';
export * from './wrap-unwrap-block';
41 changes: 41 additions & 0 deletions playwright-tests/pages/components/stats.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import { Locator, Page } from '@playwright/test';
import { TIMEOUT } from '@test-data';
import { waitForCallback } from '@services';

export class StatsBlock {
page: Page;
Expand All @@ -22,4 +24,43 @@ export class StatsBlock {
this.wstethBalance = this.mainComponent.getByTestId('wstETH');
this.amountInput = this.mainComponent.getByTestId('amountInput');
}

async getEthBalance() {
return await waitForCallback(
async (locator: Locator) => {
return await locator.evaluate((element) => {
const balance = parseFloat(element.textContent);
return balance || balance == 0 ? String(balance) : null;
});
},
this.ethBalance,
TIMEOUT.RPC_WAIT,
);
}

async getStEthBalance() {
return await waitForCallback(
async (locator: Locator) => {
return await locator.evaluate((element) => {
const balance = parseFloat(element.textContent);
return balance || balance == 0 ? String(balance) : null;
});
},
this.stethBalance,
TIMEOUT.RPC_WAIT,
);
}

async getWstEthBalance() {
return await waitForCallback(
async (locator: Locator) => {
return await locator.evaluate((element) => {
const balance = parseFloat(element.textContent);
return balance || balance == 0 ? String(balance) : null;
});
},
this.wstethBalance,
TIMEOUT.RPC_WAIT,
);
}
}
49 changes: 49 additions & 0 deletions playwright-tests/pages/components/wrap-unwrap-block.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import { Locator, Page, test } from '@playwright/test';

export class WrapUnwrapBlock {
page: Page;
mainComponent: Locator;
wrapBtn: Locator;
unwrapBtn: Locator;
txTypeSelector: Locator;

constructor(page: Page) {
this.page = page;
this.mainComponent = this.page
.getByTestId('WrapBlock')
.or(this.page.getByTestId('UnwrapBlock'));
this.wrapBtn = this.mainComponent.locator('button :has-text("Wrap")');
this.unwrapBtn = this.mainComponent.locator('button :has-text("Unwrap")');
this.txTypeSelector = this.mainComponent.locator('label');
}

async selectWrapEthTxType() {
await test.step('Select the Wrap ETH tx type', async () => {
await this.txTypeSelector.click();
await this.page
.locator('id=lido-ui-modal-root')
.locator('button', { hasText: 'Wrap ETH' })
.click();
});
}

async selectWrapStEthTxType() {
await test.step('Select the Wrap stETH tx type', async () => {
await this.txTypeSelector.click();
await this.page
.locator('id=lido-ui-modal-root')
.locator('button', { hasText: 'Wrap stETH' })
.click();
});
}

async selectUnwrapTxType() {
await test.step('Select the Unwrap tx type', async () => {
await this.txTypeSelector.click();
await this.page
.locator('id=lido-ui-modal-root')
.locator('button', { hasText: 'Unwrap' })
.click();
});
}
}
31 changes: 18 additions & 13 deletions playwright-tests/pages/reefKnot.page.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,18 @@ import {
WalletModal,
StakeBlock,
Toast,
WrapUnwrapBlock,
} from './components';
import { Locator, Page, test } from '@playwright/test';
import { Page, test } from '@playwright/test';
import { TIMEOUT } from '@test-data';
import { waitForCallback } from '@services';

export class ReefKnotPage {
readonly page: Page;
header: Header;
walletModal: WalletModal;
statsBlock: StatsBlock;
stakeBlock: StakeBlock;
wrapUnwrapBlock: WrapUnwrapBlock;
walletListModal: WalletListModal;
toast: Toast;

Expand All @@ -25,6 +26,7 @@ export class ReefKnotPage {
this.walletModal = new WalletModal(this.page);
this.statsBlock = new StatsBlock(this.page);
this.stakeBlock = new StakeBlock(this.page);
this.wrapUnwrapBlock = new WrapUnwrapBlock(this.page);
this.walletListModal = new WalletListModal(this.page);
this.toast = new Toast(this.page);
}
Expand Down Expand Up @@ -76,16 +78,19 @@ export class ReefKnotPage {
return txPage;
}

async waitForBalance(locator: Locator, timeout = TIMEOUT.RPC_WAIT) {
return await waitForCallback(
async (locator: Locator) => {
return await locator.evaluate((element) => {
const balance = parseFloat(element.textContent);
return balance ? String(balance) : null;
});
},
locator,
timeout,
);
async clickWrapButton() {
const [txPage] = await Promise.all([
this.waitForPage(TIMEOUT.RPC_WAIT),
this.wrapUnwrapBlock.wrapBtn.click(),
]);
return txPage;
}

async clickUnwrapButton() {
const [txPage] = await Promise.all([
this.waitForPage(TIMEOUT.RPC_WAIT),
this.wrapUnwrapBlock.unwrapBtn.click(),
]);
return txPage;
}
}
2 changes: 1 addition & 1 deletion playwright-tests/playwright.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ const config: PlaywrightTestConfig = {
},
fullyParallel: false,
forbidOnly: !!process.env.CI,
retries: 1,
retries: process.env.CI ? 1 : 0,
workers: 1,
reporter: getReportConfig(),
use: {
Expand Down
18 changes: 12 additions & 6 deletions playwright-tests/services/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,22 @@
*
* Example:
* ```ts
* 0.12345 => toCut('0.12345', 3) => '0.123'
* 0.12345 => toСutDecimalsDigit('0.12345', 3) => '0.123'
* 0.101 => toСutDecimalsDigit('0.101', 2, true) => '0.1'
* ```
*/
export function toCut(floatAmount: string, decimalPlaces: number) {
const result = floatAmount.split(/\./);
export function toCutDecimalsDigit(
amount: any,
decimalPlaces: number,
removeZero = false,
) {
const parts = String(amount).split(/\./);
let respLength: number;
if (result.length === 2) {
respLength = result[0].length + 1 + decimalPlaces;
if (parts.length === 2) {
respLength = parts[0].length + 1 + decimalPlaces;
}
return floatAmount.slice(0, respLength);
const response = String(amount).slice(0, respLength);
return removeZero ? String(parseFloat(response)) : response;
}

/**
Expand Down
38 changes: 35 additions & 3 deletions playwright-tests/services/sdk.service.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { LidoSDK, VIEM_CHAINS } from '@lidofinance/lido-ethereum-sdk';
import { HDAccount } from 'viem/accounts';
import { createWalletClient, formatEther, http } from 'viem';
import { createWalletClient, formatEther, http, type Address } from 'viem';
import { REEF_KNOT_CONFIG } from '@config';
import { toCut } from './helpers';
import { toCutDecimalsDigit } from './helpers';

global.fetch = fetch;

Expand All @@ -26,6 +26,14 @@ export class SdkService extends LidoSDK {
});
}

/**
Get token balance from SDK without 0 in the end of number
Example:
```ts
// ETH balance = 1.102
getBalanceByToken(stdToken.ETH, 2) => '1.1'
```
*/
async getBalanceByToken(token: sdkToken, decimalPlaces: number) {
let balance: string;
switch (token) {
Expand All @@ -39,6 +47,30 @@ export class SdkService extends LidoSDK {
balance = formatEther(await this.wsteth.balance());
break;
}
return toCut(balance, decimalPlaces);
return toCutDecimalsDigit(balance, decimalPlaces, true);
}

async exchangeStEthToWstEth(amount: any) {
const wstethRate = formatEther(
await this.wrap.convertStethToWsteth(1000000000000000000n),
);
return parseFloat(wstethRate) * parseFloat(amount);
}

async exchangeWstEthToStEth(amount: any) {
const wstethRate = formatEther(
await this.wrap.convertWstethToSteth(1000000000000000000n),
);
return parseFloat(wstethRate) * parseFloat(amount);
}

async getStEthAllowance() {
return parseFloat(
formatEther(
await this.steth.allowance({
to: REEF_KNOT_CONFIG.STAND_CONFIG.contracts.wrap as Address,
}),
),
);
}
}
4 changes: 2 additions & 2 deletions playwright-tests/test-data/matomo.data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,8 @@ export const CONFIG_MATOMO_CLICK_TO_WALLET_EVENTS = [
walletName: 'imToken',
},
{
eventMessage: 'xdefi clicked',
walletName: 'XDEFI',
eventMessage: 'ctrl clicked',
walletName: 'Ctrl',
},
{
eventMessage: 'binanceWallet clicked',
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { expect, test } from '@playwright/test';
import { Tags, TIMEOUT } from '@test-data';
import { BrowserService, initBrowserWithWallet } from '@browser';
import { ReefKnotService, toCut } from '@services';
import { ReefKnotService, toCutDecimalsDigit } from '@services';
import { ReefKnotPage } from '@pages';
import { qase } from 'playwright-qase-reporter';
import { REEF_KNOT_CONFIG } from '@config';
Expand Down Expand Up @@ -31,20 +31,18 @@ REEF_KNOT_CONFIG.WALLETS.forEach((wallet) => {
await browserService.teardown();
});

test(`Stake ${stakeAmount} ETH`, async () => {
test(qase(444, `Stake ${stakeAmount} ETH`), async () => {
await qase.groupParameters({
wallet: wallet.name,
txAmount: stakeAmount,
});

const newStEthBalance =
await test.step('Calculate the stETH amount result', async () => {
const stEthBalance = parseFloat(
await reefKnotPage.waitForBalance(
reefKnotPage.statsBlock.stethBalance,
),
);
return toCut(String(stEthBalance + parseFloat(stakeAmount)), 4);
const stethResult =
parseFloat(await reefKnotPage.statsBlock.getStEthBalance()) +
parseFloat(stakeAmount);
return toCutDecimalsDigit(stethResult, 4);
});

const txPage =
Expand Down
Loading

0 comments on commit 621536a

Please sign in to comment.