Merge pull request #16 from Instadapp/feat/paraswap-v6

Paraswap v6.2 Connector
This commit is contained in:
Shriya Tyagi 2024-06-29 00:33:06 +05:30 committed by GitHub
commit 874e46d5c2
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
40 changed files with 1961 additions and 2 deletions

View File

@ -0,0 +1,91 @@
//SPDX-License-Identifier: MIT
pragma solidity ^0.8.2;
import {TokenInterface} from "./interfaces.sol";
import {Stores} from "./stores.sol";
import {DSMath} from "./math.sol";
abstract contract Basic is DSMath, Stores {
function convert18ToDec(
uint _dec,
uint256 _amt
) internal pure returns (uint256 amt) {
amt = (_amt / 10 ** (18 - _dec));
}
function convertTo18(
uint _dec,
uint256 _amt
) internal pure returns (uint256 amt) {
amt = mul(_amt, 10 ** (18 - _dec));
}
function getTokenBal(
TokenInterface token
) internal view returns (uint _amt) {
_amt = address(token) == ethAddr
? address(this).balance
: token.balanceOf(address(this));
}
function getTokensDec(
TokenInterface buyAddr,
TokenInterface sellAddr
) internal view returns (uint buyDec, uint sellDec) {
buyDec = address(buyAddr) == ethAddr ? 18 : buyAddr.decimals();
sellDec = address(sellAddr) == ethAddr ? 18 : sellAddr.decimals();
}
function encodeEvent(
string memory eventName,
bytes memory eventParam
) internal pure returns (bytes memory) {
return abi.encode(eventName, eventParam);
}
function approve(
TokenInterface token,
address spender,
uint256 amount
) internal {
try token.approve(spender, amount) {} catch {
token.approve(spender, 0);
token.approve(spender, amount);
}
}
function changeEthAddress(
address buy,
address sell
) internal pure returns (TokenInterface _buy, TokenInterface _sell) {
_buy = buy == ethAddr ? TokenInterface(wethAddr) : TokenInterface(buy);
_sell = sell == ethAddr
? TokenInterface(wethAddr)
: TokenInterface(sell);
}
function changeEthAddrToWethAddr(
address token
) internal pure returns (address tokenAddr) {
tokenAddr = token == ethAddr ? wethAddr : token;
}
function convertEthToWeth(
bool isEth,
TokenInterface token,
uint amount
) internal {
if (isEth) token.deposit{value: amount}();
}
function convertWethToEth(
bool isEth,
TokenInterface token,
uint amount
) internal {
if (isEth) {
approve(token, address(token), amount);
token.withdraw(amount);
}
}
}

View File

@ -0,0 +1,48 @@
//SPDX-License-Identifier: MIT
pragma solidity ^0.8.2;
interface TokenInterface {
function approve(address, uint256) external;
function transfer(address, uint) external;
function transferFrom(address, address, uint) external;
function deposit() external payable;
function withdraw(uint) external;
function balanceOf(address) external view returns (uint);
function decimals() external view returns (uint);
function totalSupply() external view returns (uint);
function allowance(
address owner,
address spender
) external view returns (uint256);
}
interface MemoryInterface {
function getUint(uint id) external returns (uint num);
function setUint(uint id, uint val) external;
}
interface InstaMapping {
function cTokenMapping(address) external view returns (address);
function gemJoinMapping(bytes32) external view returns (address);
}
interface AccountInterface {
function enable(address) external;
function disable(address) external;
function isAuth(address) external view returns (bool);
function cast(
string[] calldata _targetNames,
bytes[] calldata _datas,
address _origin
) external payable returns (bytes32[] memory responses);
}
interface ListInterface {
function accountID(address) external returns (uint64);
}
interface InstaConnectors {
function isConnectors(
string[] calldata
) external returns (bool, address[] memory);
}

View File

@ -0,0 +1,54 @@
//SPDX-License-Identifier: MIT
pragma solidity ^0.8.2;
import {SafeMath} from "@openzeppelin/contracts/utils/math/SafeMath.sol";
contract DSMath {
uint constant WAD = 10 ** 18;
uint constant RAY = 10 ** 27;
function add(uint x, uint y) internal pure returns (uint z) {
z = SafeMath.add(x, y);
}
function sub(uint x, uint y) internal pure virtual returns (uint z) {
z = SafeMath.sub(x, y);
}
function mul(uint x, uint y) internal pure returns (uint z) {
z = SafeMath.mul(x, y);
}
function div(uint x, uint y) internal pure returns (uint z) {
z = SafeMath.div(x, y);
}
function wmul(uint x, uint y) internal pure returns (uint z) {
z = SafeMath.add(SafeMath.mul(x, y), WAD / 2) / WAD;
}
function wdiv(uint x, uint y) internal pure returns (uint z) {
z = SafeMath.add(SafeMath.mul(x, WAD), y / 2) / y;
}
function rdiv(uint x, uint y) internal pure returns (uint z) {
z = SafeMath.add(SafeMath.mul(x, RAY), y / 2) / y;
}
function rmul(uint x, uint y) internal pure returns (uint z) {
z = SafeMath.add(SafeMath.mul(x, y), RAY / 2) / RAY;
}
function toInt(uint x) internal pure returns (int y) {
y = int(x);
require(y >= 0, "int-overflow");
}
function toUint(int256 x) internal pure returns (uint256) {
require(x >= 0, "int-overflow");
return uint256(x);
}
function toRad(uint wad) internal pure returns (uint rad) {
rad = mul(wad, 10 ** 27);
}
}

View File

@ -0,0 +1,50 @@
//SPDX-License-Identifier: MIT
pragma solidity ^0.8.2;
import {MemoryInterface, InstaMapping, ListInterface, InstaConnectors} from "./interfaces.sol";
abstract contract Stores {
/**
* @dev Return ethereum address
*/
address internal constant ethAddr =
0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE;
/**
* @dev Return Wrapped ETH address
*/
address internal constant wethAddr =
0x82aF49447D8a07e3bd95BD0d56f35241523fBab1;
/**
* @dev Return memory variable address
*/
MemoryInterface internal constant instaMemory =
MemoryInterface(0xc109f7Ef06152c3a63dc7254fD861E612d3Ac571);
/**
* @dev Return InstaList Address
*/
ListInterface internal constant instaList =
ListInterface(0x3565F6057b7fFE36984779A507fC87b31EFb0f09);
/**
* @dev Return connectors registry address
*/
InstaConnectors internal constant instaConnectors =
InstaConnectors(0x67fCE99Dd6d8d659eea2a1ac1b8881c57eb6592B);
/**
* @dev Get Uint value from InstaMemory Contract.
*/
function getUint(uint getId, uint val) internal returns (uint returnVal) {
returnVal = getId == 0 ? val : instaMemory.getUint(getId);
}
/**
* @dev Set Uint value in InstaMemory Contract.
*/
function setUint(uint setId, uint val) internal virtual {
if (setId != 0) instaMemory.setUint(setId, val);
}
}

View File

@ -0,0 +1,10 @@
//SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
contract Events {
event LogFlashBorrow(address token, uint256 tokenAmt);
event LogFlashPayback(address token, uint256 tokenAmt);
event LogFlashMultiBorrow(address[] token, uint256[] tokenAmts);
event LogFlashMultiPayback(address[] token, uint256[] tokenAmts);
}

