-
Notifications
You must be signed in to change notification settings - Fork 38
/
checkpointBypassAttack.ts
282 lines (252 loc) · 11.8 KB
/
checkpointBypassAttack.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
import { expect } from "chai";
import { Contract, Wallet } from "ethers";
import type { Fixture, MockContract, MockProvider } from "ethereum-waffle";
import { waffle, ethers } from "hardhat";
const { loadFixture } = waffle;
const { parseEther, parseUnits } = ethers.utils;
const parseBtc = (value: string) => parseUnits(value, 8);
import { deployMockForName } from "./mock";
import {
TRANCHE_Q,
TRANCHE_B,
TRANCHE_R,
DAY,
SETTLEMENT_TIME,
FixtureWalletMap,
advanceBlockAtTime,
setAutomine,
} from "./utils";
const UPPER_REBALANCE_THRESHOLD = parseEther("2");
const LOWER_REBALANCE_THRESHOLD = parseEther("0.5");
describe("checkpointBypassAttack", function () {
interface FixtureData {
readonly wallets: FixtureWalletMap;
readonly startDay: number;
readonly startTimestamp: number;
readonly twapOracle: MockContract;
readonly btc: Contract;
readonly aprOracle: MockContract;
readonly interestRateBallot: MockContract;
readonly shareQ: MockContract;
readonly shareB: MockContract;
readonly shareR: MockContract;
readonly primaryMarket: Contract;
readonly aprOracleProxy: Contract;
readonly staking: Contract;
readonly fund: Contract;
}
let currentFixture: Fixture<FixtureData>;
let fixtureData: FixtureData;
let startDay: number;
let user1: Wallet;
let user2: Wallet;
let owner: Wallet;
let twapOracle: MockContract;
let btc: Contract;
let primaryMarket: Contract;
let aprOracleProxy: Contract;
let staking: Contract;
let fund: Contract;
async function deployFixture(_wallets: Wallet[], provider: MockProvider): Promise<FixtureData> {
// Initiating transactions from a Waffle mock contract doesn't work well in Hardhat
// and may fail with gas estimating errors. We use EOAs for the shares to make
// test development easier.
const [user1, user2, owner, feeCollector, strategy] = provider.getWallets();
// Start at 12 hours after settlement time of the 6th day in a week, which makes sure that
// the first settlement after the fund's deployment settles the last day in a week and
// starts a new week by updating interest rate of BISHOP. Many test cases in this file
// rely on this fact to change the interest rate.
//
// As Fund settles at 14:00 everyday and an Unix timestamp starts a week on Thursday,
// the test cases starts at 2:00 on Thursday (`startTimestamp`) and the first day settles
// at 14:00 on Thursday (`startDay`).
let startTimestamp = (await ethers.provider.getBlock("latest")).timestamp;
const lastDay = Math.ceil(startTimestamp / DAY / 7) * DAY * 7 + DAY * 6 + SETTLEMENT_TIME;
const startDay = lastDay + DAY;
startTimestamp = lastDay + 3600 * 12;
await advanceBlockAtTime(startTimestamp);
const twapOracle = await deployMockForName(owner, "ITwapOracle");
await twapOracle.mock.getTwap.withArgs(lastDay).returns(parseEther("1000"));
const MockToken = await ethers.getContractFactory("MockToken");
const btc = await MockToken.connect(owner).deploy("Wrapped BTC", "BTC", 8);
await btc.mint(user1.address, parseBtc("10000"));
await btc.mint(user2.address, parseBtc("10000"));
const aprOracle = await deployMockForName(owner, "IAprOracle");
await aprOracle.mock.capture.returns(parseEther("0.001")); // 0.1% per day
const interestRateBallot = await deployMockForName(owner, "IBallot");
await interestRateBallot.mock.count.returns(0);
const shareQ = await deployMockForName(owner, "IShareV2");
const shareB = await deployMockForName(owner, "IShareV2");
const shareR = await deployMockForName(owner, "IShareV2");
for (const share of [shareQ, shareB, shareR]) {
await share.mock.fundEmitTransfer.returns();
await share.mock.fundEmitApproval.returns();
}
const primaryMarketAddress = ethers.utils.getContractAddress({
from: owner.address,
nonce: (await owner.getTransactionCount("pending")) + 1,
});
const Fund = await ethers.getContractFactory("FundV4");
const fund = await Fund.connect(owner).deploy([
btc.address,
8,
shareQ.address,
shareB.address,
shareR.address,
primaryMarketAddress,
ethers.constants.AddressZero,
0,
UPPER_REBALANCE_THRESHOLD,
LOWER_REBALANCE_THRESHOLD,
twapOracle.address,
aprOracle.address,
interestRateBallot.address,
feeCollector.address,
]);
const PrimaryMarket = await ethers.getContractFactory("PrimaryMarketV3");
const primaryMarket = await PrimaryMarket.connect(owner).deploy(
fund.address,
0,
0,
parseEther("1000000")
);
expect(primaryMarket.address).to.equal(primaryMarketAddress);
await fund.initialize(parseEther("500"), parseEther("1"), parseEther("1"), 0);
const chessSchedule = await deployMockForName(owner, "IChessSchedule");
await chessSchedule.mock.getWeeklySupply.returns(0);
await chessSchedule.mock.getRate.returns(0);
await chessSchedule.mock.mint.returns();
const chessController = await deployMockForName(owner, "IChessController");
await chessController.mock.getFundRelativeWeight.returns(parseEther("1"));
const votingEscrow = await deployMockForName(owner, "IVotingEscrow");
await votingEscrow.mock.balanceOf.returns(0);
await votingEscrow.mock.totalSupply.returns(1);
const ShareStaking = await ethers.getContractFactory("ShareStaking");
const staking = await ShareStaking.connect(owner).deploy(
fund.address,
chessSchedule.address,
chessController.address,
votingEscrow.address,
0
);
const BscAprOracleProxy = await ethers.getContractFactory("BscAprOracleProxy");
const aprOracleProxy = await BscAprOracleProxy.connect(owner).deploy(
aprOracle.address,
fund.address,
staking.address
);
await fund.updateAprOracle(aprOracleProxy.address);
return {
wallets: { user1, user2, owner, feeCollector, strategy },
startDay,
startTimestamp,
twapOracle,
btc: btc.connect(user1),
aprOracle,
interestRateBallot,
shareQ,
shareB,
shareR,
primaryMarket: primaryMarket.connect(user1),
aprOracleProxy,
staking: staking.connect(user1),
fund: fund.connect(user1),
};
}
async function advanceOneDayAndSettle() {
await advanceBlockAtTime((await fund.currentDay()).toNumber());
await fund.settle();
}
before(function () {
currentFixture = deployFixture;
});
beforeEach(async function () {
fixtureData = await loadFixture(currentFixture);
user1 = fixtureData.wallets.user1;
user2 = fixtureData.wallets.user2;
owner = fixtureData.wallets.owner;
startDay = fixtureData.startDay;
twapOracle = fixtureData.twapOracle;
btc = fixtureData.btc;
aprOracleProxy = fixtureData.aprOracleProxy;
primaryMarket = fixtureData.primaryMarket;
staking = fixtureData.staking;
fund = fixtureData.fund;
});
describe("Settlement of a non-empty fund", function () {
const DAILY_PROTOCOL_FEE_BPS = 1; // 0.01% per day
beforeEach(async function () {
await fund
.connect(owner)
.updateDailyProtocolFeeRate(parseEther("0.0001").mul(DAILY_PROTOCOL_FEE_BPS));
// Create 10 QUEEN with 10 BTC on the first day.
await btc.transfer(primaryMarket.address, parseBtc("10"));
await primaryMarket.create(user1.address, 0, 0);
await twapOracle.mock.getTwap.withArgs(startDay).returns(parseEther("1000"));
await advanceOneDayAndSettle();
await primaryMarket.split(aprOracleProxy.address, parseEther("1"), 0);
await primaryMarket.split(user1.address, parseEther("2"), 0);
await fund.trancheApprove(TRANCHE_Q, staking.address, parseEther("0.5"), 0);
await fund.trancheApprove(TRANCHE_B, staking.address, parseEther("0.5"), 0);
await fund.trancheApprove(TRANCHE_R, staking.address, parseEther("0.5"), 0);
await staking.deposit(TRANCHE_Q, parseEther("0.5"), user1.address, 0);
await staking.deposit(TRANCHE_B, parseEther("0.5"), user1.address, 0);
await staking.deposit(TRANCHE_R, parseEther("0.5"), user1.address, 0);
await advanceBlockAtTime((await fund.currentDay()).toNumber());
});
it("Should allow settlement if no upper rebalance triggered", async function () {
const price = parseEther("1500");
await twapOracle.mock.getTwap.withArgs(startDay + DAY).returns(price);
await setAutomine(false);
await staking.claimRewards(user2.address);
await setAutomine(true);
await fund.settle();
expect(await fund.getRebalanceSize()).to.equal(0);
});
it("Should block settlement if upper rebalance triggered", async function () {
const price = parseEther("1510");
await twapOracle.mock.getTwap.withArgs(startDay + DAY).returns(price);
await setAutomine(false);
await staking.claimRewards(user2.address);
await setAutomine(true);
await expect(fund.settle()).to.be.revertedWith("Rebalance check failed");
expect(await fund.getRebalanceSize()).to.equal(0);
await fund.settle();
expect(await fund.getRebalanceSize()).to.equal(1);
});
it("Should allow settlement if no lower rebalance triggered", async function () {
const price = parseEther("755");
await twapOracle.mock.getTwap.withArgs(startDay + DAY).returns(price);
await setAutomine(false);
await staking.claimRewards(user2.address);
await setAutomine(true);
await fund.settle();
expect(await fund.getRebalanceSize()).to.equal(0);
});
it("Should block settlement if lower rebalance triggered", async function () {
const price = parseEther("750");
await twapOracle.mock.getTwap.withArgs(startDay + DAY).returns(price);
// Supply enough BISHOP and ROOK to shareStaking to enable the attack
await fund.trancheTransfer(TRANCHE_B, staking.address, parseEther("0.8"), 0);
await fund.trancheTransfer(TRANCHE_R, staking.address, parseEther("0.8"), 0);
await setAutomine(false);
await staking.claimRewards(user2.address);
await setAutomine(true);
await expect(fund.settle()).to.be.revertedWith("Rebalance check failed");
expect(await fund.getRebalanceSize()).to.equal(0);
await fund.settle();
expect(await fund.getRebalanceSize()).to.equal(1);
});
it("Should block settlement if extreme lower rebalance triggered", async function () {
const price = parseEther("250");
await twapOracle.mock.getTwap.withArgs(startDay + DAY).returns(price);
await setAutomine(false);
await staking.claimRewards(user2.address);
await setAutomine(true);
await expect(fund.settle()).to.be.revertedWith("SafeMath: subtraction overflow");
expect(await fund.getRebalanceSize()).to.equal(0);
await fund.settle();
expect(await fund.getRebalanceSize()).to.equal(1);
});
});
});