mirror of
https://github.com/Instadapp/dsa-connectors.git
synced 2024-07-29 22:37:00 +00:00
Added Pangolin Exchange Connector
Fixed Contract Add: test to Pangolin connector Fix InstaDapp Addresses/Scripts/PNG Connector Removed unnecessary scripts Moved PNG connector contract and Connector tests
This commit is contained in:
parent
a7cd775056
commit
358b9bfebb
41
contracts/avalanche/connectors/pangolin/exchange/events.sol
Normal file
41
contracts/avalanche/connectors/pangolin/exchange/events.sol
Normal file
|
@ -0,0 +1,41 @@
|
|||
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
|
||||
);
|
||||
}
|
146
contracts/avalanche/connectors/pangolin/exchange/helpers.sol
Normal file
146
contracts/avalanche/connectors/pangolin/exchange/helpers.sol
Normal file
|
@ -0,0 +1,146 @@
|
|||
pragma solidity ^0.7.0;
|
||||
|
||||
import { TokenInterface } from "../../../common/interfaces.sol";
|
||||
import { DSMath } from "../../../common/math.sol";
|
||||
import { Basic } from "../../../common/basic.sol";
|
||||
import { IPangolinRouter, IPangolinFactory } from "./interface.sol";
|
||||
|
||||
abstract contract Helpers is DSMath, Basic {
|
||||
|
||||
/**
|
||||
* @dev Pangolin Router
|
||||
*/
|
||||
IPangolinRouter internal constant router = IPangolinRouter(0xE54Ca86531e17Ef3616d22Ca28b0D458b6C89106);
|
||||
|
||||
function getExpectedBuyAmt(
|
||||
address[] memory paths,
|
||||
uint sellAmt
|
||||
) internal view returns(uint buyAmt) {
|
||||
uint[] memory amts = router.getAmountsOut(
|
||||
sellAmt,
|
||||
paths
|
||||
);
|
||||
buyAmt = amts[1];
|
||||
}
|
||||
|
||||
function getExpectedSellAmt(
|
||||
address[] memory paths,
|
||||
uint buyAmt
|
||||
) internal view returns(uint sellAmt) {
|
||||
uint[] memory amts = router.getAmountsIn(
|
||||
buyAmt,
|
||||
paths
|
||||
);
|
||||
sellAmt = amts[0];
|
||||
}
|
||||
|
||||
function checkPair(
|
||||
address[] memory paths
|
||||
) internal view {
|
||||
address pair = IPangolinFactory(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,
|
||||
uint amt,
|
||||
uint slippage
|
||||
) internal view returns(uint minAmt) {
|
||||
uint _amt18 = convertTo18(token.decimals(), amt);
|
||||
minAmt = wmul(_amt18, sub(WAD, slippage));
|
||||
minAmt = convert18ToDec(token.decimals(), minAmt);
|
||||
}
|
||||
|
||||
function _addLiquidity(
|
||||
address tokenA,
|
||||
address tokenB,
|
||||
uint _amt,
|
||||
uint unitAmt,
|
||||
uint slippage
|
||||
) internal returns (uint _amtA, uint _amtB, uint _liquidity) {
|
||||
(TokenInterface _tokenA, TokenInterface _tokenB) = changeAvaxAddress(tokenA, tokenB);
|
||||
|
||||
_amtA = _amt == uint(-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);
|
||||
|
||||
uint minAmtA = getMinAmount(_tokenA, _amtA, slippage);
|
||||
uint 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,
|
||||
uint _amt,
|
||||
uint unitAmtA,
|
||||
uint unitAmtB
|
||||
) internal returns (uint _amtA, uint _amtB, uint _uniAmt) {
|
||||
TokenInterface _tokenA;
|
||||
TokenInterface _tokenB;
|
||||
(_tokenA, _tokenB, _uniAmt) = _getRemoveLiquidityData(
|
||||
tokenA,
|
||||
tokenB,
|
||||
_amt
|
||||
);
|
||||
{
|
||||
uint minAmtA = convert18ToDec(_tokenA.decimals(), wmul(unitAmtA, _uniAmt));
|
||||
uint 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,
|
||||
uint _amt
|
||||
) internal returns (TokenInterface _tokenA, TokenInterface _tokenB, uint _uniAmt) {
|
||||
(_tokenA, _tokenB) = changeAvaxAddress(tokenA, tokenB);
|
||||
address exchangeAddr = IPangolinFactory(router.factory()).getPair(address(_tokenA), address(_tokenB));
|
||||
require(exchangeAddr != address(0), "pair-not-found.");
|
||||
|
||||
TokenInterface pngToken = TokenInterface(exchangeAddr);
|
||||
_uniAmt = _amt == uint(-1) ? pngToken.balanceOf(address(this)) : _amt;
|
||||
approve(pngToken, address(router), _uniAmt);
|
||||
}
|
||||
}
|
164
contracts/avalanche/connectors/pangolin/exchange/interface.sol
Normal file
164
contracts/avalanche/connectors/pangolin/exchange/interface.sol
Normal file
|
@ -0,0 +1,164 @@
|
|||
pragma solidity >=0.6.2;
|
||||
|
||||
interface IPangolinRouter {
|
||||
function factory() external pure returns (address);
|
||||
function WAVAX() 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 addLiquidityAVAX(
|
||||
address token,
|
||||
uint amountTokenDesired,
|
||||
uint amountTokenMin,
|
||||
uint amountAVAXMin,
|
||||
address to,
|
||||
uint deadline
|
||||
) external payable returns (uint amountToken, uint amountAVAX, 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 removeLiquidityAVAX(
|
||||
address token,
|
||||
uint liquidity,
|
||||
uint amountTokenMin,
|
||||
uint amountAVAXMin,
|
||||
address to,
|
||||
uint deadline
|
||||
) external returns (uint amountToken, uint amountAVAX);
|
||||
|
||||
function removeLiquidityWithPermit(
|
||||
address tokenA,
|
||||
address tokenB,
|
||||
uint liquidity,
|
||||
uint amountAMin,
|
||||
uint amountBMin,
|
||||
address to,
|
||||
uint deadline,
|
||||
bool approveMax, uint8 v, bytes32 r, bytes32 s
|
||||
) external returns (uint amountA, uint amountB);
|
||||
|
||||
function removeLiquidityAVAXWithPermit(
|
||||
address token,
|
||||
uint liquidity,
|
||||
uint amountTokenMin,
|
||||
uint amountAVAXMin,
|
||||
address to,
|
||||
uint deadline,
|
||||
bool approveMax, uint8 v, bytes32 r, bytes32 s
|
||||
) external returns (uint amountToken, uint amountAVAX);
|
||||
|
||||
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 swapExactAVAXForTokens(uint amountOutMin, address[] calldata path, address to, uint deadline)
|
||||
external
|
||||
payable
|
||||
returns (uint[] memory amounts);
|
||||
|
||||
function swapTokensForExactAVAX(uint amountOut, uint amountInMax, address[] calldata path, address to, uint deadline)
|
||||
external
|
||||
returns (uint[] memory amounts);
|
||||
|
||||
function swapExactTokensForAVAX(uint amountIn, uint amountOutMin, address[] calldata path, address to, uint deadline)
|
||||
external
|
||||
returns (uint[] memory amounts);
|
||||
|
||||
function swapAVAXForExactTokens(uint amountOut, address[] calldata path, address to, uint deadline)
|
||||
external
|
||||
payable
|
||||
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);
|
||||
|
||||
function removeLiquidityAVAXSupportingFeeOnTransferTokens(
|
||||
address token,
|
||||
uint liquidity,
|
||||
uint amountTokenMin,
|
||||
uint amountAVAXMin,
|
||||
address to,
|
||||
uint deadline
|
||||
) external returns (uint amountAVAX);
|
||||
|
||||
function removeLiquidityAVAXWithPermitSupportingFeeOnTransferTokens(
|
||||
address token,
|
||||
uint liquidity,
|
||||
uint amountTokenMin,
|
||||
uint amountAVAXMin,
|
||||
address to,
|
||||
uint deadline,
|
||||
bool approveMax, uint8 v, bytes32 r, bytes32 s
|
||||
) external returns (uint amountAVAX);
|
||||
|
||||
function swapExactTokensForTokensSupportingFeeOnTransferTokens(
|
||||
uint amountIn,
|
||||
uint amountOutMin,
|
||||
address[] calldata path,
|
||||
address to,
|
||||
uint deadline
|
||||
) external;
|
||||
|
||||
function swapExactAVAXForTokensSupportingFeeOnTransferTokens(
|
||||
uint amountOutMin,
|
||||
address[] calldata path,
|
||||
address to,
|
||||
uint deadline
|
||||
) external payable;
|
||||
|
||||
function swapExactTokensForAVAXSupportingFeeOnTransferTokens(
|
||||
uint amountIn,
|
||||
uint amountOutMin,
|
||||
address[] calldata path,
|
||||
address to,
|
||||
uint deadline
|
||||
) external;
|
||||
}
|
||||
|
||||
interface IPangolinFactory {
|
||||
event PairCreated(address indexed token0, address indexed token1, address pair, uint);
|
||||
|
||||
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);
|
||||
|
||||
function setFeeTo(address) external;
|
||||
function setFeeToSetter(address) external;
|
||||
}
|
196
contracts/avalanche/connectors/pangolin/exchange/main.sol
Normal file
196
contracts/avalanche/connectors/pangolin/exchange/main.sol
Normal file
|
@ -0,0 +1,196 @@
|
|||
pragma solidity ^0.7.0;
|
||||
|
||||
/**
|
||||
* @title Pangolin.
|
||||
* @dev Decentralized Exchange.
|
||||
*/
|
||||
|
||||
import { TokenInterface } from "../../../common/interfaces.sol";
|
||||
import { Helpers } from "./helpers.sol";
|
||||
import { Events } from "./events.sol";
|
||||
|
||||
abstract contract PangolinResolver is Helpers, Events {
|
||||
/**
|
||||
* @dev Deposit Liquidity.
|
||||
* @notice Deposit Liquidity to a Pangolin 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 Pangolin 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 Pangolin
|
||||
* @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 Pangolin
|
||||
* @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 ConnectV2PngAvalanche is PangolinResolver {
|
||||
string public constant name = "Pangolin-v1";
|
||||
}
|
283
test/avalanche/pangolin/pangolin.test.ts
Normal file
283
test/avalanche/pangolin/pangolin.test.ts
Normal file
|
@ -0,0 +1,283 @@
|
|||
import { expect } from "chai";
|
||||
import hre from "hardhat";
|
||||
|
||||
const { web3, deployments, waffle, ethers } = hre;
|
||||
const { provider, deployContract } = waffle;
|
||||
|
||||
import { deployAndEnableConnector } from "../../../scripts/tests/deployAndEnableConnector";
|
||||
import { buildDSAv2 } from "../../../scripts/tests/buildDSAv2";
|
||||
import { encodeSpells } from "../../../scripts/tests/encodeSpells";
|
||||
import { getMasterSigner } from "../../../scripts/tests/getMasterSigner";
|
||||
import { addLiquidity } from "../../../scripts/tests/addLiquidity";
|
||||
import { addresses } from "../../../scripts/tests/avalanche/addresses";
|
||||
import { abis } from "../../../scripts/constant/abis";
|
||||
import type { Signer, Contract } from "ethers";
|
||||
|
||||
import { ConnectV2PngAvalanche__factory } from "../../../typechain";
|
||||
|
||||
const PNG_ADDRESS = "0x60781C2586D68229fde47564546784ab3fACA982";
|
||||
const WAVAX_ADDRESS = "0xB31f66AA3C1e785363F0875A1B74E27b85FD66c7";
|
||||
const PNG_AVAX_LP_ADDRESS = "0xd7538cABBf8605BdE1f4901B47B8D42c61DE0367";
|
||||
|
||||
describe("Pangolin DEX - Avalanche", function () {
|
||||
const pangolinConnectorName = "PANGOLIN-TEST-A"
|
||||
|
||||
let dsaWallet0: any;
|
||||
let masterSigner: any;
|
||||
let instaConnectorsV2: any;
|
||||
let pangolinConnector: any;
|
||||
|
||||
const wallets = provider.getWallets()
|
||||
const [wallet0, wallet1, wallet2, wallet3] = wallets
|
||||
before(async () => {
|
||||
await hre.network.provider.request({
|
||||
method: "hardhat_reset",
|
||||
params: [
|
||||
{
|
||||
forking: {
|
||||
jsonRpcUrl: `https://api.avax.network/ext/bc/C/rpc`,
|
||||
blockNumber: 8197390
|
||||
},
|
||||
},
|
||||
],
|
||||
});
|
||||
|
||||
masterSigner = await getMasterSigner();
|
||||
instaConnectorsV2 = await ethers.getContractAt(
|
||||
abis.core.connectorsV2,
|
||||
addresses.core.connectorsV2
|
||||
);
|
||||
|
||||
// Deploy and enable Pangolin Connector
|
||||
pangolinConnector = await deployAndEnableConnector({
|
||||
connectorName: pangolinConnectorName,
|
||||
contractArtifact: ConnectV2PngAvalanche__factory,
|
||||
signer: masterSigner,
|
||||
connectors: instaConnectorsV2
|
||||
});
|
||||
console.log("Pangolin Connector address: "+ pangolinConnector.address);
|
||||
})
|
||||
|
||||
it("Should have contracts deployed.", async function () {
|
||||
expect(!!instaConnectorsV2.address).to.be.true;
|
||||
expect(!!pangolinConnector.address).to.be.true;
|
||||
expect(!!masterSigner.address).to.be.true;
|
||||
});
|
||||
|
||||
describe("DSA wallet setup", function () {
|
||||
it("Should build DSA v2", async function () {
|
||||
dsaWallet0 = await buildDSAv2(wallet0.address)
|
||||
expect(!!dsaWallet0.address).to.be.true;
|
||||
});
|
||||
|
||||
it("Deposit 10 AVAX into DSA wallet", async function () {
|
||||
await wallet0.sendTransaction({
|
||||
to: dsaWallet0.address,
|
||||
value: ethers.utils.parseEther("10")
|
||||
});
|
||||
expect(await ethers.provider.getBalance(dsaWallet0.address)).to.be.gte(ethers.utils.parseEther("10"));
|
||||
});
|
||||
});
|
||||
|
||||
describe("Main - PANGOLIN PNG/AVAX Liquidity Test", function () {
|
||||
|
||||
it("Should use pangolin to swap AVAX for PNG, and deposit to PNG/AVAX LP", async function () {
|
||||
const amount = ethers.utils.parseEther("100"); // 100 PNG
|
||||
const int_slippage = 0.03
|
||||
const slippage = ethers.utils.parseEther(int_slippage.toString());
|
||||
const setId = "83528353";
|
||||
|
||||
const PangolinRouterABI = [
|
||||
"function getAmountsOut(uint amountIn, address[] calldata path) external view returns (uint[] memory amounts)"
|
||||
];
|
||||
|
||||
// Get amount of AVAX for 100 POOL from Pangolin
|
||||
const PangolinRouter = await ethers.getContractAt(
|
||||
PangolinRouterABI,
|
||||
"0xE54Ca86531e17Ef3616d22Ca28b0D458b6C89106"
|
||||
);
|
||||
const amounts = await PangolinRouter.getAmountsOut(
|
||||
amount,
|
||||
[
|
||||
PNG_ADDRESS,
|
||||
WAVAX_ADDRESS
|
||||
]
|
||||
);
|
||||
|
||||
const amtA = amounts[0];
|
||||
const amtB = amounts[1];
|
||||
const unitAmt = (amtB * (1 + int_slippage)) / amtA;
|
||||
const unitAmount = ethers.utils.parseEther(unitAmt.toString());
|
||||
|
||||
const spells = [
|
||||
{
|
||||
connector: pangolinConnectorName,
|
||||
method: "buy",
|
||||
args: [
|
||||
PNG_ADDRESS,
|
||||
"0xeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee",
|
||||
amount,
|
||||
unitAmount,
|
||||
0,
|
||||
setId
|
||||
]
|
||||
},
|
||||
{
|
||||
connector: pangolinConnectorName,
|
||||
method: "deposit",
|
||||
args: [
|
||||
PNG_ADDRESS,
|
||||
"0xeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee",
|
||||
amount,
|
||||
unitAmount,
|
||||
slippage,
|
||||
0,
|
||||
setId
|
||||
]
|
||||
},
|
||||
];
|
||||
|
||||
// Before Spell
|
||||
let avaxBalance = await ethers.provider.getBalance(dsaWallet0.address);
|
||||
expect(avaxBalance, `AVAX Balance equals 10`).to.be.eq(ethers.utils.parseEther("10"));
|
||||
|
||||
let pngToken = await ethers.getContractAt(abis.basic.erc20, PNG_ADDRESS);
|
||||
const pngBalance = await pngToken.balanceOf(dsaWallet0.address);
|
||||
expect(pngBalance, `PNG Token greater than 0`).to.be.eq(0);
|
||||
|
||||
let pangolinLPToken = await ethers.getContractAt(
|
||||
abis.basic.erc20,
|
||||
PNG_AVAX_LP_ADDRESS
|
||||
);
|
||||
const pangolinPoolAVAXBalance = await pangolinLPToken.balanceOf(dsaWallet0.address);
|
||||
expect(pangolinPoolAVAXBalance, `Pangolin PNG/AVAX LP equals 0`).to.be.eq(0);
|
||||
|
||||
// Run spell transaction
|
||||
const tx = await dsaWallet0.connect(wallet0).cast(
|
||||
...encodeSpells(spells), wallet1.address
|
||||
);
|
||||
const receipt = await tx.wait();
|
||||
|
||||
// After spell
|
||||
avaxBalance = await ethers.provider.getBalance(dsaWallet0.address);
|
||||
expect(avaxBalance, `AVAX Balance less than 10`).to.be.lt(ethers.utils.parseEther("10"));
|
||||
|
||||
const pngBalanceAfter = await pngToken.balanceOf(dsaWallet0.address)
|
||||
expect(pngBalanceAfter, `PNG Token to be same after spell`).to.be.eq(pngBalance);
|
||||
|
||||
const pangolinPoolAVAXBalanceAfter = await pangolinLPToken.balanceOf(dsaWallet0.address);
|
||||
expect(
|
||||
pangolinPoolAVAXBalanceAfter,
|
||||
`Pangolin PNG/AVAX LP greater than 0`
|
||||
).to.be.gt(0);
|
||||
});
|
||||
|
||||
it("Should use pangolin to withdraw to PNG/AVAX LP, and swap PNG for AVAX", async function () {
|
||||
const amount = ethers.utils.parseEther("100"); // 100 PNG
|
||||
const int_slippage = 0.03
|
||||
|
||||
// Before Spell
|
||||
let avaxBalance = await ethers.provider.getBalance(dsaWallet0.address);
|
||||
let pngToken = await ethers.getContractAt(abis.basic.erc20, PNG_ADDRESS);
|
||||
let pangolinLPToken = await ethers.getContractAt(
|
||||
abis.basic.erc20,
|
||||
PNG_AVAX_LP_ADDRESS
|
||||
);
|
||||
|
||||
const pngBalance = await pngToken.balanceOf(dsaWallet0.address)
|
||||
expect(pngBalance, `PNG Token balance equal to 0`).to.be.eq(0);
|
||||
|
||||
const pangolinPoolAVAXBalance = await pangolinLPToken.balanceOf(dsaWallet0.address);
|
||||
expect(pangolinPoolAVAXBalance, `Pangolin PNG/AVAX LP greater than 0`).to.be.gt(0);
|
||||
|
||||
const PangolinRouterABI = [
|
||||
"function getAmountsOut(uint amountIn, address[] calldata path) external view returns (uint[] memory amounts)"
|
||||
];
|
||||
|
||||
// Get amount of avax for 100 PNG from Pangolin
|
||||
const PangolinRouter= await ethers.getContractAt(
|
||||
PangolinRouterABI,
|
||||
"0xE54Ca86531e17Ef3616d22Ca28b0D458b6C89106"
|
||||
);
|
||||
const amounts = await PangolinRouter.getAmountsOut(
|
||||
amount,
|
||||
[
|
||||
PNG_ADDRESS,
|
||||
WAVAX_ADDRESS
|
||||
]
|
||||
);
|
||||
const amtA = amounts[0];
|
||||
const amtB = amounts[1];
|
||||
const unitAmtA = ethers.utils.parseEther(
|
||||
(amtA * (1 - int_slippage) / pangolinPoolAVAXBalance).toString()
|
||||
);
|
||||
const unitAmtB = ethers.utils.parseEther(
|
||||
(amtB * (1 - int_slippage) / pangolinPoolAVAXBalance).toString()
|
||||
);
|
||||
|
||||
let spells = [
|
||||
{
|
||||
connector: pangolinConnectorName,
|
||||
method: "withdraw",
|
||||
args: [
|
||||
PNG_ADDRESS,
|
||||
"0xeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee",
|
||||
pangolinPoolAVAXBalance,
|
||||
unitAmtA,
|
||||
unitAmtB,
|
||||
0,
|
||||
[
|
||||
0,
|
||||
0
|
||||
]
|
||||
]
|
||||
},
|
||||
];
|
||||
|
||||
// Run spell transaction (withdraw token of pool)
|
||||
const tx = await dsaWallet0.connect(wallet0).cast(
|
||||
...encodeSpells(spells),
|
||||
wallet1.address
|
||||
);
|
||||
const receipt = await tx.wait();
|
||||
|
||||
// After spell
|
||||
const pangolinPoolAVAXBalanceAfter = await pangolinLPToken.balanceOf(
|
||||
dsaWallet0.address
|
||||
);
|
||||
expect(pangolinPoolAVAXBalanceAfter, `Pangolin PNG/AVAX LP equal 0`).to.be.eq(0);
|
||||
|
||||
let pngBalanceAfter = await pngToken.balanceOf(dsaWallet0.address);
|
||||
expect(pngBalanceAfter, `PNG Token balance greater than`).to.be.gt(0);
|
||||
const unitAmt = amount.div(pngBalanceAfter);
|
||||
|
||||
spells = [
|
||||
{
|
||||
connector: pangolinConnectorName,
|
||||
method: "sell",
|
||||
args: [
|
||||
"0xeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee",
|
||||
PNG_ADDRESS,
|
||||
pngBalanceAfter,
|
||||
unitAmt,
|
||||
0,
|
||||
0
|
||||
]
|
||||
},
|
||||
];
|
||||
|
||||
// Run spell transaction (withdraw token of pool)
|
||||
const tx2 = await dsaWallet0.connect(wallet0).cast(...encodeSpells(spells), wallet1.address);
|
||||
const receipt2 = await tx2.wait();
|
||||
|
||||
let avaxBalanceAfter = await ethers.provider.getBalance(dsaWallet0.address);
|
||||
expect(
|
||||
avaxBalanceAfter,
|
||||
`AVAX Balance After greater than AVAX Balance Before`
|
||||
).to.be.gt(avaxBalance);
|
||||
|
||||
pngBalanceAfter = await pngToken.balanceOf(dsaWallet0.address);
|
||||
expect(pngBalanceAfter, `PNG Token balance equal 0`).to.be.eq(0);
|
||||
});
|
||||
})
|
||||
});
|
Loading…
Reference in New Issue
Block a user