View File

@ -0,0 +1,11 @@
//SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
interface InstaFlashV5Interface {
function flashLoan(address[] memory tokens, uint256[] memory amts, uint route, bytes memory data, bytes memory extraData) external;
}
interface AccountInterface {
function enable(address) external;
function disable(address) external;
}

View File

@ -0,0 +1,138 @@
//SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
/**
* @title Instapool.
* @dev Inbuilt Flash Loan in DSA
*/
import { SafeERC20 } from "@openzeppelin/contracts/token/ERC20/utils/SafeERC20.sol";
import { Address } from "@openzeppelin/contracts/utils/Address.sol";
import { IERC20 } from "@openzeppelin/contracts/token/ERC20/IERC20.sol";
import { TokenInterface } from "../../common/interfaces.sol";
import { AccountInterface } from "./interfaces.sol";
import { Stores } from "../../common/stores.sol";
import { Variables } from "./variables.sol";
import { Events } from "./events.sol";
contract LiquidityResolver is Stores, Variables, Events {
using SafeERC20 for IERC20;
/**
* @dev Borrow Flashloan and Cast spells.
* @notice Borrow Flashloan and Cast spells.
* @param token Token Address.
* @param amt Token Amount.
* @param route Flashloan source route.
* @param data targets & data for cast.
* @param extraData to be kept bytes(0) in most cases. Can be useful to decide data for some particular routes
*/
function flashBorrowAndCast(
address token,
uint amt,
uint route,
bytes memory data,
bytes memory extraData
) external payable returns (string memory _eventName, bytes memory _eventParam) {
AccountInterface(address(this)).enable(address(instaPool));
(string[] memory _targets, bytes[] memory callDatas) = abi.decode(data, (string[], bytes[]));
bytes memory callData_ = abi.encodeWithSignature("cast(string[],bytes[],address)", _targets, callDatas, address(instaPool));
address[] memory tokens_ = new address[](1);
tokens_[0] = token;
uint[] memory amts_ = new uint[](1);
amts_[0] = amt;
instaPool.flashLoan(tokens_, amts_, route, callData_, extraData);
AccountInterface(address(this)).disable(address(instaPool));
_eventName = "LogFlashBorrow(address,uint256)";
_eventParam = abi.encode(token, amt);
}
/**
* @dev Return token to InstaPool.
* @notice Return token to InstaPool.
* @param token Token Address.
* @param amt Token Amount.
* @param getId Get token amount at this ID from `InstaMemory` Contract.
* @param setId Set token amount at this ID in `InstaMemory` Contract.
*/
function flashPayback(
address token,
uint amt,
uint getId,
uint setId
) external payable returns (string memory _eventName, bytes memory _eventParam) {
uint _amt = getUint(getId, amt);
IERC20 tokenContract = IERC20(token);
tokenContract.safeTransfer(address(instaPool), _amt);
setUint(setId, _amt);
_eventName = "LogFlashPayback(address,uint256)";
_eventParam = abi.encode(token, amt);
}
/**
* @dev Borrow multi-tokens Flashloan and Cast spells.
* @notice Borrow multi-tokens Flashloan and Cast spells.
* @param tokens_ Array of Token Addresses.
* @param amts_ Array of Token Amounts.
* @param route Flashloan source route.
* @param data targets & data for cast.
* @param extraData to be kept bytes(0) in most cases. Can be useful to decide data for some particular routes
*/
function flashMultiBorrowAndCast(
address[] memory tokens_,
uint[] memory amts_,
uint route,
bytes memory data,
bytes memory extraData
) external payable returns (string memory _eventName, bytes memory _eventParam) {
AccountInterface(address(this)).enable(address(instaPool));
(string[] memory _targets, bytes[] memory callDatas) = abi.decode(data, (string[], bytes[]));
bytes memory callData_ = abi.encodeWithSignature("cast(string[],bytes[],address)", _targets, callDatas, address(instaPool));
instaPool.flashLoan(tokens_, amts_, route, callData_, extraData);
AccountInterface(address(this)).disable(address(instaPool));
_eventName = "LogFlashMultiBorrow(address[],uint256[])";
_eventParam = abi.encode(tokens_, amts_);
}
/**
* @dev Return multi-tokens to InstaPool.
* @notice Return multi-tokens to InstaPool.
* @param tokens_ Array of Token Addresses.
* @param amts_ Array of Token Amounts.
* @param getIds Array of getId token amounts.
* @param setIds Array of setId token amounts.
*/
function flashMultiPayback(
address[] memory tokens_,
uint[] memory amts_,
uint[] memory getIds,
uint[] memory setIds
) external payable returns (string memory _eventName, bytes memory _eventParam) {
for (uint i = 0; i < tokens_.length; i++) {
amts_[i] = getUint(getIds[i], amts_[i]);
IERC20(tokens_[i]).safeTransfer(address(instaPool), amts_[i]);
setUint(setIds[i], amts_[i]);
}
_eventName = "LogFlashMultiPayback(address[],uint256[])";
_eventParam = abi.encode(tokens_, amts_);
}
}
contract ConnectV2InstaPoolV5 is LiquidityResolver {
string public name = "Instapool-v5";
}

View File

@ -0,0 +1,14 @@
//SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
import { InstaFlashV5Interface } from "./interfaces.sol";
contract Variables {
/**
* @dev Instapool contract proxy
*/
InstaFlashV5Interface public constant instaPool =
InstaFlashV5Interface(0x352423e2fA5D5c99343d371C9e3bC56C87723Cc7);
}

View File

@ -0,0 +1,13 @@
//SPDX-License-Identifier: MIT
pragma solidity 0.8.19;
contract Events {
event LogSwap(
address buyToken,
address sellToken,
uint256 buyAmt,
uint256 sellAmt,
uint256 setId
);
}

View File

@ -0,0 +1,68 @@
//SPDX-License-Identifier: MIT
pragma solidity 0.8.19;
import {DSMath} from "../../common/math.sol";
import {Basic} from "../../common/basic.sol";
import {TokenInterface} from "../../common/interfaces.sol";
abstract contract Helpers is DSMath, Basic {
struct SwapData {
TokenInterface sellToken;
TokenInterface buyToken;
uint256 _sellAmt;
uint256 _buyAmt;
uint256 unitAmt;
bytes callData;
}
address internal constant AUGUSTUS_V6 =
0x6A000F20005980200259B80c5102003040001068;
function _swapHelper(
SwapData memory swapData,
uint256 wethAmt
) internal returns (uint256 buyAmt) {
TokenInterface buyToken = swapData.buyToken;
(uint256 _buyDec, uint256 _sellDec) = getTokensDec(
buyToken,
swapData.sellToken
);
uint256 _sellAmt18 = convertTo18(_sellDec, swapData._sellAmt);
uint256 _slippageAmt = convert18ToDec(
_buyDec,
wmul(swapData.unitAmt, _sellAmt18)
);
uint256 initalBal = getTokenBal(buyToken);
(bool success, ) = AUGUSTUS_V6.call{value: wethAmt}(swapData.callData);
if (!success) revert("paraswap-failed");
uint256 finalBal = getTokenBal(buyToken);
buyAmt = sub(finalBal, initalBal);
require(_slippageAmt <= buyAmt, "Too much slippage");
}
function _swap(
SwapData memory swapData,
uint256 setId
) internal returns (SwapData memory) {
TokenInterface _sellAddr = swapData.sellToken;
uint256 ethAmt;
if (address(_sellAddr) == ethAddr) {
ethAmt = swapData._sellAmt;
} else {
approve(TokenInterface(_sellAddr), AUGUSTUS_V6, swapData._sellAmt);
}
swapData._buyAmt = _swapHelper(swapData, ethAmt);
setUint(setId, swapData._buyAmt);
return swapData;
}
}

