sushiswap: avalanche

This commit is contained in:
pradyuman-verma 2022-01-06 00:04:33 +05:30
parent aeae64a15f
commit 9f73e96f35
No known key found for this signature in database
GPG Key ID: E36FD6BC8923221F
4 changed files with 482 additions and 0 deletions

View File

@ -0,0 +1,42 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.7.0;
contract Events {
event LogDepositLiquidity(
address indexed tokenA,
address indexed tokenB,
uint256 amtA,
uint256 amtB,
uint256 uniAmount,
uint256 getId,
uint256 setId
);
event LogWithdrawLiquidity(
address indexed tokenA,
address indexed tokenB,
uint256 amountA,
uint256 amountB,
uint256 uniAmount,
uint256 getId,
uint256[] setId
);
event LogBuy(
address indexed buyToken,
address indexed sellToken,
uint256 buyAmt,
uint256 sellAmt,
uint256 getId,
uint256 setId
);
event LogSell(
address indexed buyToken,
address indexed sellToken,
uint256 buyAmt,
uint256 sellAmt,
uint256 getId,
uint256 setId
);
}

View File

@ -0,0 +1,185 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.7.0;
import {TokenInterface} from "../../common/interfaces.sol";
import {DSMath} from "../../common/math.sol";
import {Basic} from "../../common/basic.sol";
import {ISushiSwapRouter, ISushiSwapFactory} from "./interface.sol";
abstract contract Helpers is DSMath, Basic {
/**
* @dev ISushiSwapRouter
*/
ISushiSwapRouter internal constant router =
ISushiSwapRouter(0x1b02dA8Cb0d097eB8D57A175b88c7D8b47997506);
function getExpectedBuyAmt(address[] memory paths, uint256 sellAmt)
internal
view
returns (uint256 buyAmt)
{
uint256[] memory amts = router.getAmountsOut(sellAmt, paths);
buyAmt = amts[1];
}
function getExpectedSellAmt(address[] memory paths, uint256 buyAmt)
internal
view
returns (uint256 sellAmt)
{
uint256[] memory amts = router.getAmountsIn(buyAmt, paths);
sellAmt = amts[0];
}
function checkPair(address[] memory paths) internal view {
address pair = ISushiSwapFactory(router.factory()).getPair(
paths[0],
paths[1]
);
require(pair != address(0), "No-exchange-address");
}
function getPaths(address buyAddr, address sellAddr)
internal
pure
returns (address[] memory paths)
{
paths = new address[](2);
paths[0] = address(sellAddr);
paths[1] = address(buyAddr);
}
function getMinAmount(
TokenInterface token,
uint256 amt,
uint256 slippage
) internal view returns (uint256 minAmt) {
uint256 _amt18 = convertTo18(token.decimals(), amt);
minAmt = wmul(_amt18, sub(WAD, slippage));
minAmt = convert18ToDec(token.decimals(), minAmt);
}
function _addLiquidity(
address tokenA,
address tokenB,
uint256 _amt,
uint256 unitAmt,
uint256 slippage
)
internal
returns (
uint256 _amtA,
uint256 _amtB,
uint256 _liquidity
)
{
(TokenInterface _tokenA, TokenInterface _tokenB) = changeAvaxAddress(
tokenA,
tokenB
);
_amtA = _amt == uint256(-1)
? getTokenBal(TokenInterface(tokenA))
: _amt;
_amtB = convert18ToDec(
_tokenB.decimals(),
wmul(unitAmt, convertTo18(_tokenA.decimals(), _amtA))
);
bool isAvax = address(_tokenA) == wavaxAddr;
convertAvaxToWavax(isAvax, _tokenA, _amtA);
isAvax = address(_tokenB) == wavaxAddr;
convertAvaxToWavax(isAvax, _tokenB, _amtB);
approve(_tokenA, address(router), _amtA);
approve(_tokenB, address(router), _amtB);
uint256 minAmtA = getMinAmount(_tokenA, _amtA, slippage);
uint256 minAmtB = getMinAmount(_tokenB, _amtB, slippage);
(_amtA, _amtB, _liquidity) = router.addLiquidity(
address(_tokenA),
address(_tokenB),
_amtA,
_amtB,
minAmtA,
minAmtB,
address(this),
block.timestamp + 1
);
}
function _removeLiquidity(
address tokenA,
address tokenB,
uint256 _amt,
uint256 unitAmtA,
uint256 unitAmtB
)
internal
returns (
uint256 _amtA,
uint256 _amtB,
uint256 _uniAmt
)
{
TokenInterface _tokenA;
TokenInterface _tokenB;
(_tokenA, _tokenB, _uniAmt) = _getRemoveLiquidityData(
tokenA,
tokenB,
_amt
);
{
uint256 minAmtA = convert18ToDec(
_tokenA.decimals(),
wmul(unitAmtA, _uniAmt)
);
uint256 minAmtB = convert18ToDec(
_tokenB.decimals(),
wmul(unitAmtB, _uniAmt)
);
(_amtA, _amtB) = router.removeLiquidity(
address(_tokenA),
address(_tokenB),
_uniAmt,
minAmtA,
minAmtB,
address(this),
block.timestamp + 1
);
}
bool isAvax = address(_tokenA) == wavaxAddr;
convertWavaxToAvax(isAvax, _tokenA, _amtA);
isAvax = address(_tokenB) == wavaxAddr;
convertWavaxToAvax(isAvax, _tokenB, _amtB);
}
function _getRemoveLiquidityData(
address tokenA,
address tokenB,
uint256 _amt
)
internal
returns (
TokenInterface _tokenA,
TokenInterface _tokenB,
uint256 _uniAmt
)
{
(_tokenA, _tokenB) = changeAvaxAddress(tokenA, tokenB);
address exchangeAddr = ISushiSwapFactory(router.factory()).getPair(
address(_tokenA),
address(_tokenB)
);
require(exchangeAddr != address(0), "pair-not-found.");
TokenInterface uniToken = TokenInterface(exchangeAddr);
_uniAmt = _amt == uint256(-1)
? uniToken.balanceOf(address(this))
: _amt;
approve(uniToken, address(router), _uniAmt);
}
}

