Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[PROTOCOL-622] Manager Profit Fee #477

Open
wants to merge 8 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions config/platform-settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,12 @@ const mainnetPlatformSettings: PlatformSettings = {
min: 0,
max: 10000,
},
ProfitFeePercent: {
processOnDeployment: true,
value: 500,
min: 0,
max: 10000,
},
MaximumLoanDuration: {
processOnDeployment: true,
value: 5184000,
Expand Down Expand Up @@ -101,6 +107,12 @@ export const platformSettings: Record<string, PlatformSettings> = {
min: 0,
max: 10000,
},
ProfitFeePercent: {
processOnDeployment: true,
value: 500,
min: 0,
max: 10000,
},
MaximumLoanDuration: {
processOnDeployment: true,
value: 5184000,
Expand Down Expand Up @@ -163,6 +175,12 @@ export const platformSettings: Record<string, PlatformSettings> = {
min: 500,
max: 10000,
},
ProfitFeePercent: {
processOnDeployment: true,
value: 500,
min: 0,
max: 10000,
},
MaximumLoanDuration: {
processOnDeployment: true,
value: 5184000,
Expand Down Expand Up @@ -225,6 +243,12 @@ export const platformSettings: Record<string, PlatformSettings> = {
min: 0,
max: 10000,
},
ProfitFeePercent: {
processOnDeployment: true,
value: 500,
min: 0,
max: 10000,
},
MaximumLoanDuration: {
processOnDeployment: true,
value: 5184000,
Expand Down
59 changes: 55 additions & 4 deletions contracts/market/RepayFacet.sol
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,12 @@ contract RepayFacet is RolesMods, ReentryMods, PausableMods, EscrowClaimTokens {
uint256 totalOwed
);

event ProfitFeeClaimed(
uint256 indexed loanID,
uint256 amount,
address sender
);

/**
* @notice This event is emitted when a loan has been successfully liquidated
* @param loanID ID of loan from which collateral was withdrawn
Expand Down Expand Up @@ -216,6 +222,47 @@ contract RepayFacet is RolesMods, ReentryMods, PausableMods, EscrowClaimTokens {
);
}

//MGMT Profit Fee
address lendingToken = LibLoans.loan(loanID).lendingToken;

uint256 lendingTokenBalance = LibEscrow.balanceOf(
loanID,
lendingToken
);

uint256 excessProfits;

if (
lendingTokenBalance > LibLoans.loan(loanID).borrowedAmount
) {
//Determine how much excess profit the borrower has earned (look at amt of borrowed $$)
excessProfits =
lendingTokenBalance -
LibLoans.loan(loanID).borrowedAmount;
}

// Multiply excess profits by the profit fee (5%)
uint256 amountForProfitFee = NumbersLib.percent(
excessProfits,
uint16(PlatformSettingsLib.getProfitFeePercent())
);

//If excess profit should be claimed for profit fee, send that amount to the tToken liq pool
if (amountForProfitFee > 0) {
LibEscrow.e(loanID).claimToken(
lendingToken,
address(tToken),
amountForProfitFee
);

emit ProfitFeeClaimed(
loanID,
amountForProfitFee,
msg.sender
);
}
//End MGMT profit fee

// Claim tokens in the escrow for the loan if any
__claimEscrowTokens(loanID);

Expand Down Expand Up @@ -353,7 +400,8 @@ library RepayLib {

// Calculate available collateral for reward
if (collateralAmount > 0) {
collateralValue_ = AppStorageLib.store()
collateralValue_ = AppStorageLib
.store()
.priceAggregator
.getValueFor(
LibLoans.loan(loanID).collateralToken,
Expand Down Expand Up @@ -399,7 +447,8 @@ library RepayLib {
// if the lending reward is less than the collateral lending tokens, then aggregate
// the value for the lending token with the collateral token and send it to the liquidator
if (rewardInLending <= collateralInLending) {
uint256 rewardInCollateral = AppStorageLib.store()
uint256 rewardInCollateral = AppStorageLib
.store()
.priceAggregator
.getValueFor(
LibLoans.loan(loanID).lendingToken,
Expand Down Expand Up @@ -432,7 +481,8 @@ library RepayLib {
address recipient,
uint256 value
) private {
EnumerableSet.AddressSet storage tokens = MarketStorageLib.store()
EnumerableSet.AddressSet storage tokens = MarketStorageLib
.store()
.escrowTokens[loanID];
uint256 valueLeftToTransfer = value;

Expand Down Expand Up @@ -482,7 +532,8 @@ library RepayLib {
if (token == LibLoans.loan(loanID).lendingToken) {
balanceInLending = balance;
} else {
balanceInLending = AppStorageLib.store()
balanceInLending = AppStorageLib
.store()
.priceAggregator
.getValueFor(
token,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,14 @@ library PlatformSettingsLib {
value_ = s(NAMES.LIQUIDATE_REWARD_PERCENT).value;
}

/**
* @notice It gets the current "ProfitFeePercent" setting's value
* @return value_ the current value.
*/
function getProfitFeePercent() internal view returns (uint16 value_) {
value_ = uint16(s(NAMES.PROFIT_FEE_PERCENT).value);
}

/**
* @notice It gets the current "MaximumLoanDuration" setting's value
* @return value_ the current value.
Expand Down
7 changes: 7 additions & 0 deletions contracts/settings/platform/names.sol
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,13 @@ bytes32 constant TERMS_EXPIRY_TIME = keccak256("TermsExpiryTime");
*/
bytes32 constant LIQUIDATE_REWARD_PERCENT = keccak256("LiquidateRewardPercent");

/**
@dev The setting name for the profit fee percent setting.
@dev It represents the percentage value (with 2 decimal places) for the percent of excess profit on borrowed money that will be collected by the protocol.
i.e. A fee of 5% is stored as 500
*/
bytes32 constant PROFIT_FEE_PERCENT = keccak256("ProfitFeePercent");

/**
@dev The setting name for the maximum loan duration settings.
@dev The maximum loan duration setting is defined in seconds. Loans will not be given for timespans larger than the one specified here.
Expand Down
2 changes: 1 addition & 1 deletion tasks/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ task('test').setAction(async (args, hre, runSuper) => {
})

// Disable logging
process.env.DISABLE_LOGS = 'true'
process.env.DISABLE_LOGS = 'false'

// Run the actual test task
await runSuper({
Expand Down
1 change: 1 addition & 0 deletions test/fixtures/markets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ export const fundedMarket = async (
})

tags.push('markets')
tags.push('platform-settings')
await deployments.fixture(tags, {
keepExistingDeployments,
})
Expand Down
Loading