mirror of
https://github.com/Instadapp/dsa-connectors.git
synced 2024-07-29 22:37:00 +00:00
Merge pull request #198 from Instadapp/paraswap-ftm
Added fantom paraswap
This commit is contained in:
commit
c718151a87
11
contracts/fantom/connectors/paraswap/events.sol
Normal file
11
contracts/fantom/connectors/paraswap/events.sol
Normal file
|
@ -0,0 +1,11 @@
|
||||||
|
pragma solidity ^0.7.0;
|
||||||
|
|
||||||
|
contract Events {
|
||||||
|
event LogSwap(
|
||||||
|
address buyToken,
|
||||||
|
address sellToken,
|
||||||
|
uint256 buyAmt,
|
||||||
|
uint256 sellAmt,
|
||||||
|
uint256 setId
|
||||||
|
);
|
||||||
|
}
|
70
contracts/fantom/connectors/paraswap/helpers.sol
Normal file
70
contracts/fantom/connectors/paraswap/helpers.sol
Normal file
|
@ -0,0 +1,70 @@
|
||||||
|
pragma solidity ^0.7.0;
|
||||||
|
|
||||||
|
import { DSMath } from "../../common/math.sol";
|
||||||
|
import { Basic } from "../../common/basic.sol";
|
||||||
|
import { TokenInterface } from "../../common/interfaces.sol";
|
||||||
|
import { AugustusSwapperInterface } from "./interface.sol";
|
||||||
|
|
||||||
|
abstract contract Helpers is DSMath, Basic {
|
||||||
|
struct SwapData {
|
||||||
|
TokenInterface sellToken;
|
||||||
|
TokenInterface buyToken;
|
||||||
|
uint256 _sellAmt;
|
||||||
|
uint256 _buyAmt;
|
||||||
|
uint256 unitAmt;
|
||||||
|
bytes callData;
|
||||||
|
}
|
||||||
|
|
||||||
|
address internal constant paraswap =
|
||||||
|
0xDEF171Fe48CF0115B1d80b88dc8eAB59176FEe57;
|
||||||
|
|
||||||
|
function _swapHelper(SwapData memory swapData, uint256 wftmAmt)
|
||||||
|
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, ) = paraswap.call{ value: wftmAmt }(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 ftmAmt;
|
||||||
|
|
||||||
|
if (address(_sellAddr) == ftmAddr) {
|
||||||
|
ftmAmt = swapData._sellAmt;
|
||||||
|
} else {
|
||||||
|
address tokenProxy = AugustusSwapperInterface(paraswap)
|
||||||
|
.getTokenTransferProxy();
|
||||||
|
approve(TokenInterface(_sellAddr), tokenProxy, swapData._sellAmt);
|
||||||
|
}
|
||||||
|
|
||||||
|
swapData._buyAmt = _swapHelper(swapData, ftmAmt);
|
||||||
|
|
||||||
|
setUint(setId, swapData._buyAmt);
|
||||||
|
|
||||||
|
return swapData;
|
||||||
|
}
|
||||||
|
}
|
5
contracts/fantom/connectors/paraswap/interface.sol
Normal file
5
contracts/fantom/connectors/paraswap/interface.sol
Normal file
|
@ -0,0 +1,5 @@
|
||||||
|
pragma solidity ^0.7.0;
|
||||||
|
|
||||||
|
interface AugustusSwapperInterface {
|
||||||
|
function getTokenTransferProxy() external view returns (address);
|
||||||
|
}
|
59
contracts/fantom/connectors/paraswap/main.sol
Normal file
59
contracts/fantom/connectors/paraswap/main.sol
Normal file
|
@ -0,0 +1,59 @@
|
||||||
|
pragma solidity ^0.7.0;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @title Paraswap.
|
||||||
|
* @dev DEX Aggregator.
|
||||||
|
*/
|
||||||
|
|
||||||
|
import { TokenInterface } from "../../common/interfaces.sol";
|
||||||
|
import { Stores } from "../../common/stores.sol";
|
||||||
|
import { Helpers } from "./helpers.sol";
|
||||||
|
|
||||||
|
abstract contract ParaswapResolver is Helpers {
|
||||||
|
/**
|
||||||
|
* @dev Sell FTM/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 FTM: 0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE)
|
||||||
|
* @param sellAddr The address of the token to sell.(For FTM: 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 ConnectV2ParaswapV5Fantom is ParaswapResolver {
|
||||||
|
string public name = "Paraswap-v5";
|
||||||
|
}
|
|
@ -24,7 +24,7 @@ async function deployRunner() {
|
||||||
name: "chain",
|
name: "chain",
|
||||||
message: "What chain do you want to deploy on?",
|
message: "What chain do you want to deploy on?",
|
||||||
type: "list",
|
type: "list",
|
||||||
choices: ["mainnet", "polygon", "avalanche", "arbitrum", "optimism"]
|
choices: ["mainnet", "polygon", "avalanche", "arbitrum", "optimism", "fantom"]
|
||||||
}
|
}
|
||||||
]);
|
]);
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user