PrimaryLendingPlatformV2Core

Overview

License: MIT

abstract contract PrimaryLendingPlatformV2Core is Initializable, AccessControlUpgradeable, ReentrancyGuardUpgradeable

Core contract for the Primary Lending Platform V2.

Abstract contract that defines the core functionality of the primary lending platform.

Structs info

Ratio

struct Ratio {
	uint8 numerator;
	uint8 denominator;
}

ProjectTokenInfo

struct ProjectTokenInfo {
	bool isListed;
	bool isDepositPaused;
	bool isWithdrawPaused;
	PrimaryLendingPlatformV2Core.Ratio loanToValueRatio;
}

LendingTokenInfo

struct LendingTokenInfo {
	bool isListed;
	bool isPaused;
	BLendingToken bLendingToken;
	PrimaryLendingPlatformV2Core.Ratio loanToValueRatio;
}

BorrowPosition

struct BorrowPosition {
	uint256 loanBody;
	uint256 accrual;
}

Events info

Deposit

event Deposit(address indexed who, address indexed tokenPrj, uint256 prjDepositAmount, address indexed beneficiary)

Emitted when a user deposits project tokens.

Parameters:

Withdraw

event Withdraw(address indexed who, address indexed tokenPrj, address lendingToken, uint256 prjWithdrawAmount, address indexed beneficiary)

Emitted when a user withdraws project tokens.

Parameters:

Supply

event Supply(address indexed who, address indexed supplyToken, uint256 supplyAmount, address indexed supplyBToken, uint256 amountSupplyBTokenReceived)

Emitted when a user supplies lending tokens.

Parameters:

Redeem

event Redeem(address indexed who, address indexed redeemToken, address indexed redeemBToken, uint256 redeemAmount)

Emitted when a user redeems bTokens for the underlying token.

Parameters:

RedeemUnderlying

event RedeemUnderlying(address indexed who, address indexed redeemToken, address indexed redeemBToken, uint256 redeemAmountUnderlying)

Emitted when a user redeems underlying token for the bToken.

Parameters:

Borrow

event Borrow(address indexed who, address indexed borrowToken, uint256 borrowAmount, address indexed prjAddress, uint256 prjAmount)

Emitted when a user borrows lending tokens.

Parameters:

RepayBorrow

event RepayBorrow(address indexed who, address indexed borrowToken, uint256 borrowAmount, address indexed prjAddress, bool isPositionFullyRepaid)

Emitted when a user repays borrowed lending tokens.

Parameters:

SetModeratorContract

event SetModeratorContract(address indexed newAddress)

Emitted when the moderator contract address is updated.

Parameters:

Constants info

MODERATOR_ROLE (0x797669c9)

bytes32 constant MODERATOR_ROLE = keccak256("MODERATOR_ROLE")

State variables info

priceOracle (0x2630c12f)

contract IPriceProviderAggregator priceOracle

projectTokens (0xb269449f)

address[] projectTokens

projectTokenInfo (0x85f4da5c)

mapping(address => struct PrimaryLendingPlatformV2Core.ProjectTokenInfo) projectTokenInfo

lendingTokens (0x6f5f74f2)

address[] lendingTokens

lendingTokenInfo (0x3299093b)

mapping(address => struct PrimaryLendingPlatformV2Core.LendingTokenInfo) lendingTokenInfo

totalDepositedProjectToken (0xef867f0f)

mapping(address => uint256) totalDepositedProjectToken

borrowPosition (0xa9ce9417)

mapping(address => mapping(address => mapping(address => struct PrimaryLendingPlatformV2Core.BorrowPosition))) borrowPosition

totalBorrow (0xb090cf22)

mapping(address => mapping(address => uint256)) totalBorrow

borrowLimitPerCollateral (0x739d7547)

mapping(address => uint256) borrowLimitPerCollateral

totalBorrowPerLendingToken (0xc3ee8b6f)

mapping(address => uint256) totalBorrowPerLendingToken

borrowLimitPerLendingToken (0x9f9184db)

mapping(address => uint256) borrowLimitPerLendingToken

isRelatedContract (0x25de3084)

mapping(address => bool) isRelatedContract

primaryLendingPlatformLeverage (0x10c0bbed)

contract IPrimaryLendingPlatformLeverage primaryLendingPlatformLeverage

primaryLendingPlatformModerator (0xf32d0f2f)

address primaryLendingPlatformModerator

Modifiers info

onlyAdmin

