Skip to content

Commit

Permalink
fix(PrizeDistV2): pass drawPickIndices to claim
Browse files Browse the repository at this point in the history
  • Loading branch information
PierrickGT committed Jun 1, 2022
1 parent 7ae2349 commit 0f242fb
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 46 deletions.
18 changes: 7 additions & 11 deletions contracts/DrawCalculatorV3.sol
Original file line number Diff line number Diff line change
Expand Up @@ -87,26 +87,22 @@ contract DrawCalculatorV3 is IDrawCalculatorV3, Manageable {
ITicket _ticket,
address _user,
uint32[] calldata _drawIds,
bytes calldata _pickIndicesForDraws
uint64 [][] calldata _drawPickIndices
) external view override returns (
uint256[] memory prizesAwardable,
bytes memory prizeCounts,
uint64[][] memory drawPickIndices
bytes memory prizeCounts
) {
uint64[][] memory _pickIndices = abi.decode(_pickIndicesForDraws, (uint64 [][]));
require(_pickIndices.length == _drawIds.length, "DrawCalc/invalid-pick-indices");
require(_drawPickIndices.length == _drawIds.length, "DrawCalc/invalid-pick-indices");

// User address is hashed once.
bytes32 _userRandomNumber = keccak256(abi.encodePacked(_user));

drawPickIndices = _pickIndices;

(prizesAwardable, prizeCounts) = _calculatePrizesAwardable(
_ticket,
_user,
_userRandomNumber,
_drawIds,
_pickIndices
_drawPickIndices
);
}

Expand Down Expand Up @@ -187,14 +183,14 @@ contract DrawCalculatorV3 is IDrawCalculatorV3, Manageable {
* @param _user Address of the user for which to calculate awardable prizes for
* @param _userRandomNumber Random number of the user to consider over draws
* @param _drawIds Array of DrawIds for which to calculate awardable prizes for
* @param _pickIndicesForDraws Pick indices for each Draw
* @param _drawPickIndices Pick indices for each Draw
*/
function _calculatePrizesAwardable(
ITicket _ticket,
address _user,
bytes32 _userRandomNumber,
uint32[] memory _drawIds,
uint64[][] memory _pickIndicesForDraws
uint64[][] memory _drawPickIndices
) internal view returns (
uint256[] memory prizesAwardable,
bytes memory prizeCounts
Expand Down Expand Up @@ -227,7 +223,7 @@ contract DrawCalculatorV3 is IDrawCalculatorV3, Manageable {
_draw.winningRandomNumber,
_totalUserPicks,
_userRandomNumber,
_pickIndicesForDraws[_drawIndex],
_drawPickIndices[_drawIndex],
_prizeConfig
);
}
Expand Down
14 changes: 9 additions & 5 deletions contracts/PrizeDistributorV2.sol
Original file line number Diff line number Diff line change
Expand Up @@ -108,19 +108,23 @@ contract PrizeDistributorV2 is Manageable {
* @param _ticket Address of the Ticket to claim prizes for
* @param _user Address of the user to claim rewards for. Does NOT need to be msg.sender
* @param _drawIds Draw IDs from global DrawBuffer reference
* @param _data Draws pick indices
* @param _drawPickIndices Pick indices for each drawId
* @return Total claim payout. May include calculations from multiple draws.
*/
function claim(
ITicket _ticket,
address _user,
uint32[] calldata _drawIds,
bytes calldata _data
uint64[][] calldata _drawPickIndices
) external returns (uint256) {
uint256 totalPayout;

(uint256[] memory drawPayouts, , uint64[][] memory drawPickIndices) = drawCalculator
.calculate(_ticket, _user, _drawIds, _data);
(uint256[] memory drawPayouts, ) = drawCalculator.calculate(
_ticket,
_user,
_drawIds,
_drawPickIndices
);

uint256 drawPayoutsLength = drawPayouts.length;

Expand All @@ -141,7 +145,7 @@ contract PrizeDistributorV2 is Manageable {

totalPayout += payoutDiff;

emit ClaimedDraw(_user, drawId, payoutDiff, drawPickIndices[payoutIndex]);
emit ClaimedDraw(_user, drawId, payoutDiff, _drawPickIndices[payoutIndex]);
}

_awardPayout(_user, totalPayout);
Expand Down
8 changes: 3 additions & 5 deletions contracts/interfaces/IDrawCalculatorV3.sol
Original file line number Diff line number Diff line change
Expand Up @@ -18,23 +18,21 @@ interface IDrawCalculatorV3 {
* @param ticket Address of the ticket to calculate awardable prizes for
* @param user Address of the user for which to calculate awardable prizes for
* @param drawIds Array of DrawIds for which to calculate awardable prizes for
* @param data ABI encoded pick indices for all Draws. Expected to be winning picks. Pick indices must be less than the totalUserPicks.
* @param drawPickIndices Pick indices for each drawId
* @return List of awardable prize amounts ordered by drawId.
* @return List of prize counts ordered by tiers.
* @return Pick indices for each drawId.
*/
function calculate(
ITicket ticket,
address user,
uint32[] calldata drawIds,
bytes calldata data
uint64 [][] calldata drawPickIndices
)
external
view
returns (
uint256[] memory,
bytes memory,
uint64[][] memory
bytes memory
);

/**
Expand Down
25 changes: 11 additions & 14 deletions test/DrawCalculatorV3.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -735,7 +735,7 @@ describe('DrawCalculatorV3', () => {
);

const timestamps = [(await provider.getBlock('latest')).timestamp];
const pickIndices = encoder.encode(['uint256[][]'], [[['1']]]);
const pickIndices = [['1']];
const ticketBalance = toWei('10');
const totalSupply = toWei('100');

Expand Down Expand Up @@ -813,7 +813,7 @@ describe('DrawCalculatorV3', () => {
);

const timestamps = [(await provider.getBlock('latest')).timestamp];
const pickIndices = encoder.encode(['uint256[][]'], [[['1']]]);
const pickIndices = [['1']];
const ticketBalance = toWei('10');
const totalSupply = toWei('100');

Expand Down Expand Up @@ -889,7 +889,7 @@ describe('DrawCalculatorV3', () => {
);

const timestamps = [(await provider.getBlock('latest')).timestamp];
const pickIndices = encoder.encode(['uint256[][]'], [[['1', '1']]]); // this isn't valid
const pickIndices = [['1', '1']]; // this isn't valid
const ticketBalance = toWei('10');
const totalSupply = toWei('100');

Expand Down Expand Up @@ -942,10 +942,7 @@ describe('DrawCalculatorV3', () => {

const timestamps = [(await provider.getBlock('latest')).timestamp];

const pickIndices = encoder.encode(
['uint256[][]'],
[[[...new Array<number>(1000).keys()]]],
);
const pickIndices = [[...new Array<number>(1000).keys()]];

const totalSupply = toWei('10000');
const ticketBalance = toWei('1000'); // 10 percent of total supply
Expand Down Expand Up @@ -1013,7 +1010,7 @@ describe('DrawCalculatorV3', () => {
await prizeConfigHistory.mock.getPrizeConfig.withArgs(1).returns(prizeConfig);

const timestamps = [(await provider.getBlock('latest')).timestamp];
const pickIndices = encoder.encode(['uint256[][]'], [[['1']]]);
const pickIndices = [['1']];
const ticketBalance = toWei('10');
const totalSupply = toWei('100');

Expand Down Expand Up @@ -1074,7 +1071,7 @@ describe('DrawCalculatorV3', () => {
await prizeConfigHistory.mock.getPrizeConfig.withArgs(1).returns(prizeConfig);

const timestamps = [(await provider.getBlock('latest')).timestamp];
const pickIndices = encoder.encode(['uint256[][]'], [[['1']]]);
const pickIndices = [['1']];
const ticketBalance = toWei('10');
const totalSupply = toWei('100');

Expand Down Expand Up @@ -1144,7 +1141,7 @@ describe('DrawCalculatorV3', () => {
await prizeConfigHistory.mock.getPrizeConfig.withArgs(1).returns(prizeConfig);

const timestamps = [(await provider.getBlock('latest')).timestamp];
const pickIndices = encoder.encode(['uint256[][]'], [[['1']]]);
const pickIndices = [['1']];
const ticketBalance = toWei('10');
const totalSupply = toWei('100');

Expand Down Expand Up @@ -1205,7 +1202,7 @@ describe('DrawCalculatorV3', () => {
(await provider.getBlock('latest')).timestamp - 5,
];

const pickIndices = encoder.encode(['uint256[][]'], [[['1'], ['2']]]);
const pickIndices = [['1'], ['2']];
const ticketBalance = toWei('10');
const ticketBalance2 = toWei('10');
const totalSupply1 = toWei('100');
Expand Down Expand Up @@ -1321,7 +1318,7 @@ describe('DrawCalculatorV3', () => {
const totalSupply1 = toWei('100');
const totalSupply2 = toWei('100');

const pickIndices = encoder.encode(['uint256[][]'], [[['1'], ['2']]]);
const pickIndices = [['1'], ['2']];
const ticketBalance = toWei('6'); // they had 6pc of all tickets

const prizeConfig: PrizeConfig = {
Expand Down Expand Up @@ -1419,7 +1416,7 @@ describe('DrawCalculatorV3', () => {

const timestamps = [(await provider.getBlock('latest')).timestamp];
const totalSupply = toWei('100');
const pickIndices = encoder.encode(['uint256[][]'], [[['1', '2', '3']]]);
const pickIndices = [['1', '2', '3']];
const ticketBalance = toWei('6');

const prizeConfig: PrizeConfig = {
Expand Down Expand Up @@ -1488,7 +1485,7 @@ describe('DrawCalculatorV3', () => {
const timestamps = [(await provider.getBlock('latest')).timestamp];
const totalSupply = toWei('100');

const pickIndices = encoder.encode(['uint256[][]'], [[['1']]]);
const pickIndices = [['1']];
const ticketBalance = toWei('10');

const offsetStartTimestamps = modifyTimestampsWithOffset(
Expand Down
28 changes: 17 additions & 11 deletions test/PrizeDistributorV2.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,38 +58,44 @@ describe('PrizeDistributorV2', () => {
* 5. return totalPayout
*/
describe('claim(ITicket _ticket,address _user,uint32[] calldata _drawIds,bytes calldata _data))', () => {
let pickIndices: string;
let pickIndicesDecoded: utils.Result;
let pickIndices: string[][];

beforeEach(() => {
pickIndices = encoder.encode(['uint64[][]'], [[['1']]]);
pickIndicesDecoded = encoder.decode(['uint64[][]'], pickIndices);
pickIndices = [['1']];
});

it('should SUCCEED to claim and emit ClaimedDraw event', async () => {
await drawCalculator.mock.calculate
.withArgs(ticket.address, wallet1.address, [1], pickIndices)
.returns([toWei('10')], '0x', pickIndicesDecoded[0]);
.returns([toWei('10')], '0x');

await expect(
prizeDistributorV2.claim(ticket.address, wallet1.address, [1], pickIndices),
)
.to.emit(prizeDistributorV2, 'ClaimedDraw')
.withArgs(wallet1.address, 1, toWei('10'), pickIndicesDecoded[0][0]);
.withArgs(wallet1.address, 1, toWei('10'), pickIndices[0]);
});

it('should SUCCEED to payout the difference if user claims more', async () => {
await drawCalculator.mock.calculate
.withArgs(ticket.address, wallet1.address, [1], pickIndices)
.returns([toWei('10')], '0x', pickIndicesDecoded[0]);
.returns([toWei('10')], '0x');

await prizeDistributorV2.claim(ticket.address, wallet1.address, [1], pickIndices);
await expect(
prizeDistributorV2.claim(ticket.address, wallet1.address, [1], pickIndices),
)
.to.emit(prizeDistributorV2, 'ClaimedDraw')
.withArgs(wallet1.address, 1, toWei('10'), pickIndices[0]);

await drawCalculator.mock.calculate
.withArgs(ticket.address, wallet1.address, [1], pickIndices)
.returns([toWei('20')], '0x', pickIndicesDecoded[0]);
.returns([toWei('20')], '0x');

await prizeDistributorV2.claim(ticket.address, wallet1.address, [1], pickIndices);
await expect(
prizeDistributorV2.claim(ticket.address, wallet1.address, [1], pickIndices),
)
.to.emit(prizeDistributorV2, 'ClaimedDraw')
.withArgs(wallet1.address, 1, toWei('10'), pickIndices[0]);

expect(await prizeDistributorV2.getDrawPayoutBalanceOf(wallet1.address, 1)).to.equal(
toWei('20'),
Expand All @@ -99,7 +105,7 @@ describe('PrizeDistributorV2', () => {
it('should REVERT on 2.update because the prize was previously claimed', async () => {
await drawCalculator.mock.calculate
.withArgs(ticket.address, wallet1.address, [0], pickIndices)
.returns([toWei('10')], '0x', pickIndicesDecoded[0]);
.returns([toWei('10')], '0x');

await prizeDistributorV2.claim(ticket.address, wallet1.address, [0], pickIndices);

Expand Down

0 comments on commit 0f242fb

Please sign in to comment.