View File

@ -0,0 +1,55 @@
//SPDX-License-Identifier: MIT
pragma solidity 0.8.19;
import {TokenInterface} from "../../common/interfaces.sol";
import {Stores} from "../../common/stores.sol";
import {Helpers} from "./helpers.sol";
contract ParaswapResolver is Helpers {
/**
* @dev Sell ETH/ERC20_Token using ParaSwap.
* @notice Swap tokens from exchanges like kyber, 0x etc, with calculation done off-chain.
* @param buyAddr The address of the token to buy.(For ETH: 0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE)
* @param sellAddr The address of the token to sell.(For ETH: 0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE)
* @param sellAmt The amount of the token to sell.
* @param unitAmt The amount of buyAmt/sellAmt with slippage.
* @param callData Data from paraswap API.
* @param setId ID stores the amount of token brought.
*/
function swap(
address buyAddr,
address sellAddr,
uint256 sellAmt,
uint256 unitAmt,
bytes calldata callData,
uint256 setId
)
external
payable
returns (string memory _eventName, bytes memory _eventParam)
{
Helpers.SwapData memory swapData = Helpers.SwapData({
buyToken: TokenInterface(buyAddr),
sellToken: TokenInterface(sellAddr),
unitAmt: unitAmt,
callData: callData,
_sellAmt: sellAmt,
_buyAmt: 0
});
swapData = _swap(swapData, setId);
_eventName = "LogSwap(address,address,uint256,uint256,uint256)";
_eventParam = abi.encode(
address(swapData.buyToken),
address(swapData.sellToken),
swapData._buyAmt,
swapData._sellAmt,
setId
);
}
}
contract ConnectV2ParaswapV6Arbitrum is ParaswapResolver {
string public name = "Paraswap-v6.2";
}

View File

@ -0,0 +1,91 @@
//SPDX-License-Identifier: MIT
pragma solidity ^0.8.2;
import {TokenInterface} from "./interfaces.sol";
import {Stores} from "./stores.sol";
import {DSMath} from "./math.sol";
abstract contract Basic is DSMath, Stores {
function convert18ToDec(
uint _dec,
uint256 _amt
) internal pure returns (uint256 amt) {
amt = (_amt / 10 ** (18 - _dec));
}
function convertTo18(
uint _dec,
uint256 _amt
) internal pure returns (uint256 amt) {
amt = mul(_amt, 10 ** (18 - _dec));
}
function getTokenBal(
TokenInterface token
) internal view returns (uint _amt) {
_amt = address(token) == avaxAddr
? address(this).balance
: token.balanceOf(address(this));
}
function getTokensDec(
TokenInterface buyAddr,
TokenInterface sellAddr
) internal view returns (uint buyDec, uint sellDec) {
buyDec = address(buyAddr) == avaxAddr ? 18 : buyAddr.decimals();
sellDec = address(sellAddr) == avaxAddr ? 18 : sellAddr.decimals();
}
function encodeEvent(
string memory eventName,
bytes memory eventParam
) internal pure returns (bytes memory) {
return abi.encode(eventName, eventParam);
}
function approve(
TokenInterface token,
address spender,
uint256 amount
) internal {
try token.approve(spender, amount) {} catch {
token.approve(spender, 0);
token.approve(spender, amount);
}
}
function changeEthAddress(
address buy,
address sell
) internal pure returns (TokenInterface _buy, TokenInterface _sell) {
_buy = buy == avaxAddr ? TokenInterface(wavaxAddr) : TokenInterface(buy);
_sell = sell == avaxAddr
? TokenInterface(wavaxAddr)
: TokenInterface(sell);
}
function changeEthAddrToWethAddr(
address token
) internal pure returns (address tokenAddr) {
tokenAddr = token == avaxAddr ? wavaxAddr : token;
}
function convertEthToWeth(
bool isEth,
TokenInterface token,
uint amount
) internal {
if (isEth) token.deposit{value: amount}();
}
function convertWethToEth(
bool isEth,
TokenInterface token,
uint amount
) internal {
if (isEth) {
approve(token, address(token), amount);
token.withdraw(amount);
}
}
}

View File

@ -0,0 +1,48 @@
//SPDX-License-Identifier: MIT
pragma solidity ^0.8.2;
interface TokenInterface {
function approve(address, uint256) external;
function transfer(address, uint) external;
function transferFrom(address, address, uint) external;
function deposit() external payable;
function withdraw(uint) external;
function balanceOf(address) external view returns (uint);
function decimals() external view returns (uint);
function totalSupply() external view returns (uint);
function allowance(
address owner,
address spender
) external view returns (uint256);
}
interface MemoryInterface {
function getUint(uint id) external returns (uint num);
function setUint(uint id, uint val) external;
}
interface InstaMapping {
function cTokenMapping(address) external view returns (address);
function gemJoinMapping(bytes32) external view returns (address);
}
interface AccountInterface {
function enable(address) external;
function disable(address) external;
function isAuth(address) external view returns (bool);
function cast(
string[] calldata _targetNames,
bytes[] calldata _datas,
address _origin
) external payable returns (bytes32[] memory responses);
}
interface ListInterface {
function accountID(address) external returns (uint64);
}
interface InstaConnectors {
function isConnectors(
string[] calldata
) external returns (bool, address[] memory);
}

View File

@ -0,0 +1,55 @@
//SPDX-License-Identifier: MIT
pragma solidity ^0.8.2;
import {SafeMath} from "@openzeppelin/contracts/utils/math/SafeMath.sol";
contract DSMath {
uint constant WAD = 10 ** 18;
uint constant RAY = 10 ** 27;
function add(uint x, uint y) internal pure returns (uint z) {
z = SafeMath.add(x, y);
}
function sub(uint x, uint y) internal pure virtual returns (uint z) {
z = SafeMath.sub(x, y);
}
function mul(uint x, uint y) internal pure returns (uint z) {
z = SafeMath.mul(x, y);
}
function div(uint x, uint y) internal pure returns (uint z) {
z = SafeMath.div(x, y);
}
function wmul(uint x, uint y) internal pure returns (uint z) {
z = SafeMath.add(SafeMath.mul(x, y), WAD / 2) / WAD;
}
function wdiv(uint x, uint y) internal pure returns (uint z) {
z = SafeMath.add(SafeMath.mul(x, WAD), y / 2) / y;
}
function rdiv(uint x, uint y) internal pure returns (uint z) {
z = SafeMath.add(SafeMath.mul(x, RAY), y / 2) / y;
}
function rmul(uint x, uint y) internal pure returns (uint z) {
z = SafeMath.add(SafeMath.mul(x, y), RAY / 2) / RAY;
}
function toInt(uint x) internal pure returns (int y) {
y = int(x);
require(y >= 0, "int-overflow");
}
function toUint(int256 x) internal pure returns (uint256) {
require(x >= 0, "int-overflow");
return uint256(x);
}
function toRad(uint wad) internal pure returns (uint rad) {
rad = mul(wad, 10 ** 27);
}
}

