Skip to content

Commit

Permalink
feat: apply diamond to home and market pages
Browse files Browse the repository at this point in the history
  • Loading branch information
bojan07 committed Oct 2, 2023
1 parent bd43fae commit 2e662b5
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 14 deletions.
22 changes: 13 additions & 9 deletions packages/dapp/components/redeem/lib/use-prices.ts
Original file line number Diff line number Diff line change
@@ -1,28 +1,32 @@
import useDeployedContracts from "@/components/lib/hooks/contracts/use-deployed-contracts";
import useManagerManaged from "@/components/lib/hooks/contracts/use-manager-managed";
import { getIMetaPoolContract } from "@/components/utils/contracts";
import useProtocolContracts from "@/components/lib/hooks/contracts/use-protocol-contracts";
import useWeb3 from "@/components/lib/hooks/use-web-3";
import { BigNumber, utils } from "ethers";
import { useEffect, useState } from "react";

const usePrices = (): [BigNumber | null, BigNumber | null, () => Promise<void>] => {
const deployedContracts = useDeployedContracts();
const managedContracts = useManagerManaged();
const protocolContracts = useProtocolContracts();
const { provider } = useWeb3();

const [twapPrice, setTwapPrice] = useState<BigNumber | null>(null);
const [spotPrice, setSpotPrice] = useState<BigNumber | null>(null);

async function refreshPrices() {
if (managedContracts && deployedContracts) {
const dollarTokenAddress = await deployedContracts.manager.dollarTokenAddress();
const newTwapPrice = await managedContracts.dollarTwapOracle.consult(dollarTokenAddress);
const newSpotPrice = await managedContracts.dollarMetapool["get_dy(int128,int128,uint256)"](0, 1, utils.parseEther("1"));
if (protocolContracts && provider) {
const dollarTokenAddress = protocolContracts.managerFacet && await protocolContracts.managerFacet.dollarTokenAddress();
const newTwapPrice = protocolContracts.twapOracleDollar3poolFacet && await protocolContracts.twapOracleDollar3poolFacet.consult(dollarTokenAddress);

const dollar3poolMarket = protocolContracts.managerFacet && await protocolContracts.managerFacet.stableSwapMetaPoolAddress();
const dollarMetapool = getIMetaPoolContract(dollar3poolMarket, provider)
const newSpotPrice = await dollarMetapool["get_dy(int128,int128,uint256)"](0, 1, utils.parseEther("1"));
setTwapPrice(newTwapPrice);
setSpotPrice(newSpotPrice);
}
}

useEffect(() => {
refreshPrices();
}, [managedContracts, deployedContracts]);
}, [protocolContracts, provider]);

return [twapPrice, spotPrice, refreshPrices];
};
Expand Down
10 changes: 5 additions & 5 deletions packages/dapp/pages/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { ethers } from "ethers";
import { FC, useState } from "react";
import "@uniswap/widgets/fonts.css";

import useManagerManaged from "@/components/lib/hooks/contracts/use-manager-managed";
import useProtocolContracts from "@/components/lib/hooks/contracts/use-protocol-contracts";
import useEffectAsync from "@/components/lib/hooks/use-effect-async";
import DollarPrice from "@/components/redeem/dollar-price";
import { fetchData } from "@/components/utils/local-data";
Expand All @@ -12,21 +12,21 @@ const WalletConnectionWall = dynamic(() => import("@/components/ui/wallet-connec

const index: FC = (): JSX.Element => {
const [twapPrice, setTwapPrice] = useState<ethers.BigNumber | null>(null);
const managedContracts = useManagerManaged();
const protocolContracts = useProtocolContracts();

useEffectAsync(async () => {
if (managedContracts != null) {
if (protocolContracts != null) {
try {
console.log(twapPrice, "priced in ");
} catch (error) {
console.log("Error occurred while executing contract call", error);
setTwapPrice(null);
}
} else {
console.log("managedContracts is null");
console.log("protocolContracts is null");
setTwapPrice(null);
}
}, [managedContracts]);
}, [protocolContracts]);

if (process.env.DEBUG === "true") {
fetchData();
Expand Down

0 comments on commit 2e662b5

Please sign in to comment.