Skip to content

Commit

Permalink
Merge pull request #37 from DyadStablecoin/useLastEthPrice
Browse files Browse the repository at this point in the history
Use last eth price
  • Loading branch information
shafu0x authored Dec 14, 2022
2 parents 43a5de3 + 79eea2e commit 487a787
Show file tree
Hide file tree
Showing 7 changed files with 102 additions and 37 deletions.
7 changes: 1 addition & 6 deletions src/components/NFT.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -96,12 +96,7 @@ export default function NFT({ index, xps, xpsAverage }) {
/>
</Popup>
<Popup isOpen={isOpenSync} onClose={onCloseSync}>
<Sync
tokenId={tokenId}
onClose={onCloseSync}
setTxHash={setTxHash}
nft={nft}
/>
<Sync onClose={onCloseSync} setTxHash={setTxHash} nft={nft} />
</Popup>
</>
)}
Expand Down
43 changes: 13 additions & 30 deletions src/components/Sync.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +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 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"],
Expand All @@ -23,7 +24,7 @@ export default function Sync({ onClose, setTxHash, tokenId, nft }) {
});

const { gasCost } = useGasCost(config);
const { nftAfterSimulation, isLoading } = useNftSyncSimulation(tokenId);
const { isLoading } = useNftSyncSimulation(nft.id);

return (
<PopupContent
Expand All @@ -38,33 +39,15 @@ export default function Sync({ onClose, setTxHash, tokenId, nft }) {
infoOnClick={() => window.open(DOCS_URL + "/pool#sync")}
>
<div className="flex flex-col gap-4">
{nftAfterSimulation && (
<>
<div className="flex justify-between">
<div className="text-sm">Before</div>
<div className="text-sm">After</div>
</div>
<div className="flex justify-between items-center">
<div>
<div className="text-[#737E76]">XP</div>
<div>{nft.xp}</div>
</div>
<div>
<SwapRightOutlined />
</div>
<div className="flex gap-6 items-center justify-center">
{parseInt(nftAfterSimulation[2]._hex)}
<div className="flex gap-1 items-center">
<div className="text-sm text-green-300">+</div>
<div className="text-sm">
{parseInt(nftAfterSimulation[2]._hex) - nft.xp}
</div>
</div>
</div>
</div>
<div className="bg-[#3A403C] h-[1px] w-full"></div>
</>
)}
<>
<div className="flex justify-between">
<div className="text-sm">Before</div>
<div className="text-sm">After</div>
</div>
<SyncLastEthPrice />
<SyncXp nft={nft} />
<div className="bg-[#3A403C] h-[1px] w-full"></div>
</>
<div>+ help sync ALL DYAD NFT's for all players!</div>
<div className="bg-[#3A403C] h-[1px] w-full"></div>
<div className="flex justify-between">
Expand Down
36 changes: 36 additions & 0 deletions src/components/SyncLastEthPrice.jsx
Original file line number Diff line number Diff line change
@@ -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 (
<div className="flex justify-between items-center">
<div>
<div className="text-[#737E76]">Price</div>
<div>{lastEthPrice}</div>
</div>
<div>
<SwapRightOutlined />
</div>
<div className="flex gap-6 items-center justify-center">
{ethPrice}
<div className="flex gap-1 items-center">
<div className="text-sm ">
{lastEthPrice - ethPrice < 0 ? (
<span className="text-green-300">+</span>
) : (
<span className="text-red-300">-</span>
)}
</div>
<div className="text-sm">
{Math.abs(round(lastEthPrice - ethPrice, 2))}
</div>
</div>
</div>
</div>
);
}
31 changes: 31 additions & 0 deletions src/components/SyncXp.jsx
Original file line number Diff line number Diff line change
@@ -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 && (
<div className="flex justify-between items-center">
<div>
<div className="text-[#737E76]">XP</div>
<div>{nft.xp}</div>
</div>
<div>
<SwapRightOutlined />
</div>
<div className="flex gap-6 items-center justify-between">
{parseInt(nftAfterSimulation[2]._hex)}
<div className="flex gap-1 items-center">
<div className="text-sm text-green-300">+</div>
<div className="text-sm">
{parseInt(nftAfterSimulation[2]._hex) - nft.xp}
</div>
</div>
</div>
</div>
)}
</>
);
}
1 change: 0 additions & 1 deletion src/hooks/useEthPrice.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down
19 changes: 19 additions & 0 deletions src/hooks/useLastEthPrice.js
Original file line number Diff line number Diff line change
@@ -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 };
}
2 changes: 2 additions & 0 deletions src/hooks/useNft.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ export default function useNft(id) {
withdrawn: 0,
deposit: 0,
xp: 0,
id: id,
});

const { refetch, isLoading, isFetching } = useContractRead({
Expand All @@ -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,
});
},
});
Expand Down

1 comment on commit 487a787

@vercel
Copy link

@vercel vercel bot commented on 487a787 Dec 14, 2022

Choose a reason for hiding this comment

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

Successfully deployed to the following URLs:

frontend – ./

frontend-git-main-dyad.vercel.app
frontend-dyad.vercel.app
frontend-woad-five.vercel.app

Please sign in to comment.