View File

@ -0,0 +1,50 @@
//SPDX-License-Identifier: MIT
pragma solidity ^0.8.2;
import {MemoryInterface, InstaMapping, ListInterface, InstaConnectors} from "./interfaces.sol";
abstract contract Stores {
/**
* @dev Return avax address
*/
address internal constant avaxAddr =
0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE;
/**
* @dev Return Wrapped AVAX address
*/
address internal constant wavaxAddr =
0xB31f66AA3C1e785363F0875A1B74E27b85FD66c7;
/**
* @dev Return memory variable address
*/
MemoryInterface internal constant instaMemory =
MemoryInterface(0x3254Ce8f5b1c82431B8f21Df01918342215825C2);
/**
* @dev Return InstaList address
*/
ListInterface internal constant instaList =
ListInterface(0x9926955e0Dd681Dc303370C52f4Ad0a4dd061687);
/**
* @dev Return connectors registry address
*/
InstaConnectors internal constant instaConnectors =
InstaConnectors(0x127d8cD0E2b2E0366D522DeA53A787bfE9002C14);
/**
* @dev Get Uint value from InstaMemory Contract.
*/
function getUint(uint getId, uint val) internal returns (uint returnVal) {
returnVal = getId == 0 ? val : instaMemory.getUint(getId);
}
/**
* @dev Set Uint value in InstaMemory Contract.
*/
function setUint(uint setId, uint val) internal virtual {
if (setId != 0) instaMemory.setUint(setId, val);
}
}

View File

@ -0,0 +1,12 @@
//SPDX-License-Identifier: MIT
pragma solidity 0.8.19;
contract Events {
event LogSwap(
address buyToken,
address sellToken,
uint256 buyAmt,
uint256 sellAmt,
uint256 setId
);
}

View File

@ -0,0 +1,68 @@
//SPDX-License-Identifier: MIT
pragma solidity 0.8.19;
import {DSMath} from "../../common/math.sol";
import {Basic} from "../../common/basic.sol";
import {TokenInterface} from "../../common/interfaces.sol";
abstract contract Helpers is DSMath, Basic {
struct SwapData {
TokenInterface sellToken;
TokenInterface buyToken;
uint256 _sellAmt;
uint256 _buyAmt;
uint256 unitAmt;
bytes callData;
}
address internal constant AUGUSTUS_V6 =
0x6A000F20005980200259B80c5102003040001068;
function _swapHelper(
SwapData memory swapData,
uint256 wavaxAmt
) internal returns (uint256 buyAmt) {
TokenInterface buyToken = swapData.buyToken;
(uint256 _buyDec, uint256 _sellDec) = getTokensDec(
buyToken,
swapData.sellToken
);
uint256 _sellAmt18 = convertTo18(_sellDec, swapData._sellAmt);
uint256 _slippageAmt = convert18ToDec(
_buyDec,
wmul(swapData.unitAmt, _sellAmt18)
);
uint256 initalBal = getTokenBal(buyToken);
(bool success, ) = AUGUSTUS_V6.call{value: wavaxAmt}(swapData.callData);
if (!success) revert("paraswap-failed");
uint256 finalBal = getTokenBal(buyToken);
buyAmt = sub(finalBal, initalBal);
require(_slippageAmt <= buyAmt, "Too much slippage");
}
function _swap(
SwapData memory swapData,
uint256 setId
) internal returns (SwapData memory) {
TokenInterface _sellAddr = swapData.sellToken;
uint256 avaxAmt;
if (address(_sellAddr) == avaxAddr) {
avaxAmt = swapData._sellAmt;
} else {
approve(TokenInterface(_sellAddr), AUGUSTUS_V6, swapData._sellAmt);
}
swapData._buyAmt = _swapHelper(swapData, avaxAmt);
setUint(setId, swapData._buyAmt);
return swapData;
}
}

View File

@ -0,0 +1,55 @@
//SPDX-License-Identifier: MIT
pragma solidity 0.8.19;
import {TokenInterface} from "../../common/interfaces.sol";
import {Stores} from "../../common/stores.sol";
import {Helpers} from "./helpers.sol";
contract ParaswapResolver is Helpers {
/**
* @dev Sell AVAX/ERC20_Token using ParaSwap.
* @notice Swap tokens from exchanges like kyber, 0x etc, with calculation done off-chain.
* @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 amount of buyAmt/sellAmt with slippage.
* @param callData Data from paraswap API.
* @param setId ID stores the amount of token brought.
*/
function swap(
address buyAddr,
address sellAddr,
uint256 sellAmt,
uint256 unitAmt,
bytes calldata callData,
uint256 setId
)
external
payable
returns (string memory _eventName, bytes memory _eventParam)
{
Helpers.SwapData memory swapData = Helpers.SwapData({
buyToken: TokenInterface(buyAddr),
sellToken: TokenInterface(sellAddr),
unitAmt: unitAmt,
callData: callData,
_sellAmt: sellAmt,
_buyAmt: 0
});
swapData = _swap(swapData, setId);
_eventName = "LogSwap(address,address,uint256,uint256,uint256)";
_eventParam = abi.encode(
address(swapData.buyToken),
address(swapData.sellToken),
swapData._buyAmt,
swapData._sellAmt,
setId
);
}
}
contract ConnectV2ParaswapV6Avalanche is ParaswapResolver {
string public name = "Paraswap-v6.2";
}

View File

@ -0,0 +1,91 @@
//SPDX-License-Identifier: MIT
pragma solidity ^0.8.2;
import {TokenInterface} from "./interfaces.sol";
import {Stores} from "./stores.sol";
import {DSMath} from "./math.sol";
abstract contract Basic is DSMath, Stores {
function convert18ToDec(
uint _dec,
uint256 _amt
) internal pure returns (uint256 amt) {
amt = (_amt / 10 ** (18 - _dec));
}
function convertTo18(
uint _dec,
uint256 _amt
) internal pure returns (uint256 amt) {
amt = mul(_amt, 10 ** (18 - _dec));
}
function getTokenBal(
TokenInterface token
) internal view returns (uint _amt) {
_amt = address(token) == ethAddr
? address(this).balance
: token.balanceOf(address(this));
}
function getTokensDec(
TokenInterface buyAddr,
TokenInterface sellAddr
) internal view returns (uint buyDec, uint sellDec) {
buyDec = address(buyAddr) == ethAddr ? 18 : buyAddr.decimals();
sellDec = address(sellAddr) == ethAddr ? 18 : sellAddr.decimals();
}
function encodeEvent(
string memory eventName,
bytes memory eventParam
) internal pure returns (bytes memory) {
return abi.encode(eventName, eventParam);
}
function approve(
TokenInterface token,
address spender,
uint256 amount
) internal {
try token.approve(spender, amount) {} catch {
token.approve(spender, 0);
token.approve(spender, amount);
}
}
function changeEthAddress(
address buy,
address sell
) internal pure returns (TokenInterface _buy, TokenInterface _sell) {
_buy = buy == ethAddr ? TokenInterface(wethAddr) : TokenInterface(buy);
_sell = sell == ethAddr
? TokenInterface(wethAddr)
: TokenInterface(sell);
}
function changeEthAddrToWethAddr(
address token
) internal pure returns (address tokenAddr) {
tokenAddr = token == ethAddr ? wethAddr : token;
}
function convertEthToWeth(
bool isEth,
TokenInterface token,
uint amount
) internal {
if (isEth) token.deposit{value: amount}();
}
function convertWethToEth(
bool isEth,
TokenInterface token,
uint amount
) internal {
if (isEth) {
approve(token, address(token), amount);
token.withdraw(amount);
}
}
}

