integrate native coin into dex
Signed-off-by: Uncle Fatso <uncle.fatso@ghostchain.io>
This commit is contained in:
parent
7be03aaa9f
commit
8d23d55ae2
@ -1,7 +1,7 @@
|
|||||||
{
|
{
|
||||||
"name": "ghost-dao-interface",
|
"name": "ghost-dao-interface",
|
||||||
"private": true,
|
"private": true,
|
||||||
"version": "0.6.4",
|
"version": "0.6.5",
|
||||||
"type": "module",
|
"type": "module",
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"dev": "vite",
|
"dev": "vite",
|
||||||
|
|||||||
@ -215,7 +215,7 @@ function App() {
|
|||||||
: <Route path="/wrapper" element={<Wrapper config={config} connect={tryConnectInjected} address={address} chainId={addressChainId ? addressChainId : chainId} />} />
|
: <Route path="/wrapper" element={<Wrapper config={config} connect={tryConnectInjected} address={address} chainId={addressChainId ? addressChainId : chainId} />} />
|
||||||
}
|
}
|
||||||
<Route path="/bridge" element={<Bridge config={config} connect={tryConnectInjected} address={address} chainId={addressChainId ? addressChainId : chainId} />} />
|
<Route path="/bridge" element={<Bridge config={config} connect={tryConnectInjected} address={address} chainId={addressChainId ? addressChainId : chainId} />} />
|
||||||
<Route path="/dex/:name" element={<Dex connect={tryConnectInjected} address={address} chainId={addressChainId ? addressChainId : chainId} />} />
|
<Route path="/dex/:name" element={<Dex config={config} connect={tryConnectInjected} address={address} chainId={addressChainId ? addressChainId : chainId} />} />
|
||||||
{isGovernanceAvailable(chainId, addressChainId) && <Route path="/governance" element={<Governance config={config} connect={tryConnectInjected} address={address} chainId={addressChainId ? addressChainId : chainId} />} />}
|
{isGovernanceAvailable(chainId, addressChainId) && <Route path="/governance" element={<Governance config={config} connect={tryConnectInjected} address={address} chainId={addressChainId ? addressChainId : chainId} />} />}
|
||||||
{isGovernanceAvailable(chainId, addressChainId) && <Route path="/governance/:id" element={<ProposalDetails config={config} connect={tryConnectInjected} address={address} chainId={addressChainId ? addressChainId : chainId} />} />}
|
{isGovernanceAvailable(chainId, addressChainId) && <Route path="/governance/:id" element={<ProposalDetails config={config} connect={tryConnectInjected} address={address} chainId={addressChainId ? addressChainId : chainId} />} />}
|
||||||
{isGovernanceAvailable(chainId, addressChainId) && <Route path="/governance/create" element={<NewProposal config={config} connect={tryConnectInjected} address={address} chainId={addressChainId ? addressChainId : chainId} />} />}
|
{isGovernanceAvailable(chainId, addressChainId) && <Route path="/governance/create" element={<NewProposal config={config} connect={tryConnectInjected} address={address} chainId={addressChainId ? addressChainId : chainId} />} />}
|
||||||
|
|||||||
@ -58,6 +58,7 @@ export const TokenAllowanceGuard = ({
|
|||||||
height = "auto",
|
height = "auto",
|
||||||
spendAmount,
|
spendAmount,
|
||||||
tokenName,
|
tokenName,
|
||||||
|
isNative,
|
||||||
owner,
|
owner,
|
||||||
spender,
|
spender,
|
||||||
decimals,
|
decimals,
|
||||||
@ -88,7 +89,7 @@ export const TokenAllowanceGuard = ({
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (allowance && spendAmount && allowance.lt(spendAmount))
|
if (!isNative && allowance && spendAmount && allowance.lt(spendAmount))
|
||||||
return (
|
return (
|
||||||
<Grid container alignItems="center">
|
<Grid container alignItems="center">
|
||||||
<Grid item xs={12} sm={isVertical ? 12 : 8}>
|
<Grid item xs={12} sm={isVertical ? 12 : 8}>
|
||||||
|
|||||||
@ -16,6 +16,7 @@ import { formatCurrency, formatNumber } from "../../../helpers";
|
|||||||
import { DecimalBigNumber } from "../../../helpers/DecimalBigNumber"
|
import { DecimalBigNumber } from "../../../helpers/DecimalBigNumber"
|
||||||
import { tokenNameConverter } from "../../../helpers/tokenConverter";
|
import { tokenNameConverter } from "../../../helpers/tokenConverter";
|
||||||
import { isNetworkLegacy } from "../../../constants";
|
import { isNetworkLegacy } from "../../../constants";
|
||||||
|
import { EMPTY_ADDRESS } from "../../../constants/addresses";
|
||||||
|
|
||||||
import GhostStyledIcon from "../../Icon/GhostIcon";
|
import GhostStyledIcon from "../../Icon/GhostIcon";
|
||||||
import TokenStack from "../../TokenStack/TokenStack";
|
import TokenStack from "../../TokenStack/TokenStack";
|
||||||
@ -305,7 +306,7 @@ export const Tokens = ({ address, tokens, onClose }) => {
|
|||||||
const tokenProps = (token) => ({
|
const tokenProps = (token) => ({
|
||||||
...token,
|
...token,
|
||||||
expanded: expanded === token.symbol,
|
expanded: expanded === token.symbol,
|
||||||
reserveAddress: tokens.reserve.address,
|
reserveAddress: EMPTY_ADDRESS,
|
||||||
onChangeExpanded: (e, isExpanded) => setExpanded(isExpanded ? token.symbol : null),
|
onChangeExpanded: (e, isExpanded) => setExpanded(isExpanded ? token.symbol : null),
|
||||||
onAddTokenToWallet: () => addTokenToWallet(token, address),
|
onAddTokenToWallet: () => addTokenToWallet(token, address),
|
||||||
onClose: () => onClose(),
|
onClose: () => onClose(),
|
||||||
|
|||||||
@ -1,5 +1,7 @@
|
|||||||
import { NetworkId } from "../constants";
|
import { NetworkId } from "../constants";
|
||||||
|
|
||||||
|
export const EMPTY_ADDRESS = "0x0000000000000000000000000000000000000000";
|
||||||
|
|
||||||
export const STAKING_ADDRESSES = {
|
export const STAKING_ADDRESSES = {
|
||||||
[NetworkId.TESTNET_SEPOLIA]: "0xC2C579631Bf6daA93252154080fecfd68c6aa506",
|
[NetworkId.TESTNET_SEPOLIA]: "0xC2C579631Bf6daA93252154080fecfd68c6aa506",
|
||||||
[NetworkId.TESTNET_HOODI]: "0x25F62eDc6C89FF84E957C22336A35d2dfc861a86",
|
[NetworkId.TESTNET_HOODI]: "0x25F62eDc6C89FF84E957C22336A35d2dfc861a86",
|
||||||
|
|||||||
@ -10,7 +10,7 @@ import {
|
|||||||
useTheme,
|
useTheme,
|
||||||
} from "@mui/material";
|
} from "@mui/material";
|
||||||
import SettingsIcon from '@mui/icons-material/Settings';
|
import SettingsIcon from '@mui/icons-material/Settings';
|
||||||
import { useEffect, useState } from "react";
|
import { useEffect, useMemo, useState } from "react";
|
||||||
import { useParams, useLocation, useSearchParams } from "react-router-dom";
|
import { useParams, useLocation, useSearchParams } from "react-router-dom";
|
||||||
import { Helmet } from "react-helmet";
|
import { Helmet } from "react-helmet";
|
||||||
import ReactGA from "react-ga4";
|
import ReactGA from "react-ga4";
|
||||||
@ -28,14 +28,17 @@ import {
|
|||||||
UNISWAP_V2_FACTORY,
|
UNISWAP_V2_FACTORY,
|
||||||
RESERVE_ADDRESSES,
|
RESERVE_ADDRESSES,
|
||||||
FTSO_ADDRESSES,
|
FTSO_ADDRESSES,
|
||||||
|
EMPTY_ADDRESS,
|
||||||
|
WETH_ADDRESSES,
|
||||||
} from "../../constants/addresses";
|
} from "../../constants/addresses";
|
||||||
import { useTokenSymbol } from "../../hooks/tokens";
|
import { useTokenSymbol } from "../../hooks/tokens";
|
||||||
|
import { getTokenAddress } from "../../hooks/helpers";
|
||||||
|
|
||||||
import PoolContainer from "./PoolContainer";
|
import PoolContainer from "./PoolContainer";
|
||||||
import SwapContainer from "./SwapContainer";
|
import SwapContainer from "./SwapContainer";
|
||||||
import TokenModal from "./TokenModal";
|
import TokenModal from "./TokenModal";
|
||||||
|
|
||||||
const Dex = ({ chainId, address, connect }) => {
|
const Dex = ({ chainId, address, connect, config }) => {
|
||||||
const location = useLocation();
|
const location = useLocation();
|
||||||
const pathname = useParams();
|
const pathname = useParams();
|
||||||
|
|
||||||
@ -58,11 +61,44 @@ const Dex = ({ chainId, address, connect }) => {
|
|||||||
const [actualDestinationAddress, setActualDestinationAddress] = useState(localStorage.getItem("dex-destination"));
|
const [actualDestinationAddress, setActualDestinationAddress] = useState(localStorage.getItem("dex-destination"));
|
||||||
const [destinationAddress, setDestinationAddress] = useState(actualDestinationAddress);
|
const [destinationAddress, setDestinationAddress] = useState(actualDestinationAddress);
|
||||||
|
|
||||||
const [tokenAddressTop, setTokenAddressTop] = useState(RESERVE_ADDRESSES[chainId]);
|
const [tokenAddressTop, setTokenAddressTop] = useState(EMPTY_ADDRESS);
|
||||||
const [tokenAddressBottom, setTokenAddressBottom] = useState(FTSO_ADDRESSES[chainId]);
|
const [tokenAddressBottom, setTokenAddressBottom] = useState(FTSO_ADDRESSES[chainId]);
|
||||||
|
|
||||||
const { symbol: tokenNameTop } = useTokenSymbol(chainId, tokenAddressTop);
|
const { symbol: tokenNameTopInner } = useTokenSymbol(chainId, tokenAddressTop);
|
||||||
const { symbol: tokenNameBottom } = useTokenSymbol(chainId, tokenAddressBottom);
|
const { symbol: tokenNameBottomInner } = useTokenSymbol(chainId, tokenAddressBottom);
|
||||||
|
|
||||||
|
const chainSymbol = useMemo(() => {
|
||||||
|
const chainSymbol = config?.getClient()?.chain?.nativeCurrency?.symbol;
|
||||||
|
if (chainSymbol) return chainSymbol;
|
||||||
|
return "WTF";
|
||||||
|
}, [config])
|
||||||
|
|
||||||
|
const tokenNameTop = useMemo(() => {
|
||||||
|
if (chainSymbol && tokenAddressTop === EMPTY_ADDRESS) {
|
||||||
|
return chainSymbol;
|
||||||
|
}
|
||||||
|
return tokenNameTopInner;
|
||||||
|
}, [tokenAddressTop, tokenNameTopInner, chainSymbol]);
|
||||||
|
|
||||||
|
const tokenNameBottom = useMemo(() => {
|
||||||
|
const chainSymbol = config?.getClient()?.chain?.nativeCurrency?.symbol;
|
||||||
|
if (chainSymbol && tokenAddressBottom === EMPTY_ADDRESS) {
|
||||||
|
return chainSymbol;
|
||||||
|
}
|
||||||
|
return tokenNameBottomInner;
|
||||||
|
}, [tokenAddressBottom, tokenNameBottomInner, config]);
|
||||||
|
|
||||||
|
const isWrapping = useMemo(() => {
|
||||||
|
const isNative = tokenAddressTop === EMPTY_ADDRESS;
|
||||||
|
const isWrappedNative = tokenAddressBottom === WETH_ADDRESSES[chainId];
|
||||||
|
return isNative && isWrappedNative;
|
||||||
|
}, [chainId, tokenAddressTop, tokenAddressBottom]);
|
||||||
|
|
||||||
|
const isUnwrapping = useMemo(() => {
|
||||||
|
const isWrappedNative = tokenAddressTop === WETH_ADDRESSES[chainId];
|
||||||
|
const isNative = tokenAddressBottom === EMPTY_ADDRESS;
|
||||||
|
return isNative && isWrappedNative;
|
||||||
|
}, [chainId, tokenAddressTop, tokenAddressBottom]);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (currentQueryParameters.has("pool")) {
|
if (currentQueryParameters.has("pool")) {
|
||||||
@ -77,8 +113,8 @@ const Dex = ({ chainId, address, connect }) => {
|
|||||||
setTokenAddressTop(currentQueryParameters.get("from"));
|
setTokenAddressTop(currentQueryParameters.get("from"));
|
||||||
newQueryParameters.set("from", currentQueryParameters.get("from"));
|
newQueryParameters.set("from", currentQueryParameters.get("from"));
|
||||||
} else {
|
} else {
|
||||||
setTokenAddressTop(RESERVE_ADDRESSES[chainId]);
|
setTokenAddressTop(EMPTY_ADDRESS);
|
||||||
newQueryParameters.set("from", RESERVE_ADDRESSES[chainId]);
|
newQueryParameters.set("from", EMPTY_ADDRESS);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (currentQueryParameters.has("to")) {
|
if (currentQueryParameters.has("to")) {
|
||||||
@ -94,7 +130,7 @@ const Dex = ({ chainId, address, connect }) => {
|
|||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
ReactGA.send({ hitType: "pageview", page: location.pathname + location.search });
|
ReactGA.send({ hitType: "pageview", page: location.pathname + location.search });
|
||||||
}, [location])
|
}, [location]);
|
||||||
|
|
||||||
const dexAddresses = {
|
const dexAddresses = {
|
||||||
router: UNISWAP_V2_ROUTER[chainId],
|
router: UNISWAP_V2_ROUTER[chainId],
|
||||||
@ -115,7 +151,7 @@ const Dex = ({ chainId, address, connect }) => {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const changeSwapTab = (swap) => {
|
const changeSwapTab = (swap) => {
|
||||||
if (swap) newQueryParameters.delete("pool");
|
if (swap || (isWrapping || isUnwrapping)) newQueryParameters.delete("pool");
|
||||||
else newQueryParameters.set("pool", true);
|
else newQueryParameters.set("pool", true);
|
||||||
newQueryParameters.set("from", currentQueryParameters.get("from"));
|
newQueryParameters.set("from", currentQueryParameters.get("from"));
|
||||||
newQueryParameters.set("to", currentQueryParameters.get("to"));
|
newQueryParameters.set("to", currentQueryParameters.get("to"));
|
||||||
@ -315,6 +351,7 @@ const Dex = ({ chainId, address, connect }) => {
|
|||||||
</Modal>
|
</Modal>
|
||||||
|
|
||||||
<TokenModal
|
<TokenModal
|
||||||
|
chainSymbol={chainSymbol}
|
||||||
account={address}
|
account={address}
|
||||||
chainId={chainId}
|
chainId={chainId}
|
||||||
listOpen={topTokenListOpen}
|
listOpen={topTokenListOpen}
|
||||||
@ -322,6 +359,7 @@ const Dex = ({ chainId, address, connect }) => {
|
|||||||
setTokenAddress={setInnerTokenAddressTop}
|
setTokenAddress={setInnerTokenAddressTop}
|
||||||
/>
|
/>
|
||||||
<TokenModal
|
<TokenModal
|
||||||
|
chainSymbol={chainSymbol}
|
||||||
account={address}
|
account={address}
|
||||||
chainId={chainId}
|
chainId={chainId}
|
||||||
listOpen={bottomTokenListOpen}
|
listOpen={bottomTokenListOpen}
|
||||||
@ -373,6 +411,8 @@ const Dex = ({ chainId, address, connect }) => {
|
|||||||
setBottomTokenListOpen={setBottomTokenListOpen}
|
setBottomTokenListOpen={setBottomTokenListOpen}
|
||||||
setIsSwap={setIsSwap}
|
setIsSwap={setIsSwap}
|
||||||
formatDecimals={formatDecimals}
|
formatDecimals={formatDecimals}
|
||||||
|
isWrapping={isWrapping}
|
||||||
|
isUnwrapping={isUnwrapping}
|
||||||
/>
|
/>
|
||||||
:
|
:
|
||||||
<PoolContainer
|
<PoolContainer
|
||||||
|
|||||||
@ -12,7 +12,12 @@ import { DecimalBigNumber } from "../../helpers/DecimalBigNumber";
|
|||||||
import { formatNumber, formatCurrency, bigIntSqrt } from "../../helpers";
|
import { formatNumber, formatCurrency, bigIntSqrt } from "../../helpers";
|
||||||
|
|
||||||
import { useBalance, useTotalSupply } from "../../hooks/tokens";
|
import { useBalance, useTotalSupply } from "../../hooks/tokens";
|
||||||
import { useUniswapV2Pair, useUniswapV2PairReserves, addLiquidity } from "../../hooks/uniswapv2";
|
import {
|
||||||
|
useUniswapV2Pair,
|
||||||
|
useUniswapV2PairReserves,
|
||||||
|
addLiquidity,
|
||||||
|
addLiquidityETH,
|
||||||
|
} from "../../hooks/uniswapv2";
|
||||||
|
|
||||||
const PoolContainer = ({
|
const PoolContainer = ({
|
||||||
tokenNameTop,
|
tokenNameTop,
|
||||||
@ -27,7 +32,7 @@ const PoolContainer = ({
|
|||||||
secondsToWait,
|
secondsToWait,
|
||||||
setTopTokenListOpen,
|
setTopTokenListOpen,
|
||||||
setBottomTokenListOpen,
|
setBottomTokenListOpen,
|
||||||
formatDecimals
|
formatDecimals,
|
||||||
}) => {
|
}) => {
|
||||||
const theme = useTheme();
|
const theme = useTheme();
|
||||||
const isSmallScreen = useMediaQuery("(max-width: 456px)");
|
const isSmallScreen = useMediaQuery("(max-width: 456px)");
|
||||||
@ -40,11 +45,13 @@ const PoolContainer = ({
|
|||||||
balance: balanceTop,
|
balance: balanceTop,
|
||||||
refetch: balanceRefetchTop,
|
refetch: balanceRefetchTop,
|
||||||
contractAddress: addressTop,
|
contractAddress: addressTop,
|
||||||
|
isNative: topIsNative,
|
||||||
} = useBalance(chainId, tokenNameTop, address);
|
} = useBalance(chainId, tokenNameTop, address);
|
||||||
const {
|
const {
|
||||||
balance: balanceBottom,
|
balance: balanceBottom,
|
||||||
refetch: balanceRefetchBottom,
|
refetch: balanceRefetchBottom,
|
||||||
contractAddress: addressBottom,
|
contractAddress: addressBottom,
|
||||||
|
isNative: bottomIsNative,
|
||||||
} = useBalance(chainId, tokenNameBottom, address);
|
} = useBalance(chainId, tokenNameBottom, address);
|
||||||
|
|
||||||
const {
|
const {
|
||||||
@ -142,7 +149,7 @@ const PoolContainer = ({
|
|||||||
const amountAMin = amountADesired * bigIntSlippage / one;
|
const amountAMin = amountADesired * bigIntSlippage / one;
|
||||||
const amountBMin = amountBDesired * bigIntSlippage / one;
|
const amountBMin = amountBDesired * bigIntSlippage / one;
|
||||||
|
|
||||||
await addLiquidity(
|
const params = {
|
||||||
chainId,
|
chainId,
|
||||||
tokenNameTop,
|
tokenNameTop,
|
||||||
tokenNameBottom,
|
tokenNameBottom,
|
||||||
@ -150,9 +157,16 @@ const PoolContainer = ({
|
|||||||
amountBDesired,
|
amountBDesired,
|
||||||
amountAMin,
|
amountAMin,
|
||||||
amountBMin,
|
amountBMin,
|
||||||
|
address,
|
||||||
destination,
|
destination,
|
||||||
deadline,
|
deadline,
|
||||||
);
|
}
|
||||||
|
|
||||||
|
if (topIsNative || bottomIsNative) {
|
||||||
|
await addLiquidityETH(params)
|
||||||
|
} else {
|
||||||
|
await addLiquidity(params);
|
||||||
|
}
|
||||||
|
|
||||||
await balanceRefetchTop();
|
await balanceRefetchTop();
|
||||||
await balanceRefetchBottom();
|
await balanceRefetchBottom();
|
||||||
@ -233,6 +247,7 @@ const PoolContainer = ({
|
|||||||
owner={address}
|
owner={address}
|
||||||
spender={dexAddresses.router}
|
spender={dexAddresses.router}
|
||||||
decimals={balanceTop._decimals}
|
decimals={balanceTop._decimals}
|
||||||
|
isNative={topIsNative}
|
||||||
approvalText={"Approve " + tokenNameTop}
|
approvalText={"Approve " + tokenNameTop}
|
||||||
approvalPendingText={"Approving..."}
|
approvalPendingText={"Approving..."}
|
||||||
connect={connect}
|
connect={connect}
|
||||||
@ -245,6 +260,7 @@ const PoolContainer = ({
|
|||||||
owner={address}
|
owner={address}
|
||||||
spender={dexAddresses.router}
|
spender={dexAddresses.router}
|
||||||
decimals={balanceBottom._decimals}
|
decimals={balanceBottom._decimals}
|
||||||
|
isNative={bottomIsNative}
|
||||||
approvalText={"Approve " + tokenNameBottom}
|
approvalText={"Approve " + tokenNameBottom}
|
||||||
approvalPendingText={"Approving..."}
|
approvalPendingText={"Approving..."}
|
||||||
connect={connect}
|
connect={connect}
|
||||||
|
|||||||
@ -1,4 +1,4 @@
|
|||||||
import { useState, useEffect } from "react";
|
import { useState, useMemo, useEffect } from "react";
|
||||||
import { Box, Typography, useTheme, useMediaQuery } from "@mui/material";
|
import { Box, Typography, useTheme, useMediaQuery } from "@mui/material";
|
||||||
import toast from "react-hot-toast";
|
import toast from "react-hot-toast";
|
||||||
|
|
||||||
@ -13,8 +13,16 @@ import { DecimalBigNumber } from "../../helpers/DecimalBigNumber";
|
|||||||
import { prettifySecondsInDays } from "../../helpers/timeUtil";
|
import { prettifySecondsInDays } from "../../helpers/timeUtil";
|
||||||
|
|
||||||
import { getTokenAddress } from "../../hooks/helpers";
|
import { getTokenAddress } from "../../hooks/helpers";
|
||||||
import { useBalance } from "../../hooks/tokens";
|
import { useBalance, depositNative, withdrawWeth } from "../../hooks/tokens";
|
||||||
import { useUniswapV2Pair, useUniswapV2PairReserves, swapExactTokensForTokens } from "../../hooks/uniswapv2";
|
import {
|
||||||
|
useUniswapV2Pair,
|
||||||
|
useUniswapV2PairReserves,
|
||||||
|
swapExactTokensForTokens,
|
||||||
|
swapExactETHForTokens,
|
||||||
|
swapExactTokensForETH,
|
||||||
|
} from "../../hooks/uniswapv2";
|
||||||
|
|
||||||
|
import { EMPTY_ADDRESS } from "../../constants/addresses";
|
||||||
|
|
||||||
const SwapContainer = ({
|
const SwapContainer = ({
|
||||||
tokenNameTop,
|
tokenNameTop,
|
||||||
@ -30,7 +38,9 @@ const SwapContainer = ({
|
|||||||
destination,
|
destination,
|
||||||
secondsToWait,
|
secondsToWait,
|
||||||
setIsSwap,
|
setIsSwap,
|
||||||
formatDecimals
|
formatDecimals,
|
||||||
|
isWrapping,
|
||||||
|
isUnwrapping,
|
||||||
}) => {
|
}) => {
|
||||||
const theme = useTheme();
|
const theme = useTheme();
|
||||||
const isSmallScreen = useMediaQuery("(max-width: 456px)");
|
const isSmallScreen = useMediaQuery("(max-width: 456px)");
|
||||||
@ -46,12 +56,14 @@ const SwapContainer = ({
|
|||||||
balance: balanceTop,
|
balance: balanceTop,
|
||||||
refetch: balanceRefetchTop,
|
refetch: balanceRefetchTop,
|
||||||
contractAddress: addressTop,
|
contractAddress: addressTop,
|
||||||
|
isNative: topIsNative,
|
||||||
} = useBalance(chainId, tokenNameTop, address);
|
} = useBalance(chainId, tokenNameTop, address);
|
||||||
|
|
||||||
const {
|
const {
|
||||||
balance: balanceBottom,
|
balance: balanceBottom,
|
||||||
refetch: balanceRefetchBottom,
|
refetch: balanceRefetchBottom,
|
||||||
contractAddress: addressBottom,
|
contractAddress: addressBottom,
|
||||||
|
isNative: bottomIsNative,
|
||||||
} = useBalance(chainId, tokenNameBottom, address);
|
} = useBalance(chainId, tokenNameBottom, address);
|
||||||
|
|
||||||
const {
|
const {
|
||||||
@ -75,14 +87,18 @@ const SwapContainer = ({
|
|||||||
const setMax = () => setAmountTop(balanceTop.toString());
|
const setMax = () => setAmountTop(balanceTop.toString());
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
|
if (isWrapping || isUnwrapping) {
|
||||||
|
setAmountBottom(amountTop.toString());
|
||||||
|
setNextPrice("1");
|
||||||
|
setCurrentPrice("1");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
const zero = new DecimalBigNumber(0n, 0);
|
const zero = new DecimalBigNumber(0n, 0);
|
||||||
const raw = new DecimalBigNumber(amountTop, balanceTop._decimals);
|
const raw = new DecimalBigNumber(amountTop, balanceTop._decimals);
|
||||||
const amountInRaw = new DecimalBigNumber(raw._value.toBigInt(), balanceTop._decimals);
|
const amountInRaw = new DecimalBigNumber(raw._value.toBigInt(), balanceTop._decimals);
|
||||||
const amountInWithFee = amountInRaw.mul(new DecimalBigNumber(997n, 3));
|
const amountInWithFee = amountInRaw.mul(new DecimalBigNumber(997n, 3));
|
||||||
|
|
||||||
const topAddress = getTokenAddress(chainId, tokenNameTop);
|
|
||||||
const bottomAddress = getTokenAddress(chainId, tokenNameBottom);
|
|
||||||
|
|
||||||
const amountIn = addressTop.toUpperCase() === tokenAddresses.token0.toUpperCase() ? pairReserves.reserve0 : pairReserves.reserve1;
|
const amountIn = addressTop.toUpperCase() === tokenAddresses.token0.toUpperCase() ? pairReserves.reserve0 : pairReserves.reserve1;
|
||||||
const amountOut = addressBottom.toUpperCase() === tokenAddresses.token1.toUpperCase() ? pairReserves.reserve1 : pairReserves.reserve0;
|
const amountOut = addressBottom.toUpperCase() === tokenAddresses.token1.toUpperCase() ? pairReserves.reserve1 : pairReserves.reserve0;
|
||||||
|
|
||||||
@ -103,7 +119,15 @@ const SwapContainer = ({
|
|||||||
setAmountBottom(amountOut.sub(newAmountOut).toString());
|
setAmountBottom(amountOut.sub(newAmountOut).toString());
|
||||||
setNextPrice(denominator.div(newAmountOut).toString())
|
setNextPrice(denominator.div(newAmountOut).toString())
|
||||||
}
|
}
|
||||||
}, [amountTop, addressTop]);
|
}, [amountTop, addressTop, isWrapping, isUnwrapping]);
|
||||||
|
|
||||||
|
const buttonText = useMemo(() => {
|
||||||
|
let text = "Swap";
|
||||||
|
if (isWrapping) text = "Wrap";
|
||||||
|
else if (isUnwrapping) text = "Unwrap";
|
||||||
|
else if (pairAddress === EMPTY_ADDRESS) text = "Create Pool";
|
||||||
|
return text;
|
||||||
|
}, [isWrapping, isUnwrapping, pairAddress]);
|
||||||
|
|
||||||
const swapTokens = async () => {
|
const swapTokens = async () => {
|
||||||
setIsPending(true);
|
setIsPending(true);
|
||||||
@ -120,14 +144,30 @@ const SwapContainer = ({
|
|||||||
const amountBDesired = BigInt(Math.round(parseFloat(amountBottom) * Math.pow(10, balanceBottom._decimals)));
|
const amountBDesired = BigInt(Math.round(parseFloat(amountBottom) * Math.pow(10, balanceBottom._decimals)));
|
||||||
const amountBMin = amountBDesired * bigIntSlippage / one;
|
const amountBMin = amountBDesired * bigIntSlippage / one;
|
||||||
|
|
||||||
await swapExactTokensForTokens(
|
if (isWrapping) {
|
||||||
chainId,
|
await depositNative(chainId, address, amountADesired);
|
||||||
amountADesired,
|
} else if (isUnwrapping) {
|
||||||
amountBMin,
|
await withdrawWeth(chainId, address, amountADesired);
|
||||||
[tokenNameTop, tokenNameBottom],
|
} else {
|
||||||
destination,
|
const params = {
|
||||||
deadline
|
chainId,
|
||||||
);
|
amountADesired,
|
||||||
|
amountBMin,
|
||||||
|
tokenNameTop,
|
||||||
|
tokenNameBottom,
|
||||||
|
destination,
|
||||||
|
address,
|
||||||
|
deadline
|
||||||
|
};
|
||||||
|
|
||||||
|
if (topIsNative) {
|
||||||
|
await swapExactETHForTokens(params)
|
||||||
|
} else if (bottomIsNative) {
|
||||||
|
await swapExactTokensForETH(params)
|
||||||
|
} else {
|
||||||
|
await swapExactTokensForTokens(params);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
await balanceRefetchTop();
|
await balanceRefetchTop();
|
||||||
await balanceRefetchBottom();
|
await balanceRefetchBottom();
|
||||||
@ -204,6 +244,7 @@ const SwapContainer = ({
|
|||||||
owner={address}
|
owner={address}
|
||||||
spender={dexAddresses.router}
|
spender={dexAddresses.router}
|
||||||
decimals={balanceTop._decimals}
|
decimals={balanceTop._decimals}
|
||||||
|
isNative={topIsNative}
|
||||||
approvalText={"Approve " + tokenNameTop}
|
approvalText={"Approve " + tokenNameTop}
|
||||||
approvalPendingText={"Approving..."}
|
approvalPendingText={"Approving..."}
|
||||||
connect={connect}
|
connect={connect}
|
||||||
@ -223,17 +264,10 @@ const SwapContainer = ({
|
|||||||
onClick={() => address === "" ?
|
onClick={() => address === "" ?
|
||||||
connect()
|
connect()
|
||||||
:
|
:
|
||||||
pairAddress === "0x0000000000000000000000000000000000000000" ? setIsSwap(false) : swapTokens()
|
(!isWrapping && !isUnwrapping) && pairAddress === EMPTY_ADDRESS ? setIsSwap(false) : swapTokens()
|
||||||
}
|
}
|
||||||
>
|
>
|
||||||
{address === "" ?
|
{address === "" ? "Connect" : buttonText }
|
||||||
"Connect"
|
|
||||||
:
|
|
||||||
pairAddress === "0x0000000000000000000000000000000000000000" ?
|
|
||||||
"Create Pool"
|
|
||||||
:
|
|
||||||
"Swap"
|
|
||||||
}
|
|
||||||
</SecondaryButton>
|
</SecondaryButton>
|
||||||
</TokenAllowanceGuard>
|
</TokenAllowanceGuard>
|
||||||
</Box>
|
</Box>
|
||||||
|
|||||||
@ -21,9 +21,15 @@ import { DecimalBigNumber } from "../../helpers/DecimalBigNumber";
|
|||||||
import { formatNumber } from "../../helpers/";
|
import { formatNumber } from "../../helpers/";
|
||||||
import { useBalance, useTokenSymbol } from "../../hooks/tokens";
|
import { useBalance, useTokenSymbol } from "../../hooks/tokens";
|
||||||
import { isNetworkLegacy } from "../../constants";
|
import { isNetworkLegacy } from "../../constants";
|
||||||
import { RESERVE_ADDRESSES, FTSO_ADDRESSES, STNK_ADDRESSES, GHST_ADDRESSES } from "../../constants/addresses";
|
import {
|
||||||
|
RESERVE_ADDRESSES,
|
||||||
|
FTSO_ADDRESSES,
|
||||||
|
STNK_ADDRESSES,
|
||||||
|
GHST_ADDRESSES,
|
||||||
|
EMPTY_ADDRESS
|
||||||
|
} from "../../constants/addresses";
|
||||||
|
|
||||||
const TokenModal = ({ chainId, account, listOpen, setListOpen, setTokenAddress }) => {
|
const TokenModal = ({ chainId, account, chainSymbol, listOpen, setListOpen, setTokenAddress }) => {
|
||||||
const isSmallScreen = useMediaQuery("(max-width: 599px)");
|
const isSmallScreen = useMediaQuery("(max-width: 599px)");
|
||||||
const isVerySmallScreen = useMediaQuery("(max-width: 425px)");
|
const isVerySmallScreen = useMediaQuery("(max-width: 425px)");
|
||||||
|
|
||||||
@ -39,6 +45,7 @@ const TokenModal = ({ chainId, account, listOpen, setListOpen, setTokenAddress }
|
|||||||
const { symbol: searchSymbol } = useTokenSymbol(chainId, address);
|
const { symbol: searchSymbol } = useTokenSymbol(chainId, address);
|
||||||
const { balance: searchBalance } = useBalance(chainId, address, account);
|
const { balance: searchBalance } = useBalance(chainId, address, account);
|
||||||
|
|
||||||
|
const { balance: nativeBalance } = useBalance(chainId, chainSymbol, account);
|
||||||
const { balance: reserveBalance } = useBalance(chainId, "RESERVE", account);
|
const { balance: reserveBalance } = useBalance(chainId, "RESERVE", account);
|
||||||
const { balance: ftsoBalance } = useBalance(chainId, "FTSO", account);
|
const { balance: ftsoBalance } = useBalance(chainId, "FTSO", account);
|
||||||
const { balance: stnkBalance } = useBalance(chainId, "STNK", account);
|
const { balance: stnkBalance } = useBalance(chainId, "STNK", account);
|
||||||
@ -49,9 +56,6 @@ const TokenModal = ({ chainId, account, listOpen, setListOpen, setTokenAddress }
|
|||||||
const { symbol: stnkSymbol } = useTokenSymbol(chainId, "STNK");
|
const { symbol: stnkSymbol } = useTokenSymbol(chainId, "STNK");
|
||||||
const { symbol: ghstSymbol } = useTokenSymbol(chainId, "GHST");
|
const { symbol: ghstSymbol } = useTokenSymbol(chainId, "GHST");
|
||||||
|
|
||||||
const config = useConfig();
|
|
||||||
const nativeSymbol = config?.getClient()?.chain?.nativeCurrency?.symbol;
|
|
||||||
|
|
||||||
const searchToken = useMemo(() => {
|
const searchToken = useMemo(() => {
|
||||||
return [{
|
return [{
|
||||||
name: searchSymbol,
|
name: searchSymbol,
|
||||||
@ -63,9 +67,15 @@ const TokenModal = ({ chainId, account, listOpen, setListOpen, setTokenAddress }
|
|||||||
|
|
||||||
const knownTokens = useMemo(() => {
|
const knownTokens = useMemo(() => {
|
||||||
return [
|
return [
|
||||||
|
{
|
||||||
|
name: chainSymbol,
|
||||||
|
icons: [chainSymbol],
|
||||||
|
balance: nativeBalance,
|
||||||
|
address: EMPTY_ADDRESS,
|
||||||
|
},
|
||||||
{
|
{
|
||||||
name: reserveSymbol,
|
name: reserveSymbol,
|
||||||
icons: isNetworkLegacy(chainId) ? ["GDAI"] : [nativeSymbol],
|
icons: isNetworkLegacy(chainId) ? ["GDAI"] : [chainSymbol],
|
||||||
balance: reserveBalance,
|
balance: reserveBalance,
|
||||||
address: RESERVE_ADDRESSES[chainId]
|
address: RESERVE_ADDRESSES[chainId]
|
||||||
},
|
},
|
||||||
|
|||||||
@ -22,7 +22,7 @@ import { isNetworkLegacy } from "../../../constants";
|
|||||||
import { useLpValuation } from "../../../hooks/treasury";
|
import { useLpValuation } from "../../../hooks/treasury";
|
||||||
import { useTotalSupply, useTokenSymbol } from "../../../hooks/tokens";
|
import { useTotalSupply, useTokenSymbol } from "../../../hooks/tokens";
|
||||||
|
|
||||||
import { RESERVE_ADDRESSES, FTSO_ADDRESSES } from "../../../constants/addresses";
|
import { EMPTY_ADDRESS, FTSO_ADDRESSES } from "../../../constants/addresses";
|
||||||
|
|
||||||
const FarmPools = ({ chainId }) => {
|
const FarmPools = ({ chainId }) => {
|
||||||
const isSmallScreen = useMediaQuery("(max-width: 775px)");
|
const isSmallScreen = useMediaQuery("(max-width: 775px)");
|
||||||
@ -42,7 +42,7 @@ const FarmPools = ({ chainId }) => {
|
|||||||
tvl: reserveFtsoUniValuation,
|
tvl: reserveFtsoUniValuation,
|
||||||
params: createSearchParams({
|
params: createSearchParams({
|
||||||
pool: "true",
|
pool: "true",
|
||||||
from: `${RESERVE_ADDRESSES[chainId]}`,
|
from: `${EMPTY_ADDRESS}`,
|
||||||
to: `${FTSO_ADDRESSES[chainId]}`,
|
to: `${FTSO_ADDRESSES[chainId]}`,
|
||||||
})
|
})
|
||||||
},
|
},
|
||||||
|
|||||||
@ -1,4 +1,4 @@
|
|||||||
export const tokenNameConverter = (chainId, name) => {
|
export const tokenNameConverter = (chainId, name, address) => {
|
||||||
if (name?.toUpperCase() === "WETH") {
|
if (name?.toUpperCase() === "WETH") {
|
||||||
switch (chainId) {
|
switch (chainId) {
|
||||||
case 63:
|
case 63:
|
||||||
|
|||||||
@ -142,6 +142,12 @@ export const getTokenAddress = (chainId, name) => {
|
|||||||
case "WMETC":
|
case "WMETC":
|
||||||
address = WETH_ADDRESSES[chainId];
|
address = WETH_ADDRESSES[chainId];
|
||||||
break;
|
break;
|
||||||
|
case "ETH":
|
||||||
|
address = undefined;
|
||||||
|
break;
|
||||||
|
case "METC":
|
||||||
|
address = undefined;
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
return address;
|
return address;
|
||||||
}
|
}
|
||||||
|
|||||||
@ -9,6 +9,7 @@ import { DecimalBigNumber } from "../../helpers/DecimalBigNumber";
|
|||||||
import { shorten } from "../../helpers";
|
import { shorten } from "../../helpers";
|
||||||
import { tokenNameConverter } from "../../helpers/tokenConverter";
|
import { tokenNameConverter } from "../../helpers/tokenConverter";
|
||||||
import { config } from "../../config";
|
import { config } from "../../config";
|
||||||
|
import { WETH_ADDRESSES } from "../../constants/addresses";
|
||||||
|
|
||||||
export const usePastVotes = (chainId, name, timepoint, address) => {
|
export const usePastVotes = (chainId, name, timepoint, address) => {
|
||||||
const decimals = getTokenDecimals(name);
|
const decimals = getTokenDecimals(name);
|
||||||
@ -60,20 +61,29 @@ export const useTotalSupply = (chainId, name) => {
|
|||||||
};
|
};
|
||||||
|
|
||||||
export const useBalance = (chainId, name, address) => {
|
export const useBalance = (chainId, name, address) => {
|
||||||
const contractAddress = getTokenAddress(chainId, name);
|
let contractAddress = getTokenAddress(chainId, name);
|
||||||
const { data, refetch, error } = useInnerBalance({
|
let isNative = false;
|
||||||
|
|
||||||
|
let requestObj = {
|
||||||
address,
|
address,
|
||||||
chainId,
|
chainId,
|
||||||
scopeKey: `balance-${contractAddress}-${address}-${chainId}`,
|
scopeKey: `balance-${contractAddress}-${address}-${chainId}`,
|
||||||
token: contractAddress,
|
};
|
||||||
});
|
|
||||||
|
|
||||||
|
if (contractAddress !== undefined) {
|
||||||
|
requestObj.token = contractAddress;
|
||||||
|
} else {
|
||||||
|
contractAddress = WETH_ADDRESSES[chainId];
|
||||||
|
isNative = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
const { data, refetch, error } = useInnerBalance(requestObj);
|
||||||
const balancePrepared = data ? data.value : 0n;
|
const balancePrepared = data ? data.value : 0n;
|
||||||
const decimals = data ? data.decimals : getTokenDecimals(name);
|
const decimals = data ? data.decimals : getTokenDecimals(name);
|
||||||
|
|
||||||
const balance = new DecimalBigNumber(balancePrepared, decimals);
|
const balance = new DecimalBigNumber(balancePrepared, decimals);
|
||||||
|
|
||||||
return { balance, refetch, contractAddress };
|
return { balance, refetch, contractAddress, isNative };
|
||||||
}
|
}
|
||||||
|
|
||||||
export const useAllowance = (chainId, name, owner, spender, decimals) => {
|
export const useAllowance = (chainId, name, owner, spender, decimals) => {
|
||||||
@ -96,6 +106,7 @@ export const useAllowance = (chainId, name, owner, spender, decimals) => {
|
|||||||
|
|
||||||
export const useTokenSymbol = (chainId, name) => {
|
export const useTokenSymbol = (chainId, name) => {
|
||||||
const contractAddress = getTokenAddress(chainId, name);
|
const contractAddress = getTokenAddress(chainId, name);
|
||||||
|
|
||||||
const { data, refetch } = useReadContract({
|
const { data, refetch } = useReadContract({
|
||||||
abi: getTokenAbi(name),
|
abi: getTokenAbi(name),
|
||||||
address: contractAddress,
|
address: contractAddress,
|
||||||
|
|||||||
@ -4,14 +4,12 @@ import { abi as UniswapV2Factory } from "../../abi/UniswapV2Factory.json";
|
|||||||
import { UNISWAP_V2_FACTORY } from "../../constants/addresses";
|
import { UNISWAP_V2_FACTORY } from "../../constants/addresses";
|
||||||
|
|
||||||
export const useUniswapV2Pair = (chainId, factoryAddress, token0, token1) => {
|
export const useUniswapV2Pair = (chainId, factoryAddress, token0, token1) => {
|
||||||
const t0 = token0 > token1 ? token0 : token1;
|
|
||||||
const t1 = token0 > token1 ? token1 : token0;
|
|
||||||
const { data, refetch } = useReadContract({
|
const { data, refetch } = useReadContract({
|
||||||
abi: UniswapV2Factory,
|
abi: UniswapV2Factory,
|
||||||
address: factoryAddress,
|
address: factoryAddress,
|
||||||
functionName: "getPair",
|
functionName: "getPair",
|
||||||
args: [token0, token1],
|
args: [token0, token1],
|
||||||
scopeKey: `getPair-${t0}-${t1}-${chainId}`,
|
scopeKey: `getPair-${token0}-${token1}-${chainId}`,
|
||||||
chainId: chainId,
|
chainId: chainId,
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
@ -2,91 +2,189 @@ import { simulateContract, writeContract, waitForTransactionReceipt } from "@wag
|
|||||||
import toast from "react-hot-toast";
|
import toast from "react-hot-toast";
|
||||||
|
|
||||||
import { isNetworkLegacyType } from "../../constants";
|
import { isNetworkLegacyType } from "../../constants";
|
||||||
import { UNISWAP_V2_ROUTER } from "../../constants/addresses";
|
import { UNISWAP_V2_ROUTER, WETH_ADDRESSES } from "../../constants/addresses";
|
||||||
import { abi as RouterAbi } from "../../abi/UniswapV2Router.json";
|
import { abi as RouterAbi } from "../../abi/UniswapV2Router.json";
|
||||||
import { getTokenAddress } from "../helpers";
|
import { getTokenAddress } from "../helpers";
|
||||||
|
|
||||||
import { config } from "../../config";
|
import { config } from "../../config";
|
||||||
|
|
||||||
export const swapExactTokensForTokens = async (
|
const swapMessages = {
|
||||||
|
replacedMsg: "Swap transaction was replaced. Wait for inclusion please.",
|
||||||
|
successMsg: "Swap executed successfully! Wait for balances update.",
|
||||||
|
errorMsg: "Swap tokens failed. Check logs for error detalization.",
|
||||||
|
};
|
||||||
|
|
||||||
|
const addMessages = {
|
||||||
|
replacedMsg: "Add liquidity transaction was replaced. Wait for inclusion please.",
|
||||||
|
successMsg: "Liquidity added successfully! You should get LP tokens to your wallet.",
|
||||||
|
errorMsg: "Adding liquidity failed. Check logs for error detalization.",
|
||||||
|
};
|
||||||
|
|
||||||
|
export const swapExactETHForTokens = async ({
|
||||||
chainId,
|
chainId,
|
||||||
amountDesired,
|
amountADesired,
|
||||||
amountMin,
|
amountBMin,
|
||||||
pathRaw,
|
tokenNameTop,
|
||||||
|
tokenNameBottom,
|
||||||
destination,
|
destination,
|
||||||
deadline
|
deadline
|
||||||
) => {
|
}) => {
|
||||||
|
|
||||||
const path = pathRaw.map(tokenName => getTokenAddress(chainId, tokenName));
|
|
||||||
const args = [
|
const args = [
|
||||||
amountDesired,
|
amountBMin,
|
||||||
amountMin,
|
[WETH_ADDRESSES[chainId], getTokenAddress(chainId, tokenNameBottom)],
|
||||||
path,
|
|
||||||
destination,
|
destination,
|
||||||
deadline
|
deadline
|
||||||
];
|
];
|
||||||
|
|
||||||
const messages = {
|
await executeOnChainTransaction({
|
||||||
replacedMsg: "Swap transaction was replaced. Wait for inclusion please.",
|
|
||||||
successMsg: "Swap executed successfully! Wait for balances update.",
|
|
||||||
errorMsg: "Swap tokens failed. Check logs for error detalization.",
|
|
||||||
};
|
|
||||||
|
|
||||||
await executeOnChainTransaction(
|
|
||||||
chainId,
|
chainId,
|
||||||
"swapExactTokensForTokens",
|
functionName: "swapExactETHForTokens",
|
||||||
args,
|
args,
|
||||||
destination,
|
account: destination,
|
||||||
messages
|
messages: swapMessages,
|
||||||
);
|
value: amountADesired,
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
export const addLiquidity = async (
|
export const swapExactTokensForETH = async ({
|
||||||
chainId,
|
chainId,
|
||||||
tokenA,
|
amountADesired,
|
||||||
tokenB,
|
amountBMin,
|
||||||
|
tokenNameTop,
|
||||||
|
tokenNameBottom,
|
||||||
|
destination,
|
||||||
|
address,
|
||||||
|
deadline
|
||||||
|
}) => {
|
||||||
|
const args = [
|
||||||
|
amountADesired,
|
||||||
|
amountBMin,
|
||||||
|
[getTokenAddress(chainId, tokenNameTop), WETH_ADDRESSES[chainId]],
|
||||||
|
destination,
|
||||||
|
deadline
|
||||||
|
];
|
||||||
|
|
||||||
|
await executeOnChainTransaction({
|
||||||
|
chainId,
|
||||||
|
functionName: "swapExactTokensForETH",
|
||||||
|
args,
|
||||||
|
account: address,
|
||||||
|
messages: swapMessages,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
export const swapExactTokensForTokens = async ({
|
||||||
|
chainId,
|
||||||
|
amountADesired,
|
||||||
|
amountBMin,
|
||||||
|
tokenNameTop,
|
||||||
|
tokenNameBottom,
|
||||||
|
destination,
|
||||||
|
address,
|
||||||
|
deadline
|
||||||
|
}) => {
|
||||||
|
const args = [
|
||||||
|
amountADesired,
|
||||||
|
amountBMin,
|
||||||
|
[getTokenAddress(chainId, tokenNameTop), getTokenAddress(chainId, tokenNameBottom)],
|
||||||
|
destination,
|
||||||
|
deadline
|
||||||
|
];
|
||||||
|
|
||||||
|
await executeOnChainTransaction({
|
||||||
|
chainId,
|
||||||
|
functionName: "swapExactTokensForTokens",
|
||||||
|
args,
|
||||||
|
account: address,
|
||||||
|
messages: swapMessages
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
export const addLiquidity = async ({
|
||||||
|
chainId,
|
||||||
|
tokenNameTop,
|
||||||
|
tokenNameBottom,
|
||||||
amountADesired,
|
amountADesired,
|
||||||
amountBDesired,
|
amountBDesired,
|
||||||
amountAMin,
|
amountAMin,
|
||||||
amountBMin,
|
amountBMin,
|
||||||
to,
|
address,
|
||||||
|
destination,
|
||||||
deadline,
|
deadline,
|
||||||
) => {
|
}) => {
|
||||||
const token1 = getTokenAddress(chainId, tokenA);
|
|
||||||
const token2 = getTokenAddress(chainId, tokenB);
|
|
||||||
|
|
||||||
const args = [
|
const args = [
|
||||||
token1,
|
getTokenAddress(chainId, tokenNameTop),
|
||||||
token2,
|
getTokenAddress(chainId, tokenNameBottom),
|
||||||
amountADesired,
|
amountADesired,
|
||||||
amountBDesired,
|
amountBDesired,
|
||||||
amountAMin,
|
amountAMin,
|
||||||
amountBMin,
|
amountBMin,
|
||||||
to,
|
destination,
|
||||||
deadline
|
deadline
|
||||||
];
|
];
|
||||||
const messages = {
|
|
||||||
replacedMsg: "Add liquidity transaction was replaced. Wait for inclusion please.",
|
|
||||||
successMsg: "Liquidity added successfully! You should get LP tokens to your wallet.",
|
|
||||||
errorMsg: "Adding liquidity failed. Check logs for error detalization.",
|
|
||||||
};
|
|
||||||
|
|
||||||
await executeOnChainTransaction(
|
await executeOnChainTransaction({
|
||||||
chainId,
|
chainId,
|
||||||
"addLiquidity",
|
functionName: "addLiquidity",
|
||||||
args,
|
args,
|
||||||
to,
|
account: address,
|
||||||
messages
|
messages: addMessages
|
||||||
);
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
const executeOnChainTransaction = async (
|
export const addLiquidityETH = async ({
|
||||||
|
chainId,
|
||||||
|
tokenNameTop,
|
||||||
|
tokenNameBottom,
|
||||||
|
amountADesired,
|
||||||
|
amountBDesired,
|
||||||
|
amountAMin,
|
||||||
|
amountBMin,
|
||||||
|
address,
|
||||||
|
destination,
|
||||||
|
deadline,
|
||||||
|
}) => {
|
||||||
|
let token = getTokenAddress(chainId, tokenNameTop);
|
||||||
|
let amountTokenDesired = amountADesired;
|
||||||
|
let amountETHDesired = amountBDesired;
|
||||||
|
let amountTokenMin = amountAMin;
|
||||||
|
let amountETHMin = amountBMin;
|
||||||
|
|
||||||
|
if (token === undefined) {
|
||||||
|
token = getTokenAddress(chainId, tokenNameBottom);
|
||||||
|
amountTokenDesired = amountBDesired;
|
||||||
|
amountETHDesired = amountADesired;
|
||||||
|
amountTokenMin = amountBMin;
|
||||||
|
amountETHMin = amountAMin;
|
||||||
|
}
|
||||||
|
|
||||||
|
const args = [
|
||||||
|
token,
|
||||||
|
amountTokenDesired,
|
||||||
|
amountTokenMin,
|
||||||
|
amountETHMin,
|
||||||
|
destination,
|
||||||
|
deadline
|
||||||
|
];
|
||||||
|
|
||||||
|
await executeOnChainTransaction({
|
||||||
|
chainId,
|
||||||
|
functionName: "addLiquidityETH",
|
||||||
|
args,
|
||||||
|
account: address,
|
||||||
|
messages: addMessages,
|
||||||
|
value: amountETHDesired,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
const executeOnChainTransaction = async ({
|
||||||
chainId,
|
chainId,
|
||||||
functionName,
|
functionName,
|
||||||
args,
|
args,
|
||||||
account,
|
account,
|
||||||
messages
|
messages,
|
||||||
) => {
|
value,
|
||||||
|
}) => {
|
||||||
try {
|
try {
|
||||||
const { request } = await simulateContract(config, {
|
const { request } = await simulateContract(config, {
|
||||||
abi: RouterAbi,
|
abi: RouterAbi,
|
||||||
@ -96,6 +194,7 @@ const executeOnChainTransaction = async (
|
|||||||
account,
|
account,
|
||||||
chainId,
|
chainId,
|
||||||
type: isNetworkLegacyType(chainId) ? 'legacy' : 'eip1559',
|
type: isNetworkLegacyType(chainId) ? 'legacy' : 'eip1559',
|
||||||
|
value: value ?? 0n,
|
||||||
});
|
});
|
||||||
|
|
||||||
const txHash = await writeContract(config, request);
|
const txHash = await writeContract(config, request);
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user