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

#102 #108

Merged
merged 2 commits into from
Aug 3, 2022
Merged

#102 #108

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
32 changes: 18 additions & 14 deletions src/components/SwapComponent/SwapCurrencySelector.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import SelectUnstyled, {
} from '@mui/base/SelectUnstyled';
import { PopperUnstyled } from '@mui/base';
import { StyledOption } from "../DepositComponent";
import { Box } from "@mui/material";

const StyledButton = styled('button')(
({ theme }) => `
Expand Down Expand Up @@ -133,6 +134,8 @@ export const CustomSelect = React.forwardRef(function CustomSelect(
...props.components,
};

console.log(props)

return <SelectUnstyled {...props} ref={ref} components={components} />;
});

Expand Down Expand Up @@ -184,13 +187,9 @@ const SwapCurrencySelector = ({
return null;
}

// const symbol = api.getCurrencyInfo(value);
const symbol = {};
// const image = getCurrencyLogo(value);

const handleTokenSelect = async (e: any) => {
// e.preventDefault();
console.log("a=================",e)
const val = e;
//await tokenApproval();
changeIndex(parseInt(val));
Expand All @@ -200,16 +199,21 @@ const SwapCurrencySelector = ({
return (
<CustomSelect onChange={handleTokenSelect} value={tokenIndex} borderBox={borderBox} width={listWidth}>
{tokens.map((c: any, index: number) => (
<StyledOption key={c.symbol} value={index}>
<img
loading="lazy"
width="30"
src={c.logo}
srcSet={c.logo}
alt={`coin`}
/>
{c.symbol}
</StyledOption>
<Box display="flex" justifyContent="space-between" alignItems="center" px="10px" >
<StyledOption key={c.symbol} value={index} >
<Box display="flex" alignItems={"center"}>
<img
loading="lazy"
width="30"
src={c.logo}
srcSet={c.logo}
alt={`coin`}
/>
{c.symbol}
</Box>
</StyledOption>
<Box>{Number(balances[index]).toFixed(4)}</Box>
</Box>
))}
</CustomSelect>

Expand Down
37 changes: 23 additions & 14 deletions src/pages/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ import { ethers } from "ethers";
const Home: NextPage = () => {
const [isLoading, changeIsLoading] = useState(false);
const [loadingMsg, changeLoadingMsg] = useState("Awaiting Swap");
const [txComplete, changeTxComplete] = useState(false);
const [txComplete, changeTxComplete] = useState({status:false, tx: ""});
const [txMessage, changeTxMsg] = useState("Swap Complete");
const [isTokenApproved, changeTokenApproved] = useState(false);
const [failMsg, changeFailMsg] = useState("");
Expand All @@ -58,7 +58,7 @@ const Home: NextPage = () => {

useEffect(() => {
onEvent();
setInterval(() => { onEvent(); }, 60000);
// setInterval(() => { onEvent(); }, 60000);
}, [])

useEffect(() => {
Expand Down Expand Up @@ -115,8 +115,8 @@ const Home: NextPage = () => {
}, [toDetails.symbol, fromDetails])

useEffect(() => {
if (!txComplete) return;
openErrorWindow(txMessage, 1)
if (!txComplete.status) return;
openErrorWindow(txMessage, 1, txComplete.tx)
}, [txComplete])

useEffect(() => {
Expand Down Expand Up @@ -152,8 +152,9 @@ const Home: NextPage = () => {
`Swap ${tokens[getTokenIndex(fromDetails.symbol)].symbol} for ${tokens[getTokenIndex(toDetails.symbol)].symbol} success`
);
let success = true;
let tx = ""
try {
await swapPool(
tx = await swapPool(
getString2Number(fromDetails.amount),
getTokenIndex(toDetails.symbol),
getTokenIndex(fromDetails.symbol)
Expand All @@ -167,7 +168,7 @@ const Home: NextPage = () => {
}
changeIsLoading(false);
if (success) {
changeTxComplete(true);
changeTxComplete({status: true, tx : tx});
}
};

Expand All @@ -179,10 +180,11 @@ const Home: NextPage = () => {

changeIsLoading(true);
changeLoadingMsg(`Approving ${tokens[getTokenIndex(fromDetails.symbol)].symbol}`);
changeTxMsg(`${tokens[getTokenIndex(fromDetails.symbol)].symbol} approved`);

let success = true;
let tx = "";
try {
await approveToken(
tx = await approveToken(
getTokenIndex(fromDetails.symbol),
Number(fromDetails.amount) * 1.05
);
Expand All @@ -194,15 +196,17 @@ const Home: NextPage = () => {
}
changeIsLoading(false);
if (success) {
changeTxComplete(true);
changeTxMsg(`${tokens[getTokenIndex(fromDetails.symbol)].symbol} approved`);
changeTxComplete({status: true, tx : tx});
}
};

const switchTransferType = (e: any) => {
setFromDetails(toDetails);
const tmp = {...toDetails}
tmp.amount = Number(tmp.amount).toFixed(4)
setFromDetails(tmp);
const value = { amount: "" };
setToDetails({ ...fromDetails, ...value })
console.log({ ...fromDetails, ...value })
}

const setSwapDetails = async (values: {amount: string, symbol: string}, from: boolean) => {
Expand All @@ -227,7 +231,7 @@ const Home: NextPage = () => {
}
}

const openErrorWindow = (value: string, flag: number) => {
const openErrorWindow = (value: string, flag: number, tx = "") => {
if(toast.isActive(flag)) return;
if(flag === 3) {
toast.info(
Expand All @@ -253,7 +257,10 @@ const Home: NextPage = () => {
}
if(flag === 1) {
toast.success(
value,
<Box display="flex" p="20px" flexDirection="column">
<Box mb="5px">{value}</Box>
<Box component="a" href={`https://goerli.voyager.online/tx/${tx}`} target="_blank" fontSize="12px" width="100%" textAlign="right">view TX</Box>
</Box>,
{
closeOnClick: false,
autoClose: 15000,
Expand Down Expand Up @@ -362,9 +369,11 @@ const Home: NextPage = () => {
<Box fontSize="12px" fontWeight="400">1 {fromDetails.symbol} = {
(fromDetails.amount && toDetails.amount) ? formatPrice(Number(fromDetails.amount) / Number(toDetails.amount)) : 0
} {toDetails.symbol}</Box>
<Box fontSize="12px" fontWeight="400">Available Balance: {Number(userBalances[getTokenIndex(toDetails.symbol)]).toFixed(4)} {toDetails.symbol}</Box>

</div>
<SwapSwapInput
// balances={balances}
balances={userBalances}
// currencies={currencies}
from={false}
value={{symbol: toDetails.symbol, amount: formatPrice(toDetails.amount)}} // format to details amount
Expand Down