mirror of
https://github.com/Instadapp/aave-protocol-v2.git
synced 2024-07-29 21:47:30 +00:00
41 lines
1.3 KiB
TypeScript
41 lines
1.3 KiB
TypeScript
import BigNumber from 'bignumber.js';
|
|
import { BigNumberValue, valueToZDBigNumber } from './bignumber';
|
|
|
|
export function getLinearCumulatedRewards(
|
|
emissionPerSecond: BigNumberValue,
|
|
lastUpdateTimestamp: BigNumberValue,
|
|
currentTimestamp: BigNumberValue
|
|
): BigNumber {
|
|
const timeDelta = valueToZDBigNumber(currentTimestamp).minus(lastUpdateTimestamp.toString());
|
|
return timeDelta.multipliedBy(emissionPerSecond.toString());
|
|
}
|
|
|
|
export function getNormalizedDistribution(
|
|
balance: BigNumberValue,
|
|
oldIndex: BigNumberValue,
|
|
emissionPerSecond: BigNumberValue,
|
|
lastUpdateTimestamp: BigNumberValue,
|
|
currentTimestamp: BigNumberValue,
|
|
emissionEndTimestamp: BigNumberValue,
|
|
precision: number = 18
|
|
): BigNumber {
|
|
if (
|
|
balance.toString() === '0' ||
|
|
valueToZDBigNumber(lastUpdateTimestamp).gte(emissionEndTimestamp.toString())
|
|
) {
|
|
return valueToZDBigNumber(oldIndex);
|
|
}
|
|
const linearReward = getLinearCumulatedRewards(
|
|
emissionPerSecond,
|
|
lastUpdateTimestamp,
|
|
valueToZDBigNumber(currentTimestamp).gte(emissionEndTimestamp.toString())
|
|
? emissionEndTimestamp
|
|
: currentTimestamp
|
|
);
|
|
|
|
return linearReward
|
|
.multipliedBy(valueToZDBigNumber(10).exponentiatedBy(precision))
|
|
.div(balance.toString())
|
|
.plus(oldIndex.toString());
|
|
}
|