Skip to content

Commit

Permalink
fix: incentive calculations (#251)
Browse files Browse the repository at this point in the history
* fix: principalUserBalance and totalSupply inputs for incentive calculations

* feat: add incentives test

* fix: lint error on test

* fix: lint error on test
  • Loading branch information
defispartan authored Sep 21, 2021
1 parent 58feb26 commit 6a5ddd6
Show file tree
Hide file tree
Showing 2 changed files with 166 additions and 7 deletions.
159 changes: 159 additions & 0 deletions src/test/v2/incentives.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,159 @@
// import BigNumber from 'bignumber.js';
import { formatUserSummaryData } from '../../v2';

describe('calculateUserReserveIncentives', () => {
/*const reserveIncentiveData = {
underlyingAsset: '0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2',
aIncentiveData: {
emissionPerSecond: new BigNumber(1979166666666666),
incentivesLastUpdateTimestamp: new BigNumber(1631587511),
tokenIncentivesIndex: new BigNumber(24733519699535219),
emissionEndTimestamp: new BigNumber(1637573428),
tokenAddress: '0x030ba81f1c18d280636f32af80b9aad02cf0854e',
rewardTokenAddress: '0x0000000000000000000000000000000000000000',
},
vIncentiveData: {
emissionPerSecond: new BigNumber(104166666666666),
incentivesLastUpdateTimestamp: new BigNumber(1631587387),
tokenIncentivesIndex: new BigNumber(26465727412280876),
emissionEndTimestamp: new BigNumber(1637573428),
tokenAddress: '0xf63b34710400cad3e044cffdcab00a0f32e33ecf',
rewardTokenAddress: '0x0000000000000000000000000000000000000000',
},
sIncentiveData: {
emissionPerSecond: new BigNumber(0),
incentivesLastUpdateTimestamp: new BigNumber(0),
tokenIncentivesIndex: new BigNumber(0),
emissionEndTimestamp: new BigNumber(1637573428),
tokenAddress: '0x4e977830ba4bd783c0bb7f15d3e243f73ff57121',
rewardTokenAddress: '0x0000000000000000000000000000000000000000',
},
};
const userReserveIncentiveData = {
underlyingAsset: '0x030ba81f1c18d280636f32af80b9aad02cf0854e',
aIncentiveData: {
tokenIncentivesUserIndex: new BigNumber(0),
userUnclaimedRewards: new BigNumber(43921819137644870),
tokenAddress: '0x0000000000000000000000000000000000000000',
rewardTokenAddress: '0x0000000000000000000000000000000000000000',
},
vIncentiveData: {
tokenIncentivesUserIndex: new BigNumber(24934844000963410),
userUnclaimedRewards: new BigNumber(43921819137644870),
tokenAddress: '0x0000000000000000000000000000000000000000',
rewardTokenAddress: '0x0000000000000000000000000000000000000000',
},
sIncentiveData: {
tokenIncentivesUserIndex: new BigNumber(0),
userUnclaimedRewards: new BigNumber(43921819137644870),
tokenAddress: '0x0000000000000000000000000000000000000000',
rewardTokenAddress: '0x0000000000000000000000000000000000000000',
},
};*/
const reserveData = {
id:
'0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc20xb53c1a33016b2dc2ff3653530bff1848a515c8c5',
underlyingAsset: '0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2',
symbol: 'aWETH',
decimals: 18,
baseLTVasCollateral: '8000',
reserveLiquidationThreshold: '8500',
reserveLiquidationBonus: '10500',
reserveFactor: '1000',
usageAsCollateralEnabled: true,
borrowingEnabled: true,
stableBorrowRateEnabled: true,
isActive: true,
liquidityIndex: '1007431539067282236768346040',
variableBorrowIndex: '1009130500436609696185241835',
liquidityRate: '222438954027153387451224',
variableBorrowRate: '5352853290785089356838046',
stableBorrowRate: '36691066613481361696047558',
lastUpdateTimestamp: 1631587511,
aTokenAddress: '0x030ba81f1c18d280636f32af80b9aad02cf0854e',
stableDebtTokenAddress: '0x4e977830ba4bd783c0bb7f15d3e243f73ff57121',
variableDebtTokenAddress: '0xf63b34710400cad3e044cffdcab00a0f32e33ecf',
interestRateStrategyAddress: '0x4ce076b9dd956196b814e54e1714338f18fde3f4',
availableLiquidity: '1827977661703998535260749',
totalPrincipalStableDebt: '681322787738991248642',
averageStableRate: '45598025477532527825795055',
stableDebtLastUpdateTimestamp: 1631554490,
totalScaledVariableDebt: '81689984341288838884434',

price: { priceInEth: '1000000000000000000' },
priceInEth: '1000000000000000000',
variableRateSlope1: '80000000000000000000000000',
variableRateSlope2: '1000000000000000000000000000',
stableRateSlope1: '100000000000000000000000000',
stableRateSlope2: '1000000000000000000000000000',
aEmissionPerSecond: '1979166666666666',
vEmissionPerSecond: '104166666666666',
sEmissionPerSecond: '0',
aIncentivesLastUpdateTimestamp: 1631587511,
vIncentivesLastUpdateTimestamp: 1631587387,
sIncentivesLastUpdateTimestamp: 0,
aTokenIncentivesIndex: '24733519699535219',
vTokenIncentivesIndex: '26465727412280876',
sTokenIncentivesIndex: '0',
};
const rawUserReserve = {
userReserve: {
scaledATokenBalance: '99353924118371338',
usageAsCollateralEnabledOnUser: true,
scaledVariableDebt: '0',
variableBorrowIndex: '0',
stableBorrowRate: '0',
principalStableDebt: '0',
stableBorrowLastUpdateTimestamp: 0,
aTokenincentivesUserIndex: '0',
vTokenincentivesUserIndex: '24934844000963410',
sTokenincentivesUserIndex: '0',
reserve: {
id:
'0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc20xb53c1a33016b2dc2ff3653530bff1848a515c8c5',
symbol: 'ETH',
decimals: 18,
liquidityRate: '222438954027153387451224',
reserveLiquidationBonus: '10500',
lastUpdateTimestamp: 1631587511,
price: {
priceInEth: '1000000000000000000',
},
reserveFactor: '1000',
baseLTVasCollateral: '8000',
averageStableRate: '45598025477532527825795055',
stableDebtLastUpdateTimestamp: 1631554490,
liquidityIndex: '1007431539067282236768346040',
reserveLiquidationThreshold: '8250',
variableBorrowIndex: '1009130500436609696185241835',
variableBorrowRate: '5352853290785089356838046',
availableLiquidity: '1827977661703998535260749',
stableBorrowRate: '37172994479933736354050909',
totalPrincipalStableDebt: '681322787738991248642',
totalScaledVariableDebt: '81689984341288838884434',
usageAsCollateralEnabled: true,
},
},
currentTimestamp: 1631587561,
usdPriceEth: 329302000000,
};

const formattedUserReserve = formatUserSummaryData(
[reserveData as any],
[rawUserReserve.userReserve as any],
'0x0',
'0',
1631587561,
{
emissionEndTimestamp: 1637573428,
rewardTokenAddress: '0x030ba81f1c18d280636f32af80b9aad02cf0854e',
incentivePrecision: 18,
rewardTokenDecimals: 18,
rewardTokenPriceEth: '11',
}
);

it('should calculate the correct aWETH incentives', () => {
expect(formattedUserReserve.totalRewards).toBe('0.002457377422282363');
});
});
14 changes: 7 additions & 7 deletions src/v2/computations-and-formatting.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import {
calculateCompoundedInterest,
getLinearBalance,
} from '../helpers/pool-math';
import { rayMul } from '../helpers/ray-math';
import { rayDiv, rayMul } from '../helpers/ray-math';
import {
ComputedUserReserve,
ReserveData,
Expand Down Expand Up @@ -148,14 +148,14 @@ export function computeUserReserveData(

const aTokenRewards = totalLiquidity.gt(0)
? calculateRewards(
underlyingBalance,
userReserve.scaledATokenBalance,
poolReserve.aTokenIncentivesIndex,
userReserve.aTokenincentivesUserIndex,
rewardsInfo.incentivePrecision,
rewardsInfo.rewardTokenDecimals,
poolReserve.aIncentivesLastUpdateTimestamp,
poolReserve.aEmissionPerSecond,
totalLiquidity,
rayDiv(totalLiquidity, poolReserve.liquidityIndex),
currentTimestamp,
rewardsInfo.emissionEndTimestamp
)
Expand All @@ -170,14 +170,14 @@ export function computeUserReserveData(

const vTokenRewards = totalVariableDebt.gt(0)
? calculateRewards(
variableBorrows,
userReserve.scaledVariableDebt,
poolReserve.vTokenIncentivesIndex,
userReserve.vTokenincentivesUserIndex,
rewardsInfo.incentivePrecision,
rewardsInfo.rewardTokenDecimals,
poolReserve.vIncentivesLastUpdateTimestamp,
poolReserve.vEmissionPerSecond,
totalVariableDebt,
new BigNumber(poolReserve.totalScaledVariableDebt),
currentTimestamp,
rewardsInfo.emissionEndTimestamp
)
Expand All @@ -191,14 +191,14 @@ export function computeUserReserveData(
);
const sTokenRewards = totalStableDebt.gt(0)
? calculateRewards(
stableBorrows,
userReserve.principalStableDebt,
poolReserve.sTokenIncentivesIndex,
userReserve.sTokenincentivesUserIndex,
rewardsInfo.incentivePrecision,
rewardsInfo.rewardTokenDecimals,
poolReserve.sIncentivesLastUpdateTimestamp,
poolReserve.sEmissionPerSecond,
totalStableDebt,
new BigNumber(poolReserve.totalPrincipalStableDebt),
currentTimestamp,
rewardsInfo.emissionEndTimestamp
)
Expand Down

0 comments on commit 6a5ddd6

Please sign in to comment.