Skip to content

Commit

Permalink
swapTokensForExactTokens
Browse files Browse the repository at this point in the history
  • Loading branch information
fadeev committed Dec 13, 2023
1 parent 81e15fa commit bc629af
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 8 deletions.
39 changes: 39 additions & 0 deletions contracts/SwapHelperLib.sol
Original file line number Diff line number Diff line change
Expand Up @@ -123,4 +123,43 @@ library SwapHelperLib {
);
return amounts[path.length - 1];
}

function swapTokensForExactTokens(
address zetaToken,
address uniswapV2Factory,
address uniswapV2Router,
address zrc20,
uint256 amount,
address targetZRC20,
uint256 amountInMax
) internal returns (uint256) {
bool existsPairPool = _existsPairPool(
uniswapV2Factory,
zrc20,
targetZRC20
);

address[] memory path;
if (existsPairPool) {
path = new address[](2);
path[0] = zrc20;
path[1] = targetZRC20;
} else {
path = new address[](3);
path[0] = zrc20;
path[1] = zetaToken;
path[2] = targetZRC20;
}

IZRC20(zrc20).approve(address(uniswapV2Router), amountInMax);
uint256[] memory amounts = IUniswapV2Router01(uniswapV2Router)
.swapTokensForExactTokens(
amount,
amountInMax,
path,
address(this),
block.timestamp + MAX_DEADLINE
);
return amounts[0];
}
}
18 changes: 10 additions & 8 deletions tasks/pools.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ const main = async (args: any, hre: HardhatRuntimeEnvironment) => {
addressToInfo[WZETA_ADDRESS] = { symbol: "WZETA", decimals: 18 };

Check failure on line 23 in tasks/pools.ts

View workflow job for this annotation

GitHub Actions / build

Expected object keys to be in ascending order. 'decimals' should be before 'symbol'

const poolsWithSymbolsAndDecimals = pools.map((pool) => {
pool.t0.reserve = formatUnits(pool.t0.reserve, pool.t0.decimals);
pool.t1.reserve = formatUnits(pool.t1.reserve, pool.t1.decimals);
const t0Info = addressToInfo[pool.t0.address.toLowerCase()] || {
symbol: "Unknown",
decimals: 18,

Check failure on line 30 in tasks/pools.ts

View workflow job for this annotation

GitHub Actions / build

Expected object keys to be in ascending order. 'decimals' should be before 'symbol'
Expand All @@ -41,20 +43,20 @@ const main = async (args: any, hre: HardhatRuntimeEnvironment) => {

const tableData = {} as any;
poolsWithSymbolsAndDecimals.forEach((pool) => {
const t0ReserveFormatted = parseFloat(
formatUnits(pool.t0.reserve, pool.t0.decimals)
).toFixed(2);
const t1ReserveFormatted = parseFloat(
formatUnits(pool.t1.reserve, pool.t1.decimals)
).toFixed(2);
const r0 = parseFloat(pool.t0.reserve).toFixed(2);
const r1 = parseFloat(pool.t1.reserve).toFixed(2);

tableData[pool.pair] = {
Pool: `${pool.t0.symbol} / ${pool.t1.symbol}`,
Reserves: `${t0ReserveFormatted} / ${t1ReserveFormatted}`,
Reserves: `${r0} / ${r1}`,
};
});

console.table(tableData);
if (args.json) {
console.log(poolsWithSymbolsAndDecimals);
} else {
console.table(tableData);
}
};

export const poolsTask = task("pools", "", main).addFlag(
Expand Down

0 comments on commit bc629af

Please sign in to comment.