From 36007ac4489147d2914d261d3ba2fd084141b5ac Mon Sep 17 00:00:00 2001 From: Deepak Date: Tue, 17 Oct 2023 13:37:00 +0530 Subject: [PATCH] Bug fixes --- ui/src/components/SwapForm.jsx | 94 +++++++++++++++++++++++++++++++++- ui/src/utils/liquidity.js | 60 ---------------------- ui/src/utils/swap.js | 34 ------------ 3 files changed, 92 insertions(+), 96 deletions(-) delete mode 100644 ui/src/utils/liquidity.js delete mode 100644 ui/src/utils/swap.js diff --git a/ui/src/components/SwapForm.jsx b/ui/src/components/SwapForm.jsx index 2d024c9..42b03f7 100644 --- a/ui/src/components/SwapForm.jsx +++ b/ui/src/components/SwapForm.jsx @@ -1,10 +1,100 @@ import { ethers } from "ethers"; import { useContext, useEffect, useState } from "react"; import { MetaMaskContext } from "../context/Metamask"; -import swap from "../utils/swap"; -import addLiquidity from "../utils/liquidity"; import TokensList from "./TokensList"; +const addLiquidity = ( + account, + { token0, token1, manager }, + { managerAddress, poolAddress } +) => { + if (!token0 || !token1) { + return; + } + + const amount0 = ethers.utils.parseEther("0.998976618347425280"); + const amount1 = ethers.utils.parseEther("5000"); // 5000 USDC + const lowerTick = 84222; + const upperTick = 86129; + const liquidity = ethers.BigNumber.from("1517882343751509868544"); + const extra = ethers.utils.defaultAbiCoder.encode( + ["address", "address", "address"], + [token0.address, token1.address, account] + ); + + // Approve the manager to spend the tokens + Promise.all([ + token0.allowance(account, managerAddress), + token1.allowance(account, managerAddress), + ]) + .then(([allowance0, allowance1]) => { + return ( + Promise.resolve() + .then(() => { + if (allowance0.lt(amount0)) { + return token0 + .approve(managerAddress, amount0) + .then((tx) => tx.wait()); + } + }) + .then(() => { + if (allowance1.lt(amount1)) { + return token1 + .approve(managerAddress, amount1) + .then((tx) => tx.wait()); + } + }) + + // Once the manager is approved, mint the liquidity + .then(() => { + return manager + .mint(poolAddress, lowerTick, upperTick, liquidity, extra) + .then((tx) => tx.wait()); + }) + .then(() => { + alert("Liquidity added!"); + }) + ); + }) + .catch((err) => { + console.error(err); + alert("Failed!"); + }); +}; + +const swap = ( + amountIn, + account, + { tokenIn, manager, token0, token1 }, + { managerAddress, poolAddress } +) => { + const amountInWei = ethers.utils.parseEther(amountIn); + const extra = ethers.utils.defaultAbiCoder.encode( + ["address", "address", "address"], + [token0.address, token1.address, account] + ); + + tokenIn + .allowance(account, managerAddress) + .then((allowance) => { + if (allowance.lt(amountInWei)) { + return tokenIn + .approve(managerAddress, amountInWei) + .then((tx) => tx.wait()); + } + }) + .then(() => { + return manager.swap(poolAddress, extra).then((tx) => tx.wait()); + }) + .then(() => { + alert("Swap succeeded!"); + }) + .catch((err) => { + console.error(err); + alert("Failed!"); + }); +}; + const SwapForm = (props) => { const metamaskContext = useContext(MetaMaskContext); const enabled = metamaskContext.status === "connected"; diff --git a/ui/src/utils/liquidity.js b/ui/src/utils/liquidity.js deleted file mode 100644 index ff5cd85..0000000 --- a/ui/src/utils/liquidity.js +++ /dev/null @@ -1,60 +0,0 @@ -import { ethers } from "ethers"; - -export const addLiquidity = ( - account, - { token0, token1, manager }, - { managerAddress, poolAddress } -) => { - if (!token0 || !token1) { - return; - } - - const amount0 = ethers.utils.parseEther("0.998976618347425280"); - const amount1 = ethers.utils.parseEther("5000"); // 5000 USDC - const lowerTick = 84222; - const upperTick = 86129; - const liquidity = ethers.BigNumber.from("1517882343751509868544"); - const extra = ethers.utils.defaultAbiCoder.encode( - ["address", "address", "address"], - [token0.address, token1.address, account] - ); - - // Approve the manager to spend the tokens - Promise.all([ - token0.allowance(account, managerAddress), - token1.allowance(account, managerAddress), - ]) - .then(([allowance0, allowance1]) => { - return ( - Promise.resolve() - .then(() => { - if (allowance0.lt(amount0)) { - return token0 - .approve(managerAddress, amount0) - .then((tx) => tx.wait()); - } - }) - .then(() => { - if (allowance1.lt(amount1)) { - return token1 - .approve(managerAddress, amount1) - .then((tx) => tx.wait()); - } - }) - - // Once the manager is approved, mint the liquidity - .then(() => { - return manager - .mint(poolAddress, lowerTick, upperTick, liquidity, extra) - .then((tx) => tx.wait()); - }) - .then(() => { - alert("Liquidity added!"); - }) - ); - }) - .catch((err) => { - console.error(err); - alert("Failed!"); - }); -}; diff --git a/ui/src/utils/swap.js b/ui/src/utils/swap.js deleted file mode 100644 index 37987ea..0000000 --- a/ui/src/utils/swap.js +++ /dev/null @@ -1,34 +0,0 @@ -import { ethers } from "ethers"; - -export const swap = ( - amountIn, - account, - { tokenIn, manager, token0, token1 }, - { managerAddress, poolAddress } -) => { - const amountInWei = ethers.utils.parseEther(amountIn); - const extra = ethers.utils.defaultAbiCoder.encode( - ["address", "address", "address"], - [token0.address, token1.address, account] - ); - - tokenIn - .allowance(account, managerAddress) - .then((allowance) => { - if (allowance.lt(amountInWei)) { - return tokenIn - .approve(managerAddress, amountInWei) - .then((tx) => tx.wait()); - } - }) - .then(() => { - return manager.swap(poolAddress, extra).then((tx) => tx.wait()); - }) - .then(() => { - alert("Swap succeeded!"); - }) - .catch((err) => { - console.error(err); - alert("Failed!"); - }); -};