Loan Lifecycle

A complete walkthrough of the DegenPrime Smart Loan lifecycle: create, fund, borrow, manage, repay, and withdraw.

1
Create
2
Fund
3
Borrow
4
Manage
5
Repay
6
Withdraw

1. Create a Smart Loan

Every user gets a dedicated Smart Loan account -- an EIP-2535 Diamond proxy deployed via the Factory. This isolated account holds your collateral and borrowed assets.

typescript
const FACTORY = "0x5A6a0e2702cF4603a098C3Df01f3F0DF56115456";

// Option A: Create empty loan
const hash = await walletClient.writeContract({
  address: FACTORY,
  abi: factoryAbi,
  functionName: "createLoan",
});

// Option B: Create + fund in one tx (saves gas)
const tokenId = keccak256(encodePacked(["string"], ["USDC"]));
const amount = 1000n * 10n ** 6n;

const hash2 = await walletClient.writeContract({
  address: FACTORY,
  abi: factoryAbi,
  functionName: "createAndFundLoan",
  args: [tokenId, amount],
});

2. Fund with Collateral

Deposit tokens into your loan to establish collateral. The token must be approved to your loan address before calling fund().

typescript
// Approve token transfer
await walletClient.writeContract({
  address: usdcAddress,
  abi: erc20Abi,
  functionName: "approve",
  args: [loanAddress, amount],
});

// Fund the loan
const tokenId = keccak256(encodePacked(["string"], ["USDC"]));
await walletClient.writeContract({
  address: loanAddress,
  abi: fundingFacetAbi,
  functionName: "fund",
  args: [tokenId, amount],
});

3. Borrow Assets

With collateral deposited, you can borrow from lending pools. DegenPrime allows up to 8x leverage. Borrow calls trigger a solvency check and must include RedStone price data.

Important: Borrow transactions must be wrapped with RedStone price data using WrapperBuilder. See the Oracle Integration docs.

typescript
import { WrapperBuilder } from "@redstone-finance/evm-connector";

const wrappedContract = WrapperBuilder
  .wrap(contract)
  .usingDataService({
    dataServiceId: "redstone-arbitrum-prod",
    uniqueSignersCount: 3,
  });

const tokenId = keccak256(encodePacked(["string"], ["WETH"]));
const borrowAmount = parseEther("2"); // Borrow 2 WETH

await wrappedContract.borrow(tokenId, borrowAmount);

4. Manage Position

While your loan is active, you can swap tokens (ParaSwap), add Aerodrome LP positions, or adjust your collateral. Monitor your health ratio to stay above 1.0.

typescript
// Check health ratio
const healthRatio = await client.readContract({
  address: loanAddress,
  abi: viewFacetAbi,
  functionName: "getHealthRatio",
});
console.log("Health:", Number(healthRatio) / 1e18);

// Swap assets within the loan via ParaSwap
await wrappedContract.paraSwap(swapCalldata, minOutput);

// Add Aerodrome liquidity
await wrappedContract.addLiquidityAerodrome(
  poolAddress, amount0, amount1
);

5. Repay Debt

Repay borrowed assets to reduce your debt and improve your health ratio. You can do partial or full repayments.

typescript
const tokenId = keccak256(encodePacked(["string"], ["WETH"]));
const repayAmount = parseEther("1"); // Repay 1 WETH

await walletClient.writeContract({
  address: loanAddress,
  abi: borrowingFacetAbi,
  functionName: "repay",
  args: [tokenId, repayAmount],
});

6. Withdraw Collateral

DegenPrime uses a 24-hour withdrawal intent system. You must first declare your intent and wait before executing. See the Withdrawal System docs for details.

typescript
// Step 1: Create withdrawal intent (starts 24h timer)
await walletClient.writeContract({
  address: loanAddress,
  abi: withdrawalIntentFacetAbi,
  functionName: "createWithdrawalIntent",
  args: [tokenId, withdrawAmount],
});

// Step 2: Wait 24 hours...

// Step 3: Fulfill the withdrawal
await wrappedContract.fulfillWithdrawalIntent(tokenId);
// Tokens are now in your wallet