modifier onlyAdmin()

Modifier that allows only the admin to call the function.

isProjectTokenListed

modifier isProjectTokenListed(address projectToken)

Modifier that requires the project token to be listed.

Parameters:

isLendingTokenListed

modifier isLendingTokenListed(address lendingToken)

Modifier that requires the lending token to be listed.

Parameters:

onlyRelatedContracts

modifier onlyRelatedContracts()

Modifier that allows only related contracts to call the function.

onlyModeratorContract

modifier onlyModeratorContract()

Modifier that allows only the moderator contract to call the function.

Functions info

initialize (0x8129fc1c)

function initialize() public initializer

Initializes the contract and sets the name, symbol, and default roles.

setPrimaryLendingPlatformModerator (0x4123cf11)

function setPrimaryLendingPlatformModerator(
    address newModeratorContract
) external onlyAdmin

Sets the address of the new moderator contract for the Primary Lending Platform.

Requirements:

  • newModeratorContract cannot be the zero address.

  • Only the admin can call this function.

Parameters:

setPriceOracle (0x530e784f)

function setPriceOracle(address newPriceOracle) external onlyModeratorContract

Sets the price oracle contract address.

Requirements:

  • Only the moderator contract can call this function.

Parameters:

setPrimaryLendingPlatformLeverage (0xa1ab5419)

function setPrimaryLendingPlatformLeverage(
    address newPrimaryLendingPlatformLeverage
) external onlyModeratorContract

Sets the address of the new primary index token leverage contract by the moderator contract.

Requirements:

  • Only the moderator contract can call this function.

Parameters:

setRelatedContract (0xdcb4252a)

function setRelatedContract(
    address relatedContract,
    bool isRelated
) public onlyModeratorContract

Sets the related contract status for a given contract address.

Requirements:

  • The caller must be the moderator contract.

Parameters:

removeProjectToken (0x3af44bfa)

function removeProjectToken(
    uint256 projectTokenId,
    address projectToken
) external onlyModeratorContract

Removes a project token from the platform.

Requirements:

  • The caller must be the moderator contract.

  • The project token must exist in the platform.

Parameters:

removeLendingToken (0xc1ab02ee)

function removeLendingToken(
    uint256 lendingTokenId,
    address lendingToken
) external onlyModeratorContract

Removes a lending token from the platform.

Requirements:

  • The caller must be the moderator contract.

  • The lending token address must be valid.

Parameters:

setBorrowLimitPerCollateralAsset (0x8e85cdfa)

function setBorrowLimitPerCollateralAsset(
    address projectToken,
    uint256 newBorrowLimit
) external onlyModeratorContract

Sets the borrow limit for a specific collateral asset.

Requirements:

  • The caller must be the moderator contract.

Parameters:

setBorrowLimitPerLendingAsset (0x92a39190)

function setBorrowLimitPerLendingAsset(
    address lendingToken,
    uint256 newBorrowLimit
) external onlyModeratorContract

Sets the borrow limit for a specific lending asset.

Requirements:

  • The caller must be the moderator contract.

Parameters:

setProjectTokenInfo (0x4a5333bc)

function setProjectTokenInfo(
    address projectToken,
    bool isDepositPaused,
    bool isWithdrawPaused,
    uint8 loanToValueRatioNumerator,
    uint8 loanToValueRatioDenominator
) external onlyModeratorContract

Sets the information of a project token.

Requirements:

  • The caller must be the moderator contract.

Parameters:

setLendingTokenInfo (0x821363a0)

function setLendingTokenInfo(
    address lendingToken,
    address bLendingToken,
    bool isPaused,
    uint8 loanToValueRatioNumerator,
    uint8 loanToValueRatioDenominator
) external onlyModeratorContract

Sets the lending token information for a given lending token.

Requirements:

  • The caller must be the moderator contract.

Parameters:

deposit (0x47e7ef24)

function deposit(
    address projectToken,
    uint256 projectTokenAmount
) external isProjectTokenListed(projectToken) nonReentrant

Deposits project tokens into the platform.

Deposits project tokens and calculates the deposit position.

Requirements:

  • The project token must be listed.

  • The project token must not be paused for deposits.

  • The project token amount must be greater than 0.

Effects:

  • Transfers the project tokens from the user to the contract.

  • Calculates the deposit position for the user.

Parameters:

depositFromRelatedContracts (0xbf423b75)

