add usePoolListing hook

This commit is contained in:
João Geonizeli
2021-09-14 23:22:19 -03:00
parent a5aead46ea
commit d01502d765
3 changed files with 70 additions and 56 deletions

View File

@@ -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<PoolConfig[]>(
"validPools",
[],
1200000 // 20 minutes
);
const [isLoadingPools, setIsLoadingPools] = useState(true);
const { isLoading, validPools } = usePoolListing();
// TODO<wallet>: 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 (
<div className="w-full grid place-items-center mt-12">
<Spinner />

View File

@@ -0,0 +1 @@
export * from "./usePoolListing";

View File

@@ -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<PoolConfig[]>(
"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,
};
};