Skip to content

Commit

Permalink
Merge df30639 into 7ae2349
Browse files Browse the repository at this point in the history
  • Loading branch information
PierrickGT committed Jun 1, 2022
2 parents 7ae2349 + df30639 commit e685c9a
Show file tree
Hide file tree
Showing 2 changed files with 106 additions and 18 deletions.
52 changes: 44 additions & 8 deletions contracts/PrizeDistributorV2.sol
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ contract PrizeDistributorV2 is Manageable {
using SafeERC20 for IERC20;

/**
* @notice Emit when user has claimed token from the PrizeDistributorV2.
* @notice Emitted when user has claimed token from the PrizeDistributorV2.
* @param user User address receiving draw claim payouts
* @param drawId Draw id that was paid out
* @param payout Payout for draw
Expand All @@ -35,20 +35,27 @@ contract PrizeDistributorV2 is Manageable {
);

/**
* @notice Emit when IDrawCalculatorV3 is set.
* @notice Emitted when IDrawCalculatorV3 is set.
* @param caller Address who has set the new DrawCalculator
* @param calculator IDrawCalculatorV3 address
*/
event DrawCalculatorSet(address indexed caller, IDrawCalculatorV3 indexed calculator);

/**
* @notice Emit when Token is set.
* @notice Emitted when Token is set.
* @param token Token address
*/
event TokenSet(IERC20 indexed token);

/**
* @notice Emit when ERC20 tokens are withdrawn.
* @notice Emitted when tokenVault is set.
* @param caller Address who has set the new tokenVault
* @param tokenVault Address of the tokenVault that was set
*/
event TokenVaultSet(address indexed caller, address indexed tokenVault);

/**
* @notice Emitted when ERC20 tokens are withdrawn.
* @param token ERC20 token transferred
* @param to Address that received funds
* @param amount Amount of tokens transferred
Expand All @@ -63,12 +70,12 @@ contract PrizeDistributorV2 is Manageable {
/// @notice Token address
IERC20 internal immutable token;

/// @notice The tokenVault that stores the prize tokens
address internal tokenVault;

/// @notice Maps users => drawId => paid out balance
mapping(address => mapping(uint256 => uint256)) internal userDrawPayouts;

/// @notice The tokenVault that stores the prize tokens
address public tokenVault;

/* ============ Constructor ============ */

/**
Expand All @@ -88,9 +95,9 @@ contract PrizeDistributorV2 is Manageable {
require(address(_token) != address(0), "PDistV2/token-not-zero-address");

_setDrawCalculator(_drawCalculator);
_setTokenVault(_tokenVault);

token = _token;
tokenVault = _tokenVault;

emit TokenSet(_token);
}
Expand Down Expand Up @@ -197,6 +204,14 @@ contract PrizeDistributorV2 is Manageable {
return token;
}

/**
* @notice Read global tokenVault address.
* @return Address of the tokenVault
*/
function getTokenVault() external view returns (address) {
return tokenVault;
}

/**
* @notice Sets DrawCalculator reference contract.
* @param _newCalculator DrawCalculator address
Expand All @@ -211,6 +226,16 @@ contract PrizeDistributorV2 is Manageable {
return _newCalculator;
}

/**
* @notice Sets TokenVault address.
* @param _tokenVault Address of the new TokenVault
* @return New TokenVault address
*/
function setTokenVault(address _tokenVault) external onlyManagerOrOwner returns (address) {
_setTokenVault(_tokenVault);
return _tokenVault;
}

/* ============ Internal Functions ============ */

/**
Expand Down Expand Up @@ -252,6 +277,17 @@ contract PrizeDistributorV2 is Manageable {
emit DrawCalculatorSet(msg.sender, _newCalculator);
}

/**
* @notice Sets TokenVault address.
* @param _tokenVault Address of the new TokenVault
*/
function _setTokenVault(address _tokenVault) internal {
require(_tokenVault != address(0), "PDistV2/vault-not-zero-address");
tokenVault = _tokenVault;

emit TokenVaultSet(msg.sender, _tokenVault);
}

/**
* @notice Transfer claimed draw(s) total payout to user.
* @param _to Address of the user to award payout to
Expand Down
72 changes: 62 additions & 10 deletions test/PrizeDistributorV2.test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { SignerWithAddress } from '@nomiclabs/hardhat-ethers/signers';
import { expect } from 'chai';
import { deployMockContract, MockContract } from 'ethereum-waffle';
import { utils, constants, Contract, ContractFactory, BigNumber } from 'ethers';
Expand All @@ -8,16 +9,18 @@ const { defaultAbiCoder: encoder, parseEther: toWei } = utils;
const { AddressZero } = constants;

describe('PrizeDistributorV2', () => {
let wallet1: any;
let wallet2: any;
let vault: any;
let wallet1: SignerWithAddress;
let wallet2: SignerWithAddress;
let wallet3: SignerWithAddress;
let vault: SignerWithAddress;

let token: Contract;
let ticket: Contract;
let prizeDistributorV2: Contract;
let drawCalculator: MockContract;

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

beforeEach(async () => {
Expand Down Expand Up @@ -117,26 +120,69 @@ describe('PrizeDistributorV2', () => {
* 3. emit DrawCalculatorSet event
*/
describe('setDrawCalculator(DrawCalculatorInterface _newCalculator)', () => {
it('should SUCCEED updating the drawCalculator global variable', async () => {
expect(await prizeDistributorV2.setDrawCalculator(wallet2.address))
it('should SUCCEED to update the drawCalculator global variable if owner', async () => {
expect(await prizeDistributorV2.setDrawCalculator(wallet3.address))
.to.emit(prizeDistributorV2, 'DrawCalculatorSet')
.withArgs(wallet1.address, wallet2.address);
.withArgs(wallet1.address, wallet3.address);
});

it('should SUCCEED to update the drawCalculator global variable if manager', async () => {
await prizeDistributorV2.setManager(wallet2.address);

expect(await prizeDistributorV2.connect(wallet2).setDrawCalculator(wallet3.address))
.to.emit(prizeDistributorV2, 'DrawCalculatorSet')
.withArgs(wallet2.address, wallet3.address);
});

it('should REVERT on 1.authorized because wallet is NOT an OWNER or MANAGER', async () => {
const PrizeDistributorV2Unauthorized = prizeDistributorV2.connect(wallet2);
const PrizeDistributorV2Unauthorized = prizeDistributorV2.connect(wallet3);
await expect(
PrizeDistributorV2Unauthorized.setDrawCalculator(AddressZero),
).to.be.revertedWith('Manageable/caller-not-manager-or-owner');
});

it('should REVERT on 2.update because the drawCalculator address is NULL', async () => {
it('should REVERT on 2.update because the drawCalculator address is address zero', async () => {
await expect(prizeDistributorV2.setDrawCalculator(AddressZero)).to.be.revertedWith(
'PDistV2/calc-not-zero-address',
);
});
});

/**
* @description Test setTokenVault(address _tokenVault) function
* -= Expected Behavior =-
* 1. authorize the `msg.sender` has OWNER or MANAGER role
* 2. update global tokenVault variable
* 3. emit TokenVaultSet event
*/
describe('setTokenVault(address _tokenVault)', () => {
it('should SUCCEED to update the tokenVault global variable if owner', async () => {
expect(await prizeDistributorV2.setTokenVault(wallet3.address))
.to.emit(prizeDistributorV2, 'TokenVaultSet')
.withArgs(wallet1.address, wallet3.address);
});

it('should SUCCEED to update the tokenVault global variable if manager', async () => {
await prizeDistributorV2.setManager(wallet2.address);

expect(await prizeDistributorV2.connect(wallet2).setTokenVault(wallet3.address))
.to.emit(prizeDistributorV2, 'TokenVaultSet')
.withArgs(wallet2.address, wallet3.address);
});

it('should REVERT on 1.authorized because wallet is NOT an OWNER or MANAGER', async () => {
await expect(
prizeDistributorV2.connect(wallet2).setTokenVault(wallet3.address),
).to.be.revertedWith('Manageable/caller-not-manager-or-owner');
});

it('should REVERT on 2.update because the tokenVault address is address zero', async () => {
await expect(prizeDistributorV2.setTokenVault(AddressZero)).to.be.revertedWith(
'PDistV2/vault-not-zero-address',
);
});
});

/**
* @description Test withdrawERC20(IERC20 _erc20Token, address _to, uint256 _amount) function
* -= Expected Behavior =-
Expand Down Expand Up @@ -199,8 +245,14 @@ describe('PrizeDistributorV2', () => {
});

describe('getToken()', () => {
it('should succesfully read global token variable', async () => {
it('should successfully read global token variable', async () => {
expect(await prizeDistributorV2.getToken()).to.equal(ticket.address);
});
});

describe('getTokenVault()', () => {
it('should successfully read global tokenVault variable', async () => {
expect(await prizeDistributorV2.getTokenVault()).to.equal(vault.address);
});
});
});

0 comments on commit e685c9a

Please sign in to comment.