Skip to content

Commit

Permalink
refactor(SwapHelperLib)!: replace system contract with uniswap router…
Browse files Browse the repository at this point in the history
… address (#197)
  • Loading branch information
fadeev authored Nov 29, 2024
1 parent cf3b5f0 commit b38d277
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 55 deletions.
80 changes: 35 additions & 45 deletions contracts/SwapHelperLib.sol
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ pragma solidity 0.8.26;
import "@uniswap/v2-periphery/contracts/interfaces/IUniswapV2Router02.sol";
import "@uniswap/v2-periphery/contracts/interfaces/IUniswapV2Router01.sol";
import "./shared/interfaces/IZRC20.sol";
import "./SystemContract.sol";
import "./shared/libraries/UniswapV2Library.sol";

library SwapHelperLib {
Expand Down Expand Up @@ -112,41 +111,39 @@ library SwapHelperLib {
}

function swapExactTokensForTokens(
SystemContract systemContract,
address router,
address zrc20,
uint256 amount,
address targetZRC20,
uint256 minAmountOut
) internal returns (uint256) {
address factory = IUniswapV2Router01(router).factory();
address wzeta = IUniswapV2Router01(router).WETH();

address[] memory path;
path = new address[](2);
path[0] = zrc20;
path[1] = targetZRC20;

bool isSufficientLiquidity = _isSufficientLiquidity(
systemContract.uniswapv2FactoryAddress(),
factory,
amount,
minAmountOut,
path
);

bool isZETA = targetZRC20 == systemContract.wZetaContractAddress() ||
zrc20 == systemContract.wZetaContractAddress();
bool isZETA = targetZRC20 == wzeta || zrc20 == wzeta;

if (!isSufficientLiquidity && !isZETA) {
path = new address[](3);
path[0] = zrc20;
path[1] = systemContract.wZetaContractAddress();
path[1] = wzeta;
path[2] = targetZRC20;
}

IZRC20(zrc20).approve(
address(systemContract.uniswapv2Router02Address()),
amount
);
uint256[] memory amounts = IUniswapV2Router01(
systemContract.uniswapv2Router02Address()
).swapExactTokensForTokens(
IZRC20(zrc20).approve(router, amount);
uint256[] memory amounts = IUniswapV2Router01(router)
.swapExactTokensForTokens(
amount,
minAmountOut,
path,
Expand All @@ -157,17 +154,16 @@ library SwapHelperLib {
}

function swapExactTokensForTokensDirectly(
SystemContract systemContract,
address router,
address zrc20,
uint256 amount,
address targetZRC20,
uint256 minAmountOut
) internal returns (uint256) {
bool existsPairPool = _existsPairPool(
systemContract.uniswapv2FactoryAddress(),
zrc20,
targetZRC20
);
address factory = IUniswapV2Router01(router).factory();
address wzeta = IUniswapV2Router01(router).WETH();

bool existsPairPool = _existsPairPool(factory, zrc20, targetZRC20);

address[] memory path;
if (existsPairPool) {
Expand All @@ -177,17 +173,13 @@ library SwapHelperLib {
} else {
path = new address[](3);
path[0] = zrc20;
path[1] = systemContract.wZetaContractAddress();
path[1] = wzeta;
path[2] = targetZRC20;
}

IZRC20(zrc20).approve(
address(systemContract.uniswapv2Router02Address()),
amount
);
uint256[] memory amounts = IUniswapV2Router01(
systemContract.uniswapv2Router02Address()
).swapExactTokensForTokens(
IZRC20(zrc20).approve(router, amount);
uint256[] memory amounts = IUniswapV2Router01(router)
.swapExactTokensForTokens(
amount,
minAmountOut,
path,
Expand All @@ -198,17 +190,16 @@ library SwapHelperLib {
}

function swapTokensForExactTokens(
SystemContract systemContract,
address router,
address zrc20,
uint256 amount,
address targetZRC20,
uint256 amountInMax
) internal returns (uint256) {
bool existsPairPool = _existsPairPool(
systemContract.uniswapv2FactoryAddress(),
zrc20,
targetZRC20
);
address factory = IUniswapV2Router01(router).factory();
address wzeta = IUniswapV2Router01(router).WETH();

bool existsPairPool = _existsPairPool(factory, zrc20, targetZRC20);

address[] memory path;
if (existsPairPool) {
Expand All @@ -218,17 +209,13 @@ library SwapHelperLib {
} else {
path = new address[](3);
path[0] = zrc20;
path[1] = systemContract.wZetaContractAddress();
path[1] = wzeta;
path[2] = targetZRC20;
}

IZRC20(zrc20).approve(
address(systemContract.uniswapv2Router02Address()),
amountInMax
);
uint256[] memory amounts = IUniswapV2Router01(
systemContract.uniswapv2Router02Address()
).swapTokensForExactTokens(
IZRC20(zrc20).approve(router, amountInMax);
uint256[] memory amounts = IUniswapV2Router01(router)
.swapTokensForExactTokens(
amount,
amountInMax,
path,
Expand All @@ -239,28 +226,31 @@ library SwapHelperLib {
}

function getMinOutAmount(
SystemContract systemContract,
address router,
address zrc20,
address target,
uint256 amountIn
) public view returns (uint256 minOutAmount) {
address factory = IUniswapV2Router01(router).factory();
address wzeta = IUniswapV2Router01(router).WETH();

address[] memory path;

path = new address[](2);
path[0] = zrc20;
path[1] = target;
uint[] memory amounts1 = UniswapV2Library.getAmountsOut(
systemContract.uniswapv2FactoryAddress(),
factory,
amountIn,
path
);

path = new address[](3);
path[0] = zrc20;
path[1] = systemContract.wZetaContractAddress();
path[1] = wzeta;
path[2] = target;
uint[] memory amounts2 = UniswapV2Library.getAmountsOut(
systemContract.uniswapv2FactoryAddress(),
factory,
amountIn,
path
);
Expand Down
12 changes: 6 additions & 6 deletions typechain-types/contracts/SwapHelperLib.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import type {

export interface SwapHelperLibInterface extends utils.Interface {
functions: {
"getMinOutAmount(SystemContract,address,address,uint256)": FunctionFragment;
"getMinOutAmount(address,address,address,uint256)": FunctionFragment;
"uniswapv2PairFor(address,address,address)": FunctionFragment;
};

Expand Down Expand Up @@ -89,7 +89,7 @@ export interface SwapHelperLib extends BaseContract {

functions: {
getMinOutAmount(
systemContract: PromiseOrValue<string>,
router: PromiseOrValue<string>,
zrc20: PromiseOrValue<string>,
target: PromiseOrValue<string>,
amountIn: PromiseOrValue<BigNumberish>,
Expand All @@ -105,7 +105,7 @@ export interface SwapHelperLib extends BaseContract {
};

getMinOutAmount(
systemContract: PromiseOrValue<string>,
router: PromiseOrValue<string>,
zrc20: PromiseOrValue<string>,
target: PromiseOrValue<string>,
amountIn: PromiseOrValue<BigNumberish>,
Expand All @@ -121,7 +121,7 @@ export interface SwapHelperLib extends BaseContract {

callStatic: {
getMinOutAmount(
systemContract: PromiseOrValue<string>,
router: PromiseOrValue<string>,
zrc20: PromiseOrValue<string>,
target: PromiseOrValue<string>,
amountIn: PromiseOrValue<BigNumberish>,
Expand All @@ -140,7 +140,7 @@ export interface SwapHelperLib extends BaseContract {

estimateGas: {
getMinOutAmount(
systemContract: PromiseOrValue<string>,
router: PromiseOrValue<string>,
zrc20: PromiseOrValue<string>,
target: PromiseOrValue<string>,
amountIn: PromiseOrValue<BigNumberish>,
Expand All @@ -157,7 +157,7 @@ export interface SwapHelperLib extends BaseContract {

populateTransaction: {
getMinOutAmount(
systemContract: PromiseOrValue<string>,
router: PromiseOrValue<string>,
zrc20: PromiseOrValue<string>,
target: PromiseOrValue<string>,
amountIn: PromiseOrValue<BigNumberish>,
Expand Down
Loading

0 comments on commit b38d277

Please sign in to comment.