View File

@ -0,0 +1,48 @@
//SPDX-License-Identifier: MIT
pragma solidity ^0.8.2;
interface TokenInterface {
function approve(address, uint256) external;
function transfer(address, uint) external;
function transferFrom(address, address, uint) external;
function deposit() external payable;
function withdraw(uint) external;
function balanceOf(address) external view returns (uint);
function decimals() external view returns (uint);
function totalSupply() external view returns (uint);
function allowance(
address owner,
address spender
) external view returns (uint256);
}
interface MemoryInterface {
function getUint(uint id) external returns (uint num);
function setUint(uint id, uint val) external;
}
interface InstaMapping {
function cTokenMapping(address) external view returns (address);
function gemJoinMapping(bytes32) external view returns (address);
}
interface AccountInterface {
function enable(address) external;
function disable(address) external;
function isAuth(address) external view returns (bool);
function cast(
string[] calldata _targetNames,
bytes[] calldata _datas,
address _origin
) external payable returns (bytes32[] memory responses);
}
interface ListInterface {
function accountID(address) external returns (uint64);
}
interface InstaConnectors {
function isConnectors(
string[] calldata
) external returns (bool, address[] memory);
}

View File

@ -0,0 +1,55 @@
//SPDX-License-Identifier: MIT
pragma solidity ^0.8.2;
import {SafeMath} from "@openzeppelin/contracts/utils/math/SafeMath.sol";
contract DSMath {
uint constant WAD = 10 ** 18;
uint constant RAY = 10 ** 27;
function add(uint x, uint y) internal pure returns (uint z) {
z = SafeMath.add(x, y);
}
function sub(uint x, uint y) internal pure virtual returns (uint z) {
z = SafeMath.sub(x, y);
}
function mul(uint x, uint y) internal pure returns (uint z) {
z = SafeMath.mul(x, y);
}
function div(uint x, uint y) internal pure returns (uint z) {
z = SafeMath.div(x, y);
}
function wmul(uint x, uint y) internal pure returns (uint z) {
z = SafeMath.add(SafeMath.mul(x, y), WAD / 2) / WAD;
}
function wdiv(uint x, uint y) internal pure returns (uint z) {
z = SafeMath.add(SafeMath.mul(x, WAD), y / 2) / y;
}
function rdiv(uint x, uint y) internal pure returns (uint z) {
z = SafeMath.add(SafeMath.mul(x, RAY), y / 2) / y;
}
function rmul(uint x, uint y) internal pure returns (uint z) {
z = SafeMath.add(SafeMath.mul(x, y), RAY / 2) / RAY;
}
function toInt(uint x) internal pure returns (int y) {
y = int(x);
require(y >= 0, "int-overflow");
}
function toUint(int256 x) internal pure returns (uint256) {
require(x >= 0, "int-overflow");
return uint256(x);
}
function toRad(uint wad) internal pure returns (uint rad) {
rad = mul(wad, 10 ** 27);
}
}

View File

@ -0,0 +1,50 @@
//SPDX-License-Identifier: MIT
pragma solidity ^0.8.2;
import {MemoryInterface, InstaMapping, ListInterface, InstaConnectors} from "./interfaces.sol";
abstract contract Stores {
/**
* @dev Return ethereum address
*/
address internal constant ethAddr =
0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE;
/**
* @dev Return Wrapped ETH address
*/
address internal constant wethAddr =
0x4200000000000000000000000000000000000006;
/**
* @dev Return memory variable address
*/
MemoryInterface internal constant instaMemory =
MemoryInterface(0x3254Ce8f5b1c82431B8f21Df01918342215825C2);
/**
* @dev Return InstaList Address
*/
ListInterface internal constant instaList =
ListInterface(0x9926955e0Dd681Dc303370C52f4Ad0a4dd061687);
/**
* @dev Return connectors registry address
*/
InstaConnectors internal constant instaConnectors =
InstaConnectors(0x127d8cD0E2b2E0366D522DeA53A787bfE9002C14);
/**
* @dev Get Uint value from InstaMemory Contract.
*/
function getUint(uint getId, uint val) internal returns (uint returnVal) {
returnVal = getId == 0 ? val : instaMemory.getUint(getId);
}
/**
* @dev Set Uint value in InstaMemory Contract.
*/
function setUint(uint setId, uint val) internal virtual {
if (setId != 0) instaMemory.setUint(setId, val);
}
}

View File

@ -0,0 +1,12 @@
//SPDX-License-Identifier: MIT
pragma solidity 0.8.19;
contract Events {
event LogSwap(
address buyToken,
address sellToken,
uint256 buyAmt,
uint256 sellAmt,
uint256 setId
);
}

View File

@ -0,0 +1,68 @@
//SPDX-License-Identifier: MIT
pragma solidity 0.8.19;
import {DSMath} from "../../common/math.sol";
import {Basic} from "../../common/basic.sol";
import {TokenInterface} from "../../common/interfaces.sol";
abstract contract Helpers is DSMath, Basic {
struct SwapData {
TokenInterface sellToken;
TokenInterface buyToken;
uint256 _sellAmt;
uint256 _buyAmt;
uint256 unitAmt;
bytes callData;
}
address internal constant AUGUSTUS_V6 =
0x6A000F20005980200259B80c5102003040001068;
function _swapHelper(
SwapData memory swapData,
uint256 wethAmt
) internal returns (uint256 buyAmt) {
TokenInterface buyToken = swapData.buyToken;
(uint256 _buyDec, uint256 _sellDec) = getTokensDec(
buyToken,
swapData.sellToken
);
uint256 _sellAmt18 = convertTo18(_sellDec, swapData._sellAmt);
uint256 _slippageAmt = convert18ToDec(
_buyDec,
wmul(swapData.unitAmt, _sellAmt18)
);
uint256 initalBal = getTokenBal(buyToken);
(bool success, ) = AUGUSTUS_V6.call{value: wethAmt}(swapData.callData);
if (!success) revert("paraswap-failed");
uint256 finalBal = getTokenBal(buyToken);
buyAmt = sub(finalBal, initalBal);
require(_slippageAmt <= buyAmt, "Too much slippage");
}
function _swap(
SwapData memory swapData,
uint256 setId
) internal returns (SwapData memory) {
TokenInterface _sellAddr = swapData.sellToken;
uint256 ethAmt;
if (address(_sellAddr) == ethAddr) {
ethAmt = swapData._sellAmt;
} else {
approve(TokenInterface(_sellAddr), AUGUSTUS_V6, swapData._sellAmt);
}
swapData._buyAmt = _swapHelper(swapData, ethAmt);
setUint(setId, swapData._buyAmt);
return swapData;
}
}

