Skip to content

Commit

Permalink
fix(PrizeSplitStrategy): merge tests
Browse files Browse the repository at this point in the history
  • Loading branch information
PierrickGT committed Jan 4, 2022
1 parent f78cda0 commit cfc77e8
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 162 deletions.
115 changes: 0 additions & 115 deletions test/PrizeSplitStrategy.test.ts

This file was deleted.

131 changes: 84 additions & 47 deletions test/prize-strategy/PrizeSplitStrategy.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,38 +4,44 @@ import { ethers, artifacts } from 'hardhat';
import { Artifact } from 'hardhat/types';
import { Signer } from '@ethersproject/abstract-signer';
import { SignerWithAddress } from '@nomiclabs/hardhat-ethers/signers';
import { constants, Contract, ContractFactory } from 'ethers';
import { Contract, ContractFactory } from 'ethers';

const { constants, getContractFactory, getSigners, utils } = ethers;
const { AddressZero } = constants;

const { getSigners } = ethers;
const debug = require('debug')('ptv4:PrizeSplitStrategy');
const toWei = (val: string | number) => ethers.utils.parseEther('' + val);

const toWei = (val: string | number) => utils.parseEther('' + val);

describe('PrizeSplitStrategy', () => {
let wallet1: SignerWithAddress;
let wallet2: SignerWithAddress;
let wallet3: SignerWithAddress;

let prizeSplitStrategy: Contract;
let ticket: Contract;
let PrizePool: Artifact;
let prizePool: MockContract;

let prizeSplitStrategyFactory: ContractFactory;
let erc20MintableFactory: ContractFactory;

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

prizeSplitStrategyFactory = await ethers.getContractFactory('PrizeSplitStrategyHarness');
prizeSplitStrategyFactory = await getContractFactory('PrizeSplitStrategyHarness');

erc20MintableFactory = await ethers.getContractFactory('ERC20Mintable');
erc20MintableFactory = await getContractFactory('ERC20Mintable');

PrizePool = await artifacts.readArtifact('PrizePool');
});

