Merge pull request #138 from Instadapp/feat/quickswap-fix

updated sushi-incentive connector
This commit is contained in:
Thrilok kumar 2022-01-30 21:12:09 +05:30 committed by GitHub
commit 7823f8299e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 428 additions and 331 deletions

View File

@ -1,31 +1,42 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.7.0;
contract Events {
event LogDeposit(
uint256 indexed pid,
uint256 indexed version,
uint256 amount
);
event LogWithdraw(
uint256 indexed pid,
uint256 indexed version,
uint256 amount
);
event LogEmergencyWithdraw(
uint256 indexed pid,
uint256 indexed version,
uint256 lpAmount,
uint256 rewardsAmount
);
event LogHarvest(
uint256 indexed pid,
uint256 indexed version,
uint256 amount
);
event LogWithdrawAndHarvest(
uint256 indexed pid,
uint256 indexed version,
uint256 widrawAmount,
uint256 harvestAmount
);
event LogDeposit(
address token1,
address token2,
uint256 indexed pid,
uint256 indexed version,
uint256 amount
);
event LogWithdraw(
address token1,
address token2,
uint256 indexed pid,
uint256 indexed version,
uint256 amount
);
event LogEmergencyWithdraw(
address token1,
address token2,
uint256 indexed pid,
uint256 indexed version,
uint256 lpAmount,
uint256 rewardsAmount
);
event LogHarvest(
address token1,
address token2,
uint256 indexed pid,
uint256 indexed version,
uint256 amount
);
event LogWithdrawAndHarvest(
address token1,
address token2,
uint256 indexed pid,
uint256 indexed version,
uint256 widrawAmount,
uint256 harvestAmount
);
}

View File

