PWN Fee Calculator

1. Summary

PWNFeeCalculator.sol is a library that implements the calculateFeeAmount function to calculate the fee amount based on the protocol fee and the amount lent.

3. Contract details

  • PWNFeeCalculator.sol is written in Solidity version 0.8.16

calculateFeeAmount

Overview

Based on the protocol fee and the amount that is being lent, this function calculates and returns the fee and the amount lent with the fee deducted.

This function takes two arguments supplied by the caller:

  • uint16fee - Fee value in basis points. The value of 100 is a 1% fee.

  • uint256loanAmount - Amount of an asset used as a loan credit.

Implementation

function calculateFeeAmount(uint16 fee, uint256 loanAmount) internal pure returns (uint256 feeAmount, uint256 newLoanAmount) {
    if (fee == 0)
        return (0, loanAmount);

    unchecked {
        if ((loanAmount * fee) / fee == loanAmount)
            feeAmount = loanAmount * uint256(fee) / 1e4;
        else
            feeAmount = loanAmount / 1e4 * uint256(fee);
    }
    newLoanAmount = loanAmount - feeAmount;
}

Last updated