View File

@ -0,0 +1,55 @@
//SPDX-License-Identifier: MIT
pragma solidity 0.8.19;
import {TokenInterface} from "../../common/interfaces.sol";
import {Stores} from "../../common/stores.sol";
import {Helpers} from "./helpers.sol";
contract ParaswapResolver is Helpers {
/**
* @dev Sell ETH/ERC20_Token using ParaSwap.
* @notice Swap tokens from exchanges like kyber, 0x etc, with calculation done off-chain.
* @param buyAddr The address of the token to buy.(For ETH: 0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE)
* @param sellAddr The address of the token to sell.(For ETH: 0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE)
* @param sellAmt The amount of the token to sell.
* @param unitAmt The amount of buyAmt/sellAmt with slippage.
* @param callData Data from paraswap API.
* @param setId ID stores the amount of token brought.
*/
function swap(
address buyAddr,
address sellAddr,
uint256 sellAmt,
uint256 unitAmt,
bytes calldata callData,
uint256 setId
)
external
payable
returns (string memory _eventName, bytes memory _eventParam)
{
Helpers.SwapData memory swapData = Helpers.SwapData({
buyToken: TokenInterface(buyAddr),
sellToken: TokenInterface(sellAddr),
unitAmt: unitAmt,
callData: callData,
_sellAmt: sellAmt,
_buyAmt: 0
});
swapData = _swap(swapData, setId);
_eventName = "LogSwap(address,address,uint256,uint256,uint256)";
_eventParam = abi.encode(
address(swapData.buyToken),
address(swapData.sellToken),
swapData._buyAmt,
swapData._sellAmt,
setId
);
}
}
contract ConnectV2ParaswapV6Base is ParaswapResolver {
string public name = "Paraswap-v6.2";
}

View File

@ -132,5 +132,5 @@ contract LiquidityResolver is Stores, Variables, Events {
}
contract ConnectV2InstaPoolV5 is LiquidityResolver {
string public name = "Instapool-v5";
string public name = "Instapool-v5.1";
}

View File

@ -9,6 +9,6 @@ contract Variables {
* @dev Instapool contract proxy
*/
InstaFlashV5Interface public constant instaPool =
InstaFlashV5Interface(0xAB50Dd1C57938218627Df2311ef65b4e2e84aF48);
InstaFlashV5Interface(0x352423e2fA5D5c99343d371C9e3bC56C87723Cc7);
}

View File

@ -0,0 +1,12 @@
//SPDX-License-Identifier: MIT
pragma solidity 0.8.19;
contract Events {
event LogSwap(
address buyToken,
address sellToken,
uint256 buyAmt,
uint256 sellAmt,
uint256 setId
);
}

View File

@ -0,0 +1,68 @@
//SPDX-License-Identifier: MIT
pragma solidity 0.8.19;
import {DSMath} from "../../common/math.sol";
import {Basic} from "../../common/basic.sol";
import {TokenInterface} from "../../common/interfaces.sol";
abstract contract Helpers is DSMath, Basic {
struct SwapData {
TokenInterface sellToken;
TokenInterface buyToken;
uint256 _sellAmt;
uint256 _buyAmt;
uint256 unitAmt;
bytes callData;
}
address internal constant AUGUSTUS_V6 =
0x6A000F20005980200259B80c5102003040001068;
function _swapHelper(
SwapData memory swapData,
uint256 wethAmt
) internal returns (uint256 buyAmt) {
TokenInterface buyToken = swapData.buyToken;
(uint256 _buyDec, uint256 _sellDec) = getTokensDec(
buyToken,
swapData.sellToken
);
uint256 _sellAmt18 = convertTo18(_sellDec, swapData._sellAmt);
uint256 _slippageAmt = convert18ToDec(
_buyDec,
wmul(swapData.unitAmt, _sellAmt18)
);
uint256 initalBal = getTokenBal(buyToken);
(bool success, ) = AUGUSTUS_V6.call{value: wethAmt}(swapData.callData);
if (!success) revert("paraswap-failed");
uint256 finalBal = getTokenBal(buyToken);
buyAmt = sub(finalBal, initalBal);
require(_slippageAmt <= buyAmt, "Too much slippage");
}
function _swap(
SwapData memory swapData,
uint256 setId
) internal returns (SwapData memory) {
TokenInterface _sellAddr = swapData.sellToken;
uint256 ethAmt;
if (address(_sellAddr) == ethAddr) {
ethAmt = swapData._sellAmt;
} else {
approve(TokenInterface(_sellAddr), AUGUSTUS_V6, swapData._sellAmt);
}
swapData._buyAmt = _swapHelper(swapData, ethAmt);
setUint(setId, swapData._buyAmt);
return swapData;
}
}

View File

@ -0,0 +1,55 @@
//SPDX-License-Identifier: MIT
pragma solidity 0.8.19;
import {TokenInterface} from "../../common/interfaces.sol";
import {Stores} from "../../common/stores.sol";
import {Helpers} from "./helpers.sol";
contract ParaswapResolver is Helpers {
/**
* @dev Sell ETH/ERC20_Token using ParaSwap.
* @notice Swap tokens from exchanges like kyber, 0x etc, with calculation done off-chain.
* @param buyAddr The address of the token to buy.(For ETH: 0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE)
* @param sellAddr The address of the token to sell.(For ETH: 0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE)
* @param sellAmt The amount of the token to sell.
* @param unitAmt The amount of buyAmt/sellAmt with slippage.
* @param callData Data from paraswap API.
* @param setId ID stores the amount of token brought.
*/
function swap(
address buyAddr,
address sellAddr,
uint256 sellAmt,
uint256 unitAmt,
bytes calldata callData,
uint256 setId
)
external
payable
returns (string memory _eventName, bytes memory _eventParam)
{
Helpers.SwapData memory swapData = Helpers.SwapData({
buyToken: TokenInterface(buyAddr),
sellToken: TokenInterface(sellAddr),
unitAmt: unitAmt,
callData: callData,
_sellAmt: sellAmt,
_buyAmt: 0
});
swapData = _swap(swapData, setId);
_eventName = "LogSwap(address,address,uint256,uint256,uint256)";
_eventParam = abi.encode(
address(swapData.buyToken),
address(swapData.sellToken),
swapData._buyAmt,
swapData._sellAmt,
setId
);
}
}
contract ConnectV2ParaswapV6 is ParaswapResolver {
string public name = "Paraswap-v6.2";
}

View File