@ -1,88 +1,106 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.7.0;
pragma experimental ABIEncoderV2;
import {DSMath} from "../../common/math.sol";
import {Basic} from "../../common/basic.sol";
import { DSMath } from "../../common/math.sol";
import { Basic } from "../../common/basic.sol";
import "./interface.sol";
contract Helpers is DSMath, Basic {
IMasterChefV2 immutable masterChefV2 =
IMasterChefV2(0xEF0881eC094552b2e128Cf945EF17a6752B4Ec5d);
IMasterChef immutable masterChef =
IMasterChef(0xc2EdaD668740f1aA35E4D8f227fB8E17dcA888Cd);
ISushiSwapFactory immutable factory =
ISushiSwapFactory(0xC0AEe478e3658e2610c5F7A4A2E1777cE9e4f2Ac);
IMasterChefV2 immutable masterChefV2 =
IMasterChefV2(0xEF0881eC094552b2e128Cf945EF17a6752B4Ec5d);
IMasterChef immutable masterChef =
IMasterChef(0xc2EdaD668740f1aA35E4D8f227fB8E17dcA888Cd);
ISushiSwapFactory immutable factory =
ISushiSwapFactory(0xC0AEe478e3658e2610c5F7A4A2E1777cE9e4f2Ac);
function _deposit(uint256 _pid, uint256 _amount, uint256 _version) internal {
if(_version == 2)
masterChefV2.deposit(_pid, _amount, address(this));
else
masterChef.deposit(_pid, _amount);
}
struct Metadata {
uint256 poolId;
uint256 version;
address lpToken;
}
function _withdraw(uint256 _pid, uint256 _amount, uint256 _version) internal {
if(_version == 2)
masterChefV2.withdraw(_pid, _amount, address(this));
else
masterChef.withdraw(_pid, _amount);
}
function _deposit(Metadata memory data, uint256 _amount) internal {
if (data.version == 2)
masterChefV2.deposit(data.poolId, _amount, address(this));
else masterChef.deposit(data.poolId, _amount);
}
function _harvest(uint256 _pid) internal {
masterChefV2.harvest(_pid, address(this));
}
function _withdraw(Metadata memory data, uint256 _amount) internal {
if (data.version == 2)
masterChefV2.withdraw(data.poolId, _amount, address(this));
else masterChef.withdraw(data.poolId, _amount);
}
function _withdrawAndHarvest(uint256 _pid, uint256 _amount, uint256 _version) internal {
if(_version == 2)
masterChefV2.withdrawAndHarvest(_pid, _amount, address(this));
else _withdraw(_pid, _amount, _version);
}
function _harvest(Metadata memory data) internal {
masterChefV2.harvest(data.poolId, address(this));
}
function _emergencyWithdraw(uint256 _pid, uint256 _version) internal {
if(_version == 2)
masterChefV2.emergencyWithdraw(_pid, address(this));
else
masterChef.emergencyWithdraw(_pid, address(this));
}
function _withdrawAndHarvest(Metadata memory data, uint256 _amount)
internal
{
if (data.version == 2)
masterChefV2.withdrawAndHarvest(
data.poolId,
_amount,
address(this)
);
else _withdraw(data, _amount);
}
function _getPoolId(address tokenA, address tokenB)
internal
view
returns (uint256 poolId, uint256 version, address lpToken)
{
address pair = factory.getPair(tokenA, tokenB);
uint256 length = masterChefV2.poolLength();
version = 2;
poolId = uint256(-1);
function _emergencyWithdraw(Metadata memory data) internal {
if (data.version == 2)
masterChefV2.emergencyWithdraw(data.poolId, address(this));
else masterChef.emergencyWithdraw(data.poolId, address(this));
}
for (uint256 i = 0; i < length; i++) {
lpToken = masterChefV2.lpToken(i);
if (pair == lpToken) {
poolId = i;
break;
}
}
function _getPoolId(address tokenA, address tokenB)
internal
view
returns (Metadata memory data)
{
address pair = factory.getPair(tokenA, tokenB);
uint256 length = masterChefV2.poolLength();
data.version = 2;
data.poolId = uint256(-1);
uint256 lengthV1 = masterChef.poolLength();
for (uint256 i = 0; i < lengthV1; i++) {
(lpToken, , , ) = masterChef.poolInfo(i);
if (pair == lpToken) {
poolId = i;
version = 1;
break;
}
}
}
for (uint256 i = 0; i < length; i++) {
data.lpToken = masterChefV2.lpToken(i);
if (pair == data.lpToken) {
data.poolId = i;
break;
}
}
function _getUserInfo(uint256 _pid, uint256 _version)
internal
view
returns (uint256 lpAmount, uint256 rewardsAmount)
{
if(_version == 2)
(lpAmount, rewardsAmount) = masterChefV2.userInfo(_pid, address(this));
else
(lpAmount, rewardsAmount) = masterChef.userInfo(_pid, address(this));
}
uint256 lengthV1 = masterChef.poolLength();
for (uint256 i = 0; i < lengthV1; i++) {
(data.lpToken, , , ) = masterChef.poolInfo(i);
if (pair == data.lpToken) {
data.poolId = i;
data.version = 1;
break;
}
}
}
function _getUserInfo(Metadata memory data)
internal
view
returns (uint256 lpAmount, uint256 rewardsAmount)
{
if (data.version == 2)
(lpAmount, rewardsAmount) = masterChefV2.userInfo(
data.poolId,
address(this)
);
else
(lpAmount, rewardsAmount) = masterChef.userInfo(
data.poolId,
address(this)
);
}
function _balance(address token1, address token2) internal view returns (uint balance) {
balance = IERC20(token1).balanceOf(address(this)) + IERC20(token2).balanceOf(address(this));
}
}

View File

