Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add DropAndBootstrapStakeRouter #61

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
89 changes: 89 additions & 0 deletions src/DropAndBootstrapStakeFactory.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.17;

import { ERC20 } from "solmate/tokens/ERC20.sol";
import { SafeTransferLib } from "solmate/utils/SafeTransferLib.sol";
import { FeeRegistry } from "./FeeRegistry.sol";
import { DropAndBootstrapStakeRouter, DropAndBootstrapStakeInstructions } from "./DropAndBootstrapStakeRouter.sol";

contract DropAndBootstrapStakeFactory {
using SafeTransferLib for ERC20;

function deployDropAndBootstrapStakeRouter(
FeeRegistry fees,
DropAndBootstrapStakeInstructions memory inst,
uint256 dropAmount
) public returns (DropAndBootstrapStakeRouter) {
require(
inst.recipient != address(0) && inst.feeReceiver != address(0) && address(inst.dropToken) != address(0)
&& inst.euphrates != address(0) && address(inst.otherContributionToken) != address(0),
"invalid inst"
);

// no need to use salt as we want to keep the router address the same for the same fees &instructions
bytes32 salt;

DropAndBootstrapStakeRouter router;
try new DropAndBootstrapStakeRouter{ salt: salt }(fees, inst) returns (DropAndBootstrapStakeRouter router_) {
router = router_;
} catch {
router = DropAndBootstrapStakeRouter(
address(
uint160(
uint256(
keccak256(
abi.encodePacked(
bytes1(0xff),
address(this),
salt,
keccak256(
abi.encodePacked(
type(DropAndBootstrapStakeRouter).creationCode, abi.encode(fees, inst)
)
)
)
)
)
)
)
);
}

if (dropAmount > 0) {
inst.dropToken.safeTransferFrom(msg.sender, address(router), dropAmount);
}

return router;
}

function deployDropAndBootstrapStakeRouterAndRoute(
FeeRegistry fees,
DropAndBootstrapStakeInstructions memory inst,
ERC20 token,
uint256 dropAmount
) public {
DropAndBootstrapStakeRouter router = deployDropAndBootstrapStakeRouter(fees, inst, dropAmount);
router.route(token, msg.sender);
}

function deployDropAndBootstrapStakeRouterAndRouteNoFee(
FeeRegistry fees,
DropAndBootstrapStakeInstructions memory inst,
ERC20 token,
uint256 dropAmount
) public {
DropAndBootstrapStakeRouter router = deployDropAndBootstrapStakeRouter(fees, inst, dropAmount);
router.routeNoFee(token);
}

function deployDropAndBootstrapStakeRouterAndRescue(
FeeRegistry fees,
DropAndBootstrapStakeInstructions memory inst,
ERC20 token,
uint256 dropAmount,
bool isGasDrop
) public {
DropAndBootstrapStakeRouter router = deployDropAndBootstrapStakeRouter(fees, inst, dropAmount);
router.rescue(token, isGasDrop);
}
}
189 changes: 189 additions & 0 deletions src/DropAndBootstrapStakeRouter.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,189 @@
// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.17;

import { ERC20 } from "solmate/tokens/ERC20.sol";
import { SafeTransferLib } from "solmate/utils/SafeTransferLib.sol";
import { Math } from "@openzeppelin/contracts/utils/math/Math.sol";
import { IDEX } from "@acala-network/contracts/dex/IDEX.sol";
import { IBootstrap } from "@acala-network/contracts/dex/IBootstrap.sol";
import { IStakingTo } from "euphrates/IStaking.sol";

import { BaseRouter } from "./BaseRouter.sol";
import { FeeRegistry } from "./FeeRegistry.sol";

struct DropAndBootstrapStakeInstructions {
address recipient;
ERC20 dropToken;
uint256 dropFee;
address feeReceiver;
address dex;
ERC20 otherContributionToken;
address euphrates;
uint256 poolId;
}

