Skip to content

Commit

Permalink
fix(PrizeSplitStrategy): mock getTicket
Browse files Browse the repository at this point in the history
  • Loading branch information
PierrickGT committed Sep 30, 2021
1 parent bcba429 commit 6373d7e
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 33 deletions.
4 changes: 0 additions & 4 deletions contracts/test/libraries/GasLib.sol
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
// SPDX-License-Identifier: GPL-3.0
pragma solidity 0.8.6;

import "hardhat/console.sol";

library GasLib {
struct Tracker {
uint256 startingGas;
Expand All @@ -18,14 +16,12 @@ library GasLib {

function mark(Tracker memory tracker, string memory message) internal view returns (Tracker memory) {
uint256 diff = tracker.deltaGas - gasleft();
console.log(message, diff);
tracker.deltaGas = gasleft();

return tracker;
}

function done(Tracker memory tracker, string memory message) internal view {
uint256 diff = tracker.startingGas - gasleft();
console.log(message, diff);
}
}
12 changes: 2 additions & 10 deletions test/Ticket.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -377,7 +377,6 @@ describe('Ticket', () => {
await ticket.mint(wallet1.address, balanceBefore);
timestamp = (await getBlock('latest')).timestamp;
debug(`minted ${ethers.utils.formatEther(balanceBefore)} @ timestamp ${timestamp}`)
// console.log(`Minted at time ${timestamp}`)
});

it('should revert on unequal lenght inputs', async () => {
Expand All @@ -388,7 +387,6 @@ describe('Ticket', () => {
})

it('should return an average of zero for pre-history requests', async () => {
// console.log(`Test getAverageBalance() : ${timestamp - 100}, ${timestamp - 50}`)
const drawStartTimestamp = timestamp - 100
const drawEndTimestamp = timestamp - 50
const result = await ticket.getAverageTotalSuppliesBetween([drawStartTimestamp], [drawEndTimestamp])
Expand All @@ -399,7 +397,6 @@ describe('Ticket', () => {

it('should not project into the future', async () => {
// at this time the user has held 1000 tokens for zero seconds
// console.log(`Test getAverageBalance() : ${timestamp - 50}, ${timestamp + 50}`)
const drawStartTimestamp = timestamp - 50
const drawEndTimestamp = timestamp + 50
const result = await ticket.getAverageTotalSuppliesBetween([drawStartTimestamp], [drawEndTimestamp])
Expand All @@ -410,7 +407,6 @@ describe('Ticket', () => {

it('should return half the minted balance when the duration is centered over first twab', async () => {
await increaseTime(100);
// console.log(`Test getAverageBalance() : ${timestamp - 50}, ${timestamp + 50}`)
const drawStartTimestamp = timestamp - 50
const drawEndTimestamp = timestamp + 50
const result = await ticket.getAverageTotalSuppliesBetween([drawStartTimestamp], [drawEndTimestamp])
Expand All @@ -421,7 +417,6 @@ describe('Ticket', () => {

it('should return an accurate average when the range is after the last twab', async () => {
await increaseTime(100);
// console.log(`Test getAverageBalance() : ${timestamp + 50}, ${timestamp + 51}`)
const drawStartTimestamp = timestamp + 50
const drawEndTimestamp = timestamp + 51
const result = await ticket.getAverageTotalSuppliesBetween([drawStartTimestamp], [drawEndTimestamp])
Expand Down Expand Up @@ -520,7 +515,6 @@ describe('Ticket', () => {
await ticket.mint(wallet1.address, balanceBefore);
timestamp = (await getBlock('latest')).timestamp;
debug(`minted ${ethers.utils.formatEther(balanceBefore)} @ timestamp ${timestamp}`)
// console.log(`Minted at time ${timestamp}`)
});

it('should revert on unequal lenght inputs', async () => {
Expand All @@ -531,7 +525,6 @@ describe('Ticket', () => {
})

it('should return an average of zero for pre-history requests', async () => {
// console.log(`Test getAverageBalance() : ${timestamp - 100}, ${timestamp - 50}`)
const drawStartTimestamp = timestamp - 100
const drawEndTimestamp = timestamp - 50
const result = await ticket.getAverageBalancesBetween(wallet1.address, [drawStartTimestamp, drawStartTimestamp - 50], [drawEndTimestamp, drawEndTimestamp - 50])
Expand All @@ -542,14 +535,13 @@ describe('Ticket', () => {

it('should return half the minted balance when the duration is centered over first twab, and zero from before', async () => {
await increaseTime(100);
// console.log(`Test getAverageBalance() : ${timestamp - 50}, ${timestamp + 50}`)

const drawStartTimestamp0 = timestamp - 100
const drawEndTimestamp0 = timestamp - 50


const drawStartTimestamp = timestamp - 50
const drawEndTimestamp = timestamp + 50
const result = await ticket.getAverageBalancesBetween(wallet1.address, [drawStartTimestamp, drawStartTimestamp0], [drawEndTimestamp, drawEndTimestamp0])

expect(result[0]).to.deep.equal(toWei('500'))
expect(result[1]).to.deep.equal(toWei('0'))
})
Expand Down
15 changes: 0 additions & 15 deletions test/TwabLibraryExposed.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,28 +80,23 @@ describe('TwabLib', () => {
});

it('should return an average of zero for pre-history requests', async () => {
// console.log(`Test getAverageBalance() : ${timestamp - 100}, ${timestamp - 50}`)
expect(await twabLib.getAverageBalanceBetween(timestamp - 100, timestamp - 50, currentTime)).to.equal('0');
});

it('should return an average of zero for pre-history requests when including the first twab', async () => {
// console.log(`Test getAverageBalance() : ${timestamp - 100}, ${timestamp - 50}`)
expect(await twabLib.getAverageBalanceBetween(timestamp - 100, timestamp, currentTime)).to.equal('0');
});

it('should not project into the future', async () => {
// at this time the user has held 1000 tokens for zero seconds
// console.log(`Test getAverageBalance() : ${timestamp - 50}, ${timestamp + 50}`)
expect(await twabLib.getAverageBalanceBetween(timestamp - 50, timestamp + 50, timestamp)).to.equal('0')
})

it('should return half the minted balance when the duration is centered over first twab', async () => {
// console.log(`Test getAverageBalance() : ${timestamp - 50}, ${timestamp + 50}`)
expect(await twabLib.getAverageBalanceBetween(timestamp - 50, timestamp + 50, currentTime)).to.equal('500')
})

it('should return an accurate average when the range is after the last twab', async () => {
// console.log(`Test getAverageBalance() : ${timestamp + 50}, ${timestamp + 51}`)
expect(await twabLib.getAverageBalanceBetween(timestamp + 50, timestamp + 51, currentTime)).to.equal('1000')
})
})
Expand All @@ -128,27 +123,22 @@ describe('TwabLib', () => {
*/

it('should return an average of zero for pre-history requests', async () => {
// console.log(`Test getAverageBalance() : ${timestamp - 100}, ${timestamp - 50}`)
expect(await twabLib.getAverageBalanceBetween(timestamp1 - 100, timestamp1 - 50, currentTime)).to.equal(toWei('0'));
});

it('should return half the minted balance when the duration is centered over first twab', async () => {
// console.log(`Test getAverageBalance() : ${timestamp - 50}, ${timestamp + 50}`)
expect(await twabLib.getAverageBalanceBetween(timestamp1 - 50, timestamp1 + 50, currentTime)).to.equal(toWei('500'))
})

it('should return an accurate average when the range is between twabs', async () => {
// console.log(`Test getAverageBalance() : ${timestamp + 50}, ${timestamp + 55}`)
expect(await twabLib.getAverageBalanceBetween(timestamp1 + 50, timestamp1 + 55, currentTime)).to.equal(toWei('1000'))
})

it('should return an accurate average when the end is after the last twab', async () => {
// console.log(`Test getAverageBalance() : ${timestamp2 - 50}, ${timestamp2 + 50}`)
expect(await twabLib.getAverageBalanceBetween(timestamp2 - 50, timestamp2 + 50, currentTime)).to.equal(toWei('750'))
})

it('should return an accurate average when the range is after twabs', async () => {
// console.log(`Test getAverageBalance() : ${timestamp2 + 50}, ${timestamp2 + 51}`)
expect(await twabLib.getAverageBalanceBetween(timestamp2 + 50, timestamp2 + 51, currentTime)).to.equal(toWei('500'))
})
})
Expand Down Expand Up @@ -202,27 +192,22 @@ describe('TwabLib', () => {
*/

it('should return zero when request before first twab', async () => {
// console.log(`Test getAverageBalance() : ${timestamp - 100}, ${timestamp - 50}`)
expect(await twabLib.getBalanceAt(timestamp1 - 1, currentTime)).to.equal(toWei('0'));
});

it('should return end-of-block balance when request is on first twab', async () => {
// console.log(`Test getAverageBalance() : ${timestamp - 50}, ${timestamp + 50}`)
expect(await twabLib.getBalanceAt(timestamp1, currentTime)).to.equal(mintAmount)
})

it('should return mint amount when between twabs', async () => {
// console.log(`Test getAverageBalance() : ${timestamp + 50}, ${timestamp + 55}`)
expect(await twabLib.getBalanceAt(timestamp1 + 1, currentTime)).to.equal(mintAmount)
})

it('should return current balance when on last twab', async () => {
// console.log(`Test getAverageBalance() : ${timestamp2 - 50}, ${timestamp2 + 50}`)
expect(await twabLib.getBalanceAt(timestamp2, currentTime)).to.equal(mintAmount.sub(transferAmount))
})

it('should return current balance when after last twab', async () => {
// console.log(`Test getAverageBalance() : ${timestamp2 + 50}, ${timestamp2 + 51}`)
expect(await twabLib.getBalanceAt(timestamp2 + 50, currentTime)).to.equal(mintAmount.sub(transferAmount))
})
})
Expand Down
10 changes: 6 additions & 4 deletions test/prize-strategy/PrizeSplitStrategy.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ describe('PrizeSplitStrategy', () => {
let wallet1: SignerWithAddress;
let wallet2: SignerWithAddress;
let wallet3: SignerWithAddress;
let wallet4: SignerWithAddress;
let prizeSplitStrategy: Contract;
let ticket: Contract;
let PrizePool: Artifact;
Expand All @@ -23,7 +22,7 @@ describe('PrizeSplitStrategy', () => {
let erc20MintableFactory: ContractFactory

before(async () => {
[wallet1, wallet2, wallet3, wallet4] = await getSigners();
[wallet1, wallet2, wallet3] = await getSigners();

prizeSplitStrategyFactory = await ethers.getContractFactory(
'PrizeSplitStrategyHarness',
Expand All @@ -40,7 +39,9 @@ describe('PrizeSplitStrategy', () => {
debug('mocking ticket and prizePool...');
ticket = await erc20MintableFactory.deploy('Ticket', 'TICK');
prizePool = await deployMockContract(wallet1 as Signer, PrizePool.abi);
await prizePool.mock.ticket.returns(ticket.address);

await prizePool.mock.getTicket.returns(ticket.address);

debug('deploy prizeSplitStrategy...');
prizeSplitStrategy = await prizeSplitStrategyFactory.deploy(wallet1.address, prizePool.address);
});
Expand Down Expand Up @@ -151,8 +152,9 @@ describe('PrizeSplitStrategy', () => {
/*============================================ */
describe('Internal Functions', () => {
it('should awardPrizeSplitAmount()', async () => {
await prizePool.mock.ticket.returns(ticket.address)
await prizePool.mock.getTicket.returns(ticket.address)
await prizePool.mock.award.returns()

expect(await prizeSplitStrategy.awardPrizeSplitAmount(wallet3.address, toWei('100')))
.to.emit(prizeSplitStrategy, 'PrizeSplitAwarded')
.withArgs(wallet3.address, toWei('100'), ticket.address)
Expand Down

0 comments on commit 6373d7e

Please sign in to comment.