Merge pull request 'feat(holdout): Add reasonable slippage assertion to always-leave scenario' (#436) from fix/holdout-slippage-check into master
This commit is contained in:
commit
05191bb15f
2 changed files with 17 additions and 5 deletions
|
|
@ -174,8 +174,10 @@ export async function buyKrk(page: Page, ethAmount: string, opts?: BuyKrkOptions
|
|||
* Logs a warning if the WETH balance does not increase after the swap, which
|
||||
* indicates the pool returned 0 output (possible with amountOutMinimum: 0n on
|
||||
* a partially-drained pool).
|
||||
*
|
||||
* @returns The WETH delta (wethAfter - wethBefore) received from the swap.
|
||||
*/
|
||||
export async function sellAllKrk(page: Page, config: SellConfig): Promise<void> {
|
||||
export async function sellAllKrk(page: Page, config: SellConfig): Promise<bigint> {
|
||||
const krkBalance = await erc20BalanceOf(config.rpcUrl, config.krkAddress, config.accountAddress);
|
||||
if (krkBalance === 0n) throw new Error('sellAllKrk: KRK balance is 0 — nothing to sell');
|
||||
|
||||
|
|
@ -230,9 +232,11 @@ export async function sellAllKrk(page: Page, config: SellConfig): Promise<void>
|
|||
console.log('[swap] Swap mined');
|
||||
|
||||
const wethAfter = await erc20BalanceOf(config.rpcUrl, WETH, config.accountAddress);
|
||||
if (wethAfter <= wethBefore) {
|
||||
const wethReceived = wethAfter - wethBefore;
|
||||
if (wethReceived <= 0n) {
|
||||
console.warn('[swap] WARNING: WETH balance did not increase after sell — pool may have returned 0 output');
|
||||
} else {
|
||||
console.log(`[swap] Received ${wethAfter - wethBefore} WETH`);
|
||||
console.log(`[swap] Received ${wethReceived} WETH`);
|
||||
}
|
||||
return wethReceived;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -12,7 +12,7 @@
|
|||
* so no collision occurs.
|
||||
*/
|
||||
import { expect, test } from '@playwright/test';
|
||||
import { Wallet } from 'ethers';
|
||||
import { parseEther, Wallet } from 'ethers';
|
||||
import { createWalletContext } from '../../../../tests/setup/wallet-provider';
|
||||
import { getStackConfig } from '../../../../tests/setup/stack';
|
||||
import { connectWallet, getKrkBalance } from '../../helpers/wallet';
|
||||
|
|
@ -55,7 +55,7 @@ test('I can always leave', async ({ browser }) => {
|
|||
console.log('[TEST] ✅ KRK received');
|
||||
|
||||
// ── 4. Sell all KRK back (sovereign exit) ────────────────────────────
|
||||
await sellAllKrk(page, {
|
||||
const wethReceived = await sellAllKrk(page, {
|
||||
rpcUrl: config.rpcUrl,
|
||||
krkAddress: config.contracts.Kraiken,
|
||||
accountAddress: ACCOUNT_ADDRESS,
|
||||
|
|
@ -66,6 +66,14 @@ test('I can always leave', async ({ browser }) => {
|
|||
console.log(`[TEST] KRK balance after sell: ${krkAfterSell}`);
|
||||
expect(krkAfterSell).toBeLessThan(krkAfterBuy);
|
||||
console.log('[TEST] ✅ Sovereign exit confirmed: KRK sold back to WETH');
|
||||
|
||||
// ── 6. Assert reasonable slippage (at least 90% of ETH spent) ─────────
|
||||
const ethSpent = parseEther('0.1');
|
||||
const minExpected = parseEther('0.09'); // 90% of 0.1 ETH
|
||||
expect(wethReceived).toBeGreaterThanOrEqual(minExpected);
|
||||
const slippagePercent = ((Number(wethReceived) / Number(ethSpent)) * 100).toFixed(2);
|
||||
console.log(`[TEST] ✅ Reasonable slippage: received ${wethReceived} WETH for 0.1 ETH spent (${slippagePercent}%)`);
|
||||
|
||||
} finally {
|
||||
await ctx.close();
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue