From 3b4e272090868925de4e39ea44a6cee8df96d2d4 Mon Sep 17 00:00:00 2001 From: pradyuman-verma Date: Wed, 15 Dec 2021 00:00:07 +0530 Subject: [PATCH 1/6] updated quickswap --- .../connectors/sushi-incentive/helpers.sol | 70 ++++++------ .../connectors/sushi-incentive/main.sol | 106 +++++++++++------- 2 files changed, 103 insertions(+), 73 deletions(-) diff --git a/contracts/mainnet/connectors/sushi-incentive/helpers.sol b/contracts/mainnet/connectors/sushi-incentive/helpers.sol index 97522f22..7796fb65 100644 --- a/contracts/mainnet/connectors/sushi-incentive/helpers.sol +++ b/contracts/mainnet/connectors/sushi-incentive/helpers.sol @@ -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)); } } diff --git a/contracts/mainnet/connectors/sushi-incentive/main.sol b/contracts/mainnet/connectors/sushi-incentive/main.sol index cc448434..2adfafe2 100644 --- a/contracts/mainnet/connectors/sushi-incentive/main.sol +++ b/contracts/mainnet/connectors/sushi-incentive/main.sol @@ -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); } } From 2a0a61f1e161b3a01726442e9c26b0a01df9addd Mon Sep 17 00:00:00 2001 From: pradyuman-verma Date: Wed, 15 Dec 2021 00:54:54 +0530 Subject: [PATCH 2/6] update --- .../connectors/sushi-incentive/main.sol | 10 +++++----- .../sushiswapIncentive.test.ts | 18 ++++++++++++++---- 2 files changed, 19 insertions(+), 9 deletions(-) diff --git a/contracts/mainnet/connectors/sushi-incentive/main.sol b/contracts/mainnet/connectors/sushi-incentive/main.sol index 2adfafe2..9f48ed77 100644 --- a/contracts/mainnet/connectors/sushi-incentive/main.sol +++ b/contracts/mainnet/connectors/sushi-incentive/main.sol @@ -35,7 +35,7 @@ abstract contract SushipswapIncentiveResolver is Helpers, Events { token1 = changeEthAddrToWethAddr(token1); token2 = changeEthAddrToWethAddr(token2); amount = getUint(getId, amount); - if(data.poolId == uint256(-1)|| data.version < 0 || data.lpToken == address(0)){ + if(data.poolId == uint256(-1)|| data.version <= 0 || data.lpToken == address(0)){ data = _getPoolId( token1, token2 @@ -74,7 +74,7 @@ abstract contract SushipswapIncentiveResolver is Helpers, Events { token1 = changeEthAddrToWethAddr(token1); token2 = changeEthAddrToWethAddr(token2); amount = getUint(getId, amount); - if(data.poolId == uint256(-1)|| data.version < 0){ + if(data.poolId == uint256(-1)|| data.version <= 0){ data = _getPoolId( token1, token2 @@ -106,7 +106,7 @@ abstract contract SushipswapIncentiveResolver is Helpers, Events { { token1 = changeEthAddrToWethAddr(token1); token2 = changeEthAddrToWethAddr(token2); - if(data.poolId == uint256(-1)|| data.version < 0 ){ + if(data.poolId == uint256(-1)|| data.version <= 0 ){ data = _getPoolId( token1, token2 @@ -145,7 +145,7 @@ abstract contract SushipswapIncentiveResolver is Helpers, Events { token1 = changeEthAddrToWethAddr(token1); token2 = changeEthAddrToWethAddr(token2); amount = getUint(getId, amount); - if(data.poolId == uint256(-1)|| data.version < 0){ + if(data.poolId == uint256(-1)|| data.version <= 0){ data = _getPoolId( token1, token2 @@ -178,7 +178,7 @@ abstract contract SushipswapIncentiveResolver is Helpers, Events { { token1 = changeEthAddrToWethAddr(token1); token2 = changeEthAddrToWethAddr(token2); - if(data.poolId == uint256(-1)|| data.version < 0 ){ + if(data.poolId == uint256(-1)|| data.version <= 0 ){ data = _getPoolId( token1, token2 diff --git a/test/mainnet/sushiswapIncentive/sushiswapIncentive.test.ts b/test/mainnet/sushiswapIncentive/sushiswapIncentive.test.ts index b8a9c32a..f0dca1ad 100644 --- a/test/mainnet/sushiswapIncentive/sushiswapIncentive.test.ts +++ b/test/mainnet/sushiswapIncentive/sushiswapIncentive.test.ts @@ -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 ] } ] From f511685a15ba99eca7f3fe03930c97b13da27c9b Mon Sep 17 00:00:00 2001 From: pradyuman-verma Date: Wed, 15 Dec 2021 23:03:49 +0530 Subject: [PATCH 3/6] updates events --- .../connectors/sushi-incentive/events.sol | 64 +-- .../connectors/sushi-incentive/main.sol | 381 +++++++++--------- 2 files changed, 238 insertions(+), 207 deletions(-) diff --git a/contracts/mainnet/connectors/sushi-incentive/events.sol b/contracts/mainnet/connectors/sushi-incentive/events.sol index aa1c890d..155d1983 100644 --- a/contracts/mainnet/connectors/sushi-incentive/events.sol +++ b/contracts/mainnet/connectors/sushi-incentive/events.sol @@ -1,31 +1,41 @@ 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 + ); } diff --git a/contracts/mainnet/connectors/sushi-incentive/main.sol b/contracts/mainnet/connectors/sushi-incentive/main.sol index 9f48ed77..7495e61f 100644 --- a/contracts/mainnet/connectors/sushi-incentive/main.sol +++ b/contracts/mainnet/connectors/sushi-incentive/main.sol @@ -6,193 +6,214 @@ 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, - 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(uint256,uint256,uint256)"; - _eventParam = abi.encode(data.poolId, data.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, - 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"); - _withdraw(data, amount); - _eventName = "LogWithdraw(uint256,uint256,uint256)"; - _eventParam = abi.encode(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, + 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"); + _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, - 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 = "LogHarvest(uint256,uint256,uint256)"; - _eventParam = abi.encode(data.poolId, data.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, - 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(uint256,uint256,uint256,uint256)"; - _eventParam = abi.encode(data.poolId, data.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, - 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 lpAmount, uint256 rewardsAmount) = _getUserInfo(data); - _emergencyWithdraw(data); - _eventName = "LogEmergencyWithdraw(uint256,uint256,uint256,uint256)"; - _eventParam = abi.encode(data.poolId, data.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); + } + setUint(setId, data.poolId); + require(data.poolId != uint256(-1), "pool-does-not-exist"); + (uint256 lpAmount, uint256 rewardsAmount) = _getUserInfo(data); + _emergencyWithdraw(data); + _eventName = "LogEmergencyWithdraw(address,addressuint256,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"; } From fa6886e033a934438877cf5bc65045de0d818fdc Mon Sep 17 00:00:00 2001 From: pradyuman-verma Date: Wed, 15 Dec 2021 23:21:11 +0530 Subject: [PATCH 4/6] update --- .../mainnet/connectors/sushi-incentive/main.sol | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/contracts/mainnet/connectors/sushi-incentive/main.sol b/contracts/mainnet/connectors/sushi-incentive/main.sol index 7495e61f..4d88c17e 100644 --- a/contracts/mainnet/connectors/sushi-incentive/main.sol +++ b/contracts/mainnet/connectors/sushi-incentive/main.sol @@ -37,7 +37,7 @@ abstract contract SushipswapIncentiveResolver is Helpers, Events { amount = getUint(getId, amount); if ( data.poolId == uint256(-1) || - data.version <= 0 || + data.version == 0 || data.lpToken == address(0) ) { data = _getPoolId(token1, token2); @@ -81,10 +81,10 @@ abstract contract SushipswapIncentiveResolver is Helpers, Events { token1 = changeEthAddrToWethAddr(token1); token2 = changeEthAddrToWethAddr(token2); amount = getUint(getId, amount); - if (data.poolId == uint256(-1) || data.version <= 0) { + if (data.poolId == uint256(-1) || data.version == 0) { data = _getPoolId(token1, token2); } - setUint(setId, data.poolId); + setUint(setId, amount); require(data.poolId != uint256(-1), "pool-does-not-exist"); _withdraw(data, amount); _eventName = "LogDeposit(address,address,uint256,uint256,uint256)"; @@ -116,7 +116,7 @@ abstract contract SushipswapIncentiveResolver is Helpers, Events { { token1 = changeEthAddrToWethAddr(token1); token2 = changeEthAddrToWethAddr(token2); - if (data.poolId == uint256(-1) || data.version <= 0) { + if (data.poolId == uint256(-1) || data.version == 0) { data = _getPoolId(token1, token2); } setUint(setId, data.poolId); @@ -158,7 +158,7 @@ abstract contract SushipswapIncentiveResolver is Helpers, Events { token1 = changeEthAddrToWethAddr(token1); token2 = changeEthAddrToWethAddr(token2); amount = getUint(getId, amount); - if (data.poolId == uint256(-1) || data.version <= 0) { + if (data.poolId == uint256(-1) || data.version == 0) { data = _getPoolId(token1, token2); } setUint(setId, data.poolId); @@ -195,7 +195,7 @@ abstract contract SushipswapIncentiveResolver is Helpers, Events { { token1 = changeEthAddrToWethAddr(token1); token2 = changeEthAddrToWethAddr(token2); - if (data.poolId == uint256(-1) || data.version <= 0) { + if (data.poolId == uint256(-1) || data.version == 0) { data = _getPoolId(token1, token2); } setUint(setId, data.poolId); From e56ca991e75a1b79f25e0b02217c72b11d3364c6 Mon Sep 17 00:00:00 2001 From: pradyuman-verma Date: Thu, 16 Dec 2021 11:42:11 +0530 Subject: [PATCH 5/6] update --- .../connectors/sushi-incentive/helpers.sol | 159 +++++++++--------- .../connectors/sushi-incentive/interface.sol | 155 +++++++++-------- .../connectors/sushi-incentive/main.sol | 2 +- 3 files changed, 167 insertions(+), 149 deletions(-) diff --git a/contracts/mainnet/connectors/sushi-incentive/helpers.sol b/contracts/mainnet/connectors/sushi-incentive/helpers.sol index 7796fb65..a02379db 100644 --- a/contracts/mainnet/connectors/sushi-incentive/helpers.sol +++ b/contracts/mainnet/connectors/sushi-incentive/helpers.sol @@ -3,92 +3,101 @@ 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); - struct Metadata { - uint256 poolId; - uint256 version; - address lpToken; - } + 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(data.poolId, _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 _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 _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 _harvest(Metadata memory data) internal { - masterChefV2.harvest(data.poolId, address(this)); - } + function _harvest(Metadata memory data) internal { + masterChefV2.harvest(data.poolId, 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 _withdrawAndHarvest(Metadata memory data, uint256 _amount) + internal + { + if (data.version == 2) + masterChefV2.withdrawAndHarvest( + data.poolId, + _amount, + address(this) + ); + else _withdraw(data, _amount); + } - function _emergencyWithdraw(Metadata memory data) internal { - if(data.version == 2) - masterChefV2.emergencyWithdraw(data.poolId, address(this)); - else - masterChef.emergencyWithdraw(data.poolId, address(this)); - } + function _emergencyWithdraw(Metadata memory data) internal { + if (data.version == 2) + masterChefV2.emergencyWithdraw(data.poolId, address(this)); + else masterChef.emergencyWithdraw(data.poolId, address(this)); + } - 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); + 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); - for (uint256 i = 0; i < length; i++) { - data.lpToken = masterChefV2.lpToken(i); - if (pair == data.lpToken) { - data.poolId = i; - break; - } - } + for (uint256 i = 0; i < length; 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++) { - (data.lpToken, , , ) = masterChef.poolInfo(i); - if (pair == data.lpToken) { - data.poolId = i; - data.version = 1; - break; - } - } - } + 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 _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) + ); + } } diff --git a/contracts/mainnet/connectors/sushi-incentive/interface.sol b/contracts/mainnet/connectors/sushi-incentive/interface.sol index ba3b1c6c..777e4d77 100644 --- a/contracts/mainnet/connectors/sushi-incentive/interface.sol +++ b/contracts/mainnet/connectors/sushi-incentive/interface.sol @@ -2,116 +2,125 @@ 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); } diff --git a/contracts/mainnet/connectors/sushi-incentive/main.sol b/contracts/mainnet/connectors/sushi-incentive/main.sol index 4d88c17e..bfb56d79 100644 --- a/contracts/mainnet/connectors/sushi-incentive/main.sol +++ b/contracts/mainnet/connectors/sushi-incentive/main.sol @@ -202,7 +202,7 @@ abstract contract SushipswapIncentiveResolver is Helpers, Events { require(data.poolId != uint256(-1), "pool-does-not-exist"); (uint256 lpAmount, uint256 rewardsAmount) = _getUserInfo(data); _emergencyWithdraw(data); - _eventName = "LogEmergencyWithdraw(address,addressuint256,uint256,uint256,uint256)"; + _eventName = "LogEmergencyWithdraw(address,address,uint256,uint256,uint256,uint256)"; _eventParam = abi.encode( token1, token2, From 60169ee5eea4e9407c1e4dd3c8e229d622acb60b Mon Sep 17 00:00:00 2001 From: pradyuman-verma Date: Mon, 27 Dec 2021 21:11:55 +0530 Subject: [PATCH 6/6] updated setUint --- contracts/mainnet/connectors/sushi-incentive/events.sol | 1 + contracts/mainnet/connectors/sushi-incentive/helpers.sol | 5 ++++- contracts/mainnet/connectors/sushi-incentive/interface.sol | 1 + contracts/mainnet/connectors/sushi-incentive/main.sol | 5 ++++- 4 files changed, 10 insertions(+), 2 deletions(-) diff --git a/contracts/mainnet/connectors/sushi-incentive/events.sol b/contracts/mainnet/connectors/sushi-incentive/events.sol index 155d1983..4ef2b9b3 100644 --- a/contracts/mainnet/connectors/sushi-incentive/events.sol +++ b/contracts/mainnet/connectors/sushi-incentive/events.sol @@ -1,3 +1,4 @@ +// SPDX-License-Identifier: MIT pragma solidity ^0.7.0; contract Events { diff --git a/contracts/mainnet/connectors/sushi-incentive/helpers.sol b/contracts/mainnet/connectors/sushi-incentive/helpers.sol index a02379db..c4b5db99 100644 --- a/contracts/mainnet/connectors/sushi-incentive/helpers.sol +++ b/contracts/mainnet/connectors/sushi-incentive/helpers.sol @@ -1,5 +1,4 @@ // SPDX-License-Identifier: MIT - pragma solidity ^0.7.0; pragma experimental ABIEncoderV2; @@ -100,4 +99,8 @@ contract Helpers is DSMath, Basic { address(this) ); } + + function _balance(address token1, address token2) internal view returns (uint balance) { + balance = IERC20(token1).balanceOf(address(this)) + IERC20(token2).balanceOf(address(this)); + } } diff --git a/contracts/mainnet/connectors/sushi-incentive/interface.sol b/contracts/mainnet/connectors/sushi-incentive/interface.sol index 777e4d77..d8b92e92 100644 --- a/contracts/mainnet/connectors/sushi-incentive/interface.sol +++ b/contracts/mainnet/connectors/sushi-incentive/interface.sol @@ -1,3 +1,4 @@ +// SPDX-License-Identifier: MIT pragma solidity ^0.7.0; pragma experimental ABIEncoderV2; diff --git a/contracts/mainnet/connectors/sushi-incentive/main.sol b/contracts/mainnet/connectors/sushi-incentive/main.sol index bfb56d79..87921e57 100644 --- a/contracts/mainnet/connectors/sushi-incentive/main.sol +++ b/contracts/mainnet/connectors/sushi-incentive/main.sol @@ -1,3 +1,4 @@ +// SPDX-License-Identifier: MIT pragma solidity ^0.7.0; pragma experimental ABIEncoderV2; @@ -198,10 +199,12 @@ abstract contract SushipswapIncentiveResolver is Helpers, Events { if (data.poolId == uint256(-1) || data.version == 0) { data = _getPoolId(token1, token2); } - setUint(setId, data.poolId); + 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,