@ -0,0 +1,91 @@
//SPDX-License-Identifier: MIT
pragma solidity ^0.8.2;
import {TokenInterface} from "./interfaces.sol";
import {Stores} from "./stores.sol";
import {DSMath} from "./math.sol";
abstract contract Basic is DSMath, Stores {
function convert18ToDec(
uint _dec,
uint256 _amt
) internal pure returns (uint256 amt) {
amt = (_amt / 10 ** (18 - _dec));
}
function convertTo18(
uint _dec,
uint256 _amt
) internal pure returns (uint256 amt) {
amt = mul(_amt, 10 ** (18 - _dec));
}
function getTokenBal(
TokenInterface token
) internal view returns (uint _amt) {
_amt = address(token) == ethAddr
? address(this).balance
: token.balanceOf(address(this));
}
function getTokensDec(
TokenInterface buyAddr,
TokenInterface sellAddr
) internal view returns (uint buyDec, uint sellDec) {
buyDec = address(buyAddr) == ethAddr ? 18 : buyAddr.decimals();
sellDec = address(sellAddr) == ethAddr ? 18 : sellAddr.decimals();
}
function encodeEvent(
string memory eventName,
bytes memory eventParam
) internal pure returns (bytes memory) {
return abi.encode(eventName, eventParam);
}
function approve(
TokenInterface token,
address spender,
uint256 amount
) internal {
try token.approve(spender, amount) {} catch {
token.approve(spender, 0);
token.approve(spender, amount);
}
}
function changeEthAddress(
address buy,
address sell
) internal pure returns (TokenInterface _buy, TokenInterface _sell) {
_buy = buy == ethAddr ? TokenInterface(wethAddr) : TokenInterface(buy);
_sell = sell == ethAddr
? TokenInterface(wethAddr)
: TokenInterface(sell);
}
function changeEthAddrToWethAddr(
address token
) internal pure returns (address tokenAddr) {
tokenAddr = token == ethAddr ? wethAddr : token;
}
function convertEthToWeth(
bool isEth,
TokenInterface token,
uint amount
) internal {
if (isEth) token.deposit{value: amount}();
}
function convertWethToEth(
bool isEth,
TokenInterface token,
uint amount
) internal {
if (isEth) {
approve(token, address(token), amount);
token.withdraw(amount);
}
}
}

View File

@ -0,0 +1,48 @@
//SPDX-License-Identifier: MIT
pragma solidity ^0.8.2;
interface TokenInterface {
function approve(address, uint256) external;
function transfer(address, uint) external;
function transferFrom(address, address, uint) external;
function deposit() external payable;
function withdraw(uint) external;
function balanceOf(address) external view returns (uint);
function decimals() external view returns (uint);
function totalSupply() external view returns (uint);
function allowance(
address owner,
address spender
) external view returns (uint256);
}
interface MemoryInterface {
function getUint(uint id) external returns (uint num);
function setUint(uint id, uint val) external;
}
interface InstaMapping {
function cTokenMapping(address) external view returns (address);
function gemJoinMapping(bytes32) external view returns (address);
}
interface AccountInterface {
function enable(address) external;
function disable(address) external;
function isAuth(address) external view returns (bool);
function cast(
string[] calldata _targetNames,
bytes[] calldata _datas,
address _origin
) external payable returns (bytes32[] memory responses);
}
interface ListInterface {
function accountID(address) external returns (uint64);
}
interface InstaConnectors {
function isConnectors(
string[] calldata
) external returns (bool, address[] memory);
}

View File

@ -0,0 +1,55 @@
//SPDX-License-Identifier: MIT
pragma solidity ^0.8.2;
import {SafeMath} from "@openzeppelin/contracts/utils/math/SafeMath.sol";
contract DSMath {
uint constant WAD = 10 ** 18;
uint constant RAY = 10 ** 27;
function add(uint x, uint y) internal pure returns (uint z) {
z = SafeMath.add(x, y);
}
function sub(uint x, uint y) internal pure virtual returns (uint z) {
z = SafeMath.sub(x, y);
}
function mul(uint x, uint y) internal pure returns (uint z) {
z = SafeMath.mul(x, y);
}
function div(uint x, uint y) internal pure returns (uint z) {
z = SafeMath.div(x, y);
}
function wmul(uint x, uint y) internal pure returns (uint z) {
z = SafeMath.add(SafeMath.mul(x, y), WAD / 2) / WAD;
}
function wdiv(uint x, uint y) internal pure returns (uint z) {
z = SafeMath.add(SafeMath.mul(x, WAD), y / 2) / y;
}
function rdiv(uint x, uint y) internal pure returns (uint z) {
z = SafeMath.add(SafeMath.mul(x, RAY), y / 2) / y;
}
function rmul(uint x, uint y) internal pure returns (uint z) {
z = SafeMath.add(SafeMath.mul(x, y), RAY / 2) / RAY;
}
function toInt(uint x) internal pure returns (int y) {
y = int(x);
require(y >= 0, "int-overflow");
}
function toUint(int256 x) internal pure returns (uint256) {
require(x >= 0, "int-overflow");
return uint256(x);
}
function toRad(uint wad) internal pure returns (uint rad) {
rad = mul(wad, 10 ** 27);
}
}

View File

@ -0,0 +1,50 @@
//SPDX-License-Identifier: MIT
pragma solidity ^0.8.2;
import {MemoryInterface, InstaMapping, ListInterface, InstaConnectors} from "./interfaces.sol";
abstract contract Stores {
/**
* @dev Return ethereum address
*/
address internal constant ethAddr =
0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE;
/**
* @dev Return Wrapped ETH address
*/
address internal constant wethAddr =
0x4200000000000000000000000000000000000006;
/**
* @dev Return memory variable address
*/
MemoryInterface internal constant instaMemory =
MemoryInterface(0x3254Ce8f5b1c82431B8f21Df01918342215825C2);
/**
* @dev Return InstaList Address
*/
ListInterface internal constant instaList =
ListInterface(0x9926955e0Dd681Dc303370C52f4Ad0a4dd061687);
/**
* @dev Return connectors registry address
*/
InstaConnectors internal constant instaConnectors =
InstaConnectors(0x127d8cD0E2b2E0366D522DeA53A787bfE9002C14);
/**
* @dev Get Uint value from InstaMemory Contract.
*/
function getUint(uint getId, uint val) internal returns (uint returnVal) {
returnVal = getId == 0 ? val : instaMemory.getUint(getId);
}
/**
* @dev Set Uint value in InstaMemory Contract.
*/
function setUint(uint setId, uint val) internal virtual {
if (setId != 0) instaMemory.setUint(setId, val);
}
}

View File

@ -0,0 +1,12 @@
//SPDX-License-Identifier: MIT
pragma solidity 0.8.19;
contract Events {
event LogSwap(
address buyToken,
address sellToken,
uint256 buyAmt,
uint256 sellAmt,
uint256 setId
);
}

View File