contract DropAndBootstrapStakeRouter is BaseRouter {
using SafeTransferLib for ERC20;

DropAndBootstrapStakeInstructions private _instructions;

constructor(FeeRegistry fees, DropAndBootstrapStakeInstructions memory instructions) BaseRouter(fees) {
_instructions = instructions;
}

function routeImpl(ERC20 token) internal override {
if (token.balanceOf(address(this)) < _instructions.dropFee) {
revert("DropAndBootstrapStakeRouter: cannot afford drop fee");
}
token.safeTransfer(_instructions.feeReceiver, _instructions.dropFee);

// ensure bootstrap is in process and not ended
address lpToken = IDEX(_instructions.dex).getLiquidityTokenAddress(
address(token), address(_instructions.otherContributionToken)
);
require(lpToken != address(0), "DropAndBootstrapStakeRouter: bootstrap not exist");
(uint256 rateA, uint256 rateB) = IBootstrap(_instructions.dex).getInitialShareExchangeRate(
address(token), address(_instructions.otherContributionToken)
);
require(rateA == 0 && rateB == 0, "DropAndBootstrapStakeRouter: bootstrap is ended");

// ensure bootstrap lp token is matched the share token of Euphrates pool
require(
lpToken == address(IStakingTo(_instructions.euphrates).shareTypes(_instructions.poolId)),
"DropAndBootstrapStakeRouter: Euphrates pool share token is not matched the lp token"
);

uint256 contributionTokenAmount = token.balanceOf(address(this));
uint256 otherContributionTokenAmount = _instructions.otherContributionToken.balanceOf(address(this));

if (contributionTokenAmount != 0 || otherContributionTokenAmount != 0) {
token.safeApprove(_instructions.dex, contributionTokenAmount);
_instructions.otherContributionToken.safeApprove(_instructions.dex, otherContributionTokenAmount);

bool result = IBootstrap(_instructions.dex).addProvision(
address(token),
address(_instructions.otherContributionToken),
contributionTokenAmount,
otherContributionTokenAmount
);
require(result, "DropAndBootstrapStakeRouter: addProvision failed");
}

// transfer all dropToken to recipient
_instructions.dropToken.safeTransfer(_instructions.recipient, _instructions.dropToken.balanceOf(address(this)));
}

function claimShareAndStakeTo(ERC20 token) public {
// ensure bootstrap has been existed and ended
address lpToken = IDEX(_instructions.dex).getLiquidityTokenAddress(
address(token), address(_instructions.otherContributionToken)
);
require(lpToken != address(0), "DropAndBootstrapStakeRouter: bootstrap not exist");
(uint256 rateA, uint256 rateB) = IBootstrap(_instructions.dex).getInitialShareExchangeRate(
address(token), address(_instructions.otherContributionToken)
);
require(rateA != 0 && rateB != 0, "DropAndBootstrapStakeRouter: bootstrap must be ended");

// ensure bootstrap lp token is matched the share token of Euphrates pool
require(
lpToken == address(IStakingTo(_instructions.euphrates).shareTypes(_instructions.poolId)),
"DropAndBootstrapStakeRouter: Euphrates pool share token is not matched the lp token"
);

(uint256 contributionA, uint256 contributionB) = IBootstrap(_instructions.dex).getProvisionPoolOf(
address(this), address(token), address(_instructions.otherContributionToken)
);

// there's provision still, need to claim dex share
if (contributionA != 0 || contributionB != 0) {
bool claimResult = IBootstrap(_instructions.dex).claimDexShare(
address(this), address(token), address(_instructions.otherContributionToken)
);
require(claimResult, "DropAndBootstrapStakeRouter: claimDexShare failed");
}

uint256 stakeAmount = ERC20(lpToken).balanceOf(address(this));

// there's lp token, stake to Euphrates
if (stakeAmount > 0) {
ERC20(lpToken).safeApprove(_instructions.euphrates, stakeAmount);
bool stakeResult =
IStakingTo(_instructions.euphrates).stakeTo(_instructions.poolId, stakeAmount, _instructions.recipient);
require(stakeResult, "DropAndBootstrapStakeRouter: stakeTo failed");
}
}

function refundProvision(ERC20 token) public {
// ensure bootstrap is aborted
address lpToken = IDEX(_instructions.dex).getLiquidityTokenAddress(
address(token), address(_instructions.otherContributionToken)
);
require(lpToken == address(0), "DropAndBootstrapStakeRouter: bootstrap must be aborted");
(uint256 rateA, uint256 rateB) = IBootstrap(_instructions.dex).getInitialShareExchangeRate(
address(token), address(_instructions.otherContributionToken)
);
require(rateA == 0 && rateB == 0, "DropAndBootstrapStakeRouter: bootstrap must be ended");

// ensure exist provision
(uint256 contributionA, uint256 contributionB) = IBootstrap(_instructions.dex).getProvisionPoolOf(
address(this), address(token), address(_instructions.otherContributionToken)
);

// there's provision still, need to refund provision
if (contributionA != 0 || contributionB != 0) {
bool result = IBootstrap(_instructions.dex).refundProvision(
address(this), address(token), address(_instructions.otherContributionToken)
);
require(result, "DropAndBootstrapStakeRouter: refund provision failed");
}

// refund provision to recipient
token.safeTransfer(_instructions.recipient, token.balanceOf(address(this)));
_instructions.otherContributionToken.safeTransfer(
_instructions.recipient, _instructions.otherContributionToken.balanceOf(address(this))
);
}

function rescue(ERC20 token, bool isGasDrop) public {
(uint256 contributionA, uint256 contributionB) = IBootstrap(_instructions.dex).getProvisionPoolOf(
address(this), address(token), address(_instructions.otherContributionToken)
);
require(
contributionA == 0 && contributionB == 0,
"DropAndBootstrapStakeRouter: exist provision, must claim share or refund provision"
);

if (isGasDrop) {
// require transfer full dropFee to feeReceiver
if (token.balanceOf(address(this)) < _instructions.dropFee) {
revert("DropAndBootstrapStakeRouter: cannot afford drop fee");
}
token.safeTransfer(_instructions.feeReceiver, _instructions.dropFee);
// transfer all dropToken to recipient
_instructions.dropToken.safeTransfer(
_instructions.recipient, _instructions.dropToken.balanceOf(address(this))
);
} else {
// transfer all dropToken back to feeReceiver (no gas drop)
_instructions.dropToken.safeTransfer(
_instructions.feeReceiver, _instructions.dropToken.balanceOf(address(this))
);
}

// transfer all remainning token to recipient to avoid it stuck in this contract
token.safeTransfer(_instructions.recipient, token.balanceOf(address(this)));
_instructions.otherContributionToken.safeTransfer(
_instructions.recipient, _instructions.otherContributionToken.balanceOf(address(this))
);

address lpToken = IDEX(_instructions.dex).getLiquidityTokenAddress(
address(token), address(_instructions.otherContributionToken)
);
if (lpToken != address(0)) {
ERC20(lpToken).safeTransfer(_instructions.recipient, ERC20(lpToken).balanceOf(address(this)));
}

emit RouterDestroyed(address(this));
selfdestruct(payable(_instructions.feeReceiver));
}
}