Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bug fixes #3

Merged
merged 1 commit into from
Oct 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
94 changes: 92 additions & 2 deletions ui/src/components/SwapForm.jsx
Original file line number Diff line number Diff line change
@@ -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";
Expand Down
60 changes: 0 additions & 60 deletions ui/src/utils/liquidity.js

This file was deleted.

34 changes: 0 additions & 34 deletions ui/src/utils/swap.js

This file was deleted.