Skip to content

Commit 0093d32

Browse files
author
Beau Shinkle
authored
Merge pull request #166 from keep-network/withdraw-rewards-amount-assertions
Test the output of `SortitionPool.withdrawRewards`
2 parents 27d9ef6 + a6faeb2 commit 0093d32

1 file changed

Lines changed: 83 additions & 31 deletions

File tree

test/sortitionPoolTest.js

Lines changed: 83 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -259,9 +259,24 @@ describe("SortitionPool", () => {
259259
})
260260

261261
describe("pool rewards", async () => {
262+
async function withdrawRewards(
263+
pool,
264+
owner,
265+
operatorAddress,
266+
beneficiaryAddress,
267+
) {
268+
const amount = await pool
269+
.connect(owner)
270+
.callStatic.withdrawRewards(operatorAddress, beneficiaryAddress)
271+
await pool
272+
.connect(owner)
273+
.withdrawRewards(operatorAddress, beneficiaryAddress)
274+
return amount
275+
}
276+
262277
it("can only be withdrawn by the owner", async () => {
263278
await expect(
264-
pool.connect(bob).withdrawRewards(bob.address, bobBeneficiary.address),
279+
withdrawRewards(pool, bob, bob.address, bobBeneficiary.address),
265280
).to.be.revertedWith("Ownable: caller is not the owner")
266281
})
267282

@@ -270,16 +285,29 @@ describe("SortitionPool", () => {
270285
await pool.connect(owner).insertOperator(alice.address, 10000)
271286
await pool.connect(owner).insertOperator(bob.address, 20000)
272287
await token.connect(deployer).approveAndCall(pool.address, 300, [])
273-
await pool
274-
.connect(owner)
275-
.withdrawRewards(alice.address, aliceBeneficiary.address)
276-
await pool
277-
.connect(owner)
278-
.withdrawRewards(bob.address, bobBeneficiary.address)
279-
const aliceReward = await token.balanceOf(aliceBeneficiary.address)
280-
const bobReward = await token.balanceOf(bobBeneficiary.address)
281-
expect(aliceReward).to.equal(100)
282-
expect(bobReward).to.equal(200)
288+
289+
const aliceExpectedReward = 100
290+
const bobExpectedReward = 200
291+
292+
const aliceReward = await withdrawRewards(
293+
pool,
294+
owner,
295+
alice.address,
296+
aliceBeneficiary.address,
297+
)
298+
const bobReward = await withdrawRewards(
299+
pool,
300+
owner,
301+
bob.address,
302+
bobBeneficiary.address,
303+
)
304+
expect(aliceReward).to.equal(aliceExpectedReward)
305+
expect(bobReward).to.equal(bobExpectedReward)
306+
307+
const aliceBalance = await token.balanceOf(aliceBeneficiary.address)
308+
const bobBalance = await token.balanceOf(bobBeneficiary.address)
309+
expect(aliceBalance).to.equal(aliceExpectedReward)
310+
expect(bobBalance).to.equal(bobExpectedReward)
283311
})
284312

285313
it("handles new and returning operators correctly", async () => {
@@ -291,16 +319,28 @@ describe("SortitionPool", () => {
291319
await token.connect(deployer).approveAndCall(pool.address, 300, [])
292320
await pool.connect(owner).insertOperator(alice.address, 10000)
293321
await token.connect(deployer).approveAndCall(pool.address, 300, [])
294-
await pool
295-
.connect(owner)
296-
.withdrawRewards(alice.address, aliceBeneficiary.address)
297-
await pool
298-
.connect(owner)
299-
.withdrawRewards(bob.address, bobBeneficiary.address)
300-
const aliceReward = await token.balanceOf(aliceBeneficiary.address)
301-
const bobReward = await token.balanceOf(bobBeneficiary.address)
302-
expect(aliceReward).to.equal(400)
303-
expect(bobReward).to.equal(500)
322+
const aliceExpectedReward = 400
323+
const bobExpectedReward = 500
324+
325+
const aliceReward = await withdrawRewards(
326+
pool,
327+
owner,
328+
alice.address,
329+
aliceBeneficiary.address,
330+
)
331+
const bobReward = await withdrawRewards(
332+
pool,
333+
owner,
334+
bob.address,
335+
bobBeneficiary.address,
336+
)
337+
expect(aliceReward).to.equal(aliceExpectedReward)
338+
expect(bobReward).to.equal(bobExpectedReward)
339+
340+
const aliceBalance = await token.balanceOf(aliceBeneficiary.address)
341+
const bobBalance = await token.balanceOf(bobBeneficiary.address)
342+
expect(aliceBalance).to.equal(aliceExpectedReward)
343+
expect(bobBalance).to.equal(bobExpectedReward)
304344
})
305345

306346
it("doesn't pay to ineligible operators", async () => {
@@ -311,18 +351,30 @@ describe("SortitionPool", () => {
311351
const bobID = await pool.getOperatorID(bob.address)
312352
await pool.connect(owner).setRewardIneligibility([bobID], now + 100)
313353
await token.connect(deployer).approveAndCall(pool.address, 300, [])
314-
await pool
315-
.connect(owner)
316-
.withdrawRewards(alice.address, aliceBeneficiary.address)
317-
await pool
318-
.connect(owner)
319-
.withdrawRewards(bob.address, bobBeneficiary.address)
354+
const aliceExpectedReward = 100
355+
const bobExpectedReward = 0
356+
357+
const aliceReward = await withdrawRewards(
358+
pool,
359+
owner,
360+
alice.address,
361+
aliceBeneficiary.address,
362+
)
363+
const bobReward = await withdrawRewards(
364+
pool,
365+
owner,
366+
bob.address,
367+
bobBeneficiary.address,
368+
)
369+
expect(aliceReward).to.equal(aliceExpectedReward)
370+
expect(bobReward).to.equal(bobExpectedReward)
371+
320372
await pool.connect(owner).withdrawIneligible(carol.address)
321-
const aliceReward = await token.balanceOf(aliceBeneficiary.address)
322-
const bobReward = await token.balanceOf(bobBeneficiary.address)
373+
const aliceBalance = await token.balanceOf(aliceBeneficiary.address)
374+
const bobBalance = await token.balanceOf(bobBeneficiary.address)
323375
const ineligibleReward = await token.balanceOf(carol.address)
324-
expect(aliceReward).to.equal(100)
325-
expect(bobReward).to.equal(0)
376+
expect(aliceBalance).to.equal(aliceExpectedReward)
377+
expect(bobBalance).to.equal(bobExpectedReward)
326378
expect(ineligibleReward).to.equal(200)
327379
})
328380

0 commit comments

Comments
 (0)