updated quickswap

This commit is contained in:
pradyuman-verma 2021-12-15 00:00:07 +05:30
parent 95ed18984f
commit 3b4e272090
2 changed files with 103 additions and 73 deletions

View File

@ -15,74 +15,80 @@ contract Helpers is DSMath, Basic {
ISushiSwapFactory immutable factory =
ISushiSwapFactory(0xC0AEe478e3658e2610c5F7A4A2E1777cE9e4f2Ac);
function _deposit(uint256 _pid, uint256 _amount, uint256 _version) internal {
if(_version == 2)
masterChefV2.deposit(_pid, _amount, address(this));
struct Metadata {
uint256 poolId;
uint256 version;
address lpToken;
}
function _deposit(Metadata memory data, uint256 _amount) internal {
if(data.version == 2)
masterChefV2.deposit(data.poolId, _amount, address(this));
else
masterChef.deposit(_pid, _amount);
masterChef.deposit(data.poolId, _amount);
}
function _withdraw(uint256 _pid, uint256 _amount, uint256 _version) internal {
if(_version == 2)
masterChefV2.withdraw(_pid, _amount, address(this));
function _withdraw(Metadata memory data, uint256 _amount) internal {
if(data.version == 2)
masterChefV2.withdraw(data.poolId, _amount, address(this));
else
masterChef.withdraw(_pid, _amount);
masterChef.withdraw(data.poolId, _amount);
}
function _harvest(uint256 _pid) internal {
masterChefV2.harvest(_pid, address(this));
function _harvest(Metadata memory data) internal {
masterChefV2.harvest(data.poolId, address(this));
}
function _withdrawAndHarvest(uint256 _pid, uint256 _amount, uint256 _version) internal {
if(_version == 2)
masterChefV2.withdrawAndHarvest(_pid, _amount, address(this));
else _withdraw(_pid, _amount, _version);
function _withdrawAndHarvest(Metadata memory data, uint256 _amount) internal {
if(data.version == 2)
masterChefV2.withdrawAndHarvest(data.poolId, _amount, address(this));
else _withdraw(data, _amount);
}
function _emergencyWithdraw(uint256 _pid, uint256 _version) internal {
if(_version == 2)
masterChefV2.emergencyWithdraw(_pid, address(this));
function _emergencyWithdraw(Metadata memory data) internal {
if(data.version == 2)
masterChefV2.emergencyWithdraw(data.poolId, address(this));
else
masterChef.emergencyWithdraw(_pid, address(this));
masterChef.emergencyWithdraw(data.poolId, address(this));
}
function _getPoolId(address tokenA, address tokenB)
internal
view
returns (uint256 poolId, uint256 version, address lpToken)
returns (Metadata memory data)
{
address pair = factory.getPair(tokenA, tokenB);
uint256 length = masterChefV2.poolLength();
version = 2;
poolId = uint256(-1);
data.version = 2;
data.poolId = uint256(-1);
for (uint256 i = 0; i < length; i++) {
lpToken = masterChefV2.lpToken(i);
if (pair == lpToken) {
poolId = i;
data.lpToken = masterChefV2.lpToken(i);
if (pair == data.lpToken) {
data.poolId = i;
break;
}
}
uint256 lengthV1 = masterChef.poolLength();
for (uint256 i = 0; i < lengthV1; i++) {
(lpToken, , , ) = masterChef.poolInfo(i);
if (pair == lpToken) {
poolId = i;
version = 1;
(data.lpToken, , , ) = masterChef.poolInfo(i);
if (pair == data.lpToken) {
data.poolId = i;
data.version = 1;
break;
}
}
}
function _getUserInfo(uint256 _pid, uint256 _version)
function _getUserInfo(Metadata memory data)
internal
view
returns (uint256 lpAmount, uint256 rewardsAmount)
{
if(_version == 2)
(lpAmount, rewardsAmount) = masterChefV2.userInfo(_pid, address(this));
if(data.version == 2)
(lpAmount, rewardsAmount) = masterChefV2.userInfo(data.poolId, address(this));
else
(lpAmount, rewardsAmount) = masterChef.userInfo(_pid, address(this));
(lpAmount, rewardsAmount) = masterChef.userInfo(data.poolId, address(this));
}
}

View File

@ -25,7 +25,8 @@ abstract contract SushipswapIncentiveResolver is Helpers, Events {
address token2,
uint256 amount,
uint256 getId,
uint256 setId
uint256 setId,
Metadata memory data
)
external
payable
@ -34,17 +35,19 @@ abstract contract SushipswapIncentiveResolver is Helpers, Events {
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);
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(_pid, amount, _version);
_deposit(data, amount);
_eventName = "LogDeposit(uint256,uint256,uint256)";
_eventParam = abi.encode(_pid, _version, amount);
_eventParam = abi.encode(data.poolId, data.version, amount);
}
/**
@ -61,7 +64,8 @@ abstract contract SushipswapIncentiveResolver is Helpers, Events {
address token2,
uint256 amount,
uint256 getId,
uint256 setId
uint256 setId,
Metadata memory data
)
external
payable
@ -70,12 +74,17 @@ abstract contract SushipswapIncentiveResolver is Helpers, Events {
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);
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");
_withdraw(data, amount);
_eventName = "LogWithdraw(uint256,uint256,uint256)";
_eventParam = abi.encode(_pid, _version, amount);
_eventParam = abi.encode(data.poolId, data.version, amount);
}
/**
@ -88,7 +97,8 @@ abstract contract SushipswapIncentiveResolver is Helpers, Events {
function harvest(
address token1,
address token2,
uint256 setId
uint256 setId,
Metadata memory data
)
external
payable
@ -96,14 +106,19 @@ abstract contract SushipswapIncentiveResolver is Helpers, Events {
{
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);
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 = "LogHarvest(uint256,uint256,uint256)";
_eventParam = abi.encode(_pid, _version, rewardsAmount);
_eventParam = abi.encode(data.poolId, data.version, rewardsAmount);
}
/**
@ -120,7 +135,8 @@ abstract contract SushipswapIncentiveResolver is Helpers, Events {
address token2,
uint256 amount,
uint256 getId,
uint256 setId
uint256 setId,
Metadata memory data
)
external
payable
@ -129,13 +145,18 @@ abstract contract SushipswapIncentiveResolver is Helpers, Events {
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);
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(uint256,uint256,uint256,uint256)";
_eventParam = abi.encode(_pid, _version, amount, rewardsAmount);
_eventParam = abi.encode(data.poolId, data.version, amount, rewardsAmount);
}
/**
@ -148,7 +169,8 @@ abstract contract SushipswapIncentiveResolver is Helpers, Events {
function emergencyWithdraw(
address token1,
address token2,
uint256 setId
uint256 setId,
Metadata memory data
)
external
payable
@ -156,16 +178,18 @@ abstract contract SushipswapIncentiveResolver is Helpers, Events {
{
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);
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 lpAmount, uint256 rewardsAmount) = _getUserInfo(data);
_emergencyWithdraw(data);
_eventName = "LogEmergencyWithdraw(uint256,uint256,uint256,uint256)";
_eventParam = abi.encode(_pid, _version, lpAmount, rewardsAmount);
_eventParam = abi.encode(data.poolId, data.version, lpAmount, rewardsAmount);
}
}