beforeEach(async () => {
debug('mocking ticket and prizePool...');

ticket = await erc20MintableFactory.deploy('Ticket', 'TICK');
prizePool = await deployMockContract(wallet1 as Signer, PrizePool.abi);

prizePool = await deployMockContract(wallet1 as Signer, PrizePool.abi);
await prizePool.mock.getTicket.returns(ticket.address);

debug('deploy prizeSplitStrategy...');
Expand All @@ -45,13 +51,10 @@ describe('PrizeSplitStrategy', () => {
);
});

/*============================================ */
// Core Functions ----------------------------
/*============================================ */
describe('constructor', () => {
it('should fail to set invalid prizeStrategy', async () => {
await expect(
prizeSplitStrategyFactory.deploy(wallet1.address, constants.AddressZero),
prizeSplitStrategyFactory.deploy(wallet1.address, AddressZero),
).to.be.revertedWith('PrizeSplitStrategy/prize-pool-not-zero-address');
});

Expand All @@ -64,48 +67,87 @@ describe('PrizeSplitStrategy', () => {
});
});

describe('Core Functions', () => {
describe('distribute()', () => {
it('should stop executing if captured interest is 0', async () => {
await prizePool.mock.captureAwardBalance.returns(toWei('0'));
await prizePool.mock.award.withArgs(wallet2.address, toWei('0')).returns();
const distribute = await prizeSplitStrategy.distribute();
await expect(distribute)
.to.not.emit(prizeSplitStrategy, 'Distributed')
.withArgs(toWei('100'));
});

it('should award 100% of the captured balance to the PrizeReserve', async () => {
await prizeSplitStrategy.setPrizeSplits([
{
target: wallet2.address,
percentage: 1000,
},
]);
describe('distribute()', () => {
it('should stop executing if captured interest is 0', async () => {
await prizePool.mock.captureAwardBalance.returns(toWei('0'));
await prizePool.mock.award.withArgs(wallet2.address, toWei('0')).returns();

const distribute = await prizeSplitStrategy.distribute();

await expect(distribute)
.to.not.emit(prizeSplitStrategy, 'Distributed')
.withArgs(toWei('100'));
});

it('should award 100% of the captured balance to the PrizeReserve', async () => {
await prizeSplitStrategy.setPrizeSplits([
{
target: wallet2.address,
percentage: 1000,
},
]);

await prizePool.mock.captureAwardBalance.returns(toWei('100'));
await prizePool.mock.award.withArgs(wallet2.address, toWei('100')).returns();

await prizePool.mock.captureAwardBalance.returns(toWei('100'));
await prizePool.mock.award.withArgs(wallet2.address, toWei('100')).returns();
// Distribute Award
const distribute = await prizeSplitStrategy.distribute();

const distribute = await prizeSplitStrategy.distribute();
// Verify Contract Events
await expect(distribute)
.to.emit(prizeSplitStrategy, 'Distributed')
.withArgs(toWei('100'));

await expect(distribute)
.to.emit(prizeSplitStrategy, 'Distributed')
.withArgs(toWei('100'));
await expect(distribute)
.to.emit(prizeSplitStrategy, 'PrizeSplitAwarded')
.withArgs(wallet2.address, toWei('100'), ticket.address);
});

it('should award (50%/50%) the captured balance to the PrizeReserve and a secondary account.', async () => {
await prizeSplitStrategy.setPrizeSplits([
{
target: wallet2.address,
percentage: 500,
token: 1,
},
{
target: wallet3.address,
percentage: 500,
token: 0,
},
]);

await prizePool.mock.captureAwardBalance.returns(toWei('100'));

// Mock PrizeReserve Award Sponsorhip
await prizePool.mock.award.withArgs(wallet2.address, toWei('50')).returns();

// Mock Secondary Wallet Award Sponsorhip
await prizePool.mock.award.withArgs(wallet3.address, toWei('50')).returns();

await expect(distribute)
.to.emit(prizeSplitStrategy, 'PrizeSplitAwarded')
.withArgs(wallet2.address, toWei('100'), ticket.address);
});
// Distribute Award
const distribute = await prizeSplitStrategy.distribute();

// Verify Contract Events
await expect(distribute)
.to.emit(prizeSplitStrategy, 'Distributed')
.withArgs(toWei('100'));

await expect(distribute)
.to.emit(prizeSplitStrategy, 'PrizeSplitAwarded')
.withArgs(wallet2.address, toWei('50'), ticket.address);

await expect(distribute)
.to.emit(prizeSplitStrategy, 'PrizeSplitAwarded')
.withArgs(wallet3.address, toWei('50'), ticket.address);
});
});

/*============================================ */
// Getter Functions --------------------------
/*============================================ */
describe('Getter Functions', () => {
it('should getPrizePool()', async () => {
expect(await prizeSplitStrategy.getPrizePool()).to.equal(prizePool.address);
});

it('should prizeSplit()', async () => {
await prizeSplitStrategy.setPrizeSplits([
{ target: wallet3.address, percentage: 1000 },
Expand All @@ -132,9 +174,7 @@ describe('PrizeSplitStrategy', () => {
}
});
});
/*============================================ */
// Setter Functions --------------------------
/*============================================ */

describe('Setter Functions', () => {
it('should setPrizeSplits()', async () => {
expect(
Expand All @@ -157,9 +197,6 @@ describe('PrizeSplitStrategy', () => {
});
});

/*============================================ */
// Internal Functions ----------------------------
/*============================================ */
describe('Internal Functions', () => {
it('should awardPrizeSplitAmount()', async () => {
await prizePool.mock.getTicket.returns(ticket.address);
Expand Down

0 comments on commit cfc77e8

Please sign in to comment.