From 1ad031a2a9171e22e88f518bcd8f77b7c42e6a0c Mon Sep 17 00:00:00 2001 From: "shafu.eth" Date: Wed, 14 Dec 2022 22:16:54 +0100 Subject: [PATCH 1/5] useLastEthPrice --- src/components/Home.jsx | 10 ++-- src/components/Sync.jsx | 39 ++++++++++++---- src/hooks/useEthPrice.js | 1 - src/hooks/useLastEthPrice.js | 19 ++++++++ src/hooks/useNftSyncSimulation.js | 78 +++++++++++++++---------------- 5 files changed, 95 insertions(+), 52 deletions(-) create mode 100644 src/hooks/useLastEthPrice.js diff --git a/src/components/Home.jsx b/src/components/Home.jsx index 08728c3..ecec8c5 100644 --- a/src/components/Home.jsx +++ b/src/components/Home.jsx @@ -1,13 +1,15 @@ import Claim from "./Claim"; import NFTs from "./NFTs"; +import Sync from "./Sync"; export default function Home() { return ( <> - -
- -
+ {/* */} + + {/*
*/} + {/* */} + {/*
*/} ); } diff --git a/src/components/Sync.jsx b/src/components/Sync.jsx index c4c84c8..88b48c8 100644 --- a/src/components/Sync.jsx +++ b/src/components/Sync.jsx @@ -6,6 +6,9 @@ import useGasCost from "../hooks/useGasCost"; import { SwapRightOutlined } from "@ant-design/icons"; import useNftSyncSimulation from "../hooks/useNftSyncSimulation"; import { DOCS_URL } from "../consts/consts"; +import useEthPrice from "../hooks/useEthPrice"; +import useLastEthPrice from "../hooks/useLastEthPrice"; +import { round } from "../utils/currency"; export default function Sync({ onClose, setTxHash, tokenId, nft }) { const { config } = usePrepareContractWrite({ @@ -24,6 +27,8 @@ export default function Sync({ onClose, setTxHash, tokenId, nft }) { const { gasCost } = useGasCost(config); const { nftAfterSimulation, isLoading } = useNftSyncSimulation(tokenId); + const { ethPrice } = useEthPrice(); + const { lastEthPrice } = useLastEthPrice(); return ( window.open(DOCS_URL + "/pool#sync")} >
- {nftAfterSimulation && ( - <> -
-
Before
-
After
+ <> +
+
Before
+
After
+
+
+
+
Price
+
{lastEthPrice}
+
+ +
+
+ {ethPrice} +
+
+
+
+ {round(lastEthPrice - ethPrice, 2)} +
+
+
+
+ {nftAfterSimulation && (
XP
@@ -62,9 +85,9 @@ export default function Sync({ onClose, setTxHash, tokenId, nft }) {
-
- - )} + )} +
+
+ help sync ALL DYAD NFT's for all players!
diff --git a/src/hooks/useEthPrice.js b/src/hooks/useEthPrice.js index 95bc69a..c022aa2 100644 --- a/src/hooks/useEthPrice.js +++ b/src/hooks/useEthPrice.js @@ -9,7 +9,6 @@ export default function useEthPrice() { "https://min-api.cryptocompare.com/data/price?fsym=ETH&tsyms=BTC,USD,EUR" ); const data = await res.json(); - console.log("data", data); setEthPrice(data.USD); } diff --git a/src/hooks/useLastEthPrice.js b/src/hooks/useLastEthPrice.js new file mode 100644 index 0000000..4630a76 --- /dev/null +++ b/src/hooks/useLastEthPrice.js @@ -0,0 +1,19 @@ +import { useState } from "react"; +import { useContractRead } from "wagmi"; +import { CONTRACT_POOL } from "../consts/contract"; +import poolABI from "../abi/Pool.json"; + +export default function useLastEthPrice() { + const [lastEthPrice, setLastEthPrice] = useState(0); + + const { refetch } = useContractRead({ + addressOrName: CONTRACT_POOL, + contractInterface: poolABI["abi"], + functionName: "lastEthPrice", + onSuccess: (data) => { + setLastEthPrice(parseInt(data._hex) / 10 ** 8); + }, + }); + + return { refetch, lastEthPrice }; +} diff --git a/src/hooks/useNftSyncSimulation.js b/src/hooks/useNftSyncSimulation.js index ca4be8f..82aad49 100644 --- a/src/hooks/useNftSyncSimulation.js +++ b/src/hooks/useNftSyncSimulation.js @@ -26,51 +26,51 @@ export default function useNftSyncSimulation(tokenId) { if (!tokenId || !address) return; setIsLoading(true); - const gp = new ethers.providers.JsonRpcProvider( - process.env.REACT_APP_INFURA - ); - const blockNumber = await gp.getBlockNumber(); - const body = { - network_id: CURRENT_NETWORK.id, - block_number: blockNumber, - }; + // const gp = new ethers.providers.JsonRpcProvider( + // process.env.REACT_APP_INFURA + // ); + // const blockNumber = await gp.getBlockNumber(); + // const body = { + // network_id: CURRENT_NETWORK.id, + // block_number: blockNumber, + // }; - let forkId; - await axios - .post(TENDERLY_FORK_API, body, opts) - .then((res) => { - console.log( - `Forked with fork ID ${res.data.simulation_fork.id}. Check the Dashboard!` - ); - forkId = res.data.simulation_fork.id; - }) - .catch((err) => console.log(err)); + // let forkId; + // await axios + // .post(TENDERLY_FORK_API, body, opts) + // .then((res) => { + // console.log( + // `Forked with fork ID ${res.data.simulation_fork.id}. Check the Dashboard!` + // ); + // forkId = res.data.simulation_fork.id; + // }) + // .catch((err) => console.log(err)); - const forkRPC = `https://rpc.tenderly.co/fork/${forkId}`; - const provider = new ethers.providers.JsonRpcProvider(forkRPC); - const signer = provider.getSigner(); + // const forkRPC = `https://rpc.tenderly.co/fork/${forkId}`; + // const provider = new ethers.providers.JsonRpcProvider(forkRPC); + // const signer = provider.getSigner(); - const pool = new ethers.Contract(CONTRACT_POOL, poolABI["abi"], signer); - const dnft = new ethers.Contract(CONTRACT_dNFT, dnftABI["abi"], signer); + // const pool = new ethers.Contract(CONTRACT_POOL, poolABI["abi"], signer); + // const dnft = new ethers.Contract(CONTRACT_dNFT, dnftABI["abi"], signer); - const unsignedTx = await pool.populateTransaction.sync(); - const transactionParameters = [ - { - to: pool.address, - from: address, - data: unsignedTx.data, - gas: ethers.utils.hexValue(3000000), - gasPrice: ethers.utils.hexValue(1), - value: ethers.utils.hexValue(0), - }, - ]; - await provider.send("eth_sendTransaction", transactionParameters); - let res = await dnft.idToNft(tokenId); - setNftAfterSimulation(res); + // const unsignedTx = await pool.populateTransaction.sync(); + // const transactionParameters = [ + // { + // to: pool.address, + // from: address, + // data: unsignedTx.data, + // gas: ethers.utils.hexValue(3000000), + // gasPrice: ethers.utils.hexValue(1), + // value: ethers.utils.hexValue(0), + // }, + // ]; + // await provider.send("eth_sendTransaction", transactionParameters); + // let res = await dnft.idToNft(tokenId); + // setNftAfterSimulation(res); setIsLoading(false); - const TENDERLY_FORK_ACCESS_URL = `https://api.tenderly.co/api/v1/account/${process.env.REACT_APP_TENDERLY_USER}/project/${process.env.REACT_APP_TENDERLY_PROJECT}/fork/${forkId}`; - await axios.delete(TENDERLY_FORK_ACCESS_URL, opts); + // const TENDERLY_FORK_ACCESS_URL = `https://api.tenderly.co/api/v1/account/${process.env.REACT_APP_TENDERLY_USER}/project/${process.env.REACT_APP_TENDERLY_PROJECT}/fork/${forkId}`; + // await axios.delete(TENDERLY_FORK_ACCESS_URL, opts); } updateXP(); }, [tokenId]); From 199ea204bab94d727991cea0d98adac91f5f4a00 Mon Sep 17 00:00:00 2001 From: "shafu.eth" Date: Wed, 14 Dec 2022 22:23:20 +0100 Subject: [PATCH 2/5] works --- src/components/Home.jsx | 10 ++-- src/components/Sync.jsx | 12 +++-- src/hooks/useNftSyncSimulation.js | 78 +++++++++++++++---------------- 3 files changed, 52 insertions(+), 48 deletions(-) diff --git a/src/components/Home.jsx b/src/components/Home.jsx index ecec8c5..08728c3 100644 --- a/src/components/Home.jsx +++ b/src/components/Home.jsx @@ -1,15 +1,13 @@ import Claim from "./Claim"; import NFTs from "./NFTs"; -import Sync from "./Sync"; export default function Home() { return ( <> - {/* */} - - {/*
*/} - {/* */} - {/*
*/} + +
+ +
); } diff --git a/src/components/Sync.jsx b/src/components/Sync.jsx index 88b48c8..3d0afff 100644 --- a/src/components/Sync.jsx +++ b/src/components/Sync.jsx @@ -59,9 +59,15 @@ export default function Sync({ onClose, setTxHash, tokenId, nft }) {
{ethPrice}
-
+
+
+ {lastEthPrice - ethPrice < 0 ? ( + + + ) : ( + - + )} +
- {round(lastEthPrice - ethPrice, 2)} + {Math.abs(round(lastEthPrice - ethPrice, 2))}
@@ -75,7 +81,7 @@ export default function Sync({ onClose, setTxHash, tokenId, nft }) {
-
+
{parseInt(nftAfterSimulation[2]._hex)}
+
diff --git a/src/hooks/useNftSyncSimulation.js b/src/hooks/useNftSyncSimulation.js index 82aad49..ca4be8f 100644 --- a/src/hooks/useNftSyncSimulation.js +++ b/src/hooks/useNftSyncSimulation.js @@ -26,51 +26,51 @@ export default function useNftSyncSimulation(tokenId) { if (!tokenId || !address) return; setIsLoading(true); - // const gp = new ethers.providers.JsonRpcProvider( - // process.env.REACT_APP_INFURA - // ); - // const blockNumber = await gp.getBlockNumber(); - // const body = { - // network_id: CURRENT_NETWORK.id, - // block_number: blockNumber, - // }; + const gp = new ethers.providers.JsonRpcProvider( + process.env.REACT_APP_INFURA + ); + const blockNumber = await gp.getBlockNumber(); + const body = { + network_id: CURRENT_NETWORK.id, + block_number: blockNumber, + }; - // let forkId; - // await axios - // .post(TENDERLY_FORK_API, body, opts) - // .then((res) => { - // console.log( - // `Forked with fork ID ${res.data.simulation_fork.id}. Check the Dashboard!` - // ); - // forkId = res.data.simulation_fork.id; - // }) - // .catch((err) => console.log(err)); + let forkId; + await axios + .post(TENDERLY_FORK_API, body, opts) + .then((res) => { + console.log( + `Forked with fork ID ${res.data.simulation_fork.id}. Check the Dashboard!` + ); + forkId = res.data.simulation_fork.id; + }) + .catch((err) => console.log(err)); - // const forkRPC = `https://rpc.tenderly.co/fork/${forkId}`; - // const provider = new ethers.providers.JsonRpcProvider(forkRPC); - // const signer = provider.getSigner(); + const forkRPC = `https://rpc.tenderly.co/fork/${forkId}`; + const provider = new ethers.providers.JsonRpcProvider(forkRPC); + const signer = provider.getSigner(); - // const pool = new ethers.Contract(CONTRACT_POOL, poolABI["abi"], signer); - // const dnft = new ethers.Contract(CONTRACT_dNFT, dnftABI["abi"], signer); + const pool = new ethers.Contract(CONTRACT_POOL, poolABI["abi"], signer); + const dnft = new ethers.Contract(CONTRACT_dNFT, dnftABI["abi"], signer); - // const unsignedTx = await pool.populateTransaction.sync(); - // const transactionParameters = [ - // { - // to: pool.address, - // from: address, - // data: unsignedTx.data, - // gas: ethers.utils.hexValue(3000000), - // gasPrice: ethers.utils.hexValue(1), - // value: ethers.utils.hexValue(0), - // }, - // ]; - // await provider.send("eth_sendTransaction", transactionParameters); - // let res = await dnft.idToNft(tokenId); - // setNftAfterSimulation(res); + const unsignedTx = await pool.populateTransaction.sync(); + const transactionParameters = [ + { + to: pool.address, + from: address, + data: unsignedTx.data, + gas: ethers.utils.hexValue(3000000), + gasPrice: ethers.utils.hexValue(1), + value: ethers.utils.hexValue(0), + }, + ]; + await provider.send("eth_sendTransaction", transactionParameters); + let res = await dnft.idToNft(tokenId); + setNftAfterSimulation(res); setIsLoading(false); - // const TENDERLY_FORK_ACCESS_URL = `https://api.tenderly.co/api/v1/account/${process.env.REACT_APP_TENDERLY_USER}/project/${process.env.REACT_APP_TENDERLY_PROJECT}/fork/${forkId}`; - // await axios.delete(TENDERLY_FORK_ACCESS_URL, opts); + const TENDERLY_FORK_ACCESS_URL = `https://api.tenderly.co/api/v1/account/${process.env.REACT_APP_TENDERLY_USER}/project/${process.env.REACT_APP_TENDERLY_PROJECT}/fork/${forkId}`; + await axios.delete(TENDERLY_FORK_ACCESS_URL, opts); } updateXP(); }, [tokenId]); From 9844819cd0ef0067207c03e4243e07babfbb155f Mon Sep 17 00:00:00 2001 From: "shafu.eth" Date: Wed, 14 Dec 2022 22:36:21 +0100 Subject: [PATCH 3/5] refactor --- src/components/Sync.jsx | 58 +++-------------------------- src/components/SyncLastEthPrice.jsx | 36 ++++++++++++++++++ src/components/SyncXp.jsx | 31 +++++++++++++++ src/hooks/useNft.js | 2 + src/hooks/useNftSyncSimulation.js | 2 + 5 files changed, 77 insertions(+), 52 deletions(-) create mode 100644 src/components/SyncLastEthPrice.jsx create mode 100644 src/components/SyncXp.jsx diff --git a/src/components/Sync.jsx b/src/components/Sync.jsx index 3d0afff..665741c 100644 --- a/src/components/Sync.jsx +++ b/src/components/Sync.jsx @@ -3,14 +3,12 @@ import { useContractWrite, usePrepareContractWrite } from "wagmi"; import { CONTRACT_POOL } from "../consts/contract"; import PopupContent from "./PopupContent"; import useGasCost from "../hooks/useGasCost"; -import { SwapRightOutlined } from "@ant-design/icons"; import useNftSyncSimulation from "../hooks/useNftSyncSimulation"; import { DOCS_URL } from "../consts/consts"; -import useEthPrice from "../hooks/useEthPrice"; -import useLastEthPrice from "../hooks/useLastEthPrice"; -import { round } from "../utils/currency"; +import SyncLastEthPrice from "./SyncLastEthPrice"; +import SyncXp from "./SyncXp"; -export default function Sync({ onClose, setTxHash, tokenId, nft }) { +export default function Sync({ onClose, setTxHash, nft }) { const { config } = usePrepareContractWrite({ addressOrName: CONTRACT_POOL, contractInterface: PoolABI["abi"], @@ -26,9 +24,7 @@ export default function Sync({ onClose, setTxHash, tokenId, nft }) { }); const { gasCost } = useGasCost(config); - const { nftAfterSimulation, isLoading } = useNftSyncSimulation(tokenId); - const { ethPrice } = useEthPrice(); - const { lastEthPrice } = useLastEthPrice(); + const { isLoading } = useNftSyncSimulation(nft.id); return ( Before
After
-
-
-
Price
-
{lastEthPrice}
-
-
- -
-
- {ethPrice} -
-
- {lastEthPrice - ethPrice < 0 ? ( - + - ) : ( - - - )} -
-
- {Math.abs(round(lastEthPrice - ethPrice, 2))} -
-
-
-
- {nftAfterSimulation && ( -
-
-
XP
-
{nft.xp}
-
-
- -
-
- {parseInt(nftAfterSimulation[2]._hex)} -
-
+
-
- {parseInt(nftAfterSimulation[2]._hex) - nft.xp} -
-
-
-
- )} + +
+ help sync ALL DYAD NFT's for all players!
diff --git a/src/components/SyncLastEthPrice.jsx b/src/components/SyncLastEthPrice.jsx new file mode 100644 index 0000000..aab770e --- /dev/null +++ b/src/components/SyncLastEthPrice.jsx @@ -0,0 +1,36 @@ +import { SwapRightOutlined } from "@ant-design/icons"; +import useEthPrice from "../hooks/useEthPrice"; +import useLastEthPrice from "../hooks/useLastEthPrice"; +import { round } from "../utils/currency"; + +export default function SyncLastEthPrice() { + const { ethPrice } = useEthPrice(); + const { lastEthPrice } = useLastEthPrice(); + + return ( +
+
+
Price
+
{lastEthPrice}
+
+
+ +
+
+ {ethPrice} +
+
+ {lastEthPrice - ethPrice < 0 ? ( + + + ) : ( + - + )} +
+
+ {Math.abs(round(lastEthPrice - ethPrice, 2))} +
+
+
+
+ ); +} diff --git a/src/components/SyncXp.jsx b/src/components/SyncXp.jsx new file mode 100644 index 0000000..60a0c8a --- /dev/null +++ b/src/components/SyncXp.jsx @@ -0,0 +1,31 @@ +import { SwapRightOutlined } from "@ant-design/icons"; +import useNftSyncSimulation from "../hooks/useNftSyncSimulation"; + +export default function SyncXp({ nft }) { + const { nftAfterSimulation } = useNftSyncSimulation(nft.id); + + return ( + <> + {nftAfterSimulation && ( +
+
+
XP
+
{nft.xp}
+
+
+ +
+
+ {parseInt(nftAfterSimulation[2]._hex)} +
+
+
+
+ {parseInt(nftAfterSimulation[2]._hex) - nft.xp} +
+
+
+
+ )} + + ); +} diff --git a/src/hooks/useNft.js b/src/hooks/useNft.js index f2064c1..3e0c032 100644 --- a/src/hooks/useNft.js +++ b/src/hooks/useNft.js @@ -8,6 +8,7 @@ export default function useNft(id) { withdrawn: 0, deposit: 0, xp: 0, + id: id, }); const { refetch, isLoading, isFetching } = useContractRead({ @@ -20,6 +21,7 @@ export default function useNft(id) { withdrawn: parseInt(data[0]._hex), deposit: parseInt(data[1]._hex), xp: parseInt(data[2]._hex), + id: id, }); }, }); diff --git a/src/hooks/useNftSyncSimulation.js b/src/hooks/useNftSyncSimulation.js index ca4be8f..4fc98c3 100644 --- a/src/hooks/useNftSyncSimulation.js +++ b/src/hooks/useNftSyncSimulation.js @@ -17,6 +17,7 @@ const opts = { // simulate a sync call for a specific nft export default function useNftSyncSimulation(tokenId) { + console.log("useNftSyncSimulation", tokenId); const { address } = useAccount(); const [nftAfterSimulation, setNftAfterSimulation] = useState(); const [isLoading, setIsLoading] = useState(false); @@ -24,6 +25,7 @@ export default function useNftSyncSimulation(tokenId) { useEffect(() => { async function updateXP() { if (!tokenId || !address) return; + console.log("updating xp for", tokenId); setIsLoading(true); const gp = new ethers.providers.JsonRpcProvider( From bb2d48c05fbaf9850be3fe818f178400ac4a7c8c Mon Sep 17 00:00:00 2001 From: "shafu.eth" Date: Wed, 14 Dec 2022 22:37:42 +0100 Subject: [PATCH 4/5] rm log --- src/components/NFT.jsx | 7 +------ src/hooks/useNftSyncSimulation.js | 1 - 2 files changed, 1 insertion(+), 7 deletions(-) diff --git a/src/components/NFT.jsx b/src/components/NFT.jsx index e3ffb07..4ed9490 100644 --- a/src/components/NFT.jsx +++ b/src/components/NFT.jsx @@ -96,12 +96,7 @@ export default function NFT({ index, xps, xpsAverage }) { /> - + )} diff --git a/src/hooks/useNftSyncSimulation.js b/src/hooks/useNftSyncSimulation.js index 4fc98c3..d4f0f9b 100644 --- a/src/hooks/useNftSyncSimulation.js +++ b/src/hooks/useNftSyncSimulation.js @@ -17,7 +17,6 @@ const opts = { // simulate a sync call for a specific nft export default function useNftSyncSimulation(tokenId) { - console.log("useNftSyncSimulation", tokenId); const { address } = useAccount(); const [nftAfterSimulation, setNftAfterSimulation] = useState(); const [isLoading, setIsLoading] = useState(false); From 79eea2e2a04d2334fea381406855c02583bc7a6e Mon Sep 17 00:00:00 2001 From: "shafu.eth" Date: Wed, 14 Dec 2022 22:38:22 +0100 Subject: [PATCH 5/5] rm log --- src/hooks/useNftSyncSimulation.js | 1 - 1 file changed, 1 deletion(-) diff --git a/src/hooks/useNftSyncSimulation.js b/src/hooks/useNftSyncSimulation.js index d4f0f9b..ca4be8f 100644 --- a/src/hooks/useNftSyncSimulation.js +++ b/src/hooks/useNftSyncSimulation.js @@ -24,7 +24,6 @@ export default function useNftSyncSimulation(tokenId) { useEffect(() => { async function updateXP() { if (!tokenId || !address) return; - console.log("updating xp for", tokenId); setIsLoading(true); const gp = new ethers.providers.JsonRpcProvider(