View File

@ -0,0 +1,58 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.7.0;
interface ISushiSwapRouter {
function factory() external pure returns (address);
function WETH() external pure returns (address);
function addLiquidity(
address tokenA,
address tokenB,
uint amountADesired,
uint amountBDesired,
uint amountAMin,
uint amountBMin,
address to,
uint deadline
) external returns (uint amountA, uint amountB, uint liquidity);
function removeLiquidity(
address tokenA,
address tokenB,
uint liquidity,
uint amountAMin,
uint amountBMin,
address to,
uint deadline
) external returns (uint amountA, uint amountB);
function swapExactTokensForTokens(
uint amountIn,
uint amountOutMin,
address[] calldata path,
address to,
uint deadline
) external returns (uint[] memory amounts);
function swapTokensForExactTokens(
uint amountOut,
uint amountInMax,
address[] calldata path,
address to,
uint deadline
) external returns (uint[] memory amounts);
function quote(uint amountA, uint reserveA, uint reserveB) external pure returns (uint amountB);
function getAmountOut(uint amountIn, uint reserveIn, uint reserveOut) external pure returns (uint amountOut);
function getAmountIn(uint amountOut, uint reserveIn, uint reserveOut) external pure returns (uint amountIn);
function getAmountsOut(uint amountIn, address[] calldata path) external view returns (uint[] memory amounts);
function getAmountsIn(uint amountOut, address[] calldata path) external view returns (uint[] memory amounts);
}
interface ISushiSwapFactory {
function getPair(address tokenA, address tokenB) external view returns (address pair);
function allPairs(uint) external view returns (address pair);
function allPairsLength() external view returns (uint);
function feeTo() external view returns (address);
function feeToSetter() external view returns (address);
function createPair(address tokenA, address tokenB) external returns (address pair);
}

View File

@ -0,0 +1,197 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.7.0;
/**
* @title SushiSwap.
* @dev Decentralized Exchange.
*/
import { TokenInterface } from "../../common/interfaces.sol";
import { Helpers } from "./helpers.sol";
import { Events } from "./events.sol";
abstract contract SushipswapResolver is Helpers, Events {
/**
* @dev Deposit Liquidity.
* @notice Deposit Liquidity to a SushiSwap pool.
* @param tokenA The address of token A.(For Avax: 0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE)
* @param tokenB The address of token B.(For Avax: 0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE)
* @param amtA The amount of A tokens to deposit.
* @param unitAmt The unit amount of of amtB/amtA with slippage.
* @param slippage Slippage amount.
* @param getId ID to retrieve amtA.
* @param setId ID stores the amount of pools tokens received.
*/
function deposit(
address tokenA,
address tokenB,
uint256 amtA,
uint256 unitAmt,
uint256 slippage,
uint256 getId,
uint256 setId
) external payable returns (string memory _eventName, bytes memory _eventParam) {
uint _amt = getUint(getId, amtA);
(uint _amtA, uint _amtB, uint _uniAmt) = _addLiquidity(
tokenA,
tokenB,
_amt,
unitAmt,
slippage
);
setUint(setId, _uniAmt);
_eventName = "LogDepositLiquidity(address,address,uint256,uint256,uint256,uint256,uint256)";
_eventParam = abi.encode(tokenA, tokenB, _amtA, _amtB, _uniAmt, getId, setId);
}
/**
* @dev Withdraw Liquidity.
* @notice Withdraw Liquidity from a SushiSwap pool.
* @param tokenA The address of token A.(For Avax: 0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE)
* @param tokenB The address of token B.(For Avax: 0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE)
* @param uniAmt The amount of pool tokens to withdraw.
* @param unitAmtA The unit amount of amtA/uniAmt with slippage.
* @param unitAmtB The unit amount of amtB/uniAmt with slippage.
* @param getId ID to retrieve uniAmt.
* @param setIds Array of IDs to store the amount tokens received.
*/
function withdraw(
address tokenA,
address tokenB,
uint256 uniAmt,
uint256 unitAmtA,
uint256 unitAmtB,
uint256 getId,
uint256[] calldata setIds
) external payable returns (string memory _eventName, bytes memory _eventParam) {
uint _amt = getUint(getId, uniAmt);
(uint _amtA, uint _amtB, uint _uniAmt) = _removeLiquidity(
tokenA,
tokenB,
_amt,
unitAmtA,
unitAmtB
);
setUint(setIds[0], _amtA);
setUint(setIds[1], _amtB);
_eventName = "LogWithdrawLiquidity(address,address,uint256,uint256,uint256,uint256,uint256[])";
_eventParam = abi.encode(tokenA, tokenB, _amtA, _amtB, _uniAmt, getId, setIds);
}
/**
* @dev Buy Avax/ERC20_Token.
* @notice Buy a token using a SushiSwap
* @param buyAddr The address of the token to buy.(For Avax: 0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE)
* @param sellAddr The address of the token to sell.(For Avax: 0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE)
* @param buyAmt The amount of tokens to buy.
* @param unitAmt The unit amount of sellAmt/buyAmt with slippage.
* @param getId ID to retrieve buyAmt.
* @param setId ID to store the amount of tokens sold.
*/
function buy(
address buyAddr,
address sellAddr,
uint256 buyAmt,
uint256 unitAmt,
uint256 getId,
uint256 setId
) external payable returns (string memory _eventName, bytes memory _eventParam) {
uint _buyAmt = getUint(getId, buyAmt);
(TokenInterface _buyAddr, TokenInterface _sellAddr) = changeAvaxAddress(buyAddr, sellAddr);
address[] memory paths = getPaths(address(_buyAddr), address(_sellAddr));
uint _slippageAmt = convert18ToDec(_sellAddr.decimals(),
wmul(unitAmt, convertTo18(_buyAddr.decimals(), _buyAmt))
);
checkPair(paths);
uint _expectedAmt = getExpectedSellAmt(paths, _buyAmt);
require(_slippageAmt >= _expectedAmt, "Too much slippage");
bool isAvax = address(_sellAddr) == wavaxAddr;
convertAvaxToWavax(isAvax, _sellAddr, _expectedAmt);
approve(_sellAddr, address(router), _expectedAmt);
uint _sellAmt = router.swapTokensForExactTokens(
_buyAmt,
_expectedAmt,
paths,
address(this),
block.timestamp + 1
)[0];
isAvax = address(_buyAddr) == wavaxAddr;
convertWavaxToAvax(isAvax, _buyAddr, _buyAmt);
setUint(setId, _sellAmt);
_eventName = "LogBuy(address,address,uint256,uint256,uint256,uint256)";
_eventParam = abi.encode(buyAddr, sellAddr, _buyAmt, _sellAmt, getId, setId);
}
/**
* @dev Sell Avax/ERC20_Token.
* @notice Sell a token using a SushiSwap
* @param buyAddr The address of the token to buy.(For Avax: 0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE)
* @param sellAddr The address of the token to sell.(For Avax: 0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE)
* @param sellAmt The amount of the token to sell.
* @param unitAmt The unit amount of buyAmt/sellAmt with slippage.
* @param getId ID to retrieve sellAmt.
* @param setId ID stores the amount of token brought.
*/
function sell(
address buyAddr,
address sellAddr,
uint256 sellAmt,
uint256 unitAmt,
uint256 getId,
uint256 setId
) external payable returns (string memory _eventName, bytes memory _eventParam) {
uint _sellAmt = getUint(getId, sellAmt);
(TokenInterface _buyAddr, TokenInterface _sellAddr) = changeAvaxAddress(buyAddr, sellAddr);
address[] memory paths = getPaths(address(_buyAddr), address(_sellAddr));
if (_sellAmt == uint(-1)) {
_sellAmt = sellAddr == avaxAddr ?
address(this).balance :
_sellAddr.balanceOf(address(this));
}
uint _slippageAmt = convert18ToDec(_buyAddr.decimals(),
wmul(unitAmt, convertTo18(_sellAddr.decimals(), _sellAmt))
);
checkPair(paths);
uint _expectedAmt = getExpectedBuyAmt(paths, _sellAmt);
require(_slippageAmt <= _expectedAmt, "Too much slippage");
bool isAvax = address(_sellAddr) == wavaxAddr;
convertAvaxToWavax(isAvax, _sellAddr, _sellAmt);
approve(_sellAddr, address(router), _sellAmt);
uint _buyAmt = router.swapExactTokensForTokens(
_sellAmt,
_expectedAmt,
paths,
address(this),
block.timestamp + 1
)[1];
isAvax = address(_buyAddr) == wavaxAddr;
convertWavaxToAvax(isAvax, _buyAddr, _buyAmt);
setUint(setId, _buyAmt);
_eventName = "LogSell(address,address,uint256,uint256,uint256,uint256)";
_eventParam = abi.encode(buyAddr, sellAddr, _buyAmt, _sellAmt, getId, setId);
}
}
contract ConnectV2SushiswapAvalanche is SushipswapResolver {
string public constant name = "Sushiswap-v1.1";
}