@ -1,117 +1,127 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.7.0;
pragma experimental ABIEncoderV2;
struct UserInfo {
uint256 amount;
uint256 rewardDebt;
uint256 amount;
uint256 rewardDebt;
}
struct PoolInfo {
IERC20 lpToken; // Address of LP token contract.
uint256 allocPoint; // How many allocation points assigned to this pool. SUSHIs to distribute per block.
uint256 lastRewardBlock; // Last block number that SUSHIs distribution occurs.
uint256 accSushiPerShare; // Accumulated SUSHIs per share, times 1e12. See below.
IERC20 lpToken; // Address of LP token contract.
uint256 allocPoint; // How many allocation points assigned to this pool. SUSHIs to distribute per block.
uint256 lastRewardBlock; // Last block number that SUSHIs distribution occurs.
uint256 accSushiPerShare; // Accumulated SUSHIs per share, times 1e12. See below.
}
interface IERC20 {
function totalSupply() external view returns (uint256);
function totalSupply() external view returns (uint256);
function balanceOf(address account) external view returns (uint256);
function balanceOf(address account) external view returns (uint256);
function allowance(address owner, address spender) external view returns (uint256);
function allowance(address owner, address spender)
external
view
returns (uint256);
function approve(address spender, uint256 amount) external returns (bool);
function approve(address spender, uint256 amount) external returns (bool);
event Transfer(address indexed from, address indexed to, uint256 value);
event Approval(address indexed owner, address indexed spender, uint256 value);
event Transfer(address indexed from, address indexed to, uint256 value);
event Approval(
address indexed owner,
address indexed spender,
uint256 value
);
/// @notice EIP 2612
function permit(
address owner,
address spender,
uint256 value,
uint256 deadline,
uint8 v,
bytes32 r,
bytes32 s
) external;
/// @notice EIP 2612
function permit(
address owner,
address spender,
uint256 value,
uint256 deadline,
uint8 v,
bytes32 r,
bytes32 s
) external;
}
interface IMasterChef {
function poolLength() external view returns (uint256);
function poolLength() external view returns (uint256);
function updatePool(uint256 pid) external returns (PoolInfo memory);
function updatePool(uint256 pid) external returns (PoolInfo memory);
function poolInfo(uint256 pid) external view returns (address, uint256, uint256, uint256);
function poolInfo(uint256 pid)
external
view
returns (
address,
uint256,
uint256,
uint256
);
function userInfo(uint256 _pid, address _user)
external
view
returns (uint256, uint256);
function userInfo(uint256 _pid, address _user)
external
view
returns (uint256, uint256);
function deposit(
uint256 pid,
uint256 amount
) external;
function deposit(uint256 pid, uint256 amount) external;
function withdraw(
uint256 pid,
uint256 amount
) external;
function withdraw(uint256 pid, uint256 amount) external;
function emergencyWithdraw(uint256 pid, address to) external;
function emergencyWithdraw(uint256 pid, address to) external;
}
interface IMasterChefV2 {
function poolLength() external view returns (uint256);
function poolLength() external view returns (uint256);
function updatePool(uint256 pid) external returns (PoolInfo memory);
function updatePool(uint256 pid) external returns (PoolInfo memory);
function lpToken(uint256 pid) external view returns (address);
function lpToken(uint256 pid) external view returns (address);
function userInfo(uint256 _pid, address _user)
external
view
returns (uint256, uint256);
function userInfo(uint256 _pid, address _user)
external
view
returns (uint256, uint256);
function deposit(
uint256 pid,
uint256 amount,
address to
) external;
function deposit(
uint256 pid,
uint256 amount,
address to
) external;
function withdraw(
uint256 pid,
uint256 amount,
address to
) external;
function withdraw(
uint256 pid,
uint256 amount,
address to
) external;
function emergencyWithdraw(uint256 pid, address to) external;
function emergencyWithdraw(uint256 pid, address to) external;
function harvest(uint256 pid, address to) external;
function harvest(uint256 pid, address to) external;
function withdrawAndHarvest(
uint256 pid,
uint256 amount,
address to
) external;
function withdrawAndHarvest(
uint256 pid,
uint256 amount,
address to
) external;
}
interface ISushiSwapFactory {
function getPair(address tokenA, address tokenB)
external
view
returns (address pair);
function getPair(address tokenA, address tokenB)
external
view
returns (address pair);
function allPairs(uint256) external view returns (address pair);
function allPairs(uint256) external view returns (address pair);
function allPairsLength() external view returns (uint256);
function allPairsLength() external view returns (uint256);
function feeTo() external view returns (address);
function feeTo() external view returns (address);
function feeToSetter() external view returns (address);
function feeToSetter() external view returns (address);
function createPair(address tokenA, address tokenB)
external
returns (address pair);
function createPair(address tokenA, address tokenB)
external
returns (address pair);
}

View File

