From 15b1c916c7d2acc550bf5efb72c7f82d817a4a05 Mon Sep 17 00:00:00 2001 From: Sergej Sakac <73715684+Szegoo@users.noreply.github.com> Date: Thu, 19 Sep 2024 20:56:03 +0200 Subject: [PATCH] Polkadot fixes (#235) * Add Polkadot * add missing pieces * lint & format * fix next.config.js * fixes * lint * fixes * fix --- src/components/Panels/SaleInfoPanel/index.tsx | 47 +++++--- src/contexts/balance/index.tsx | 114 ++++++++++-------- src/contexts/network/index.tsx | 2 +- src/contexts/sales/index.tsx | 10 +- src/pages/_app.tsx | 8 +- src/pages/index.tsx | 2 +- src/utils/functions/timestamps.ts | 2 + 7 files changed, 108 insertions(+), 77 deletions(-) diff --git a/src/components/Panels/SaleInfoPanel/index.tsx b/src/components/Panels/SaleInfoPanel/index.tsx index 6d6b087c..7bae5319 100644 --- a/src/components/Panels/SaleInfoPanel/index.tsx +++ b/src/components/Panels/SaleInfoPanel/index.tsx @@ -9,8 +9,9 @@ import DollarIcon from '@/assets/dollar.png'; import ListIcon from '@/assets/list.png'; import ShoppingIcon from '@/assets/shopping.png'; import { useCoretimeApi } from '@/contexts/apis'; +import { useNetwork } from '@/contexts/network'; import { useSaleInfo } from '@/contexts/sales'; -import { SalePhase } from '@/models'; +import { NetworkType, SalePhase } from '@/models'; import { DetailCard } from './DetailCard'; import styles from './index.module.scss'; @@ -18,6 +19,8 @@ import styles from './index.module.scss'; export const SaleInfoPanel = () => { const theme = useTheme(); + const { network } = useNetwork(); + const { state: { symbol, decimals }, } = useCoretimeApi(); @@ -94,27 +97,33 @@ export const SaleInfoPanel = () => { }, }} button={ - + // https://polkadot.polkassembly.io/referenda/1172 + network !== NetworkType.POLKADOT && ( + + ) } /> - openPriceModal(false)} - data-cy='price-modal' - /> + {/* https://polkadot.polkassembly.io/referenda/1172 */} + {network !== NetworkType.POLKADOT && ( + openPriceModal(false)} + data-cy='price-modal' + /> + )} ); }; diff --git a/src/contexts/balance/index.tsx b/src/contexts/balance/index.tsx index 25e9b035..be2a42c8 100644 --- a/src/contexts/balance/index.tsx +++ b/src/contexts/balance/index.tsx @@ -51,66 +51,80 @@ const BalanceProvider = ({ children }: Props) => { const [rxRcCurrencyBalance, setRxCurrencyBalance] = useState(0); useEffect(() => { + let unsubscribeCoretime: any = null; + let unsubscribeRelay: any = null; + let unsubscribeRegionx: any = null; + const subscribeBalances = async () => { - if (!activeAccount) { - setCoretimeBalance(0); - setRelayBalance(0); - setRxNativeBalance(0); - setRxCurrencyBalance(0); - return; - } - if (!isCoretimeReady || !isRelayReady || !coretimeApi || !relayApi) return; - - const { address } = activeAccount; - const unsubscribeCoretime = await coretimeApi.queryMulti( - [[coretimeApi.query.system.account, address]], - ([ - { - data: { free }, - }, - ]: [any]) => { - setCoretimeBalance(free as number); - } - ); - - const unsubscribeRelay = await relayApi.queryMulti( - [[relayApi.query.system.account, address]], - ([ - { - data: { free }, - }, - ]: [any]) => { - setRelayBalance(free as number); + try { + if (!activeAccount) { + setCoretimeBalance(0); + setRelayBalance(0); + setRxNativeBalance(0); + setRxCurrencyBalance(0); + return; } - ); - let unsubscribeRegionx: any = null; - - if (enableRegionX(network)) { - if (!regionxApi || !isRegionXReady) return; - unsubscribeRegionx = await regionxApi.queryMulti( - [ - [regionxApi.query.system?.account, address], - [regionxApi.query.tokens?.accounts, [address, 1]], // RELAY_ASSET_ID - ], + + if (!isCoretimeReady || !isRelayReady || !coretimeApi || !relayApi) return; + + const { address } = activeAccount; + + // Coretime subscription + unsubscribeCoretime = await coretimeApi.queryMulti( + [[coretimeApi.query.system.account, address]], ([ { - data: { free: freeNative }, + data: { free }, }, - { free: freeRelayCurrency }, - ]: any) => { - setRxNativeBalance(freeNative.toJSON() as number); - setRxCurrencyBalance(freeRelayCurrency.toJSON() as number); + ]: [any]) => { + setCoretimeBalance(free as number); + } + ); + + // Relay subscription + unsubscribeRelay = await relayApi.queryMulti( + [[relayApi.query.system.account, address]], + ([ + { + data: { free }, + }, + ]: [any]) => { + setRelayBalance(free as number); } ); - } - return () => { - if (unsubscribeCoretime) unsubscribeCoretime(); - if (unsubscribeRelay) unsubscribeRelay(); - if (unsubscribeRegionx) unsubscribeRegionx(); - }; + // RegionX subscription (optional) + if (enableRegionX(network)) { + if (!regionxApi || !isRegionXReady) return; + unsubscribeRegionx = await regionxApi.queryMulti( + [ + [regionxApi.query.system?.account, address], + [regionxApi.query.tokens?.accounts, [address, 1]], // RELAY_ASSET_ID + ], + ([ + { + data: { free: freeNative }, + }, + { free: freeRelayCurrency }, + ]: any) => { + setRxNativeBalance(freeNative.toJSON() as number); + setRxCurrencyBalance(freeRelayCurrency.toJSON() as number); + } + ); + } + } catch (error) { + /** */ + } }; + subscribeBalances(); + + // Cleanup function to unsubscribe + return () => { + if (unsubscribeCoretime) unsubscribeCoretime(); + if (unsubscribeRelay) unsubscribeRelay(); + if (unsubscribeRegionx) unsubscribeRegionx(); + }; }, [ activeAccount, coretimeApi, diff --git a/src/contexts/network/index.tsx b/src/contexts/network/index.tsx index 925d9776..3ee65b1c 100644 --- a/src/contexts/network/index.tsx +++ b/src/contexts/network/index.tsx @@ -30,7 +30,7 @@ const NetworkProvider = ({ children }: Props) => { useEffect(() => { if (!router.isReady) return; if (network === 'polkadot') setActiveNetwork(NetworkType.POLKADOT); - if (network === 'kusama') setActiveNetwork(NetworkType.KUSAMA); + else if (network === 'kusama') setActiveNetwork(NetworkType.KUSAMA); else if (network === 'rococo') setActiveNetwork(NetworkType.ROCOCO); else if (network === 'westend') setActiveNetwork(NetworkType.WESTEND); else { diff --git a/src/contexts/sales/index.tsx b/src/contexts/sales/index.tsx index 42356798..5d9514d9 100644 --- a/src/contexts/sales/index.tsx +++ b/src/contexts/sales/index.tsx @@ -6,6 +6,7 @@ import { getCorePriceAt, getCurrentPhase } from '@/utils/sale'; import { BrokerStatus, ContextStatus, + NetworkType, PhaseEndpoints, RELAY_CHAIN_BLOCK_TIME, SaleConfig, @@ -90,7 +91,7 @@ interface Props { const SaleInfoProvider = ({ children }: Props) => { const { network } = useNetwork(); const { - state: { api: coretimeApi, isApiReady: isCoretimeReady, height }, + state: { api: coretimeApi, isApiReady: isCoretimeReady, height, decimals }, timeslicePeriod, } = useCoretimeApi(); @@ -109,6 +110,11 @@ const SaleInfoProvider = ({ children }: Props) => { }, [saleInfo.saleStart, height, currentPhase]); useEffect(() => { + // https://polkadot.polkassembly.io/referenda/1172 + if (network === NetworkType.POLKADOT) { + setCurrentPrice(100 * Math.pow(10, decimals)); + return; + } setCurrentPrice( status !== ContextStatus.LOADED || height === 0 ? undefined : getCorePriceAt(at, saleInfo) ); @@ -186,7 +192,7 @@ const SaleInfoProvider = ({ children }: Props) => { useEffect(() => { if (height === 0) return; setCurrentPhase(getCurrentPhase(saleInfo, height)); - }, [saleInfo, height]); + }, [network, saleInfo, height]); return ( Coretime Hub - - + + @@ -101,8 +101,8 @@ export default function MyApp(props: MyAppProps) { - - + + ); diff --git a/src/pages/index.tsx b/src/pages/index.tsx index 5ab3b397..05113fa6 100644 --- a/src/pages/index.tsx +++ b/src/pages/index.tsx @@ -107,7 +107,7 @@ const Home = () => { ], bottom: { label: 'Total Burn', - value: totalBurn ? formatBalance(totalBurn) : '...', + value: formatBalance(totalBurn), dataCy: 'total-burn', }, }, diff --git a/src/utils/functions/timestamps.ts b/src/utils/functions/timestamps.ts index bd7b6596..895d675e 100644 --- a/src/utils/functions/timestamps.ts +++ b/src/utils/functions/timestamps.ts @@ -55,6 +55,8 @@ export const getBlockTime = (network: NetworkType): number => { return 6 * 1000; case NetworkType.KUSAMA: return 12 * 1000; + case NetworkType.POLKADOT: + return 12 * 1000; default: return 0; }