Skip to content

Instantly share code, notes, and snippets.

@anisoptera
Last active October 22, 2020 23:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anisoptera/25a26947017cfa038c00d5ab2c475389 to your computer and use it in GitHub Desktop.
Save anisoptera/25a26947017cfa038c00d5ab2c475389 to your computer and use it in GitHub Desktop.
Honey DRIP
contract DividendReinvestment is Owned, SimpleWallet {
Unipool public farm = Unipool(0xA6c55971F21cc1C35Ea617f47980D669a0C09cf3);
IUniswapV2Pair pair;
IUniswapV2Router02 router = IUniswapV2Router02(0x1C232F01118CB8B424793ae03F870aa7D0ac7f77);
IERC20 hnyToken;
IERC20 otherToken;
event Harvested(uint _value);
event Converted(uint _valueFrom, uint _valueTo);
event Liquidized(uint _valueA, uint _valueB, uint liquidity);
event Staked(uint liquidity);
constructor() public {
hnyToken = farm.tradedToken();
pair = IUniswapV2Pair(address(farm.uniswapTokenExchange()));
if (pair.token0() != address(hnyToken)) {
if (pair.token1() != address(hnyToken)) {
revert("Can't reinvest, rewards aren't one side of pair");
}
otherToken = IERC20(pair.token0());
} else {
otherToken = IERC20(pair.token1());
}
pair.approve(address(farm), pair.totalSupply());
hnyToken.approve(address(router), hnyToken.totalSupply());
otherToken.approve(address(router), otherToken.totalSupply());
}
function curRewards() public view returns(uint256 rewards) {
return farm.earned(address(this));
}
function curStaked() public view returns(uint256 staked) {
return farm.balanceOf(address(this));
}
function invest(uint256 minTokens) public isOwner {
uint256 liquidity = poolHny(hnyToken.balanceOf(address(this)), minTokens);
farm.stake(liquidity);
emit Staked(liquidity);
}
function reinvest(uint256 minTokens) public isOwner {
uint256 hnyToHarvest = curRewards();
// we'll update this in 2021 when this is a meaningful amount of hny
//require(hnyToHarvest > 100000000);
farm.getReward();
emit Harvested(hnyToHarvest);
uint256 liquidity = poolHny(hnyToHarvest, minTokens);
// stake liquidity
farm.stake(liquidity);
emit Staked(liquidity);
}
function poolHny(uint256 hnyAmt, uint256 minTokens) private returns(uint256 lpTokens) {
// convert half to target token
uint256 hnyToConvert = hnyAmt / 2;
address[] memory path = new address[](2);
path[0] = address(hnyToken);
path[1] = address(otherToken);
uint[] memory amounts = router.swapExactTokensForTokens(hnyToConvert, minTokens, path, address(this), block.timestamp);
emit Converted(amounts[0], amounts[1]);
// add liquidity
(uint a, uint b, uint liquidity) = router.addLiquidity(address(hnyToken),
address(otherToken),
hnyToConvert,
amounts[1],
0, 0, // min
address(this),
block.timestamp);
emit Liquidized(a, b, liquidity);
return liquidity;
}
function stake() public isOwner {
uint256 balance = pair.balanceOf(address(this));
farm.stake(balance);
emit Staked(balance);
}
function unstake() public isOwner {
farm.exit();
}
function transferContentsTo(address payable to) public isOwner {
if (farm.balanceOf(address(this)) > 0) {
farm.exit();
}
hnyToken.transfer(to, hnyToken.balanceOf(address(this)));
otherToken.transfer(to, otherToken.balanceOf(address(this)));
pair.transfer(to, pair.balanceOf(address(this)));
}
}
contract Owned {
address private owner;
// event for EVM logging
event OwnerSet(address indexed oldOwner, address indexed newOwner);
// modifier to check if caller is owner
modifier isOwner() {
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;
}
}
contract SimpleWallet is Owned {
function transfer(IERC20 token, address payable to, uint256 amount) public isOwner {
token.transfer(to, amount);
}
function transferEth(address payable to, uint256 amount) public isOwner {
to.transfer(amount);
}
function deposit() payable external {
}
receive() payable external {
}
fallback() payable external {
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment