Skip to content

Commit

Permalink
update(PrizeTierHistory): fixes from PR comments
Browse files Browse the repository at this point in the history
  • Loading branch information
kamescg committed Mar 21, 2022
1 parent 1a29642 commit 7414f01
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 14 deletions.
9 changes: 5 additions & 4 deletions contracts/PrizeTierHistory.sol
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@
pragma solidity 0.8.6;
import "@pooltogether/owner-manager-contracts/contracts/Manageable.sol";
import "./interfaces/IPrizeTierHistory.sol";
import "./abstract/IdBinarySearchLib.sol";
import "./abstract/BinarySearchLib.sol";

contract PrizeTierHistory is IPrizeTierHistory, Manageable {

using IdBinarySearchLib for uint32[];
using BinarySearchLib for uint32[];

uint32[] internal history;
mapping(uint32 => PrizeTier) internal prizeTiers;
Expand All @@ -31,8 +31,9 @@ contract PrizeTierHistory is IPrizeTierHistory, Manageable {
}

function getPrizeTierList(uint32[] calldata _drawIds) override external view returns (PrizeTier[] memory) {
PrizeTier[] memory _data = new PrizeTier[](_drawIds.length);
for (uint256 index = 0; index < _drawIds.length; index++) {
uint256 _length = _drawIds.length;
PrizeTier[] memory _data = new PrizeTier[](_length);
for (uint256 index = 0; index < _length; index++) {
_data[index] = prizeTiers[history[history.binarySearch(_drawIds[index])]];
}
return _data;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@
pragma solidity 0.8.6;

/**
* @title PoolTogether V4 IdBinarySearchLib
* @title PoolTogether V4 BinarySearchLib
* @author PoolTogether Inc Team
* @notice IdBinarySearchLib uses binary search to find a parent contract struct with the drawId parameter
* @notice BinarySearchLib uses binary search to find a parent contract struct with the drawId parameter
* @dev The implementing contract must provider access to a struct (i.e. PrizeTier) list with is both
* sorted and indexed by the drawId field for binary search to work.
*/
library IdBinarySearchLib {
library BinarySearchLib {

/**
* @notice Find ID in array of ordered IDs using Binary Search.
Expand All @@ -24,7 +24,7 @@ library IdBinarySearchLib {
uint32 oldestDrawId = _history[0];
uint32 newestDrawId = _history[rightSide];

require(_drawId >= oldestDrawId, "IdBinarySearchLib/draw-id-out-of-range");
require(_drawId >= oldestDrawId, "BinarySearchLib/draw-id-out-of-range");
if (_drawId >= newestDrawId) return rightSide;
if (_drawId == oldestDrawId) return leftSide;

Expand Down
12 changes: 6 additions & 6 deletions test/PrizeTierHistory.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ describe('PrizeTierHistory', () => {
let wallet2: SignerWithAddress;
let wallet3: SignerWithAddress;

let idBinarySearchLib: Contract;
let BinarySearchLib: Contract;
let prizeTierHistory: Contract;
let idBinarySearchLibFactory: ContractFactory;
let BinarySearchLibFactory: ContractFactory;
let prizeTierHistoryFactory: ContractFactory;

const prizeTiers = [
Expand Down Expand Up @@ -65,11 +65,11 @@ describe('PrizeTierHistory', () => {

before(async () => {
[wallet1, wallet2, wallet3] = await getSigners();
idBinarySearchLibFactory = await ethers.getContractFactory('IdBinarySearchLib');
idBinarySearchLib = await idBinarySearchLibFactory.deploy();
BinarySearchLibFactory = await ethers.getContractFactory('BinarySearchLib');
BinarySearchLib = await BinarySearchLibFactory.deploy();
prizeTierHistoryFactory = await ethers.getContractFactory('PrizeTierHistory', {
libraries: {
IdBinarySearchLib: idBinarySearchLib.address,
BinarySearchLib: BinarySearchLib.address,
}
});
});
Expand Down Expand Up @@ -111,7 +111,7 @@ describe('PrizeTierHistory', () => {
it('should fail to get a PrizeTer after history range', async () => {
await prizeTierHistory.push(prizeTiers[2]);
await expect(prizeTierHistory.getPrizeTier(4)).to.be.revertedWith(
'IdBinarySearchLib/draw-id-out-of-range',
'BinarySearchLib/draw-id-out-of-range',
);
});
});
Expand Down

0 comments on commit 7414f01

Please sign in to comment.