# @version ^0.3.9

from vyper.interfaces import ERC20

interface IWeth:
    def deposit(): nonpayable
    def withdraw(amount: uint256): nonpayable

weth: immutable(address)
blur: immutable(ERC20)
treasury: immutable(address)
UNISWAP_ROUTER: immutable(address)

manager: address

@external
def __init__(_weth: address, _blur: address, _treasury: address, _uniswap_router: address):
    weth = _weth
    blur = ERC20(_blur)
    treasury = _treasury
    UNISWAP_ROUTER = _uniswap_router
    self.manager = msg.sender

@payable
@external
def __default__():
    pass

event ForwardedToTreasury:
    amount: uint256

@internal
def _forwardToTreasury():
    balance: uint256 = ERC20(weth).balanceOf(self)
    log ForwardedToTreasury(balance)
    IWeth(weth).withdraw(balance)
    send(treasury, self.balance)

@external
def forwardToTreasury():
    self._forwardToTreasury()

@external
def withdraw(token: address):
    assert msg.sender == self.manager
    ERC20(token).transfer(msg.sender, ERC20(token).balanceOf(self))


struct ExactInputSingleParams:
    tokenIn: address
    tokenOut: address
    fee: uint24
    recipient: address
    deadline: uint256
    amountIn: uint256
    amountOutMinimum: uint256
    sqrtPriceLimitX96: uint160

interface IUniswapV3Router:
    def exactInputSingle(params: ExactInputSingleParams) -> uint256: payable

@external
def forward_blur(amount_out_min: uint256):
    # convert this contract's blur to weth

    blur.approve(UNISWAP_ROUTER, blur.balanceOf(self))
    params: ExactInputSingleParams = ExactInputSingleParams({
        tokenIn: blur.address,
        tokenOut: weth,
        fee: 3000,
        recipient: self,
        deadline: block.timestamp,
        amountIn: blur.balanceOf(self),
        amountOutMinimum: amount_out_min,
        sqrtPriceLimitX96: 0
    })

    IUniswapV3Router(UNISWAP_ROUTER).exactInputSingle(params)

    # forward weth to treasury
    self._forwardToTreasury()