Last active
August 27, 2020 23:45
-
-
Save storming0x/b3f5142312f5c3691032934bb447c2e5 to your computer and use it in GitHub Desktop.
VaultBatchEarnV2.sol
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// SPDX-License-Identifier: MIT | |
pragma solidity ^0.6.2; | |
pragma experimental ABIEncoderV2; | |
interface VaultLike { | |
function available() external view returns (uint); | |
function earn() external; | |
} | |
contract Context { | |
constructor () internal { } | |
// solhint-disable-previous-line no-empty-blocks | |
function _msgSender() internal view returns (address payable) { | |
return msg.sender; | |
} | |
function _msgData() internal view returns (bytes memory) { | |
this; // silence state mutability warning without generating bytecode - see https://github.com/ethereum/solidity/issues/2691 | |
return msg.data; | |
} | |
} | |
contract Ownable is Context { | |
address private _owner; | |
event OwnershipTransferred(address indexed previousOwner, address indexed newOwner); | |
constructor () internal { | |
_owner = _msgSender(); | |
emit OwnershipTransferred(address(0), _owner); | |
} | |
function owner() public view returns (address) { | |
return _owner; | |
} | |
modifier onlyOwner() { | |
require(isOwner(), "Ownable: caller is not the owner"); | |
_; | |
} | |
function isOwner() public view returns (bool) { | |
return _msgSender() == _owner; | |
} | |
function renounceOwnership() public onlyOwner { | |
emit OwnershipTransferred(_owner, address(0)); | |
_owner = address(0); | |
} | |
function transferOwnership(address newOwner) public onlyOwner { | |
_transferOwnership(newOwner); | |
} | |
function _transferOwnership(address newOwner) internal { | |
require(newOwner != address(0), "Ownable: new owner is the zero address"); | |
emit OwnershipTransferred(_owner, newOwner); | |
_owner = newOwner; | |
} | |
} | |
contract VaultBatchEarn is Ownable { | |
struct Vault { | |
VaultLike vault; | |
uint256 limit; | |
bool exists; | |
} | |
mapping (uint => Vault) public vaults; | |
mapping (address => uint) public indexes; | |
uint256 public numOfVaults; | |
constructor() public {} | |
function addVaults(Vault[] memory _vaults) external onlyOwner { | |
for (uint256 i; i < _vaults.length; i++) { | |
addVault(_vaults[i].vault, _vaults[i].limit); | |
} | |
} | |
function addVault(VaultLike vaultAddress, uint256 lim) public onlyOwner { | |
require(lim > 0); | |
// check if address of vault is not set already | |
require(indexes[address(vaultAddress)] != 0); | |
vaults[numOfVaults] = Vault(vaultAddress, lim, true); | |
indexes[address(vaultAddress)] = numOfVaults; | |
numOfVaults++; | |
} | |
function updateLimit(VaultLike vaultAddress, uint256 lim) public onlyOwner { | |
uint index = indexes[address(vaultAddress)]; | |
require(vaults[index].limit > 0 && lim > 0 && vaults[index].vault == vaultAddress); | |
vaults[index].limit = lim; | |
} | |
function earnAll() public { | |
for (uint256 i; i < numOfVaults; i++) { | |
Vault memory v = vaults[i]; | |
if (v.vault.available() > v.limit) { | |
v.vault.earn(); | |
} | |
} | |
} | |
function shouldCallEarn() public view returns (bool) { | |
for (uint256 i; i < numOfVaults; i++) { | |
Vault memory v = vaults[i]; | |
if (v.vault.available() > v.limit) { | |
return true; | |
} | |
} | |
return false; | |
} | |
function getVaults() public view returns (uint256[] memory) { | |
uint256[] memory readyToEarnVaults = new uint256[](0); | |
uint256 j = 0; | |
for (uint256 i; i < numOfVaults; i++) { | |
Vault memory v = vaults[i]; | |
if (v.vault.available() > v.limit) { | |
readyToEarnVaults[j] = i; | |
j++; | |
} | |
} | |
return readyToEarnVaults; | |
} | |
function earn(uint256[] memory vaultsIndex) public { | |
for (uint256 i; i < vaultsIndex.length; i++) { | |
Vault memory v = vaults[vaultsIndex[i]]; | |
if (v.exists && v.vault.available() > v.limit) { | |
v.vault.earn(); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment