Compare commits

...

10 Commits

Author SHA1 Message Date
StockiP
31d671855b revert change
Some checks are pending
Deploy Now / deploy-now (push) Waiting to run
2022-11-21 19:56:06 +01:00
StockiP
42de6d0453 trying out different ROUTER_ADDRESS 2022-11-21 19:52:16 +01:00
StockiP
7793f39d7c Changed router address to hopefully new contract 2022-11-21 17:55:42 +01:00
StockiP
264707d843 Fixed one small typo that broke the build - added the provisional router / smart contract address. 2022-11-05 19:44:58 +01:00
Daniel Wieser
90ef498adb Merge pull request #3 from kenan96/Exchange-Inputs-and-Balance
Exchange inputs and balance
2022-11-05 15:31:20 +01:00
Daniel Wieser
0ad0e0331b Merge pull request #2 from kenan96/Web3Logic
Web3 logic
2022-11-05 15:30:33 +01:00
Daniel
32f20782a6 Update App.js 2022-11-05 15:29:16 +01:00
Daniel
83c846ae8a Update AmountOut.js 2022-11-05 15:26:09 +01:00
Daniel
b172fc84a5 Update AmountIn.js 2022-11-05 15:26:07 +01:00
Daniel
da3c8798be Finish Exchange.js 2022-11-05 15:24:19 +01:00
6 changed files with 265 additions and 205 deletions

View File

@@ -1,8 +1,8 @@
import React from "react"
import { useEthers } from "@usedapp/core";
import { usePools } from "./hooks";
import styles from './styles';
import { usePools } from "./hooks";
import { fhtLogo } from './assets';
import { Exchange, Loader, WalletButton } from "./components";
@@ -29,7 +29,7 @@ const App = () => {
<div className="green_gradient" />
<div className={styles.exchange}>
{account ? (
loading ? (
poolsLoading ? (
<Loader title="Loading pools, please wait!" />
) : <Exchange pools = {pools}/>
) : <Loader title="Please connect your wallet!"/>}

View File

@@ -1,10 +1,10 @@
import React, { useState, useEffect, useRef } from "react";
import { chevronDown } from "../assets"; // icon
import { useOnClickOutside } from "../utils"; //helps to close menu bar
import { chevronDown } from "../assets";
import { useOnClickOutside } from "../utils";
import styles from "../styles";
const AmountIn = () => {
const AmountIn = ({ value, onChange, currencyValue, onSelect, currencies, isSwapping }) => {
const [showList, setShowList] = useState(false);
const [activeCurrency, setActiveCurrency] = useState("Select");
const ref = useRef()
@@ -61,9 +61,7 @@ const AmountIn = () => {
)}
</div>
</div>
)
}
);
};
export default AmountIn;

View File

@@ -28,8 +28,8 @@ const AmountOut = ({ fromToken, toToken, amountIn, pairContract, currencyValue,
placeholder="0.0"
type="number"
value={formatUnits(amountOut)}
disabled
className={styles.amountInput}
disabled
/>
<div className="relative" onClick={() => setShowList(!showList)}>

View File

@@ -7,39 +7,97 @@ import { parseUnits } from "ethers/lib/utils";
import { getAvailableTokens, getCounterpartTokens, findPoolByTokens, isOperationPending, getFailureMessage, getSuccessMessage } from '../utils';
import { ROUTER_ADDRESS } from "../config";
import AmountIn from "./";
import AmountOut from "./";
import Balance from "./";
import AmountIn from "./AmountIn";
import AmountOut from "./AmountOut";
import Balance from "./Balance";
import styles from "../styles";
const Exchange = ({ pools }) => {
const { account } = useEthers();
const [fromValue, setFromValue] = useState("0");
const [fromToken, setFromToken] = useState(pools[0].token0Address);
const [fromToken, setFromToken] = useState(pools[0].token0Address); // initialFromToken
const [toToken, setToToken] = useState("");
const [resetState, setResetState] = useState(false)
const fromValueBigNumber = parseUnits(fromValue || "0");
const availableTokens = getAvailableTokens(pools); //What tokens can we swap from?
const counterpartTokens = getCounterpartTokens(pools, fromToken); //What tokens can we swap to?
const pairAddress = findPoolByTokens(pools, fromToken, toToken)?.address ?? ""; //find a pair address ot that liquidity pair
const fromValueBigNumber = parseUnits(fromValue || "0"); // converse the string to bigNumber
const availableTokens = getAvailableTokens(pools);
const counterpartTokens = getCounterpartTokens(pools, fromToken);
const pairAddress = findPoolByTokens(pools, fromToken, toToken)?.address ?? "";
//Get contract addresses
const routerContract = new Contract(ROUTER_ADDRESS, abis.router02);
const fromTokenContract = new Contract(fromToken, ERC20.abi);
const fromTokenBalance = useTokenBalance(fromToken, account); //Know balance of "from Token"
const toTokenBalance = useTokenBalance(toToken, account); //Know balance of "to Token"
const fromTokenBalance = useTokenBalance(fromToken, account);
const toTokenBalance = useTokenBalance(toToken, account);
const tokenAllowance = useTokenAllowance(fromToken, account, ROUTER_ADDRESS) || parseUnits("0");
const approvedNeeded = fromValueBigNumber.gt(tokenAllowance); //We need to approve to make the swap
const formValueIsGreaterThan0 = fromValueBigNumber.gt(parseUnits("0")); //has to be greater than 0
const hasEnoughBalance = fromValueBigNumber.lte(fromTokenBalance ?? parseUnits("0")); //lte = lower than or equal to
const approvedNeeded = fromValueBigNumber.gt(tokenAllowance);
const formValueIsGreaterThan0 = fromValueBigNumber.gt(parseUnits("0"));
const hasEnoughBalance = fromValueBigNumber.lte(fromTokenBalance ?? parseUnits("0"));
// approve initiating a contract call (similar to use state) -> gives the state and the sender...
const { state: swapApproveState, send: swapApproveSend } =
useContractFunction(fromTokenContract, "approve", {
transactionName: "onApproveRequested",
gasLimitBufferPercentage: 10,
});
// swap initiating a contract call (similar to use state) -> gives the state and the sender...
const { state: swapExecuteState, send: swapExecuteSend } =
useContractFunction(routerContract, "swapExactTokensForTokens", {
transactionName: "swapExactTokensForTokens",
gasLimitBufferPercentage: 10,
});
const isApproving = isOperationPending(swapApproveState);
const isSwapping = isOperationPending(swapExecuteState);
const canApprove = !isApproving && approvedNeeded;
const canSwap = !approvedNeeded && !isSwapping && formValueIsGreaterThan0 && hasEnoughBalance;
const successMessage = getSuccessMessage(swapApproveState, swapExecuteState);
const failureMessage = getFailureMessage(swapApproveState, swapExecuteState);
const onApproveRequested = () => {
swapApproveSend(ROUTER_ADDRESS, ethers.constants.MaxUint256);
};
// https://docs.uniswap.org/protocol/V2/reference/smart-contracts/router-02#swapexacttokensfortokens
const onSwapRequested = () => {
swapExecuteSend(
fromValueBigNumber,
0,
[fromToken, toToken],
account,
Math.floor(Date.now() / 1000) + 60 * 20
).then((_) => {
setFromValue("0");
});
};
const onFromValueChange = (value) => {
const trimmedValue = value.trim();
try {
trimmedValue && parseUnits(value);
setFromValue(value);
} catch (e) {}
};
const onFromTokenChange = (value) => {
setFromToken(value);
};
const onToTokenChange = (value) => {
setToToken(value);
};
useEffect(() => {
if(failureMessage || successMessage) {
setTimeout(() => {
setResetState(true)
setFromValue("0")
setToToken("")
}, 5000)
}
}, [failureMessage, successMessage])
return (
<div className="flex flex-col w-full items-center">
<div className="mb-8">
@@ -104,6 +162,6 @@ const Exchange = ({ pools }) => {
)}
</div>
);
}
};
export default Exchange;

View File

@@ -6,7 +6,11 @@ import {ethereumLogo2 } from "../assets";
const Loader = ({ title }) => {
return (
<div className={styles.loader}>
<img src={ethereumLogo2} alt="Ethereum Logo" className={styles.loaderImg} />
<img
src={ethereumLogo2}
alt="Ethereum Logo"
className={styles.loaderImg}
/>
<p className={styles.loaderText}>{title}</p>
</div>

View File

@@ -1,6 +1,6 @@
import { Goerli } from "@usedapp/core";
export const ROUTER_ADDRESS = "[YOUR ADDRESS HERE]";
export const ROUTER_ADDRESS = "0x73135EBE79A1Af0D757a3F5218ED29c1Ff7F332B";
export const DAPP_CONFIG = {
readOnlyChainId: Goerli.chainId,