Skip to content

Credit Delegation

Credit delegation allows a depositor to deposit funds in the protocol to earn interest, and delegate borrowing power (i.e. their credit) to other users.

The enforcement of the loan and its terms are agreed upon between the depositor and borrowers, which can be either off-chain via legal agreements or on-chain via smart contracts.

This enables:

  • The supplier (aka delegator) to earn extra yield on top of the yield they already earn from the protocol.
  • The borrowers (aka delegatees) to access an uncollateralized loan.

WARNING

Warning
The delegatee cannot abuse credit approval to liquidate delegator i.e. if the borrow puts delegator's position in HF < HEALTH_FACTOR_LIQUIDATION_THRESHOLD, then borrow will fail.

Approving the delegation

The approveDelegation() or delegationWithSig() must be called by the supplier (delegator), approving the borrower (delegatee) a certain amount.

This is done for each debt token that needs to be delegated.

Example

To approve delegation, the approveDelegation method of the debt token contract is used. The supplier (delegator) allows the borrower (delegatee) a certain amount.

typescript
// Note: Please don't treat this code as a working solution and the usage should be more as a "template" to suit your dApp's needs.

import { ethers } from "ethers";

const provider = new ethers.providers.JsonRpcProvider(
  "https://bartio.rpc.berachain.com/"
);
const signer = new ethers.Wallet("YOUR_PRIVATE_KEY", provider);

const debtTokenAddress = "0xYOUR_DEBT_TOKEN_ADDRESS"; // Replace with actual debt token address
const delegationAddress = "0xDELEGATION_ADDRESS"; // Replace with actual delegation address
const amount = ethers.utils.parseUnits("1000.0", 18); // Amount to delegate

const debtTokenAbi = [
  "function approveDelegation(address delegatee, uint256 amount) external",
];
const debtTokenContract = new ethers.Contract(
  debtTokenAddress,
  debtTokenAbi,
  signer
);

async function approveDelegation() {
  try {
    const tx = await debtTokenContract.approveDelegation(
      delegationAddress,
      amount
    );
    await tx.wait();
    console.log(`Delegation approved: ${tx.hash}`);
  } catch (error) {
    console.error("Error approving delegation:", error);
  }
}

approveDelegation();

TIP

Tip
The delegator does not need to already have supplied funds in the protocol to approveDelegation(). However, before the delegatee executes borrow(), there must be sufficient collateral supplied by delegator in the protocol.

Borrowing the credit

The borrower (delegatee) calls the borrow() method on the 'Pool', using the supplier's (delegator's) address in final parameter onBehalfOf.

The borrower's available credit is reduced by the borrowed amount.

Example

typescript
// Note: Please don't treat this code as a working solution and the usage should be more as a "template" to suit your dApp's needs.
import { ethers } from "ethers";

const provider = new ethers.providers.JsonRpcProvider(
  "https://bartio.rpc.berachain.com/"
);
const signer = new ethers.Wallet("YOUR_PRIVATE_KEY", provider);

const poolAddressesProvider = "0x8297A07f87a8576b88d46e636c05B84E4Ea8265D";
const lendingPoolAbi = [
  "function borrow(address asset, uint256 amount, uint256 interestRateMode, uint16 referralCode, address onBehalfOf) external",
];
const assetAddress = "0xASSET_ADDRESS"; // Replace with actual asset address
const borrowAmount = ethers.utils.parseUnits("1000.0", 18);

const poolAddressesProviderAbi = [
  "function getPool() external view returns (address)",
];

async function getPoolAddress(): Promise<string> {
  const lendingPoolAddressesProvider = new ethers.Contract(
    poolAddressesProvider,
    poolAddressesProviderAbi,
    provider
  );
  return await lendingPoolAddressesProvider.getPool();
}

async function borrow() {
  try {
    const lendingPoolAddress = await getPoolAddress();
    const lendingPoolContract = new ethers.Contract(
      lendingPoolAddress,
      lendingPoolAbi,
      signer
    );

    const tx = await lendingPoolContract.borrow(
      assetAddress,
      borrowAmount,
      2, // variable interest rate
      0,
      DELEGATOR_ADDRESS
    );
    await tx.wait();
    console.log(`Borrowed: ${tx.hash}`);
  } catch (error) {
    console.error("Error borrowing:", error);
  }
}

borrow();

Repaying the credit

Anyone can repay the debt OnBehalf of the user, by calling one of the methods - repay() or repayWithPermit() The supplier (aka creditor) can also use repayWithATokens() method to repay debt with their aTokens of the underlying debt asset in the same pool.