Skip to content

feat(web): migrate price api #995

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jun 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions web/src/consts/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,4 @@
export const ONE_BASIS_POINT = 10000n;

export const KLEROS_CONTRACT_ADDRESS = "ethereum:0x93ed3fbe21207ec2e8f2d3c3de6e058cb73bc04d";
export const WETH_CONTRACT_ADDRESS = "ethereum:0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2";
15 changes: 5 additions & 10 deletions web/src/hooks/useCoinPrice.tsx
Original file line number Diff line number Diff line change
@@ -1,18 +1,13 @@
import useSWR from "swr";

const fetchCoinPrice = async (...coinIds) => {
const fetchData = async (coinId) => {
const response = await fetch(`https://api.coingecko.com/api/v3/coins/${coinId}`);
const data = await response.json();
return data.market_data.current_price.usd;
};

const prices = await Promise.all(coinIds.map(fetchData));
return prices;
const fetchCoinPrices = async (...coinIds) => {
const response = await fetch(`https://coins.llama.fi/prices/current/${coinIds.join(",")}?searchWidth=1h`);
const data = await response.json();
return data.coins;
};

export const useCoinPrice = (coinIds: string[]) => {
const { data: prices, error } = useSWR<number[]>(coinIds, fetchCoinPrice);
const { data: prices, error } = useSWR(coinIds, fetchCoinPrices);
return {
prices,
error,
Expand Down
15 changes: 12 additions & 3 deletions web/src/pages/Courts/CourtDetails/Stats.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import styled from "styled-components";
import { formatUnits, formatEther } from "viem";
import { useParams } from "react-router-dom";
import { useCourtDetails, CourtDetailsQuery } from "queries/useCourtDetails";
import { useCoinPrice } from "hooks/useCoinPrice";
import { KLEROS_CONTRACT_ADDRESS, WETH_CONTRACT_ADDRESS } from "src/consts/index";
import StatDisplay, { IStatDisplay } from "components/StatDisplay";
import BalanceIcon from "svgs/icons/law-balance.svg";
import MinStake from "svgs/icons/min-stake.svg";
Expand All @@ -12,6 +12,7 @@ import VoteStake from "svgs/icons/vote-stake.svg";
import PNKIcon from "svgs/icons/pnk.svg";
import PNKRedistributedIcon from "svgs/icons/redistributed-pnk.svg";
import EthereumIcon from "svgs/icons/ethereum.svg";
import { useCoinPrice } from "hooks/useCoinPrice";
import { isUndefined } from "~src/utils";

const StyledCard = styled.div`
Expand Down Expand Up @@ -105,11 +106,19 @@ const stats: IStat[] = [
const Stats = () => {
const { id } = useParams();
const { data } = useCourtDetails(id);
const { prices } = useCoinPrice(["kleros", "ethereum"]);
const { prices: pricesData } = useCoinPrice([KLEROS_CONTRACT_ADDRESS, WETH_CONTRACT_ADDRESS]);

return (
<StyledCard>
{stats.map(({ title, coinId, getText, getSubtext, color, icon }, i) => {
const coinPrice = prices && !isUndefined(coinId) ? prices[coinId] : undefined;
let coinPrice;
if (!isUndefined(pricesData)) {
if (coinId === 0) {
coinPrice = pricesData[KLEROS_CONTRACT_ADDRESS].price;
} else if (coinId === 1) {
coinPrice = pricesData[WETH_CONTRACT_ADDRESS].price;
}
}
return (
<StatDisplay
key={i}
Expand Down
12 changes: 10 additions & 2 deletions web/src/pages/Home/CourtOverview/Stats.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import EthereumIcon from "svgs/icons/ethereum.svg";
import PNKRedistributedIcon from "svgs/icons/redistributed-pnk.svg";
import JurorIcon from "svgs/icons/user.svg";
import BalanceIcon from "svgs/icons/law-balance.svg";
import { KLEROS_CONTRACT_ADDRESS, WETH_CONTRACT_ADDRESS } from "src/consts/index";
import { commify } from "utils/commify";
import { isUndefined } from "utils/index";
import { useHomePageContext, HomePageQuery, HomePageQueryDataPoints } from "hooks/useHomePageContext";
Expand Down Expand Up @@ -82,11 +83,18 @@ const stats: IStat[] = [
];
const Stats = () => {
const { data } = useHomePageContext();
const { prices } = useCoinPrice(["kleros", "ethereum"]);
const { prices: pricesData } = useCoinPrice([KLEROS_CONTRACT_ADDRESS, WETH_CONTRACT_ADDRESS]);
return (
<StyledCard>
{stats.map(({ title, coinId, getText, getSubtext, color, icon }, i) => {
const coinPrice = prices && !isUndefined(coinId) ? prices[coinId] : undefined;
let coinPrice;
if (!isUndefined(pricesData)) {
if (coinId === 0) {
coinPrice = pricesData[KLEROS_CONTRACT_ADDRESS].price;
} else if (coinId === 1) {
coinPrice = pricesData[WETH_CONTRACT_ADDRESS].price;
}
}
return (
<StatDisplay
key={i}
Expand Down