Skip to content

Commit

Permalink
fix: Remove discount lock period mechanism (#293)
Browse files Browse the repository at this point in the history
* fix: remove discount lock period from GhoDebtToken

* test: Update tests for new rebalance behaviour
  • Loading branch information
miguelmtzinf authored Feb 20, 2023
1 parent 7652f6a commit 81bb51b
Show file tree
Hide file tree
Showing 12 changed files with 36 additions and 181 deletions.
4 changes: 1 addition & 3 deletions src/contracts/facilitators/aave/misc/UiGhoDataProvider.sol
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ contract UiGhoDataProvider is IUiGhoDataProvider {
ghoBaseVariableBorrowRate: baseData.currentVariableBorrowRate,
ghoDiscountedPerToken: discountRateStrategy.GHO_DISCOUNTED_PER_DISCOUNT_TOKEN(),
ghoDiscountRate: discountRateStrategy.DISCOUNT_RATE(),
ghoDiscountLockPeriod: debtToken.getDiscountLockPeriod(),
ghoMinDebtTokenBalanceForDiscount: discountRateStrategy.MIN_DISCOUNT_TOKEN_BALANCE(),
ghoMinDiscountTokenBalanceForDiscount: discountRateStrategy.MIN_DEBT_TOKEN_BALANCE(),
ghoReserveLastUpdateTimestamp: baseData.lastUpdateTimestamp,
Expand All @@ -66,8 +65,7 @@ contract UiGhoDataProvider is IUiGhoDataProvider {
userGhoDiscountPercent: debtToken.getDiscountPercent(user),
userDiscountTokenBalance: IERC20(discountToken).balanceOf(user),
userPreviousGhoBorrowIndex: debtToken.getPreviousIndex(user),
userGhoScaledBorrowBalance: debtToken.scaledBalanceOf(user),
userDiscountLockPeriodEndTimestamp: debtToken.getUserRebalanceTimestamp(user)
userGhoScaledBorrowBalance: debtToken.scaledBalanceOf(user)
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ interface IUiGhoDataProvider {
uint256 ghoBaseVariableBorrowRate;
uint256 ghoDiscountedPerToken;
uint256 ghoDiscountRate;
uint256 ghoDiscountLockPeriod;
uint256 ghoMinDebtTokenBalanceForDiscount;
uint256 ghoMinDiscountTokenBalanceForDiscount;
uint40 ghoReserveLastUpdateTimestamp;
Expand All @@ -25,7 +24,6 @@ interface IUiGhoDataProvider {
uint256 userDiscountTokenBalance;
uint256 userPreviousGhoBorrowIndex;
uint256 userGhoScaledBorrowBalance;
uint256 userDiscountLockPeriodEndTimestamp;
}

/**
Expand Down
45 changes: 1 addition & 44 deletions src/contracts/facilitators/aave/tokens/GhoVariableDebtToken.sol
Original file line number Diff line number Diff line change
Expand Up @@ -47,16 +47,11 @@ contract GhoVariableDebtToken is DebtTokenBase, ScaledBalanceTokenBase, IGhoVari
uint128 accumulatedDebtInterest;
// Discount percent of the user (expressed in bps)
uint16 discountPercent;
// Timestamp when user's discount can be rebalanced
uint40 rebalanceTimestamp;
}

// Map of users' address and their gho state data (userAddress => ghoUserState)
mapping(address => GhoUserState) internal _ghoUserState;

// The amount of time a user's discount is guarded from being rebalanced (expressed in seconds)
uint256 internal _discountLockPeriod;

/**
* @dev Only discount token can call functions marked by this modifier.
*/
Expand Down Expand Up @@ -337,12 +332,6 @@ contract GhoVariableDebtToken is DebtTokenBase, ScaledBalanceTokenBase, IGhoVari

/// @inheritdoc IGhoVariableDebtToken
function rebalanceUserDiscountPercent(address user) external override {
require(
_ghoUserState[user].rebalanceTimestamp < block.timestamp,
'DISCOUNT_LOCK_PERIOD_NOT_OVER'
);
require(_ghoUserState[user].rebalanceTimestamp != 0, 'NO_USER_DISCOUNT_TO_REBALANCE');

uint256 index = POOL.getReserveNormalizedVariableDebt(_underlyingAsset);
uint256 previousScaledBalance = super.balanceOf(user);
uint256 discountPercent = _ghoUserState[user].discountPercent;
Expand All @@ -367,23 +356,6 @@ contract GhoVariableDebtToken is DebtTokenBase, ScaledBalanceTokenBase, IGhoVari
emit Mint(address(0), user, balanceIncrease, balanceIncrease, index);
}

/// @inheritdoc IGhoVariableDebtToken
function updateDiscountLockPeriod(uint256 newLockPeriod) external override onlyPoolAdmin {
uint256 oldLockPeriod = _discountLockPeriod;
_discountLockPeriod = uint40(newLockPeriod);
emit DiscountLockPeriodUpdated(oldLockPeriod, newLockPeriod);
}

/// @inheritdoc IGhoVariableDebtToken
function getDiscountLockPeriod() external view override returns (uint256) {
return _discountLockPeriod;
}

/// @inheritdoc IGhoVariableDebtToken
function getUserRebalanceTimestamp(address user) external view override returns (uint256) {
return _ghoUserState[user].rebalanceTimestamp;
}

/**
* @notice Implements the basic logic to mint a scaled balance token.
* @param caller The address performing the mint
Expand Down Expand Up @@ -532,22 +504,7 @@ contract GhoVariableDebtToken is DebtTokenBase, ScaledBalanceTokenBase, IGhoVari

if (previousDiscountPercent != newDiscountPercent) {
_ghoUserState[user].discountPercent = newDiscountPercent.toUint16();
}

if (newDiscountPercent != 0) {
uint40 newRebalanceTimestamp = uint40(block.timestamp + _discountLockPeriod);
_ghoUserState[user].rebalanceTimestamp = newRebalanceTimestamp;
emit DiscountPercentLocked(
user,
previousDiscountPercent,
newDiscountPercent,
newRebalanceTimestamp
);
} else {
if (previousDiscountPercent != newDiscountPercent) {
_ghoUserState[user].rebalanceTimestamp = 0;
emit DiscountPercentLocked(user, previousDiscountPercent, 0, 0);
}
emit DiscountPercentUpdated(user, previousDiscountPercent, newDiscountPercent);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,27 +33,15 @@ interface IGhoVariableDebtToken is IVariableDebtToken {
event DiscountTokenUpdated(address indexed oldDiscountToken, address indexed newDiscountToken);

/**
* @dev Emitted when the discount lock period is updated
* @param oldDiscountLockPeriod The value of the old DiscountLockPeriod
* @param newDiscountLockPeriod The value of the new DiscountLockPeriod
*/
event DiscountLockPeriodUpdated(
uint256 indexed oldDiscountLockPeriod,
uint256 indexed newDiscountLockPeriod
);

/**
* @dev Emitted when a user's discount or rebalanceTimestamp is updated
* @dev Emitted when a user's discount is updated
* @param user The address of the user
* @param oldDiscountPercent The old discount percent of the user
* @param newDiscountPercent The new discount percent of the user
* @param rebalanceTimestamp Timestamp when a users locked discount can be rebalanced
*/
event DiscountPercentLocked(
event DiscountPercentUpdated(
address indexed user,
uint256 oldDiscountPercent,
uint256 indexed newDiscountPercent,
uint256 indexed rebalanceTimestamp
uint256 indexed newDiscountPercent
);

/**
Expand Down Expand Up @@ -130,27 +118,8 @@ interface IGhoVariableDebtToken is IVariableDebtToken {
function decreaseBalanceFromInterest(address user, uint256 amount) external;

/**
* @notice Rebalances the discount percent of a user if they are past their rebalance timestamp
* @notice Rebalances the discount percent of a user
* @param user The address of the user
*/
function rebalanceUserDiscountPercent(address user) external;

/**
* @notice Updates the discount percent lock period
* @param newLockPeriod The new discount lock period (in seconds)
*/
function updateDiscountLockPeriod(uint256 newLockPeriod) external;

/**
* @notice Returns the discount percent lock period
* @return The discount percent lock period (in seconds)
*/
function getDiscountLockPeriod() external view returns (uint256);

/**
* @notice Returns the timestamp at which a user's discount percent can be rebalanced
* @param user The address of the user's rebalance timestamp being requested
* @return The time when a users discount percent can be rebalanced
*/
function getUserRebalanceTimestamp(address user) external view returns (uint256);
}
1 change: 0 additions & 1 deletion src/helpers/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ export const ghoTokenConfig = {

export const ghoReserveConfig = {
INTEREST_RATE: ethers.utils.parseUnits('2.0', 25),
DISCOUNT_LOCK_PERIOD: 31556952,
};

export const ghoEntityConfig = {
Expand Down
10 changes: 0 additions & 10 deletions src/tasks/testnet-setup/05_set-gho-addresses.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,14 +68,4 @@ task(
console.log(
`VariableDebtToken discount token set to: ${stkAave} in tx: ${updateDiscountTokenTxReceipt.transactionHash}`
);

// Set initial discount lock period
const updateDiscountLockPeriodReceipt = await waitForTx(
await ghoVariableDebtToken.updateDiscountLockPeriod(ghoReserveConfig.DISCOUNT_LOCK_PERIOD)
);
console.log(
`VariableDebtToken discount lock period set to: ${await ghoVariableDebtToken.getDiscountLockPeriod()} in tx: ${
updateDiscountLockPeriodReceipt.transactionHash
}`
);
});
14 changes: 7 additions & 7 deletions src/test/basic-borrow.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ makeSuite('Gho Basic Borrow Flow', (testEnv: TestEnv) => {
.withArgs(ZERO_ADDRESS, users[0].address, borrowAmount)
.to.emit(variableDebtToken, 'Mint')
.withArgs(users[0].address, users[0].address, borrowAmount, 0, oneRay)
.to.not.emit(variableDebtToken, 'DiscountPercentLocked');
.to.not.emit(variableDebtToken, 'DiscountPercentUpdated');

expect(await gho.balanceOf(users[0].address)).to.be.equal(borrowAmount);
expect(await variableDebtToken.totalSupply()).to.be.equal(borrowAmount);
Expand Down Expand Up @@ -108,7 +108,7 @@ makeSuite('Gho Basic Borrow Flow', (testEnv: TestEnv) => {
.withArgs(ZERO_ADDRESS, users[1].address, borrowAmount)
.to.emit(variableDebtToken, 'Mint')
.withArgs(users[1].address, users[1].address, borrowAmount, 0, expIndex)
.to.not.emit(variableDebtToken, 'DiscountPercentLocked');
.to.not.emit(variableDebtToken, 'DiscountPercentUpdated');

expect(await gho.balanceOf(users[1].address)).to.be.equal(borrowAmount);

Expand Down Expand Up @@ -152,7 +152,7 @@ makeSuite('Gho Basic Borrow Flow', (testEnv: TestEnv) => {
.withArgs(ZERO_ADDRESS, users[0].address, amount)
.to.emit(variableDebtToken, 'Mint')
.withArgs(users[0].address, users[0].address, amount, user1ExpectedBalanceIncrease, expIndex)
.to.not.emit(variableDebtToken, 'DiscountPercentLocked');
.to.not.emit(variableDebtToken, 'DiscountPercentUpdated');

const user1Debt = await variableDebtToken.balanceOf(users[0].address);
const user2Debt = await variableDebtToken.balanceOf(users[1].address);
Expand Down Expand Up @@ -200,7 +200,7 @@ makeSuite('Gho Basic Borrow Flow', (testEnv: TestEnv) => {
.withArgs(users[1].address, ZERO_ADDRESS, borrowAmount)
.to.emit(variableDebtToken, 'Burn')
.withArgs(users[1].address, ZERO_ADDRESS, borrowAmount, user2ExpectedInterest, expIndex)
.to.not.emit(variableDebtToken, 'DiscountPercentLocked');
.to.not.emit(variableDebtToken, 'DiscountPercentUpdated');

const user1Debt = await variableDebtToken.balanceOf(users[0].address);
const user2Debt = await variableDebtToken.balanceOf(users[1].address);
Expand Down Expand Up @@ -245,7 +245,7 @@ makeSuite('Gho Basic Borrow Flow', (testEnv: TestEnv) => {
.withArgs(ZERO_ADDRESS, users[2].address, borrowAmount.mul(3))
.to.emit(variableDebtToken, 'Mint')
.withArgs(users[2].address, users[2].address, borrowAmount.mul(3), 0, expIndex)
.to.not.emit(variableDebtToken, 'DiscountPercentLocked');
.to.not.emit(variableDebtToken, 'DiscountPercentUpdated');

expect(await gho.balanceOf(users[2].address)).to.be.equal(borrowAmount.mul(3));
expect(await variableDebtToken.getBalanceFromInterest(users[2].address)).to.be.equal(0);
Expand Down Expand Up @@ -289,7 +289,7 @@ makeSuite('Gho Basic Borrow Flow', (testEnv: TestEnv) => {
.withArgs(ZERO_ADDRESS, users[0].address, amount)
.to.emit(variableDebtToken, 'Mint')
.withArgs(users[0].address, users[0].address, amount, user1ExpectedBalanceIncrease, expIndex)
.to.not.emit(variableDebtToken, 'DiscountPercentLocked');
.to.not.emit(variableDebtToken, 'DiscountPercentUpdated');

expect(await variableDebtToken.balanceOf(users[0].address)).to.be.eq(
user1ExpectedBalance.sub(repayAmount)
Expand Down Expand Up @@ -338,7 +338,7 @@ makeSuite('Gho Basic Borrow Flow', (testEnv: TestEnv) => {
.withArgs(users[0].address, ZERO_ADDRESS, amount)
.to.emit(variableDebtToken, 'Burn')
.withArgs(users[0].address, ZERO_ADDRESS, amount, user1ExpectedBalanceIncrease, expIndex)
.to.not.emit(variableDebtToken, 'DiscountPercentLocked');
.to.not.emit(variableDebtToken, 'DiscountPercentUpdated');

expect(await variableDebtToken.balanceOf(users[0].address)).to.be.eq(0);
expect(await variableDebtToken.getBalanceFromInterest(users[0].address)).to.be.equal(0);
Expand Down
6 changes: 3 additions & 3 deletions src/test/borrow-onBehalf.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ makeSuite('Gho OnBehalf Borrow Flow', (testEnv: TestEnv) => {
.withArgs(ZERO_ADDRESS, users[0].address, borrowAmount)
.to.emit(variableDebtToken, 'Mint')
.withArgs(users[1].address, users[0].address, borrowAmount, 0, oneRay)
.to.not.emit(variableDebtToken, 'DiscountPercentLocked');
.to.not.emit(variableDebtToken, 'DiscountPercentUpdated');

expect(await gho.balanceOf(users[1].address)).to.be.equal(borrowAmount);
expect(await variableDebtToken.getBalanceFromInterest(users[0].address)).to.be.equal(0);
Expand Down Expand Up @@ -119,7 +119,7 @@ makeSuite('Gho OnBehalf Borrow Flow', (testEnv: TestEnv) => {
.withArgs(ZERO_ADDRESS, users[2].address, borrowAmount)
.to.emit(variableDebtToken, 'Mint')
.withArgs(users[2].address, users[2].address, borrowAmount, 0, expIndex)
.to.not.emit(variableDebtToken, 'DiscountPercentLocked');
.to.not.emit(variableDebtToken, 'DiscountPercentUpdated');

expect(await gho.balanceOf(users[2].address)).to.be.equal(borrowAmount);

Expand Down Expand Up @@ -164,7 +164,7 @@ makeSuite('Gho OnBehalf Borrow Flow', (testEnv: TestEnv) => {
.withArgs(users[0].address, ZERO_ADDRESS, borrowAmount)
.to.emit(variableDebtToken, 'Burn')
.withArgs(users[0].address, ZERO_ADDRESS, borrowAmount, user1ExpectedInterest, expIndex)
.to.not.emit(variableDebtToken, 'DiscountPercentLocked');
.to.not.emit(variableDebtToken, 'DiscountPercentUpdated');

const user1Debt = await variableDebtToken.balanceOf(users[0].address);
const user2Debt = await variableDebtToken.balanceOf(users[1].address);
Expand Down
21 changes: 8 additions & 13 deletions src/test/discount-borrow.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,13 +137,8 @@ makeSuite('Gho Discount Borrow Flow', (testEnv: TestEnv) => {
.withArgs(ZERO_ADDRESS, users[1].address, borrowAmount)
.to.emit(variableDebtToken, 'Mint')
.withArgs(users[1].address, users[1].address, borrowAmount, 0, expIndex)
.to.emit(variableDebtToken, 'DiscountPercentLocked')
.withArgs(
users[1].address,
discountPercentBefore,
discountPercent,
txTimestamp.add(ghoReserveConfig.DISCOUNT_LOCK_PERIOD)
);
.to.emit(variableDebtToken, 'DiscountPercentUpdated')
.withArgs(users[1].address, discountPercentBefore, discountPercent);

expect(await variableDebtToken.getDiscountPercent(users[1].address)).to.be.eq(discountPercent);

Expand Down Expand Up @@ -236,7 +231,7 @@ makeSuite('Gho Discount Borrow Flow', (testEnv: TestEnv) => {
.withArgs(ZERO_ADDRESS, users[0].address, amount)
.to.emit(variableDebtToken, 'Mint')
.withArgs(users[0].address, users[0].address, amount, user1BalanceIncrease, expIndex)
.to.not.emit(variableDebtToken, 'DiscountPercentLocked');
.to.not.emit(variableDebtToken, 'DiscountPercentUpdated');

const user1Debt = await variableDebtToken.balanceOf(users[0].address);
const user2Debt = await variableDebtToken.balanceOf(users[1].address);
Expand Down Expand Up @@ -306,8 +301,8 @@ makeSuite('Gho Discount Borrow Flow', (testEnv: TestEnv) => {
.withArgs(users[1].address, ZERO_ADDRESS, borrowAmount)
.to.emit(variableDebtToken, 'Burn')
.withArgs(users[1].address, ZERO_ADDRESS, borrowAmount, user2ExpectedInterest, expIndex)
.to.emit(variableDebtToken, 'DiscountPercentLocked')
.withArgs(users[1].address, user2DiscountPercentBefore, user2DiscountPercent, 0);
.to.emit(variableDebtToken, 'DiscountPercentUpdated')
.withArgs(users[1].address, user2DiscountPercentBefore, user2DiscountPercent);

const user1Debt = await variableDebtToken.balanceOf(users[0].address);
const user2Debt = await variableDebtToken.balanceOf(users[1].address);
Expand Down Expand Up @@ -358,7 +353,7 @@ makeSuite('Gho Discount Borrow Flow', (testEnv: TestEnv) => {
.withArgs(ZERO_ADDRESS, users[2].address, borrowAmount.mul(3))
.to.emit(variableDebtToken, 'Mint')
.withArgs(users[2].address, users[2].address, borrowAmount.mul(3), 0, expIndex)
.to.not.emit(variableDebtToken, 'DiscountPercentLocked');
.to.not.emit(variableDebtToken, 'DiscountPercentUpdated');

expect(await variableDebtToken.getDiscountPercent(users[2].address)).to.be.eq(0);

Expand Down Expand Up @@ -404,7 +399,7 @@ makeSuite('Gho Discount Borrow Flow', (testEnv: TestEnv) => {
.withArgs(ZERO_ADDRESS, users[0].address, amount)
.to.emit(variableDebtToken, 'Mint')
.withArgs(users[0].address, users[0].address, amount, user1ExpectedBalanceIncrease, expIndex)
.to.not.emit(variableDebtToken, 'DiscountPercentLocked');
.to.not.emit(variableDebtToken, 'DiscountPercentUpdated');

expect(await variableDebtToken.getDiscountPercent(users[0].address)).to.be.eq(0);

Expand Down Expand Up @@ -455,7 +450,7 @@ makeSuite('Gho Discount Borrow Flow', (testEnv: TestEnv) => {
.withArgs(users[0].address, ZERO_ADDRESS, amount)
.to.emit(variableDebtToken, 'Burn')
.withArgs(users[0].address, ZERO_ADDRESS, amount, user1ExpectedBalanceIncrease, expIndex)
.to.not.emit(variableDebtToken, 'DiscountPercentLocked');
.to.not.emit(variableDebtToken, 'DiscountPercentUpdated');

expect(await variableDebtToken.getDiscountPercent(users[0].address)).to.be.eq(0);

Expand Down
Loading

0 comments on commit 81bb51b

Please sign in to comment.