Last active
May 7, 2024 16:23
-
-
Save 0xdef1/39ff33ddf5dd0d6249243024c9eb3dd7 to your computer and use it in GitHub Desktop.
wFRIEND POC
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
pragma solidity >=0.8.20; | |
import {IPoints} from "./IPoints.sol"; | |
import {IRabbitRouter} from "./IRabbitRouter.sol"; | |
import {IBunnySwap} from "./IBunnySwap.sol"; | |
import {ERC20} from "openzeppelin-contracts/contracts/token/ERC20/ERC20.sol"; | |
contract WFRIEND is ERC20 { | |
address points; | |
address router; | |
address pair; | |
constructor( | |
address _points, | |
address _router, | |
address _pair | |
) ERC20("Wrapped FRIEND", "wFRIEND") { | |
points = _points; | |
router = _router; | |
pair = _pair; | |
} | |
// Deposit LP token, receive back ETH + wFRIEND | |
function deposit( | |
uint256 liquidity, | |
uint256 minToken, | |
uint256 minEth | |
) public { | |
// Transfer the LP here | |
ERC20(pair).transferFrom(msg.sender, address(this), liquidity); | |
// Strike the LP with proceeds to this contract | |
// Striking LP burns 1 wei ETH and equivalent FRIEND | |
ERC20(pair).approve(router, liquidity); | |
(uint256 amountToken, uint256 amountETH) = IRabbitRouter(router) | |
.removeLiquidityETH( | |
liquidity, | |
minToken, | |
minEth, | |
address(this), | |
block.timestamp | |
); | |
// Return the ETH portion of the LP to msg.sender | |
payable(msg.sender).transfer(amountETH); | |
// Mint wFRIEND | |
_mint(msg.sender, amountToken); | |
} | |
// Send ETH + wFRIEND, receive back ETH + FRIEND | |
function withdraw(uint256 amount) public payable { | |
// Add ETH from msg.sender and FRIEND from this contract to LP | |
IPoints(points).approve(router, amount); | |
( | |
uint256 amountTokenAdded, | |
uint256 amountETHAdded, | |
uint256 liquidity | |
) = IRabbitRouter(router).addLiquidityETH{value: msg.value}( | |
amount, | |
0, | |
0, | |
address(this), | |
block.timestamp | |
); | |
// Return excess ETH to sender | |
if (msg.value > amountETHAdded) { | |
payable(msg.sender).transfer(msg.value - amountETHAdded); | |
} | |
// Strike the LP with proceeds to msg.sender | |
// Striking LP burns 1 wei ETH and equivalent FRIEND | |
ERC20(pair).approve(address(router), liquidity); | |
IRabbitRouter(router).removeLiquidityETH( | |
liquidity, | |
0, | |
0, | |
msg.sender, | |
block.timestamp | |
); | |
// Burn wFRIEND | |
_burn(msg.sender, amountTokenAdded); | |
} | |
// Receive ETH from striking LPs | |
receive() external payable {} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment