From d01502d7656a95fc7b1dff9479cfde2b39c9471c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20Geonizeli?= Date: Tue, 14 Sep 2021 23:22:19 -0300 Subject: [PATCH] add usePoolListing hook --- app/javascript/src/pages/Home/PoolListing.tsx | 60 ++--------------- app/javascript/src/pages/Home/hooks/index.ts | 1 + .../src/pages/Home/hooks/usePoolListing.ts | 65 +++++++++++++++++++ 3 files changed, 70 insertions(+), 56 deletions(-) create mode 100644 app/javascript/src/pages/Home/hooks/index.ts create mode 100644 app/javascript/src/pages/Home/hooks/usePoolListing.ts diff --git a/app/javascript/src/pages/Home/PoolListing.tsx b/app/javascript/src/pages/Home/PoolListing.tsx index f325630..07392f4 100644 --- a/app/javascript/src/pages/Home/PoolListing.tsx +++ b/app/javascript/src/pages/Home/PoolListing.tsx @@ -1,68 +1,16 @@ -import React, { useEffect, useState } from "react"; -import { ethers } from "ethers"; +import React from "react"; -import { unfinishedPools } from "../../constants/Pools"; -import { useBsc } from "../../contexts/BscProvider"; -import type { PoolConfig } from "../../types"; import { Pool } from "./Pool"; -import sousChef from "../../abi/sousChef.json"; -import { getEndBlock } from "../../utils/getEndBlock"; -import { notEmpty } from "../../utils/notEmpty"; import { Spinner } from "../../components"; -import { usePersistedState } from "../../hooks/usePersistedState"; +import { usePoolListing } from "./hooks"; export const PoolListing = () => { - const { provider } = useBsc(); - const [validPools, setValidPools] = usePersistedState( - "validPools", - [], - 1200000 // 20 minutes - ); - - const [isLoadingPools, setIsLoadingPools] = useState(true); + const { isLoading, validPools } = usePoolListing(); // TODO: puxar valor da wallet const balance = "0"; - useEffect(() => { - (async () => { - if (validPools.length) return; - - const blockNumber = await provider.getBlockNumber(); - - const getChef = (pool: PoolConfig) => { - return new ethers.Contract( - pool.contractAddress[56], - new ethers.utils.Interface(sousChef), - provider - ); - }; - - await Promise.all( - unfinishedPools.map(async (pool) => { - if (pool.sousId === 0) { - return pool; - } - - try { - const chef = getChef(pool); - const endBlock = await getEndBlock(chef); - - if (endBlock >= blockNumber) { - return pool; - } - } catch (e) { - console.error(e); - } - }) - ).then((pools) => { - setIsLoadingPools(false); - setValidPools(pools.filter(notEmpty)); - }); - })(); - }, [provider, setValidPools, validPools.length]); - - if (isLoadingPools && !validPools.length) { + if (isLoading && !validPools.length) { return (
diff --git a/app/javascript/src/pages/Home/hooks/index.ts b/app/javascript/src/pages/Home/hooks/index.ts new file mode 100644 index 0000000..a09f60a --- /dev/null +++ b/app/javascript/src/pages/Home/hooks/index.ts @@ -0,0 +1 @@ +export * from "./usePoolListing"; diff --git a/app/javascript/src/pages/Home/hooks/usePoolListing.ts b/app/javascript/src/pages/Home/hooks/usePoolListing.ts new file mode 100644 index 0000000..07d6640 --- /dev/null +++ b/app/javascript/src/pages/Home/hooks/usePoolListing.ts @@ -0,0 +1,65 @@ +import { useEffect, useState } from "react"; +import { ethers } from "ethers"; + +import { usePersistedState } from "../../../hooks/usePersistedState"; +import type { PoolConfig } from "../../../types"; +import sousChef from "../../../abi/sousChef.json"; +import { useBsc } from "../../../contexts/BscProvider"; +import { unfinishedPools } from "../../../constants/Pools"; +import { getEndBlock } from "../../../utils/getEndBlock"; +import { notEmpty } from "../../../utils/notEmpty"; + +export const usePoolListing = () => { + const { provider } = useBsc(); + + const [pools, setPools] = usePersistedState( + "validPools", + [], + 1200000 // 20 minutes + ); + + const [isLoading, setIsLoading] = useState(true); + + useEffect(() => { + (async () => { + if (pools.length) return; + + const blockNumber = await provider.getBlockNumber(); + + const getChef = (pool: PoolConfig) => { + return new ethers.Contract( + pool.contractAddress[56], + new ethers.utils.Interface(sousChef), + provider + ); + }; + + await Promise.all( + unfinishedPools.map(async (pool) => { + if (pool.sousId === 0) { + return pool; + } + + try { + const chef = getChef(pool); + const endBlock = await getEndBlock(chef); + + if (endBlock >= blockNumber) { + return pool; + } + } catch (e) { + console.error(e); + } + }) + ).then((validPools) => { + setIsLoading(false); + setPools(validPools.filter(notEmpty)); + }); + })(); + }, [provider, setPools, pools.length]); + + return { + isLoading, + validPools: pools, + }; +};