function depositFromRelatedContracts(
    address projectToken,
    uint256 projectTokenAmount,
    address user,
    address beneficiary
) external isProjectTokenListed(projectToken) nonReentrant onlyRelatedContracts

Deposits project tokens from related contracts into the platform.

Requirements:

  • The project token must be listed.

  • Caller must be a related contract.

  • The project token must not be paused for deposits.

  • The project token amount must be greater than 0.

Effects:

  • Transfers the project tokens from the user to the contract.

  • Calculates the deposit position for the user.

Parameters:

calcAndTransferDepositPosition (0x556d4704)

function calcAndTransferDepositPosition(
    address projectToken,
    uint256 projectTokenAmount,
    address user,
    address receiver
)
    external
    isProjectTokenListed(projectToken)
    onlyRelatedContracts
    nonReentrant
    returns (uint256)

Calculates and transfers the deposit position of a user for a specific project token.

Requirements:

  • The project token must be listed.

  • Called by a related contract.

Effects:

  • Decreases the deposited project token amount in the user's deposit position.

  • Decreases the total deposited project token amount.

  • Transfers the project tokens to the receiver.

Parameters:

Return values:

calcDepositPosition (0xdf5e6bed)

function calcDepositPosition(
    address projectToken,
    uint256 projectTokenAmount,
    address user
) external isProjectTokenListed(projectToken) onlyRelatedContracts nonReentrant

Calculates the deposit position for a user based on the project token, project token amount and user address.

Requirements:

  • The project token must be listed.

  • Called by a related contract.

Parameters:

getCollateralAvailableToWithdraw (0x72620613)

function getCollateralAvailableToWithdraw(
    address account,
    address projectToken,
    address lendingToken
) public view returns (uint256 collateralProjectToWithdraw)

Calculates the amount of collateral available to withdraw for a given account, project token and lending token.

Parameters:

Return values:

calcBorrowPosition (0x2dfee307)

function calcBorrowPosition(
    address borrower,
    address projectToken,
    address lendingToken,
    uint256 lendingTokenAmount,
    address currentLendingToken
)
    external
    isProjectTokenListed(projectToken)
    isLendingTokenListed(lendingToken)
    onlyRelatedContracts
    nonReentrant

Allows a related contract to calculate the new borrow position of a user.

Requirements:

  • The project token must be listed.

  • The lending token must be listed.

  • Called by a related contract.

Parameters:

getLendingAvailableToBorrow (0x07445b52)

function getLendingAvailableToBorrow(
    address account,
    address projectToken,
    address lendingToken
) public view returns (uint256 availableToBorrow)

Calculates the lending token available amount for borrowing.

Parameters:

Return values:

repay (0x1da649cf)

function repay(
    address projectToken,
    address lendingToken,
    uint256 lendingTokenAmount
)
    external
    isProjectTokenListed(projectToken)
    isLendingTokenListed(lendingToken)
    nonReentrant
    returns (uint256)

Repays a specified amount of lendingToken for a given project token and lending token.

Allows a borrower to repay their outstanding loan for a given project token and lending token.

Requirements:

  • The project token must be listed.

  • The lending token must be listed.

  • The lending amount must be greater than 0.

  • The borrower must have an outstanding loan for the given project and lending token before.

Effects: Updates the interest in the borrower's borrow positions for the given lendingToken.

  • Repays the specified lendingTokenAmount towards the borrower's loan.

  • May fully or partially repay the borrow position, depending on the repayment amount and outstanding loan.

Parameters:

Return values:

repayFromRelatedContract (0xf432e4e2)

function repayFromRelatedContract(
    address projectToken,
    address lendingToken,
    uint256 lendingTokenAmount,
    address repairer,
    address borrower
)
    external
    isProjectTokenListed(projectToken)
    isLendingTokenListed(lendingToken)
    onlyRelatedContracts
    nonReentrant
    returns (uint256)

Allows a related contract to repay the outstanding loan for a given borrower's project token and lending token.

Requirements:

  • The project token must be listed.

  • The lending token must be listed.

  • Called by a related contract.

  • The lending amount must be greater than 0.

  • The borrower must have an outstanding loan for the given project and lending token before.

Effects: Updates the interest in the borrower's borrow positions for the given lendingToken.

  • Repays the specified lendingTokenAmount towards the borrower's loan.

  • May fully or partially repay the borrow position, depending on the repayment amount and outstanding loan.

Parameters:

Return values:

updateInterestInBorrowPositions (0x9a812edd)

