Executing a Loan
Executing a loan requires the calculation of specific values and calling the function acceptCommitment on the LenderCommitmentForwarderAlpha contract.
Approval checks
Before executing the loan on chain 2 approvals must be completed.
Collateral token Allowance
The collateral token's allowance must be higher that the desired collateral for the transaction. The allowance must be set on the collateralManagerAddress.
To check the collateral token allowance
functionName: "allowance",
arguments: [userAddress, collateralManagerAddress]
contract: collateralTokenAddressTo write the collateral token allowance:
functionName: "approve",
arguments: [collateralManagerAddress, amountToApprove]
contract: collateralTokenAddressThe collateralManagerAddress can be found in the introduction of this guide
Market Forwarder
The MarketForwarder for the offer's marketplace must be approved by the user.
To check if the MarketForwarder has been approved
functionName: "hasApprovedMarketForwarder",
arguments: [marketplaceId, commitmentForwarderAddress, userAddress],
contract: TellerV2To approve the market forwarder
functionName: "approveMarketForwarder",
arguments: [marketplaceId, commitmentForwarderAddress],
contract: TellerV2marketplaceId and commitmentForwarderAddress both come from the loan offer query response.
Executing the loan
Once there is enough allowance on the collateral token and the MarketForwarder has been approved, the loan can be executed.
The function lives in the LenderCommitmentForwarderAlpha contract.
Detailed arguments
commitmentId: The id of the commitment being accepted.principalAmount: The amount of currency to borrow for the loan.This is is calculated as specified in this section:
collateralAmount: The amount of collateral to use for the loan.This is inputted by the user
collateralTokenId: The tokenId of collateral to use for the loan if ERC721 or ERC1155.For tokens, this is
0
collateralTokenAddress: The contract address to use for the loan collateral token.sinterestRate: The interest rate APY to use for the loan in basis points.loanDuration: The overall duration for the loan. Must be longer than market payment cycle duration.
functionName: "acceptCommitment",
arguments: [commitmentId, principalTokenValue, collateralTokenValue, 0, collateralTokenAddress,interestRate, loanDuration]Calculating principalAmount
principalAmountTo calculate the principalAmount from the maxPrincipalPerCollateral that comes from the offer query, all you need to do is multiply the collateralAmount by the maxPrincipalPerCollateral and then divide it by 10^18 since it's the expansion factor.
const principalAmount = collateralAmount // requested collateral amount
.mul(maxPrincipalPerCollateral) // this amount comes from the subgraph response for the particular offer
.div(
BigNumber.from(10).pow(18) // expansion factor
)
);Last updated