@ -1,3 +1,4 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.7.0;
pragma experimental ABIEncoderV2;
@ -6,169 +7,216 @@ pragma experimental ABIEncoderV2;
* @dev Decentralized Exchange.
*/
import {TokenInterface} from "../../common/interfaces.sol";
import {Helpers} from "./helpers.sol";
import {Events} from "./events.sol";
import { TokenInterface } from "../../common/interfaces.sol";
import { Helpers } from "./helpers.sol";
import { Events } from "./events.sol";
abstract contract SushipswapIncentiveResolver is Helpers, Events {
/**
* @dev deposit LP token to masterChef
* @notice deposit LP token to masterChef
* @param token1 token1 of LP token
* @param token2 token2 of LP token
* @param amount amount of LP token
* @param getId ID to retrieve amount
* @param setId ID stores Pool ID
*/
function deposit(
address token1,
address token2,
uint256 amount,
uint256 getId,
uint256 setId
)
external
payable
returns (string memory _eventName, bytes memory _eventParam)
{
token1 = changeEthAddrToWethAddr(token1);
token2 = changeEthAddrToWethAddr(token2);
amount = getUint(getId, amount);
(uint256 _pid, uint256 _version, address lpTokenAddr) = _getPoolId(
token1,
token2
);
setUint(setId, _pid);
require(_pid != uint256(-1), "pool-does-not-exist");
TokenInterface lpToken = TokenInterface(lpTokenAddr);
lpToken.approve(address(masterChef), amount);
_deposit(_pid, amount, _version);
_eventName = "LogDeposit(uint256,uint256,uint256)";
_eventParam = abi.encode(_pid, _version, amount);
}
/**
* @dev deposit LP token to masterChef
* @notice deposit LP token to masterChef
* @param token1 token1 of LP token
* @param token2 token2 of LP token
* @param amount amount of LP token
* @param getId ID to retrieve amount
* @param setId ID stores Pool ID
*/
function deposit(
address token1,
address token2,
uint256 amount,
uint256 getId,
uint256 setId,
Metadata memory data
)
external
payable
returns (string memory _eventName, bytes memory _eventParam)
{
token1 = changeEthAddrToWethAddr(token1);
token2 = changeEthAddrToWethAddr(token2);
amount = getUint(getId, amount);
if (
data.poolId == uint256(-1) ||
data.version == 0 ||
data.lpToken == address(0)
) {
data = _getPoolId(token1, token2);
}
setUint(setId, data.poolId);
require(data.poolId != uint256(-1), "pool-does-not-exist");
TokenInterface lpToken = TokenInterface(data.lpToken);
lpToken.approve(address(masterChef), amount);
_deposit(data, amount);
_eventName = "LogDeposit(address,address,uint256,uint256,uint256)";
_eventParam = abi.encode(
token1,
token2,
data.poolId,
data.version,
amount
);
}
/**
* @dev withdraw LP token from masterChef
* @notice withdraw LP token from masterChef
* @param token1 token1 of LP token
* @param token2 token2 of LP token
* @param amount amount of LP token
* @param getId ID to retrieve amount
* @param setId ID stores Pool ID
*/
function withdraw(
address token1,
address token2,
uint256 amount,
uint256 getId,
uint256 setId
)
external
payable
returns (string memory _eventName, bytes memory _eventParam)
{
token1 = changeEthAddrToWethAddr(token1);
token2 = changeEthAddrToWethAddr(token2);
amount = getUint(getId, amount);
(uint256 _pid, uint256 _version, ) = _getPoolId(token1, token2);
setUint(setId, _pid);
require(_pid != uint256(-1), "pool-does-not-exist");
_withdraw(_pid, amount, _version);
_eventName = "LogWithdraw(uint256,uint256,uint256)";
_eventParam = abi.encode(_pid, _version, amount);
}
/**
* @dev withdraw LP token from masterChef
* @notice withdraw LP token from masterChef
* @param token1 token1 of LP token
* @param token2 token2 of LP token
* @param amount amount of LP token
* @param getId ID to retrieve amount
* @param setId ID stores Pool ID
*/
function withdraw(
address token1,
address token2,
uint256 amount,
uint256 getId,
uint256 setId,
Metadata memory data
)
external
payable
returns (string memory _eventName, bytes memory _eventParam)
{
token1 = changeEthAddrToWethAddr(token1);
token2 = changeEthAddrToWethAddr(token2);
amount = getUint(getId, amount);
if (data.poolId == uint256(-1) || data.version == 0) {
data = _getPoolId(token1, token2);
}
setUint(setId, amount);
require(data.poolId != uint256(-1), "pool-does-not-exist");
_withdraw(data, amount);
_eventName = "LogDeposit(address,address,uint256,uint256,uint256)";
_eventParam = abi.encode(
token1,
token2,
data.poolId,
data.version,
amount
);
}
/**
* @dev harvest from masterChef
* @notice harvest from masterChef
* @param token1 token1 deposited of LP token
* @param token2 token2 deposited LP token
* @param setId ID stores Pool ID
*/
function harvest(
address token1,
address token2,
uint256 setId
)
external
payable
returns (string memory _eventName, bytes memory _eventParam)
{
token1 = changeEthAddrToWethAddr(token1);
token2 = changeEthAddrToWethAddr(token2);
(uint256 _pid, uint256 _version, ) = _getPoolId(token1, token2);
setUint(setId, _pid);
require(_pid != uint256(-1), "pool-does-not-exist");
(, uint256 rewardsAmount) = _getUserInfo(_pid, _version);
if (_version == 2) _harvest(_pid);
else _withdraw(_pid, 0, _version);
_eventName = "LogHarvest(uint256,uint256,uint256)";
_eventParam = abi.encode(_pid, _version, rewardsAmount);
}
/**
* @dev harvest from masterChef
* @notice harvest from masterChef
* @param token1 token1 deposited of LP token
* @param token2 token2 deposited LP token
* @param setId ID stores Pool ID
*/
function harvest(
address token1,
address token2,
uint256 setId,
Metadata memory data
)
external
payable
returns (string memory _eventName, bytes memory _eventParam)
{
token1 = changeEthAddrToWethAddr(token1);
token2 = changeEthAddrToWethAddr(token2);
if (data.poolId == uint256(-1) || data.version == 0) {
data = _getPoolId(token1, token2);
}
setUint(setId, data.poolId);
require(data.poolId != uint256(-1), "pool-does-not-exist");
(, uint256 rewardsAmount) = _getUserInfo(data);
if (data.version == 2) _harvest(data);
else _withdraw(data, 0);
_eventName = "LogDeposit(address,address,uint256,uint256,uint256)";
_eventParam = abi.encode(
token1,
token2,
data.poolId,
data.version,
rewardsAmount
);
}
/**
* @dev withdraw LP token and harvest from masterChef
* @notice withdraw LP token and harvest from masterChef
* @param token1 token1 of LP token
* @param token2 token2 of LP token
* @param amount amount of LP token
* @param getId ID to retrieve amount
* @param setId ID stores Pool ID
*/
function withdrawAndHarvest(
address token1,
address token2,
uint256 amount,
uint256 getId,
uint256 setId
)
external
payable
returns (string memory _eventName, bytes memory _eventParam)
{
token1 = changeEthAddrToWethAddr(token1);
token2 = changeEthAddrToWethAddr(token2);
amount = getUint(getId, amount);
(uint256 _pid, uint256 _version, ) = _getPoolId(token1, token2);
setUint(setId, _pid);
require(_pid != uint256(-1), "pool-does-not-exist");
(, uint256 rewardsAmount) = _getUserInfo(_pid, _version);
_withdrawAndHarvest(_pid, amount, _version);
_eventName = "LogWithdrawAndHarvest(uint256,uint256,uint256,uint256)";
_eventParam = abi.encode(_pid, _version, amount, rewardsAmount);
}
/**
* @dev withdraw LP token and harvest from masterChef
* @notice withdraw LP token and harvest from masterChef
* @param token1 token1 of LP token
* @param token2 token2 of LP token
* @param amount amount of LP token
* @param getId ID to retrieve amount
* @param setId ID stores Pool ID
*/
function withdrawAndHarvest(
address token1,
address token2,
uint256 amount,
uint256 getId,
uint256 setId,
Metadata memory data
)
external
payable
returns (string memory _eventName, bytes memory _eventParam)
{
token1 = changeEthAddrToWethAddr(token1);
token2 = changeEthAddrToWethAddr(token2);
amount = getUint(getId, amount);
if (data.poolId == uint256(-1) || data.version == 0) {
data = _getPoolId(token1, token2);
}
setUint(setId, data.poolId);
require(data.poolId != uint256(-1), "pool-does-not-exist");
(, uint256 rewardsAmount) = _getUserInfo(data);
_withdrawAndHarvest(data, amount);
_eventName = "LogWithdrawAndHarvest(address,address,uint256,uint256,uint256,uint256)";
_eventParam = abi.encode(
token1,
token2,
data.poolId,
data.version,
amount,
rewardsAmount
);
}
/**
* @dev emergency withdraw from masterChef
* @notice emergency withdraw from masterChef
* @param token1 token1 deposited of LP token
* @param token2 token2 deposited LP token
* @param setId ID stores Pool ID
*/
function emergencyWithdraw(
address token1,
address token2,
uint256 setId
)
external
payable
returns (string memory _eventName, bytes memory _eventParam)
{
token1 = changeEthAddrToWethAddr(token1);
token2 = changeEthAddrToWethAddr(token2);
(uint256 _pid, uint256 _version, ) = _getPoolId(token1, token2);
setUint(setId, _pid);
require(_pid != uint256(-1), "pool-does-not-exist");
(uint256 lpAmount, uint256 rewardsAmount) = _getUserInfo(
_pid,
_version
);
_emergencyWithdraw(_pid, _version);
_eventName = "LogEmergencyWithdraw(uint256,uint256,uint256,uint256)";
_eventParam = abi.encode(_pid, _version, lpAmount, rewardsAmount);
}
/**
* @dev emergency withdraw from masterChef
* @notice emergency withdraw from masterChef
* @param token1 token1 deposited of LP token
* @param token2 token2 deposited LP token
* @param setId ID stores Pool ID
*/
function emergencyWithdraw(
address token1,
address token2,
uint256 setId,
Metadata memory data
)
external
payable
returns (string memory _eventName, bytes memory _eventParam)
{
token1 = changeEthAddrToWethAddr(token1);
token2 = changeEthAddrToWethAddr(token2);
if (data.poolId == uint256(-1) || data.version == 0) {
data = _getPoolId(token1, token2);
}
uint256 currentBal = _balance(token1, token2);
require(data.poolId != uint256(-1), "pool-does-not-exist");
(uint256 lpAmount, uint256 rewardsAmount) = _getUserInfo(data);
_emergencyWithdraw(data);
uint256 finalBal = _balance(token1, token2);
setUint(setId, uint256(currentBal - finalBal));
_eventName = "LogEmergencyWithdraw(address,address,uint256,uint256,uint256,uint256)";
_eventParam = abi.encode(
token1,
token2,
data.poolId,
data.version,
lpAmount,
rewardsAmount
);
}
}
contract ConnectV2SushiswapIncentive is SushipswapIncentiveResolver {
string public constant name = "SushipswapIncentive-v1.1";
string public constant name = "SushipswapIncentive-v1.1";
}

View File

@ -94,6 +94,12 @@ describe("Sushiswap", function () {
});
describe("Main", function () {
const data = {
poolId: 0,
version: 0,
lpToken: ethers.constants.AddressZero
}
it("Should deposit successfully", async function () {
const ethAmount = ethers.utils.parseEther("2") // 1 ETH
const daiUnitAmount = ethers.utils.parseEther("4000") // 1 ETH
@ -134,7 +140,8 @@ describe("Sushiswap", function () {
DAI_ADDR,
ethers.utils.parseEther("10"),
getId,
setId
setId,
data
]
}
]
@ -152,7 +159,8 @@ describe("Sushiswap", function () {
args: [
WETH_ADDR,
DAI_ADDR,
setId
setId,
data
]
}
]
@ -173,7 +181,8 @@ describe("Sushiswap", function () {
DAI_ADDR,
ethers.utils.parseEther("1"),
getId,
setId
setId,
data
]
}
]
@ -194,7 +203,8 @@ describe("Sushiswap", function () {
DAI_ADDR,
ethers.utils.parseEther("1"),
getId,
setId
setId,
data
]
}
]