forked from compound-finance/comet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRewardsScenario.ts
303 lines (255 loc) · 14.3 KB
/
RewardsScenario.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
import { CometContext, CometProperties, scenario } from './context/CometContext';
import { expect } from 'chai';
import { exp } from '../test/helpers';
import { isRewardSupported, matchesDeployment } from './utils';
import { Contract, ContractReceipt } from 'ethers';
import { CometRewards, ERC20__factory } from '../build/types';
import {World} from '../plugins/scenario';
function calculateRewardsOwed(
userBalance: bigint,
totalBalance: bigint,
speed: bigint,
timeElapsed: number,
trackingIndexScale: bigint,
rewardTokenScale: bigint,
rescaleFactor: bigint
): bigint {
// accrued = (user balance / total balance) * (speed / trackingIndexScale) * time * reward token scale
const accrued = userBalance * speed * BigInt(timeElapsed) * rewardTokenScale / totalBalance / trackingIndexScale;
// truncate using rescaleFactor
return accrued / rescaleFactor * rescaleFactor;
}
scenario(
'Comet#rewards > can claim supply rewards for self',
{
filter: async (ctx) => await isRewardSupported(ctx),
tokenBalances: {
albert: { $base: ' == 100' }, // in units of asset, not wei
},
},
async ({ comet, rewards, actors }, context, world) => {
const { albert } = actors;
const baseAssetAddress = await comet.baseToken();
const baseAsset = context.getAssetByAddress(baseAssetAddress);
const baseScale = (await comet.baseScale()).toBigInt();
const [rewardTokenAddress, rescaleFactor] = await rewards.rewardConfig(comet.address);
const rewardToken = new Contract(
rewardTokenAddress,
ERC20__factory.createInterface(),
world.deploymentManager.hre.ethers.provider
);
const rewardScale = exp(1, await rewardToken.decimals());
await baseAsset.approve(albert, comet.address);
await albert.safeSupplyAsset({ asset: baseAssetAddress, amount: 100n * baseScale });
expect(await rewardToken.balanceOf(albert.address)).to.be.equal(0n);
const supplyTimestamp = await world.timestamp();
const albertBalance = await albert.getCometBaseBalance();
const totalSupplyBalance = (await comet.totalSupply()).toBigInt();
await world.increaseTime(86400); // fast forward a day
const preTxnTimestamp = await world.timestamp();
const rewardsOwedBefore = (await rewards.callStatic.getRewardOwed(comet.address, albert.address)).owed.toBigInt();
const txn = await (await rewards.connect(albert.signer).claim(comet.address, albert.address, true)).wait();
const rewardsOwedAfter = (await rewards.callStatic.getRewardOwed(comet.address, albert.address)).owed.toBigInt();
const postTxnTimestamp = await world.timestamp();
const timeElapsed = postTxnTimestamp - preTxnTimestamp;
const supplySpeed = (await comet.baseTrackingSupplySpeed()).toBigInt();
const trackingIndexScale = (await comet.trackingIndexScale()).toBigInt();
const timestampDelta = preTxnTimestamp - supplyTimestamp;
const totalSupplyPrincipal = (await comet.totalsBasic()).totalSupplyBase.toBigInt();
const baseMinForRewards = (await comet.baseMinForRewards()).toBigInt();
let expectedRewardsOwed = 0n;
let expectedRewardsReceived = 0n;
if (totalSupplyPrincipal >= baseMinForRewards) {
expectedRewardsOwed = calculateRewardsOwed(albertBalance, totalSupplyBalance, supplySpeed, timestampDelta, trackingIndexScale, rewardScale, rescaleFactor.toBigInt());
expectedRewardsReceived = calculateRewardsOwed(albertBalance, totalSupplyBalance, supplySpeed, timestampDelta + timeElapsed, trackingIndexScale, rewardScale, rescaleFactor.toBigInt());
}
// Occasionally `timestampDelta` is equal to 86401
expect(timestampDelta).to.be.greaterThanOrEqual(86400);
expect(rewardsOwedBefore).to.be.equal(expectedRewardsOwed);
expect(await rewardToken.balanceOf(albert.address)).to.be.equal(expectedRewardsReceived);
expect(rewardsOwedAfter).to.be.equal(0n);
return txn; // return txn to measure gas
}
);
scenario(
'Comet#rewards > manager can claimTo supply rewards from a managed account',
{
filter: async (ctx) => await isRewardSupported(ctx) && !matchesDeployment(ctx, [{network: 'mainnet', deployment: 'weth'}]),
tokenBalances: {
albert: { $base: ' == 100' }, // in units of asset, not wei
},
},
async ({ comet, rewards, actors }, context, world) => {
const { albert, betty } = actors;
const baseAssetAddress = await comet.baseToken();
const baseAsset = context.getAssetByAddress(baseAssetAddress);
const baseScale = (await comet.baseScale()).toBigInt();
const [rewardTokenAddress, rescaleFactor] = await rewards.rewardConfig(comet.address);
const rewardToken = new Contract(
rewardTokenAddress,
ERC20__factory.createInterface(),
world.deploymentManager.hre.ethers.provider
);
const rewardScale = exp(1, await rewardToken.decimals());
await albert.allow(betty, true); // Albert allows Betty to manage his account
await baseAsset.approve(albert, comet.address);
await albert.safeSupplyAsset({ asset: baseAssetAddress, amount: 100n * baseScale });
expect(await rewardToken.balanceOf(albert.address)).to.be.equal(0n);
const supplyTimestamp = await world.timestamp();
const albertBalance = await albert.getCometBaseBalance();
const totalSupplyBalance = (await comet.totalSupply()).toBigInt();
await world.increaseTime(86400); // fast forward a day
const preTxnTimestamp = await world.timestamp();
const rewardsOwedBefore = (await rewards.callStatic.getRewardOwed(comet.address, albert.address)).owed.toBigInt();
const txn = await (await rewards.connect(betty.signer).claimTo(comet.address, albert.address, betty.address, true)).wait();
const rewardsOwedAfter = (await rewards.callStatic.getRewardOwed(comet.address, albert.address)).owed.toBigInt();
const postTxnTimestamp = await world.timestamp();
const timeElapsed = postTxnTimestamp - preTxnTimestamp;
const supplySpeed = (await comet.baseTrackingSupplySpeed()).toBigInt();
const trackingIndexScale = (await comet.trackingIndexScale()).toBigInt();
const timestampDelta = preTxnTimestamp - supplyTimestamp;
const totalSupplyPrincipal = (await comet.totalsBasic()).totalSupplyBase.toBigInt();
const baseMinForRewards = (await comet.baseMinForRewards()).toBigInt();
let expectedRewardsOwed = 0n;
let expectedRewardsReceived = 0n;
if (totalSupplyPrincipal >= baseMinForRewards) {
expectedRewardsOwed = calculateRewardsOwed(albertBalance, totalSupplyBalance, supplySpeed, timestampDelta, trackingIndexScale, rewardScale, rescaleFactor.toBigInt());
expectedRewardsReceived = calculateRewardsOwed(albertBalance, totalSupplyBalance, supplySpeed, timestampDelta + timeElapsed, trackingIndexScale, rewardScale, rescaleFactor.toBigInt());
}
// Occasionally `timestampDelta` is equal to 86401
expect(timestampDelta).to.be.greaterThanOrEqual(86400);
expect(rewardsOwedBefore).to.be.equal(expectedRewardsOwed);
expect(await rewardToken.balanceOf(betty.address)).to.be.equal(expectedRewardsReceived);
expect(rewardsOwedAfter).to.be.equal(0n);
return txn; // return txn to measure gas
}
);
scenario(
'Comet#rewards > can claim borrow rewards for self',
{
filter: async (ctx) => await isRewardSupported(ctx),
tokenBalances: {
albert: { $asset0: ' == 10000' }, // in units of asset, not wei
$comet: { $base: ' >= 1000 ' }
},
},
async ({ comet, rewards, actors }, context, world) => {
const { albert } = actors;
const { asset: collateralAssetAddress, scale: scaleBN } = await comet.getAssetInfo(0);
const collateralAsset = context.getAssetByAddress(collateralAssetAddress);
const scale = scaleBN.toBigInt();
const toSupply = 10_000n * scale;
const baseAssetAddress = await comet.baseToken();
const baseScale = (await comet.baseScale()).toBigInt();
const toBorrow = 1_000n * baseScale;
const { rescaleFactor } = await context.getRewardConfig();
const rewardToken = await context.getRewardToken();
const rewardScale = exp(1, await rewardToken.decimals());
await collateralAsset.approve(albert, comet.address);
await albert.safeSupplyAsset({ asset: collateralAssetAddress, amount: toSupply });
await albert.withdrawAsset({ asset: baseAssetAddress, amount: toBorrow });
expect(await rewardToken.balanceOf(albert.address)).to.be.equal(0n);
const borrowTimestamp = await world.timestamp();
const albertBalance = await albert.getCometBaseBalance();
const totalBorrowBalance = (await comet.totalBorrow()).toBigInt();
await world.increaseTime(86400); // fast forward a day
const preTxnTimestamp = await world.timestamp();
const rewardsOwedBefore = (await rewards.callStatic.getRewardOwed(comet.address, albert.address)).owed.toBigInt();
const txn = await (await rewards.connect(albert.signer).claim(comet.address, albert.address, true)).wait();
const rewardsOwedAfter = (await rewards.callStatic.getRewardOwed(comet.address, albert.address)).owed.toBigInt();
const postTxnTimestamp = await world.timestamp();
const timeElapsed = postTxnTimestamp - preTxnTimestamp;
const borrowSpeed = (await comet.baseTrackingBorrowSpeed()).toBigInt();
const trackingIndexScale = (await comet.trackingIndexScale()).toBigInt();
const timestampDelta = preTxnTimestamp - borrowTimestamp;
const totalBorrowPrincipal = (await comet.totalsBasic()).totalBorrowBase.toBigInt();
const baseMinForRewards = (await comet.baseMinForRewards()).toBigInt();
let expectedRewardsOwed = 0n;
let expectedRewardsReceived = 0n;
if (totalBorrowPrincipal >= baseMinForRewards) {
expectedRewardsOwed = calculateRewardsOwed(-albertBalance, totalBorrowBalance, borrowSpeed, timestampDelta, trackingIndexScale, rewardScale, rescaleFactor.toBigInt());
expectedRewardsReceived = calculateRewardsOwed(-albertBalance, totalBorrowBalance, borrowSpeed, timestampDelta + timeElapsed, trackingIndexScale, rewardScale, rescaleFactor.toBigInt());
}
// Occasionally `timestampDelta` is equal to 86401
expect(timestampDelta).to.be.greaterThanOrEqual(86400);
expect(rewardsOwedBefore).to.be.equal(expectedRewardsOwed);
expect(await rewardToken.balanceOf(albert.address)).to.be.equal(expectedRewardsReceived);
expect(rewardsOwedAfter).to.be.equal(0n);
return txn; // return txn to measure gas
}
);
const MULTIPLIERS = [
exp(55, 18),
exp(10, 18),
exp(1, 18),
exp(0.01, 18),
exp(0.00355, 18)
];
for (let i = 0; i < MULTIPLIERS.length; i++) {
scenario(
`Comet#rewards > can claim supply rewards on scaling rewards contract with multiplier of ${MULTIPLIERS[i]}`,
{
filter: async (ctx) => await isRewardSupported(ctx),
tokenBalances: {
albert: { $base: ' == 100' }, // in units of asset, not wei
},
},
async (properties, context, world) => {
return await testScalingReward(properties, context, world, MULTIPLIERS[i]);
}
);
}
async function testScalingReward(properties: CometProperties, context: CometContext, world: World, multiplier: bigint): Promise<void | ContractReceipt> {
const { comet, actors, rewards } = properties;
const { albert } = actors;
const baseAssetAddress = await comet.baseToken();
const baseAsset = context.getAssetByAddress(baseAssetAddress);
const baseScale = (await comet.baseScale()).toBigInt();
const [rewardTokenAddress, rescaleFactorWithoutMultiplier] = await rewards.rewardConfig(comet.address);
// XXX maybe try with a different reward token as well
const rewardToken = new Contract(
rewardTokenAddress,
ERC20__factory.createInterface(),
world.deploymentManager.hre.ethers.provider
);
const rewardDecimals = await rewardToken.decimals();
const rewardScale = exp(1, rewardDecimals);
// Deploy new rewards contract with a multiplier
const newRewards = await world.deploymentManager.deploy<CometRewards, [string]>(
'newRewards',
'CometRewards.sol',
[albert.address]
);
await newRewards.connect(albert.signer).setRewardConfigWithMultiplier(comet.address, rewardTokenAddress, multiplier);
await context.sourceTokens(exp(1_000, rewardDecimals), rewardTokenAddress, newRewards.address);
await baseAsset.approve(albert, comet.address);
await albert.safeSupplyAsset({ asset: baseAssetAddress, amount: 100n * baseScale });
expect(await rewardToken.balanceOf(albert.address)).to.be.equal(0n);
const supplyTimestamp = await world.timestamp();
const albertBalance = await albert.getCometBaseBalance();
const totalSupplyBalance = (await comet.totalSupply()).toBigInt();
await world.increaseTime(86400); // fast forward a day
const preTxnTimestamp = await world.timestamp();
const newRewardsOwedBefore = (await newRewards.callStatic.getRewardOwed(comet.address, albert.address)).owed.toBigInt();
const txn = await (await newRewards.connect(albert.signer).claim(comet.address, albert.address, true)).wait();
const newRewardsOwedAfter = (await newRewards.callStatic.getRewardOwed(comet.address, albert.address)).owed.toBigInt();
const postTxnTimestamp = await world.timestamp();
const timeElapsed = postTxnTimestamp - preTxnTimestamp;
const supplySpeed = (await comet.baseTrackingSupplySpeed()).toBigInt();
const trackingIndexScale = (await comet.trackingIndexScale()).toBigInt();
const timestampDelta = preTxnTimestamp - supplyTimestamp;
const totalSupplyPrincipal = (await comet.totalsBasic()).totalSupplyBase.toBigInt();
const baseMinForRewards = (await comet.baseMinForRewards()).toBigInt();
let expectedRewardsOwedWithoutMultiplier = 0n;
let expectedRewardsReceivedWithoutMultiplier = 0n;
if (totalSupplyPrincipal >= baseMinForRewards) {
expectedRewardsOwedWithoutMultiplier = calculateRewardsOwed(albertBalance, totalSupplyBalance, supplySpeed, timestampDelta, trackingIndexScale, rewardScale, rescaleFactorWithoutMultiplier.toBigInt());
expectedRewardsReceivedWithoutMultiplier = calculateRewardsOwed(albertBalance, totalSupplyBalance, supplySpeed, timestampDelta + timeElapsed, trackingIndexScale, rewardScale, rescaleFactorWithoutMultiplier.toBigInt());
}
// Occasionally `timestampDelta` is equal to 86401
expect(timestampDelta).to.be.greaterThanOrEqual(86400);
expect(newRewardsOwedBefore).to.be.equal(expectedRewardsOwedWithoutMultiplier * multiplier / exp(1, 18));
expect(await rewardToken.balanceOf(albert.address)).to.be.equal(expectedRewardsReceivedWithoutMultiplier * multiplier / exp(1, 18));
expect(newRewardsOwedAfter).to.be.equal(0n);
return txn; // return txn to measure gas
}