/
Sun.test.js
253 lines (214 loc) · 9.29 KB
/
Sun.test.js
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
const { BN } = require('@openzeppelin/test-helpers')
const { expect } = require('chai')
const { deploy } = require('../scripts/deploy.js')
const { parseJson } = require('./utils/helpers.js')
const { MAX_UINT256 } = require('./utils/constants.js')
// Set the test data
const [columns, tests] = parseJson('./coverage_data/sun.json')
var numberTests = tests.length
var startTest = 0
describe('Sun', function () {
before(async function () {
[owner,user,user2] = await ethers.getSigners()
userAddress = user.address
user2Address = user2.address
const contracts = await deploy("Test", false, true)
ownerAddress = contracts.account
this.diamond = contracts.beanstalkDiamond
this.season = await ethers.getContractAt('MockSeasonFacet', this.diamond.address)
this.field = await ethers.getContractAt('MockFieldFacet', this.diamond.address)
this.silo = await ethers.getContractAt('MockSiloFacet', this.diamond.address)
this.bean = await ethers.getContractAt('MockToken', contracts.bean)
this.pair = await ethers.getContractAt('MockUniswapV2Pair', contracts.pair)
this.pegPair = await ethers.getContractAt('MockUniswapV2Pair', contracts.pegPair)
});
[...Array(numberTests).keys()].map(i => i + startTest).forEach(function(v) {
const testStr = 'Test #'
describe(testStr.concat((v)), function () {
testData = {}
columns.forEach((key, i) => testData[key] = tests[v][i])
console.log(testData);
before(async function () {
await this.season.resetState()
await this.pair.burnBeans(this.bean.address)
this.testData = {}
columns.forEach((key, i) => this.testData[key] = tests[v][i])
for (var i = 0; i < this.testData.season-1; i++) {
await this.season.lightSunrise()
}
await this.bean.mint(this.silo.address, this.testData.beansInSilo)
await this.bean.mint(this.pair.address, this.testData.beansInPool)
await this.silo.incrementDepositedBeansE(this.testData.beansInSilo)
await this.season.incrementTotalSoilE(this.testData.soil)
await this.silo.depositSiloAssetsE(userAddress, '1', '100000')
await this.field.incrementTotalPodsE((parseInt(this.testData.unripenedPods) + parseInt(this.testData.harvestablePods)).toString())
await this.field.incrementTotalHarvestableE(this.testData.harvestablePods)
this.pair.simulateTrade(this.testData.beansInPool, this.testData.ethInPool+'000000000000')
this.result = await this.season.sunSunrise(this.testData.twapBeans, this.testData.twapUSDC, this.testData.divisor)
})
it('checks values', async function () {
expect(await this.bean.totalSupply()).to.eq(this.testData.newTotalSupply)
expect(await this.bean.balanceOf(this.silo.address)).to.eq((parseInt(this.testData.newBeansInSilo)+parseInt(this.testData.newHarvestablePods)).toString())
expect(await this.silo.totalDepositedBeans()).to.eq(this.testData.newBeansInSilo)
expect(await this.field.totalSoil()).to.eq(this.testData.newSupplyofSoil)
expect(await this.field.totalHarvestable()).to.eq(this.testData.newHarvestablePods)
expect(await this.field.totalUnripenedPods()).to.eq(this.testData.newUnripenedPods)
expect(await this.field.totalPods()).to.eq(this.testData.newTotalPods)
})
it('emits the correct event', async function () {
if (new BN(this.testData.currentTWAP).gt(new BN('1000000000000000000'))) {
await expect(this.result).to.emit(this.season, 'SupplyIncrease').withArgs(
(parseInt(this.testData.currentSeason)+1).toString(),
this.testData.currentTWAP,
this.testData.deltaHarvestablePods,
this.testData.deltaBeansInSilo,
this.testData.deltaSoil)
}
else if (new BN(this.testData.currentTWAP).eq(new BN('1000000000000000000'))) {
await expect(this.result).to.emit(this.season, 'SupplyNeutral').withArgs(
(parseInt(this.testData.currentSeason)+1).toString(),
this.testData.deltaSoil)
} else {
await expect(this.result).to.emit(this.season, 'SupplyDecrease').withArgs(
(parseInt(this.testData.currentSeason)+1).toString(),
this.testData.currentTWAP,
this.testData.deltaSoil)
}
})
})
})
})
describe('Sun Soil', function () {
before(async function () {
[owner,user,user2] = await ethers.getSigners()
userAddress = user.address
user2Address = user2.address
const contracts = await deploy("Test", false, true)
ownerAddress = contracts.account
this.diamond = contracts.beanstalkDiamond
this.season = await ethers.getContractAt('MockSeasonFacet', this.diamond.address)
this.field = await ethers.getContractAt('MockFieldFacet', this.diamond.address)
this.silo = await ethers.getContractAt('MockSiloFacet', this.diamond.address)
this.bean = await ethers.getContractAt('MockToken', contracts.bean)
this.pair = await ethers.getContractAt('MockUniswapV2Pair', contracts.pair)
this.pegPair = await ethers.getContractAt('MockUniswapV2Pair', contracts.pegPair)
await this.bean.mint(this.silo.address, '100000')
await this.season.setYieldE('100')
});
this.beforeEach(async function () {
await this.season.decrementTotalSoilE(await this.field.totalSoil())
})
it("Properly sets the soil bounds", async function () {
expect(await this.season.minSoil('100')).to.be.equal('50')
expect(await this.season.maxSoil()).to.be.equal('25000')
});
// Increase Soil
describe("Increase above max Soil", async function () {
beforeEach(async function () {
await this.season.increaseSoilE('100000')
});
it("Properly sets the soil", async function () {
expect(await this.field.totalSoil()).to.be.equal('25000')
});
});
describe("Increase when already above above max Soil", async function () {
beforeEach(async function () {
await this.season.incrementTotalSoilE('100000')
await this.season.increaseSoilE('100000')
});
it("Properly sets the soil", async function () {
expect(await this.field.totalSoil()).to.be.equal('25000')
});
});
describe("Increase little Soil", async function () {
beforeEach(async function () {
await this.season.increaseSoilE('1')
});
it("Properly sets the soil", async function () {
expect(await this.field.totalSoil()).to.be.equal('1')
});
});
describe("Increase to normal Soil", async function () {
beforeEach(async function () {
await this.season.increaseSoilE('100')
});
it("Properly sets the soil", async function () {
expect(await this.field.totalSoil()).to.be.equal('100')
});
});
// Decrease Soil
describe("Decrease above max Soil", async function () {
beforeEach(async function () {
await this.season.incrementTotalSoilE('100000')
await this.season.decreaseSoilE('1', '100')
});
it("Properly sets the max soil", async function () {
expect(await this.field.totalSoil()).to.be.equal('25000')
});
});
describe("Decrease already below min Soil", async function () {
beforeEach(async function () {
await this.season.incrementTotalSoilE('2')
await this.season.decreaseSoilE('1', '100')
});
it("Properly sets the soil", async function () {
expect(await this.field.totalSoil()).to.be.equal('50')
});
});
describe("Decrease below min Soil", async function () {
beforeEach(async function () {
await this.season.incrementTotalSoilE('110')
await this.season.decreaseSoilE('100', '100')
});
it("Properly sets the soil", async function () {
expect(await this.field.totalSoil()).to.be.equal('50')
});
});
describe("Decrease to normal Soil", async function () {
beforeEach(async function () {
await this.season.incrementTotalSoilE('110')
await this.season.decreaseSoilE('10', '100')
});
it("Properly sets the soil", async function () {
expect(await this.field.totalSoil()).to.be.equal('100')
});
});
describe("Decrease below 0 Soil", async function () {
beforeEach(async function () {
await this.season.incrementTotalSoilE('100')
await this.season.decreaseSoilE('110', '10')
});
it("Properly sets the soil", async function () {
expect(await this.field.totalSoil()).to.be.equal('5')
});
});
describe("Decrease to 0 Soil", async function () {
beforeEach(async function () {
await this.season.incrementTotalSoilE('100')
await this.season.decreaseSoilE('110', '0')
});
it("Properly sets the soil", async function () {
expect(await this.field.totalSoil()).to.be.equal('0')
});
});
// Ensure soil bounds
// Decrease Soil
describe("When above max soil", async function () {
beforeEach(async function () {
await this.season.incrementTotalSoilE('100000')
await this.season.ensureSoilBoundsE()
});
it("Properly sets the max soil", async function () {
expect(await this.field.totalSoil()).to.be.equal('25000')
});
});
describe("When normal soil", async function () {
beforeEach(async function () {
await this.season.incrementTotalSoilE('2')
await this.season.ensureSoilBoundsE()
});
it("Properly sets the soil", async function () {
expect(await this.field.totalSoil()).to.be.equal('2')
});
});
});