← Back to Projects

Web3 Lending Protocol

Built a comprehensive end-to-end automation framework for a multi-chain lending protocol, automating critical financial workflows including supply, borrow, repay, withdraw, and liquidation across 6 blockchain networks. Achieved zero test flakiness with deterministic blockchain state management and intelligent wait strategies.

Playwright
TypeScript
Viem
EthersJS
Foundry
Cast
Anvil
Docker

Project Summary

Designed and implemented a robust, multi-chain test automation framework for a lending protocol operating on Base, Optimism, Arbitrum, Berachain, Monad, and Sonic networks. The framework automates complex DeFi operations including asset supply, borrowing, repayment, withdrawals, and liquidation scenarios with precision and reliability.

The framework supports sophisticated financial logic such as collateral calculations, health factor tracking, multi-asset pricing (USDC, WETH, wstETH), liquidation mechanics, and oracle price manipulation testing. Built on Playwright + TypeScript with a modular Page Object Model architecture, it enables rapid test development and maintains high reliability across distributed blockchain environments. The framework implements deterministic state management through Anvil snapshot/revert mechanisms, eliminating traditional test sequencing constraints and enabling true parallel execution.


Key Achievements

  • 70% effort reduction
  • 35+ scenarios
  • <1% flakiness
  • 18+ page objects
  • 1,200+ lines of typed test code

What I Tested

Core Lending Operations (35+ scenarios total)

  • Supply/Collateral - Deposit assets (USDC, WETH, wstETH) with collateral enable/disable logic; validate dashboard updates and health factor projections
  • Borrowing - Multi-asset borrowing with collateral requirement validation; test borrow limits based on supplied collateral values
  • Repayment - Full and partial loan repayment with interest calculations; validate accrued debt reduction and transaction success flows
  • Withdrawal - Safe collateral withdrawal with health factor constraints; prevent unsafe withdrawals that would trigger liquidation
  • Liquidation - Automated liquidator mechanics; oracle price manipulation to trigger unhealthy positions; liquidation fee calculations
  • Wallet Connection - Web3 wallet connection flow; address display and session validation

Technical Highlights

Testing Framework & Architecture

  • Built automation using Playwright + TypeScript with custom fixture extensions for blockchain integration
  • Designed Page Object Model with 18+ page classes and modular abstractions (supply, borrow, repay, withdraw, liquidation, health factor improvement modals)
  • Implemented Fixture-based setup pattern providing Anvil networks, wallet management, and pre-configured page objects to every test
  • Created Type-safe test data structures with interfaces (AssetDetails, MarketAssets, TestData) and enums (Asset, NetworkChain, Actions) ensuring compile-time safety
  • Leveraged parameterized testing to execute identical workflows across 3 major assets and 6 blockchain networks, minimizing code duplication

Blockchain Integration & Smart Contract Testing

  • Docker-containerized Anvil - Spawned isolated forked blockchain instances per network; automatic cleanup via global setup/teardown
  • ethers.js & viem clients - Direct contract interaction for minting test assets, reading protocol state, and transaction validation
  • BigNumber.js precision - Accurate decimal arithmetic matching 18-decimal Solidity calculations (WETH: 10^18, USDC: 10^6 with upscaling)
  • Snapshot/Revert pattern - Deterministic test isolation using Anvil snapshots; tests revert to known state preventing cascading failures
  • Smart contract utilities - Custom modules for token minting, oracle price manipulation (MockOracle), and automated liquidator triggers
  • RPC management - Environment-based network URLs (BASE_SEPOLIA_RPC_URL, OPTIMISM_SEPOLIA_RPC_URL, etc.) enabling seamless network switching

Challenges & Solutions

  • Multi-Chain Network Orchestration
    Challenge: Running identical tests across 6 blockchain networks with isolated state, avoiding port conflicts and resource leaks
    → Solution: Docker-containerized Anvil instances per network, coordinated via Playwright's setup/teardown projects; global.setup.ts spawns containers, tests execute, global.teardown.ts ensures clean shutdown with failure handling

  • DeFi Math Precision & State Determinism
    Challenge: Ensuring test calculations match 18-decimal Solidity math; preventing flaky tests from accumulated rounding errors
    → Solution: BigNumber.js for arbitrary precision arithmetic; Anvil snapshot/revert for deterministic test isolation (no sequential dependencies); mock balances and oracle prices for predictable scenarios

  • Oracle Price Manipulation for Liquidation Testing
    Challenge: Testing liquidation mechanics requires triggering unhealthy health factors—risky and hard to reproduce with real price feeds
    → Solution: MockOracle smart contract utility allowing tests to set arbitrary prices; liquidation tests validate health factor calculations without relying on external price volatility

  • Health Factor Calculations Across Asset Types
    Challenge: Collateral values depend on asset decimals and collateral factors, projections require accurate calculations → Solution: Type-safe AssetDetails interface with collateral factor metadata; parameterized tests validating health factors across all asset combinations; UI-to-contract value validation using ethers.js

  • Flaky Async UI State & Transaction Confirmation
    Challenge: UI updates lag behind blockchain transaction confirmation; waiting for success modals without precise selectors causes timeouts
    → Solution: Custom wait strategies in Page Object Model (isVisible() methods); explicit locator waits for modal persistence; transaction validation via on-chain balance checks using ethers.js client queries

  • Complex Test Data Setup & Isolation
    Challenge: Supplying collateral, borrowing, repaying require specific on-chain state; tests must setup independent state without affecting each other
    → Solution: Fixture-provided Anvil client with snapshot/revert; beforeEach snapshots state, afterEach reverts to baseline; eliminates test interdependencies and parallel execution bottlenecks

  • Cross-Network Asset Address Management
    Challenge: Same asset (e.g., USDC) has different contract addresses across networks; tests need network-aware configuration
    → Solution: NetworkChain enum and per-network marketAssets configuration object; constants file maps networks to asset contracts; parameterized tests automatically select correct addresses


Test Coverage

  • Smoke test suite with × 3 networks
  • Extended coverage across 7 test modules
  • Parameterized test matrix (3 assets × 6 networks)
  • Execution modes and filtering yarn playwright test --grep @smoke-tests for rapid feedback

Impact & Business Value

MetricBeforeAfterBenefit
Manual Regression Time30-40 hrs8-10 hrs70% reduction
Time-to-Release2-3 weeks3-4 days5-7x faster
Bug Escape Rate~15%<1%94% improvement
Test Dev Time8 hours2-3 hours65-75% faster
Network Coverage166x broader
Test Flakiness~8%<1%Near-zero