Created
December 15, 2020 07:37
-
-
Save benjioh5/897ab04823c65ac06d7ca554dbc484db to your computer and use it in GitHub Desktop.
Created using remix-ide: Realtime Ethereum Contract Compiler and Runtime. Load this file by pasting this gists URL or ID at https://remix.ethereum.org/#version=soljson-v0.6.12+commit.27d51765.js&optimize=false&runs=200&gist=
This file contains hidden or 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
pragma solidity >=0.4.22 <0.7.0; | |
/** | |
* @title Storage | |
* @dev Store & retrieve value in a variable | |
*/ | |
contract Storage { | |
uint256 number; | |
/** | |
* @dev Store value in variable | |
* @param num value to store | |
*/ | |
function store(uint256 num) public { | |
number = num; | |
} | |
/** | |
* @dev Return value | |
* @return value of 'number' | |
*/ | |
function retrieve() public view returns (uint256){ | |
return number; | |
} | |
} |
This file contains hidden or 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
pragma solidity >=0.4.22 <0.7.0; | |
/** | |
* @title Owner | |
* @dev Set & change owner | |
*/ | |
contract Owner { | |
address private owner; | |
// event for EVM logging | |
event OwnerSet(address indexed oldOwner, address indexed newOwner); | |
// modifier to check if caller is owner | |
modifier isOwner() { | |
// If the first argument of 'require' evaluates to 'false', execution terminates and all | |
// changes to the state and to Ether balances are reverted. | |
// This used to consume all gas in old EVM versions, but not anymore. | |
// It is often a good idea to use 'require' to check if functions are called correctly. | |
// As a second argument, you can also provide an explanation about what went wrong. | |
require(msg.sender == owner, "Caller is not owner"); | |
_; | |
} | |
/** | |
* @dev Set contract deployer as owner | |
*/ | |
constructor() public { | |
owner = msg.sender; // 'msg.sender' is sender of current call, contract deployer for a constructor | |
emit OwnerSet(address(0), owner); | |
} | |
/** | |
* @dev Change owner | |
* @param newOwner address of new owner | |
*/ | |
function changeOwner(address newOwner) public isOwner { | |
emit OwnerSet(owner, newOwner); | |
owner = newOwner; | |
} | |
/** | |
* @dev Return owner address | |
* @return address of owner | |
*/ | |
function getOwner() external view returns (address) { | |
return owner; | |
} | |
} |
This file contains hidden or 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
pragma solidity >=0.4.22 <0.7.0; | |
/** | |
* @title Ballot | |
* @dev Implements voting process along with vote delegation | |
*/ | |
contract Ballot { | |
struct Voter { | |
uint weight; // weight is accumulated by delegation | |
bool voted; // if true, that person already voted | |
address delegate; // person delegated to | |
uint vote; // index of the voted proposal | |
} | |
struct Proposal { | |
// If you can limit the length to a certain number of bytes, | |
// always use one of bytes1 to bytes32 because they are much cheaper | |
bytes32 name; // short name (up to 32 bytes) | |
uint voteCount; // number of accumulated votes | |
} | |
address public chairperson; | |
mapping(address => Voter) public voters; | |
Proposal[] public proposals; | |
/** | |
* @dev Create a new ballot to choose one of 'proposalNames'. | |
* @param proposalNames names of proposals | |
*/ | |
constructor(bytes32[] memory proposalNames) public { | |
chairperson = msg.sender; | |
voters[chairperson].weight = 1; | |
for (uint i = 0; i < proposalNames.length; i++) { | |
// 'Proposal({...})' creates a temporary | |
// Proposal object and 'proposals.push(...)' | |
// appends it to the end of 'proposals'. | |
proposals.push(Proposal({ | |
name: proposalNames[i], | |
voteCount: 0 | |
})); | |
} | |
} | |
/** | |
* @dev Give 'voter' the right to vote on this ballot. May only be called by 'chairperson'. | |
* @param voter address of voter | |
*/ | |
function giveRightToVote(address voter) public { | |
require( | |
msg.sender == chairperson, | |
"Only chairperson can give right to vote." | |
); | |
require( | |
!voters[voter].voted, | |
"The voter already voted." | |
); | |
require(voters[voter].weight == 0); | |
voters[voter].weight = 1; | |
} | |
/** | |
* @dev Delegate your vote to the voter 'to'. | |
* @param to address to which vote is delegated | |
*/ | |
function delegate(address to) public { | |
Voter storage sender = voters[msg.sender]; | |
require(!sender.voted, "You already voted."); | |
require(to != msg.sender, "Self-delegation is disallowed."); | |
while (voters[to].delegate != address(0)) { | |
to = voters[to].delegate; | |
// We found a loop in the delegation, not allowed. | |
require(to != msg.sender, "Found loop in delegation."); | |
} | |
sender.voted = true; | |
sender.delegate = to; | |
Voter storage delegate_ = voters[to]; | |
if (delegate_.voted) { | |
// If the delegate already voted, | |
// directly add to the number of votes | |
proposals[delegate_.vote].voteCount += sender.weight; | |
} else { | |
// If the delegate did not vote yet, | |
// add to her weight. | |
delegate_.weight += sender.weight; | |
} | |
} | |
/** | |
* @dev Give your vote (including votes delegated to you) to proposal 'proposals[proposal].name'. | |
* @param proposal index of proposal in the proposals array | |
*/ | |
function vote(uint proposal) public { | |
Voter storage sender = voters[msg.sender]; | |
require(sender.weight != 0, "Has no right to vote"); | |
require(!sender.voted, "Already voted."); | |
sender.voted = true; | |
sender.vote = proposal; | |
// If 'proposal' is out of the range of the array, | |
// this will throw automatically and revert all | |
// changes. | |
proposals[proposal].voteCount += sender.weight; | |
} | |
/** | |
* @dev Computes the winning proposal taking all previous votes into account. | |
* @return winningProposal_ index of winning proposal in the proposals array | |
*/ | |
function winningProposal() public view | |
returns (uint winningProposal_) | |
{ | |
uint winningVoteCount = 0; | |
for (uint p = 0; p < proposals.length; p++) { | |
if (proposals[p].voteCount > winningVoteCount) { | |
winningVoteCount = proposals[p].voteCount; | |
winningProposal_ = p; | |
} | |
} | |
} | |
/** | |
* @dev Calls winningProposal() function to get the index of the winner contained in the proposals array and then | |
* @return winnerName_ the name of the winner | |
*/ | |
function winnerName() public view | |
returns (bytes32 winnerName_) | |
{ | |
winnerName_ = proposals[winningProposal()].name; | |
} | |
} |
This file contains hidden or 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.4.21 <0.7.0; | |
/// @dev This interfaces defines the functions of the KeeperDAO liquidity pool | |
/// that our contract needs to know about. The only function we need is the | |
/// borrow function, which allows us to take flash loans from the liquidity | |
/// pool. | |
interface LiquidityPool { | |
/// @dev Borrow ETH/ERC20s from the liquidity pool. This function will (1) | |
/// send an amount of tokens to the `msg.sender`, (2) call | |
/// `msg.sender.call(_data)` from the KeeperDAO borrow proxy, and then (3) | |
/// check that the balance of the liquidity pool is greater than it was | |
/// before the borrow. | |
/// | |
/// @param _token The address of the ERC20 to be borrowed. ETH can be | |
/// borrowed by specifying "0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE". | |
/// @param _amount The amount of the ERC20 (or ETH) to be borrowed. At least | |
/// more than this amount must be returned to the liquidity pool before the | |
/// end of the transaction, otherwise the transaction will revert. | |
/// @param _data The calldata that encodes the callback to be called on the | |
/// `msg.sender`. This is the mechanism through which the borrower is able | |
/// to implement their custom keeper logic. The callback will be called from | |
/// the KeeperDAO borrow proxy. | |
function borrow( | |
address _token, | |
uint256 _amount, | |
bytes calldata _data | |
) external; | |
} | |
/** | |
* @dev Interface of the ERC20 standard as defined in the EIP. | |
*/ | |
interface IERC20 { | |
/** | |
* @dev Moves `amount` tokens from the caller's account to `recipient`. | |
* | |
* Returns a boolean value indicating whether the operation succeeded. | |
* | |
* Emits a {Transfer} event. | |
*/ | |
function transfer(address recipient, uint256 amount) external returns (bool); | |
} | |
/// @dev This contract implements a simple keeper. It borrows ETH from the | |
/// KeeperDAO liquidity pool, and immediately returns all of the borrowed ETH, | |
/// plus some amount of "profit" from its own balance. Instead of returning | |
/// profits from their own balances, keeper contracts will usually engage in | |
/// arbitrage or liquidations to earn profits that can be returned. | |
contract HelloWorld { | |
/// @dev Owner of the contract. | |
address payable public owner; | |
/// @dev Address of the KeeperDAO borrow proxy. This will be the | |
/// `msg.sender` for calls to the `helloCallback` function. | |
address public borrowProxy; | |
/// @dev Address of the KeeperDAO liquidity pool. This is will be the | |
/// address to which the `helloCallback` function must return all bororwed | |
/// assets (and all excess profits). | |
address payable public liquidityPool; | |
/// @dev This modifier restricts the caller of a function to the owner of | |
/// this contract. | |
modifier onlyOwner { | |
if (msg.sender == owner) { | |
_; | |
} | |
} | |
/// @dev This modifier restricts the caller of a function to the KeeperDAO | |
/// borrow proxy. | |
modifier onlyBorrowProxy { | |
if (msg.sender == borrowProxy) { | |
_; | |
} | |
} | |
constructor() public { | |
owner = msg.sender; | |
} | |
fallback() external payable { | |
// Do nothing. | |
} | |
/// @dev Set the owner of this contract. This function can only be called by | |
/// the current owner. | |
/// | |
/// @param _newOwner The new owner of this contract. | |
function setOwner(address payable _newOwner) external onlyOwner { | |
owner = _newOwner; | |
} | |
/// @dev Set the borrow proxy expected by this contract. This function can | |
/// only be called by the current owner. | |
/// | |
/// @param _newBorrowProxy The new borrow proxy expected by this contract. | |
function setBorrowProxy(address _newBorrowProxy) external onlyOwner { | |
borrowProxy = _newBorrowProxy; | |
} | |
/// @dev Set the liquidity pool used by this contract. This function can | |
/// only be called by the current owner. | |
/// | |
/// @param _newLiquidityPool The new liquidity pool used by this contract. | |
/// It must be a payable address, because this contract needs to be able to | |
/// return borrowed assets and profits to the liquidty pool. | |
function setLiquidityPool(address payable _newLiquidityPool) | |
external | |
onlyOwner | |
{ | |
liquidityPool = _newLiquidityPool; | |
} | |
/// @dev This function is the entry point of this keeper. An off-chain bot | |
/// will call this function whenever it decides that it wants to borrow from | |
/// this KeeperDAO liquidity pool. This function is similar to what you | |
/// would expect in a "real" keeper implementation: it accepts paramters | |
/// telling it what / how much to borrow, and which callback on this | |
/// contract should be called once the borrowed funds have been transferred. | |
function hello(address target, uint256 _amountToBorrow, uint256 _amountOfProfitToReturn) | |
external | |
onlyOwner | |
{ | |
require(_amountOfProfitToReturn > 0, "profit is zero"); | |
require( | |
address(this).balance > _amountOfProfitToReturn, | |
"balance is too low" | |
); | |
// The liquidity pool is guarded from re-entrance, so we can only call | |
// this function once per transaction. | |
LiquidityPool(liquidityPool).borrow( | |
// Address of the token we want to borrow. Using this address | |
// means that we want to borrow ETH. | |
target, | |
// The amount of WEI that we will borrow. We have to return at least | |
// more than this amount. | |
_amountToBorrow, | |
// Encode the callback into calldata. This will be used to call a | |
// function on this contract. | |
abi.encodeWithSelector( | |
// Function selector of the callback function. | |
this.helloCallback.selector, | |
// First parameter of the callback. | |
_amountToBorrow, | |
// Second parameter of the callback. | |
_amountOfProfitToReturn | |
// Third paramter, fourth parameter, and so on (our callback | |
// only has two paramters). | |
) | |
); | |
} | |
/// @dev This is the callback function that implements our custom keeper | |
/// logic. We do not need to call this function directly; it will be called | |
/// by the KeeperDAO borrow proxy when we call borrow on the KeeperDAO | |
/// liquidity pool. In fact, usually, this function should be restricted so | |
/// that is can only be called by the KeeperDAO borrow proxy. | |
/// | |
/// Just before this callback is called by the KeeperDAO borrow proxy, all | |
/// of the assets that we want to borrow will be transferred to this | |
/// contract. In this callback, we can do whatever we want with these | |
/// assets; we can arbitrage between DEXs, liquidity positions on Compound, | |
/// and so on. The only requirement is that at least more than the borrowed | |
/// assets is returned. | |
/// | |
/// For example, imagine that we wanted borrowed 1 ETH. Before this callback | |
/// is called, the KeeperDAO liquidity pool will have transferred 1 ETH to | |
/// this contract. This callback can then do whatever it wants with that ETH. | |
/// However, before the callback returns, it must return at least more than | |
/// 1 ETH to the KeeperDAO liquidity pool (even if it is only returning | |
/// 1 ETH + 1 WEI). | |
/// | |
/// In our example, we will not implement a complicated keeper strategy. We | |
/// will simply return all of the borrowed ETH, plus a non-zero amount of | |
/// profit. The amount of profit is explicitly specified by the owner of | |
/// this contract when they initiate the borrow. Of course, this strategy | |
/// does not generate profit by interacting with other protocols (like most | |
/// keepers do). Instead, it just uses its own balance to return profits to | |
/// KeeperDAO. | |
function helloCallback( | |
uint256 _amountBorrowed, | |
uint256 _amountOfProfitToReturn | |
) external onlyBorrowProxy { | |
assert( | |
address(this).balance >= _amountOfProfitToReturn + _amountBorrowed | |
); | |
assert(_amountOfProfitToReturn > 0); | |
// Notice that assets are transferred back to the liquidity pool, not to | |
// the borrow proxy. | |
uint256 _totalAmount = _amountBorrowed + _amountOfProfitToReturn; | |
liquidityPool.call{value:_totalAmount}(""); | |
} | |
function withdrawToken(address _token, uint256 _amount) public onlyOwner { | |
if(_token == 0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE) { | |
owner.transfer(_amount); | |
} else { | |
IERC20(_token).transfer(owner, _amount); | |
} | |
} | |
} |
This file contains hidden or 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
// this line is added to create a gist. Empty file is not allowed. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment