Skip to content

Commit

Permalink
added upkeep minimum block interval
Browse files Browse the repository at this point in the history
  • Loading branch information
Aodhgan committed May 11, 2021
1 parent da8309a commit 2f5f7f9
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 1 deletion.
30 changes: 30 additions & 0 deletions contracts/PrizeStrategyUpkeep.sol
Expand Up @@ -26,12 +26,21 @@ contract PrizeStrategyUpkeep is KeeperCompatibleInterface, Ownable {
/// @dev Set accordingly to prevent out-of-gas transactions during calls to performUpkeep
uint256 public upkeepBatchSize;

/// @notice Stores the last upkeep block number
uint256 public upkeepLastUpkeepBlockNumber;

/// @notice Stores the last upkeep block number
uint256 public upkeepMinimumBlockInterval;

/// @notice Emitted when the upkeepBatchSize has been changed
event UpkeepBatchSizeUpdated(uint256 upkeepBatchSize);

/// @notice Emitted when the prize pool registry has been changed
event UpkeepPrizePoolRegistryUpdated(AddressRegistry prizePoolRegistry);

/// @notice Emitted when the Upkeep Minimum Block interval is updated
event UpkeepMinimumBlockIntervalUpdated(uint256 upkeepMinimumBlockInterval);


constructor(AddressRegistry _prizePoolRegistry, uint256 _upkeepBatchSize) Ownable() public {
prizePoolRegistry = _prizePoolRegistry;
Expand Down Expand Up @@ -70,25 +79,38 @@ contract PrizeStrategyUpkeep is KeeperCompatibleInterface, Ownable {
/// @param performData Not used in this implementation.
function performUpkeep(bytes calldata performData) override external {

uint256 _upkeepLastUpkeepBlockNumber = upkeepLastUpkeepBlockNumber;
require(block.number > _upkeepLastUpkeepBlockNumber + upkeepMinimumBlockInterval);

address[] memory prizePools = prizePoolRegistry.getAddresses();

uint256 batchCounter = upkeepBatchSize; //counter for batch

uint256 poolIndex = 0;

uint256 updatedUpkeepBlockNumber;

while(batchCounter > 0 && poolIndex < prizePools.length){

address prizeStrategy = PrizePoolInterface(prizePools[poolIndex]).prizeStrategy();

if(prizeStrategy.canStartAward()){
PeriodicPrizeStrategyInterface(prizeStrategy).startAward();
updatedUpkeepBlockNumber = block.number;
batchCounter--;
}
else if(prizeStrategy.canCompleteAward()){
PeriodicPrizeStrategyInterface(prizeStrategy).completeAward();
updatedUpkeepBlockNumber = block.number;
batchCounter--;
}
poolIndex++;
}

// SSTORE upkeepLastUpkeepBlockNumber once
if(_upkeepLastUpkeepBlockNumber != updatedUpkeepBlockNumber){
upkeepLastUpkeepBlockNumber = updatedUpkeepBlockNumber;
}

}

Expand All @@ -108,6 +130,14 @@ contract PrizeStrategyUpkeep is KeeperCompatibleInterface, Ownable {
emit UpkeepPrizePoolRegistryUpdated(_prizePoolRegistry);
}


/// @notice Updates the upkeep minimum interval blocks
/// @param _upkeepMinimumBlockInterval New upkeepMinimumBlockInterval
function updateUpkeepMinimumBlockInterval(uint256 _upkeepMinimumBlockInterval) external onlyOwner {
upkeepMinimumBlockInterval = _upkeepMinimumBlockInterval;
emit UpkeepMinimumBlockIntervalUpdated(_upkeepMinimumBlockInterval);
}

}


32 changes: 31 additions & 1 deletion test/PrizeStrategyUpkeep.test.js
Expand Up @@ -82,7 +82,31 @@ describe('PrizeStrategyUpkeep', function() {
})
})

describe('able to call upkeep keep', () => {
describe('able to update the upkeepMinimumBlockInterval', () => {
it('owner can update', async () => {
await expect(prizePoolUpkeep.updateUpkeepMinimumBlockInterval(10))
.to.emit(prizePoolUpkeep, "UpkeepMinimumBlockIntervalUpdated")
.withArgs(10)
})
it('non-owner cannot update', async () => {
await expect(prizePoolUpkeep.connect(wallet2).updateUpkeepMinimumBlockInterval(3))
.to.be.reverted
})
})

describe('able to update the prize pool registry', () => {
// it('owner can update the registry', async () => {
// await expect(prizePoolUpkeep.updatePrizePoolRegistry(prizePoolRegistry.address))
// .to.emit(prizePoolUpkeep, "UpkeepPrizePoolRegistryUpdated")
// .withArgs(prizePoolRegistry)
// })
it('non-owner cannot update', async () => {
await expect(prizePoolUpkeep.connect(wallet2).updatePrizePoolRegistry(SENTINAL))
.to.be.reverted
})
})

describe('able to call upkeep performUpkeep()', () => {

let mockContractFactory, mockContract

Expand Down Expand Up @@ -126,6 +150,12 @@ describe('PrizeStrategyUpkeep', function() {
await expect(prizePoolUpkeep.callStatic.performUpkeep("0x")).to.be.revertedWith("2")
})

it('does not supportFunction canCompleteAward', async () => {
await prizeStrategy.mock.canStartAward.returns(false)
await prizeStrategy.mock.canCompleteAward.revertsWithReason("2")
await expect(prizePoolUpkeep.callStatic.performUpkeep("0x")).to.be.revertedWith("2")
})

})


Expand Down

0 comments on commit 2f5f7f9

Please sign in to comment.