Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added conditional revert in performUpkeep #4

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
96 changes: 50 additions & 46 deletions contracts/LZRateProviderPoker.sol
Original file line number Diff line number Diff line change
Expand Up @@ -45,65 +45,68 @@ contract LZRateProviderPoker is ConfirmedOwner, Pausable, KeeperCompatibleInterf
error OnlyKeeperRegistry(address sender);



address public KeeperAddress;
EnumerableSet.AddressSet private LZRateProviders;
uint256 public MinWaitPeriodSeconds;
uint256 public LastRun;

/**
* @param minWaitPeriodSeconds The minimum wait period for address between funding (for security)
/**
* @param minWaitPeriodSeconds The minimum wait period for address between funding (for security)
*/
constructor(uint256 minWaitPeriodSeconds, address keeperAddress)
ConfirmedOwner(msg.sender) {
setMinWaitPeriodSeconds(minWaitPeriodSeconds);
setKeeperAddress(keeperAddress);
}

/**
* @notice Check if enough time has passed and if so return true and a list of rate providers to poke based on the
/**
* @notice Check if enough time has passed and if so return true and a list of rate providers to poke based on the
* @notice current contents of LZRateProviders. This is done to save gas from getting EnumerableSet values on execution.
* @return upkeepNeeded signals if upkeep is needed, performData is an abi encoded list of addresses to poke
*/
function checkUpkeep(bytes calldata) external view override whenNotPaused
returns (bool upkeepNeeded, bytes memory performData){
if(address(this).balance < 0.01 ether){
function checkUpkeep(bytes calldata) external view override whenNotPaused
returns (bool upkeepNeeded, bytes memory performData){
if (address(this).balance < 0.01 ether) {
return (false, abi.encode(new address[](0)));
}
if (
LastRun + MinWaitPeriodSeconds <= block.timestamp
) {
return (true, abi.encode(getRateProviders()));
} else {
return (false, abi.encode(new address[](0)));
}
}
if (
LastRun + MinWaitPeriodSeconds <= block.timestamp
) {
return (true, abi.encode(getRateProviders()));
} else {
return (false, abi.encode(new address[](0)));
}
}

function performUpkeep(bytes calldata performData) external override whenNotPaused onlyKeeper {
if(address(this).balance < 0.01 ether){
if (address(this).balance < 0.01 ether) {
revert("not enough funds in contract");
}
address[] memory toPoke = abi.decode(performData, (address[]));
_pokeList(toPoke);
LastRun = block.timestamp;
}
if (LastRun + MinWaitPeriodSeconds <= block.timestamp) {
address[] memory toPoke = abi.decode(performData, (address[]));
_pokeList(toPoke);
LastRun = block.timestamp;
} else {
revert("not ready");
}
}

/**
* @notice Calls updateRate() on a list of LZ Rate Providers
/**
* @notice Calls updateRate() on a list of LZ Rate Providers
*/
function pokeList(address[] memory rateProviders) external whenNotPaused onlyOwner {

_pokeList(rateProviders);
}

/**
* @notice Calls updateRate() on a list of LZ Rate Providers
/**
* @notice Calls updateRate() on a list of LZ Rate Providers
*/
function _pokeList(address[] memory rateProviders) internal whenNotPaused {
if(address(this).balance < 0.01 ether){
if (address(this).balance < 0.01 ether) {
revert("not enough funds in contract");
}
for (uint i=0; i<rateProviders.length; i++){
for (uint i = 0; i < rateProviders.length; i++) {
try ICrossChainRateProvider(rateProviders[i]).updateRate{value: 0.01 ether}(){
// updateRate() fires an event on success
}
Expand All @@ -113,34 +116,35 @@ contract LZRateProviderPoker is ConfirmedOwner, Pausable, KeeperCompatibleInterf
}
}

function addRateProvider (address rateProvider) public onlyOwner {
if(LZRateProviders.add(rateProvider)) {
function addRateProvider(address rateProvider) public onlyOwner {
if (LZRateProviders.add(rateProvider)) {
emit rateProviderAdded(rateProvider);
} else {
emit rateProviderAlreadyExists(rateProvider);
}
}

function addRateProviders (address[] memory rateProviders) public onlyOwner {
for(uint i=0; i<rateProviders.length; i++){
if(LZRateProviders.add(rateProviders[i])) {
emit rateProviderAdded(rateProviders[i]);
} else {
emit rateProviderAlreadyExists(rateProviders[i]);
}
function addRateProviders(address[] memory rateProviders) public onlyOwner {
for (uint i = 0; i < rateProviders.length; i++) {
if (LZRateProviders.add(rateProviders[i])) {
emit rateProviderAdded(rateProviders[i]);
} else {
emit rateProviderAlreadyExists(rateProviders[i]);
}
}
}

function removeRateProvider(address rateProvider) public onlyOwner {
if(LZRateProviders.remove(rateProvider)){
if (LZRateProviders.remove(rateProvider)) {
emit rateProviderRemove(rateProvider);
} else {
emit removeNonexistentRateProvider(rateProvider);
}
}

function removeRateProviders(address[] memory rateProviders) public onlyOwner {
for(uint i=0; i<rateProviders.length; i++){
if(LZRateProviders.remove(rateProviders[i])){
for (uint i = 0; i < rateProviders.length; i++) {
if (LZRateProviders.remove(rateProviders[i])) {
emit rateProviderRemove(rateProviders[i]);
} else {
emit removeNonexistentRateProvider(rateProviders[i]);
Expand Down Expand Up @@ -188,12 +192,12 @@ contract LZRateProviderPoker is ConfirmedOwner, Pausable, KeeperCompatibleInterf
emit minWaitPeriodUpdated(MinWaitPeriodSeconds);
MinWaitPeriodSeconds = minWaitSeconds;
}
/**
* @notice Receive funds
/**
* @notice Receive funds
*/
receive() external payable {
emit FundsAdded(msg.value, address(this).balance, msg.sender);
}
receive() external payable {
emit FundsAdded(msg.value, address(this).balance, msg.sender);
}

/**
* @notice Pauses the contract, which prevents executing performUpkeep
Expand All @@ -213,8 +217,8 @@ contract LZRateProviderPoker is ConfirmedOwner, Pausable, KeeperCompatibleInterf
if (msg.sender != KeeperAddress && msg.sender != owner()) {
revert OnlyKeeperRegistry(msg.sender);
}
_;
}
_;
}

}

Expand Down
8 changes: 8 additions & 0 deletions tests/test_RateProviderPoker.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,14 @@
import pytest


def test_upkeepTwice(admin, poker, upkeep_caller, rate_providers):
poker.addRateProviders(rate_providers, {"from": admin})
admin.transfer(poker, 1 * 10 ** 18)
(upkeepNeeded, performData) = poker.checkUpkeep(b"")
assert upkeepNeeded, "Poker doesn't want to poke when it's got eth, never run, and has things to poke"
tx = poker.performUpkeep(performData, {"from": upkeep_caller})
with brownie.reverts("not ready"):
tx2 = poker.performUpkeep(performData, {"from": upkeep_caller})

def test_pokeMany(admin, poker, upkeep_caller, rate_providers):
poker.addRateProviders(rate_providers, {"from": admin})
Expand Down