Skip to content

Commit

Permalink
Merge pull request #1151 from Plenty-network/stage
Browse files Browse the repository at this point in the history
Stage
  • Loading branch information
AnshuJalan authored Nov 17, 2023
2 parents 0436899 + 0b4b893 commit 81fe0d0
Show file tree
Hide file tree
Showing 9 changed files with 245 additions and 96 deletions.
60 changes: 55 additions & 5 deletions pages/airdrop/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,63 +18,105 @@ import { fetchWallet } from "../../src/redux/wallet/wallet";
import { getRewardsAprEstimate } from "../../src/redux/rewardsApr";

const Airdrop: NextPage = () => {
// Select user's wallet address from the Redux store
const userAddress = useAppSelector((state) => state.wallet.address);

// Select token data from the Redux store
const token = useAppSelector((state) => state.config.tokens);

// Select totalVotingPowerError from the Redux store
const totalVotingPowerError = useAppSelector((state) => state.pools.totalVotingPowerError);

// Select epochError from the Redux store
const epochError = useAppSelector((state) => state.epoch).epochFetchError;

// Select token prices from the Redux store
const tokenPrices = useAppSelector((state) => state.tokenPrice.tokenPrice);

// Select AMM (Automated Market Maker) data from the Redux store
const amm = useAppSelector((state) => state.config.AMMs);

// Ref to track initial price call
const initialPriceCall = useRef<boolean>(true);

// Ref to track initial LP price call
const initialLpPriceCall = useRef<boolean>(true);

// Ref to track initial rewards APR call
const initialRewardsAprCall = useRef<boolean>(true);

// Select totalVotingPower from the Redux store
const currentTotalVotingPower = useAppSelector((state) => state.pools.totalVotingPower);
const rewardsAprEstimateError = useAppSelector((state) => state.rewardsApr.rewardsAprEstimateError);

// Select rewards APR estimate error from the Redux store
const rewardsAprEstimateError = useAppSelector(
(state) => state.rewardsApr.rewardsAprEstimateError
);

const dispatch = useDispatch<AppDispatch>();

// State for showing the disclaimer
const [isDisclaimer, setIsDisclaimer] = useState(true);

// Function to handle clicking and setting local storage for disclaimer
const handleClick = () => {
localStorage.setItem(FIRST_TIME_DISCLAIMER, "true");
};

// Fetch wallet and config data on component mount
useEffect(() => {
dispatch(fetchWallet());
dispatch(getConfig());
}, []);

// Fetch epoch data if there's an epoch error
useEffect(() => {
if (epochError) {
dispatch(getEpochData());
}
}, [epochError]);

// Use an interval to periodically fetch epoch data
useInterval(() => {
dispatch(getEpochData());
}, 60000);

// Fetch total voting power based on user's address
useEffect(() => {
dispatch(getTotalVotingPower());
}, [userAddress]);

// Handle fetching total voting power if there's an error
useEffect(() => {
if (totalVotingPowerError) {
dispatch(getTotalVotingPower());
}
}, [totalVotingPowerError]);

// Fetch token prices
useEffect(() => {
if(!initialPriceCall.current) {
if (!initialPriceCall.current) {
Object.keys(token).length !== 0 && dispatch(getTokenPrice());
} else {
initialPriceCall.current = false;
}
}, [token]);

// Fetch LP token prices
useEffect(() => {
if(!initialLpPriceCall.current) {
if (!initialLpPriceCall.current) {
Object.keys(tokenPrices).length !== 0 && dispatch(getLpTokenPrice(tokenPrices));
} else {
initialLpPriceCall.current = false;
}
}, [tokenPrices]);

// Create gauge configuration when AMM data is available
useEffect(() => {
Object.keys(amm).length !== 0 && dispatch(createGaugeConfig());
}, [amm]);

// Fetch rewards APR estimate when relevant data is available
useEffect(() => {
if (!initialRewardsAprCall.current) {
if (Object.keys(tokenPrices).length !== 0) {
Expand All @@ -89,6 +131,8 @@ const Airdrop: NextPage = () => {
initialRewardsAprCall.current = false;
}
}, [currentTotalVotingPower, tokenPrices]);

// Handle fetching rewards APR estimate if there's an error
useEffect(() => {
if (rewardsAprEstimateError && Object.keys(tokenPrices).length !== 0) {
dispatch(
Expand All @@ -99,12 +143,16 @@ const Airdrop: NextPage = () => {
);
}
}, [rewardsAprEstimateError]);

// State to track the selected chain
const [chain, setChain] = useState<ChainAirdrop>(ChainAirdrop.Tezos);

return (
<>
<MetaAirdrop />
<SideBarHOC makeTopBarScroll>
{!localStorage.getItem(FIRST_TIME_DISCLAIMER) ? (
// Display disclaimer if it's the first time
<Disclaimer
show={isDisclaimer}
setShow={setIsDisclaimer}
Expand All @@ -113,18 +161,20 @@ const Airdrop: NextPage = () => {
handleClick={handleClick}
/>
) : (
// Display the main airdrop component if the disclaimer has been accepted
<MainAirdrop chain={chain} setChain={setChain} />
)}
</SideBarHOC>
{/* <div></div> */}
</>
);
};

// PropTypes for the component
Airdrop.propTypes = {
connectWallet: PropTypes.any,
disconnectWallet: PropTypes.any,
fetchWalletAddress: PropTypes.any,
userAddress: PropTypes.any,
};

export default Airdrop;
export default Airdrop;
71 changes: 59 additions & 12 deletions pages/swap/index.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import type { NextPage } from "next";
import PropTypes from "prop-types";
import { useEffect, useRef } from "react";
import BigNumber from 'bignumber.js';
import { useDispatch } from "react-redux";
import BigNumber from "bignumber.js";
import { SideBarHOC } from "../../src/components/Sidebar/SideBarHOC";

import Swap from "../../src/components/Swap";
Expand All @@ -18,64 +18,116 @@ import { useRouter } from "next/router";
import { analytics, firebase } from "../../src/config/firebaseConfig";

const Home: NextPage = () => {
// Select user's wallet address from the Redux store
const userAddress = useAppSelector((state) => state.wallet.address);

// Select token data from the Redux store
const token = useAppSelector((state) => state.config.tokens);

// Select totalVotingPowerError from the Redux store
const totalVotingPowerError = useAppSelector((state) => state.pools.totalVotingPowerError);

// Select epochError from the Redux store
const epochError = useAppSelector((state) => state.epoch).epochFetchError;

// Select token prices from the Redux store
const tokenPrices = useAppSelector((state) => state.tokenPrice.tokenPrice);

// Select AMM (Automated Market Maker) data from the Redux store
const amm = useAppSelector((state) => state.config.AMMs);

// Ref to track initial price call
const initialPriceCall = useRef<boolean>(true);
// const initialLpPriceCall = useRef<boolean>(true);

// Ref to track initial rewards APR call
const initialRewardsAprCall = useRef<boolean>(true);

// Select totalVotingPower from the Redux store
const currentTotalVotingPower = useAppSelector((state) => state.pools.totalVotingPower);

// Select rewards APR estimate error from the Redux store
const rewardsAprEstimateError = useAppSelector(
(state) => state.rewardsApr.rewardsAprEstimateError
);

// Next.js router instance
const router = useRouter();

const dispatch = useDispatch<AppDispatch>();

// Function to connect the wallet
const connectTempleWallet = () => {
return dispatch(walletConnection());
};

// Function to disconnect the user's wallet
const disconnectUserWallet = async () => {
if (userAddress) {
return dispatch(walletDisconnection());
}
};

// Props to pass to other pages
const otherPageProps = {
connectWallet: connectTempleWallet,
disconnectWallet: disconnectUserWallet,
walletAddress: userAddress,
};

// Fetch wallet and config data on component mount
useEffect(() => {
dispatch(fetchWallet());
dispatch(getConfig());
}, []);

// Fetch epoch data if there's an epoch error
useEffect(() => {
if (epochError) {
dispatch(getEpochData());
}
}, [epochError]);

// Use an interval to periodically fetch epoch data
useInterval(() => {
dispatch(getEpochData());
}, 60000);

// Fetch total voting power based on user's address
useEffect(() => {
dispatch(getTotalVotingPower());
}, [userAddress]);

// Handle fetching total voting power if there's an error
useEffect(() => {
if (totalVotingPowerError) {
dispatch(getTotalVotingPower());
}
}, [totalVotingPowerError]);

// Fetch token prices
useEffect(() => {
if (!initialPriceCall.current) {
Object.keys(token).length !== 0 && dispatch(getTokenPrice());
} else {
initialPriceCall.current = false;
}
}, [token]);

// Commented out section for LP token price (you can uncomment if needed)
/* useEffect(() => {
if(!initialLpPriceCall.current) {
Object.keys(tokenPrices).length !== 0 && dispatch(getLpTokenPrice(tokenPrices));
} else {
initialLpPriceCall.current = false;
}
}, [tokenPrices]); */

// Create gauge configuration when AMM data is available
useEffect(() => {
Object.keys(amm).length !== 0 && dispatch(createGaugeConfig());
}, [amm]);

// Fetch rewards APR estimate when relevant data is available
useEffect(() => {
if (!initialRewardsAprCall.current) {
if (Object.keys(tokenPrices).length !== 0) {
Expand All @@ -90,6 +142,8 @@ const Home: NextPage = () => {
initialRewardsAprCall.current = false;
}
}, [currentTotalVotingPower, tokenPrices]);

// Handle fetching rewards APR estimate if there's an error
useEffect(() => {
if (rewardsAprEstimateError && Object.keys(tokenPrices).length !== 0) {
dispatch(
Expand All @@ -100,16 +154,7 @@ const Home: NextPage = () => {
);
}
}, [rewardsAprEstimateError]);
const disconnectUserWallet = async () => {
if (userAddress) {
return dispatch(walletDisconnection());
}
};
const otherPageProps = {
connectWallet: connectTempleWallet,
disconnectWallet: disconnectUserWallet,
walletAddress: userAddress,
};

return (
<>
<SideBarHOC makeTopBarScroll>
Expand All @@ -118,6 +163,8 @@ const Home: NextPage = () => {
</>
);
};

// PropTypes for the component
Home.propTypes = {
connectWallet: PropTypes.any,
disconnectWallet: PropTypes.any,
Expand Down
12 changes: 9 additions & 3 deletions src/components/Pools/NewPoolMain.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -73,26 +73,30 @@ interface ILiquidityProps {
contractTokenBalance: IAllTokensBalance;
setShowLiquidityModalPopup: React.Dispatch<React.SetStateAction<boolean>>;
}
// Define a constant for pair types
export const Pair = {
VOLATILE: "Volatile pair",
STABLE: "Stable pair",
};
function NewPoolMain(props: ILiquidityProps) {
// Redux state selectors
const tokenPrice = useAppSelector((state) => state.tokenPrice.tokenPrice);
const amm = useAppSelector((state) => state.config.AMMs);
const TOKEN = useAppSelector((state) => state.config.tokens);
const walletAddress = useAppSelector((state) => state.wallet.address);
const dispatch = useDispatch<AppDispatch>();
// Function to connect to Temple Wallet
const connectTempleWallet = () => {
return dispatch(walletConnection());
};
// Local state for active state, existence, and gauge
const [activeState, setActiveState] = React.useState<ActiveLiquidity | string>(
ActiveLiquidity.Liquidity
);
const [isExist, setIsExist] = useState(false);
const [isGauge, setIsGauge] = useState(false);
const [showNewPoolsManage, setShowNewPoolsManage] = useState<boolean>(false);

// Function to show or hide the New Pools Manage popup
const handleNewPoolsManagePopup = (val: boolean) => {
setShowNewPoolsManage(val);
};
Expand All @@ -117,6 +121,7 @@ function NewPoolMain(props: ILiquidityProps) {
}
}
}, [props.tokenIn, props.tokenOut]);
// Button component based on wallet status, input validation, and other conditions

const AddButton = useMemo(() => {
if (!walletAddress) {
Expand Down Expand Up @@ -178,7 +183,7 @@ function NewPoolMain(props: ILiquidityProps) {
props.userBalances,
isExist,
]);

// Function to handle liquidity input
const handleLiquidityInput = async (
input: string | number,
tokenType: "tokenIn" | "tokenOut"
Expand Down Expand Up @@ -206,6 +211,7 @@ function NewPoolMain(props: ILiquidityProps) {
props.setSecondTokenAmount(input.toString().trim());
}
};
// Function to handle clicking on the first token amount
const onClickAmount = () => {
props.tokenIn.name === "tez"
? handleLiquidityInput(
Expand All @@ -214,7 +220,7 @@ function NewPoolMain(props: ILiquidityProps) {
)
: handleLiquidityInput(props.userBalances[props.tokenIn.name]?.balance.toNumber(), "tokenIn");
};

// Function to handle clicking on the second token amount
const onClickSecondAmount = () => {
props.tokenOut.name === "tez"
? handleLiquidityInput(
Expand Down
Loading

1 comment on commit 81fe0d0

@vercel
Copy link

@vercel vercel bot commented on 81fe0d0 Nov 17, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.