Skip to content

Commit

Permalink
Polkadot fixes (#235)
Browse files Browse the repository at this point in the history
* Add Polkadot

* add missing pieces

* lint & format

* fix next.config.js

* fixes

* lint

* fixes

* fix
  • Loading branch information
Szegoo authored Sep 19, 2024
1 parent 2399251 commit 15b1c91
Show file tree
Hide file tree
Showing 7 changed files with 108 additions and 77 deletions.
47 changes: 28 additions & 19 deletions src/components/Panels/SaleInfoPanel/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,18 @@ 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';

export const SaleInfoPanel = () => {
const theme = useTheme();

const { network } = useNetwork();

const {
state: { symbol, decimals },
} = useCoretimeApi();
Expand Down Expand Up @@ -94,27 +97,33 @@ export const SaleInfoPanel = () => {
},
}}
button={
<Button
onClick={onAnalyze}
size='small'
variant='text'
className={styles.buttonWrapper}
sx={{
background: '#e8eff7',
color: theme.palette.text.primary,
}}
data-cy='btn-analyze-price'
>
Analyze
</Button>
// https://polkadot.polkassembly.io/referenda/1172
network !== NetworkType.POLKADOT && (
<Button
onClick={onAnalyze}
size='small'
variant='text'
className={styles.buttonWrapper}
sx={{
background: '#e8eff7',
color: theme.palette.text.primary,
}}
data-cy='btn-analyze-price'
>
Analyze
</Button>
)
}
/>
</Box>
<PriceModal
open={priceModalOpen}
onClose={() => openPriceModal(false)}
data-cy='price-modal'
/>
{/* https://polkadot.polkassembly.io/referenda/1172 */}
{network !== NetworkType.POLKADOT && (
<PriceModal
open={priceModalOpen}
onClose={() => openPriceModal(false)}
data-cy='price-modal'
/>
)}
</>
);
};
114 changes: 64 additions & 50 deletions src/contexts/balance/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion src/contexts/network/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
10 changes: 8 additions & 2 deletions src/contexts/sales/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { getCorePriceAt, getCurrentPhase } from '@/utils/sale';
import {
BrokerStatus,
ContextStatus,
NetworkType,
PhaseEndpoints,
RELAY_CHAIN_BLOCK_TIME,
SaleConfig,
Expand Down Expand Up @@ -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();

Expand All @@ -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)
);
Expand Down Expand Up @@ -186,7 +192,7 @@ const SaleInfoProvider = ({ children }: Props) => {
useEffect(() => {
if (height === 0) return;
setCurrentPhase(getCurrentPhase(saleInfo, height));
}, [saleInfo, height]);
}, [network, saleInfo, height]);

return (
<SaleDataContext.Provider
Expand Down
8 changes: 4 additions & 4 deletions src/pages/_app.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,8 @@ export default function MyApp(props: MyAppProps) {
<meta name='viewport' content='initial-scale=1, width=device-width' />
<title>Coretime Hub</title>
</Head>
<UtilProviders>
<NetworkProvider>
<NetworkProvider>
<UtilProviders>
<AccountProvider>
<ApiProviders>
<BalanceProvider>
Expand All @@ -101,8 +101,8 @@ export default function MyApp(props: MyAppProps) {
</BalanceProvider>
</ApiProviders>
</AccountProvider>
</NetworkProvider>
</UtilProviders>
</UtilProviders>
</NetworkProvider>
<Analytics />
</CacheProvider>
);
Expand Down
2 changes: 1 addition & 1 deletion src/pages/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ const Home = () => {
],
bottom: {
label: 'Total Burn',
value: totalBurn ? formatBalance(totalBurn) : '...',
value: formatBalance(totalBurn),
dataCy: 'total-burn',
},
},
Expand Down
2 changes: 2 additions & 0 deletions src/utils/functions/timestamps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down

0 comments on commit 15b1c91

Please sign in to comment.