Lender
Borrower
Liquidator
Deploy
// Step 1: Approve loanToken spending
await loanToken.approve(lendingContractAddress, amount);
// Step 2: Deposit into the liquidity pool
await lending.depositLiquidity(amount);
// → Contract calls: loanToken.transferFrom(caller, self, amount)
// Withdraw (no approve needed — contract sends to you)
await lending.withdrawLiquidity(0n); // 0 = withdraw all
// → loanToken.transfer(caller, principal + interest)
// Step 1: Lock collateral
await collateralToken.approve(lendingContractAddress, collateralAmount);
await lending.depositCollateral(collateralAmount);
// Step 2: Borrow loan tokens (colPrice: 1e18 = 1:1 value)
await lending.borrow(loanAmount, BigInt(1e18));
// → loanToken.transfer(caller, loanAmount)
// Step 3: Repay debt
const { result: totalDebt } = await lending.getTotalDebt(myAddress);
await loanToken.approve(lendingContractAddress, totalDebt);
await lending.repay();
// → collateralToken returned to borrower
// Find undercollateralized positions (ratio < 120)
const { result: ratio } = await lending.getCollateralRatio(borrower, currentPrice);
if (ratio < 120n) {
// Get total outstanding debt
const { result: debt } = await lending.getTotalDebt(borrower);
// Approve loan token repayment
await loanToken.approve(lendingContractAddress, debt);
// Liquidate — receive collateral + 5% bonus
await lending.liquidate(borrower, currentPrice);
}
# Set environment variables
export WALLET_WIF="cQz..."
export LOAN_TOKEN_ADDRESS="bcrt1p..."
export COLLATERAL_TOKEN_ADDRESS="bcrt1p..."
export NETWORK="regtest"
export RPC_URL="https://regtest.opnet.org"
# Build the contract
npm run build # → build/contract.wasm
# Deploy and run full flow
npm run deploy # deploys → approve → deposit → borrow → repay → withdraw