-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: added sushiswap adapter contracts
- Loading branch information
Yashika Goyal
committed
Dec 23, 2023
1 parent
dd36d31
commit 957bd9b
Showing
3 changed files
with
279 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
// SPDX-License-Identifier: GPL-2.0-or-later | ||
pragma solidity 0.8.18; | ||
|
||
abstract contract ISushiswapNonfungiblePositionManager { | ||
struct MintParams { | ||
address token0; | ||
address token1; | ||
uint24 fee; | ||
int24 tickLower; | ||
int24 tickUpper; | ||
uint256 amount0Desired; | ||
uint256 amount1Desired; | ||
uint256 amount0Min; | ||
uint256 amount1Min; | ||
address recipient; | ||
uint256 deadline; | ||
} | ||
|
||
function mint( | ||
MintParams calldata params | ||
) | ||
external | ||
payable | ||
virtual | ||
returns ( | ||
uint256 tokenId, | ||
uint128 liquidity, | ||
uint256 amount0, | ||
uint256 amount1 | ||
); | ||
|
||
struct IncreaseLiquidityParams { | ||
uint256 tokenId; | ||
uint256 amount0Desired; | ||
uint256 amount1Desired; | ||
uint256 amount0Min; | ||
uint256 amount1Min; | ||
uint256 deadline; | ||
} | ||
|
||
function increaseLiquidity( | ||
IncreaseLiquidityParams calldata params | ||
) | ||
external | ||
payable | ||
virtual | ||
returns (uint128 liquidity, uint256 amount0, uint256 amount1); | ||
|
||
struct DecreaseLiquidityParams { | ||
uint256 tokenId; | ||
uint128 liquidity; | ||
uint256 amount0Min; | ||
uint256 amount1Min; | ||
uint256 deadline; | ||
} | ||
|
||
function decreaseLiquidity( | ||
DecreaseLiquidityParams calldata params | ||
) external payable virtual returns (uint256 amount0, uint256 amount1); | ||
|
||
// set amount0Max and amount1Max to uint256.max to collect all fees | ||
struct CollectParams { | ||
uint256 tokenId; | ||
address recipient; | ||
uint128 amount0Max; | ||
uint128 amount1Max; | ||
} | ||
|
||
function collect( | ||
CollectParams calldata params | ||
) external payable virtual returns (uint256 amount0, uint256 amount1); | ||
|
||
function burn(uint256 tokenId) external payable virtual; | ||
|
||
function positions( | ||
uint256 tokenId | ||
) | ||
external | ||
view | ||
virtual | ||
returns ( | ||
uint96 nonce, | ||
address operator, | ||
address token0, | ||
address token1, | ||
uint24 fee, | ||
int24 tickLower, | ||
int24 tickUpper, | ||
uint128 liquidity, | ||
uint256 feeGrowthInside0LastX128, | ||
uint256 feeGrowthInside1LastX128, | ||
uint128 tokensOwed0, | ||
uint128 tokensOwed1 | ||
); | ||
|
||
function balanceOf( | ||
address owner | ||
) external view virtual returns (uint256 balance); | ||
} |
23 changes: 23 additions & 0 deletions
23
contracts/intent-adapters/lp/Sushiswap/SushiswapHelpers.sol
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
// SPDX-License-Identifier: MIT | ||
pragma solidity 0.8.18; | ||
|
||
import {ISushiswapNonfungiblePositionManager} from "./Interfaces.sol"; | ||
|
||
contract SushiswapHelpers { | ||
ISushiswapNonfungiblePositionManager | ||
private immutable _nonFungiblePositionManager; | ||
|
||
constructor(address __nonFungiblePositionManager) { | ||
_nonFungiblePositionManager = ISushiswapNonfungiblePositionManager( | ||
__nonFungiblePositionManager | ||
); | ||
} | ||
|
||
function positionManager() | ||
public | ||
view | ||
returns (ISushiswapNonfungiblePositionManager) | ||
{ | ||
return _nonFungiblePositionManager; | ||
} | ||
} |
157 changes: 157 additions & 0 deletions
157
contracts/intent-adapters/lp/Sushiswap/SushiswapMint.sol
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,157 @@ | ||
// SPDX-License-Identifier: MIT | ||
pragma solidity 0.8.18; | ||
|
||
import {ISushiswapNonfungiblePositionManager} from "./Interfaces.sol"; | ||
import {RouterIntentAdapter, Errors} from "router-intents/contracts/RouterIntentAdapter.sol"; | ||
import {NitroMessageHandler} from "router-intents/contracts/NitroMessageHandler.sol"; | ||
import {DefaultRefundable} from "router-intents/contracts/DefaultRefundable.sol"; | ||
import {IERC20, SafeERC20} from "../../../utils/SafeERC20.sol"; | ||
import {SushiswapHelpers} from "./SushiswapHelpers.sol"; | ||
|
||
/** | ||
* @title SushiswapMint | ||
* @author Yashika Goyal | ||
* @notice Minting a new position on Sushiswap. | ||
*/ | ||
contract SushiswapMint is | ||
RouterIntentAdapter, | ||
NitroMessageHandler, | ||
DefaultRefundable, | ||
SushiswapHelpers | ||
{ | ||
using SafeERC20 for IERC20; | ||
|
||
event SushiswapMintPositionDest(); | ||
|
||
constructor( | ||
address __native, | ||
address __wnative, | ||
address __owner, | ||
address __assetForwarder, | ||
address __dexspan, | ||
address __defaultRefundAddress, | ||
address __nonFungiblePositionManager | ||
) | ||
RouterIntentAdapter(__native, __wnative, __owner) | ||
NitroMessageHandler(__assetForwarder, __dexspan) | ||
DefaultRefundable(__defaultRefundAddress) | ||
SushiswapHelpers(__nonFungiblePositionManager) | ||
// solhint-disable-next-line no-empty-blocks | ||
{ | ||
|
||
} | ||
|
||
function name() public pure override returns (string memory) { | ||
return "SushiswapMint"; | ||
} | ||
|
||
/** | ||
* @inheritdoc RouterIntentAdapter | ||
*/ | ||
function execute( | ||
address, | ||
address, | ||
bytes calldata data | ||
) external payable override returns (address[] memory tokens) { | ||
ISushiswapNonfungiblePositionManager.MintParams | ||
memory mintParams = parseInputs(data); | ||
|
||
// If the adapter is called using `call` and not `delegatecall` | ||
if (address(this) == self()) { | ||
if (mintParams.token0 != native()) | ||
IERC20(mintParams.token0).safeTransferFrom( | ||
msg.sender, | ||
self(), | ||
mintParams.amount0Desired | ||
); | ||
else | ||
require( | ||
msg.value == mintParams.amount0Desired, | ||
Errors.INSUFFICIENT_NATIVE_FUNDS_PASSED | ||
); | ||
|
||
if (mintParams.token1 != native()) | ||
IERC20(mintParams.token1).safeTransferFrom( | ||
msg.sender, | ||
self(), | ||
mintParams.amount1Desired | ||
); | ||
else | ||
require( | ||
msg.value == mintParams.amount1Desired, | ||
Errors.INSUFFICIENT_NATIVE_FUNDS_PASSED | ||
); | ||
} | ||
|
||
if (mintParams.token0 == native()) { | ||
convertNativeToWnative(mintParams.amount0Desired); | ||
mintParams.token0 = wnative(); | ||
} | ||
|
||
if (mintParams.token1 == native()) { | ||
convertNativeToWnative(mintParams.amount0Desired); | ||
mintParams.token1 = wnative(); | ||
} | ||
|
||
IERC20(mintParams.token0).safeIncreaseAllowance( | ||
address(positionManager()), | ||
mintParams.amount0Desired | ||
); | ||
|
||
IERC20(mintParams.token1).safeIncreaseAllowance( | ||
address(positionManager()), | ||
mintParams.amount1Desired | ||
); | ||
|
||
bytes memory logData; | ||
|
||
(tokens, logData) = _mint(mintParams); | ||
|
||
emit ExecutionEvent(name(), logData); | ||
return tokens; | ||
} | ||
|
||
/** | ||
* @inheritdoc NitroMessageHandler | ||
*/ | ||
function handleMessage( | ||
address tokenSent, | ||
uint256 amount, | ||
bytes memory | ||
) external override onlyNitro nonReentrant { | ||
withdrawTokens(tokenSent, defaultRefundAddress(), amount); | ||
emit UnsupportedOperation(tokenSent, defaultRefundAddress(), amount); | ||
} | ||
|
||
//////////////////////////// ACTION LOGIC //////////////////////////// | ||
|
||
function _mint( | ||
ISushiswapNonfungiblePositionManager.MintParams memory mintParams | ||
) internal returns (address[] memory tokens, bytes memory logData) { | ||
(uint256 tokenId, , , ) = positionManager().mint(mintParams); | ||
|
||
tokens = new address[](2); | ||
tokens[0] = mintParams.token0; | ||
tokens[1] = mintParams.token1; | ||
|
||
logData = abi.encode(mintParams, tokenId); | ||
} | ||
|
||
/** | ||
* @dev function to parse input data. | ||
* @param data input data. | ||
*/ | ||
function parseInputs( | ||
bytes memory data | ||
) | ||
public | ||
pure | ||
returns (ISushiswapNonfungiblePositionManager.MintParams memory) | ||
{ | ||
return | ||
abi.decode(data, (ISushiswapNonfungiblePositionManager.MintParams)); | ||
} | ||
|
||
// solhint-disable-next-line no-empty-blocks | ||
receive() external payable {} | ||
} |