@ -0,0 +1,68 @@
//SPDX-License-Identifier: MIT
pragma solidity 0.8.19;
import {DSMath} from "../../common/math.sol";
import {Basic} from "../../common/basic.sol";
import {TokenInterface} from "../../common/interfaces.sol";
abstract contract Helpers is DSMath, Basic {
struct SwapData {
TokenInterface sellToken;
TokenInterface buyToken;
uint256 _sellAmt;
uint256 _buyAmt;
uint256 unitAmt;
bytes callData;
}
address internal constant AUGUSTUS_V6 =
0x6A000F20005980200259B80c5102003040001068;
function _swapHelper(
SwapData memory swapData,
uint256 wethAmt
) internal returns (uint256 buyAmt) {
TokenInterface buyToken = swapData.buyToken;
(uint256 _buyDec, uint256 _sellDec) = getTokensDec(
buyToken,
swapData.sellToken
);
uint256 _sellAmt18 = convertTo18(_sellDec, swapData._sellAmt);
uint256 _slippageAmt = convert18ToDec(
_buyDec,
wmul(swapData.unitAmt, _sellAmt18)
);
uint256 initalBal = getTokenBal(buyToken);
(bool success, ) = AUGUSTUS_V6.call{value: wethAmt}(swapData.callData);
if (!success) revert("paraswap-failed");
uint256 finalBal = getTokenBal(buyToken);
buyAmt = sub(finalBal, initalBal);
require(_slippageAmt <= buyAmt, "Too much slippage");
}
function _swap(
SwapData memory swapData,
uint256 setId
) internal returns (SwapData memory) {
TokenInterface _sellAddr = swapData.sellToken;
uint256 ethAmt;
if (address(_sellAddr) == ethAddr) {
ethAmt = swapData._sellAmt;
} else {
approve(TokenInterface(_sellAddr), AUGUSTUS_V6, swapData._sellAmt);
}
swapData._buyAmt = _swapHelper(swapData, ethAmt);
setUint(setId, swapData._buyAmt);
return swapData;
}
}

View File

@ -0,0 +1,55 @@
//SPDX-License-Identifier: MIT
pragma solidity 0.8.19;
import {TokenInterface} from "../../common/interfaces.sol";
import {Stores} from "../../common/stores.sol";
import {Helpers} from "./helpers.sol";
contract ParaswapResolver is Helpers {
/**
* @dev Sell ETH/ERC20_Token using ParaSwap.
* @notice Swap tokens from exchanges like kyber, 0x etc, with calculation done off-chain.
* @param buyAddr The address of the token to buy.(For ETH: 0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE)
* @param sellAddr The address of the token to sell.(For ETH: 0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE)
* @param sellAmt The amount of the token to sell.
* @param unitAmt The amount of buyAmt/sellAmt with slippage.
* @param callData Data from paraswap API.
* @param setId ID stores the amount of token brought.
*/
function swap(
address buyAddr,
address sellAddr,
uint256 sellAmt,
uint256 unitAmt,
bytes calldata callData,
uint256 setId
)
external
payable
returns (string memory _eventName, bytes memory _eventParam)
{
Helpers.SwapData memory swapData = Helpers.SwapData({
buyToken: TokenInterface(buyAddr),
sellToken: TokenInterface(sellAddr),
unitAmt: unitAmt,
callData: callData,
_sellAmt: sellAmt,
_buyAmt: 0
});
swapData = _swap(swapData, setId);
_eventName = "LogSwap(address,address,uint256,uint256,uint256)";
_eventParam = abi.encode(
address(swapData.buyToken),
address(swapData.sellToken),
swapData._buyAmt,
swapData._sellAmt,
setId
);
}
}
contract ConnectV2ParaswapV6Optimism is ParaswapResolver {
string public name = "Paraswap-v6.2";
}

View File

@ -0,0 +1,12 @@
//SPDX-License-Identifier: MIT
pragma solidity 0.8.19;
contract Events {
event LogSwap(
address buyToken,
address sellToken,
uint256 buyAmt,
uint256 sellAmt,
uint256 setId
);
}

View File

@ -0,0 +1,68 @@
//SPDX-License-Identifier: MIT
pragma solidity 0.8.19;
import {DSMath} from "../../common/math.sol";
import {Basic} from "../../common/basic.sol";
import {TokenInterface} from "../../common/interfaces.sol";
abstract contract Helpers is DSMath, Basic {
struct SwapData {
TokenInterface sellToken;
TokenInterface buyToken;
uint256 _sellAmt;
uint256 _buyAmt;
uint256 unitAmt;
bytes callData;
}
address internal constant AUGUSTUS_V6 =
0x6A000F20005980200259B80c5102003040001068;
function _swapHelper(
SwapData memory swapData,
uint256 wmaticAmt
) internal returns (uint256 buyAmt) {
TokenInterface buyToken = swapData.buyToken;
(uint256 _buyDec, uint256 _sellDec) = getTokensDec(
buyToken,
swapData.sellToken
);
uint256 _sellAmt18 = convertTo18(_sellDec, swapData._sellAmt);
uint256 _slippageAmt = convert18ToDec(
_buyDec,
wmul(swapData.unitAmt, _sellAmt18)
);
uint256 initalBal = getTokenBal(buyToken);
(bool success, ) = AUGUSTUS_V6.call{value: wmaticAmt}(swapData.callData);
if (!success) revert("paraswap-failed");
uint256 finalBal = getTokenBal(buyToken);
buyAmt = sub(finalBal, initalBal);
require(_slippageAmt <= buyAmt, "Too much slippage");
}
function _swap(
SwapData memory swapData,
uint256 setId
) internal returns (SwapData memory) {
TokenInterface _sellAddr = swapData.sellToken;
uint256 maticAmt;
if (address(_sellAddr) == maticAddr) {
maticAmt = swapData._sellAmt;
} else {
approve(TokenInterface(_sellAddr), AUGUSTUS_V6, swapData._sellAmt);
}
swapData._buyAmt = _swapHelper(swapData, maticAmt);
setUint(setId, swapData._buyAmt);
return swapData;
}
}

View File

@ -0,0 +1,55 @@
//SPDX-License-Identifier: MIT
pragma solidity 0.8.19;
import {TokenInterface} from "../../common/interfaces.sol";
import {Stores} from "../../common/stores.sol";
import {Helpers} from "./helpers.sol";
contract ParaswapResolver is Helpers {
/**
* @dev Sell ETH/ERC20_Token using ParaSwap.
* @notice Swap tokens from exchanges like kyber, 0x etc, with calculation done off-chain.
* @param buyAddr The address of the token to buy.(For MATIC: 0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE)
* @param sellAddr The address of the token to sell.(For MATIC: 0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE)
* @param sellAmt The amount of the token to sell.
* @param unitAmt The amount of buyAmt/sellAmt with slippage.
* @param callData Data from paraswap API.
* @param setId ID stores the amount of token brought.
*/
function swap(
address buyAddr,
address sellAddr,
uint256 sellAmt,
uint256 unitAmt,
bytes calldata callData,
uint256 setId
)
external
payable
returns (string memory _eventName, bytes memory _eventParam)
{
Helpers.SwapData memory swapData = Helpers.SwapData({
buyToken: TokenInterface(buyAddr),
sellToken: TokenInterface(sellAddr),
unitAmt: unitAmt,
callData: callData,
_sellAmt: sellAmt,
_buyAmt: 0
});
swapData = _swap(swapData, setId);
_eventName = "LogSwap(address,address,uint256,uint256,uint256)";
_eventParam = abi.encode(
address(swapData.buyToken),
address(swapData.sellToken),
swapData._buyAmt,
swapData._sellAmt,
setId
);
}
}
contract ConnectV2ParaswapV6Polygon is ParaswapResolver {
string public name = "Paraswap-v6.2";
}