function updateInterestInBorrowPositions(
    address account,
    address lendingToken
) public

This function is called to update the interest in a borrower's borrow position.

Parameters:

getTokensUpdateFinalPrices (0xef290120)

function getTokensUpdateFinalPrices(
    address projectToken,
    address actualLendingToken,
    bool isBorrow
) public view returns (address[] memory tokens)

This function is called when performing operations using token prices, to determine which tokens will need to update their final price.

Parameters:

Return values:

pit (0x72d456af)

function pit(
    address account,
    address projectToken,
    address lendingToken
) public view returns (uint256)

Returns the PIT (primary index token) value for a given account and position after a position is opened.

Formula: pit = $ * LVR of position.

Parameters:

Return values:

pitCollateral (0x1893be9f)

function pitCollateral(
    address account,
    address projectToken
) public view returns (uint256)

Returns the PIT (primary index token) value for a given account and collateral before a position is opened.

Formula: pit = $ * LVR of project token.

Parameters:

Return values:

getLendingToken (0x2ce36230)

function getLendingToken(
    address user,
    address projectToken
) public view returns (address actualLendingToken)

Returns the actual lending token of a user's borrow position for a specific project token.

Parameters:

Return values:

pitRemaining (0xd1a3d2ae)

function pitRemaining(
    address account,
    address projectToken,
    address lendingToken
) public view returns (uint256 remaining)

Returns the remaining PIT (primary index token) of a user's borrow position.

Parameters:

Return values:

totalOutstanding (0xac15382f)

function totalOutstanding(
    address account,
    address projectToken,
    address lendingToken
) public view returns (uint256)

Returns the total outstanding amount of a user's borrow position for a specific project token and lending token.

Parameters:

Return values:

healthFactor (0xcc224bff)

function healthFactor(
    address account,
    address projectToken,
    address lendingToken
) public view returns (uint256 numerator, uint256 denominator)

Returns the health factor of a user's borrow position for a specific project token and lending token.

Parameters:

Return values:

getTokenEvaluation (0x3598a7a9)

function getTokenEvaluation(
    address token,
    uint256 tokenAmount
) public view returns (uint256 collateralEvaluation, uint256 capitalEvaluation)

Returns the price of a specific token amount in USD.

Parameters:

Return values:

lendingTokensLength (0x2412b575)

function lendingTokensLength() public view returns (uint256)

Returns the length of the lending tokens array.

Return values:

projectTokensLength (0x4a41d29e)

function projectTokensLength() public view returns (uint256)

Returns the length of the project tokens array.

Return values:

getPosition (0x713390f5)

function getPosition(
    address account,
    address projectToken,
    address lendingToken
)
    public
    view
    returns (
        uint256 depositedProjectTokenAmount,
        uint256 loanBody,
        uint256 accrual,
        uint256 healthFactorNumerator,
        uint256 healthFactorDenominator
    )

Returns the details of a user's borrow position for a specific project token and lending token.

Parameters:

Return values:

getDepositedAmount (0x0fefc251)

function getDepositedAmount(
    address projectToken,
    address user
) public view returns (uint256)

Returns the amount of project tokens deposited by a user for a specific project token and collateral token.

Parameters:

Return values:

getRelatedContract (0x2060128e)

function getRelatedContract(address relatedContract) public view returns (bool)

Returns whether an address is a related contract or not.

Parameters:

Return values:

getTotalBorrowPerCollateral (0x65647a59)

function getTotalBorrowPerCollateral(
    address projectToken
) public view returns (uint256)

Gets total borrow amount in USD per collateral for a specific project token.

Parameters:

Return values:

getTotalBorrowPerLendingToken (0x961540e1)

function getTotalBorrowPerLendingToken(
    address lendingToken
) public view returns (uint256 amountInUSD)

Gets total borrow amount in USD for a specific lending token.

Parameters:

Return values:

totalOutstandingInUSD (0xb189b70a)

function totalOutstandingInUSD(
    address account,
    address projectToken,
    address lendingToken
) public view returns (uint256 amountInUSD)

Converts the total outstanding amount of a user's borrow position to USD.

Parameters:

Return values:

getLoanToValueRatio (0xe84dc1b3)

function getLoanToValueRatio(
    address projectToken,
    address lendingToken
) public view returns (uint256 lvrNumerator, uint256 lvrDenominator)

Gets the loan to value ratio of a position made by a project token and a lending token.

Parameters:

Return values:

Last updated