Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
Szegoo committed Sep 19, 2024
1 parent d9a0d6b commit ee46767
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 68 deletions.
35 changes: 18 additions & 17 deletions src/components/Panels/SaleInfoPanel/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@ 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 { NetworkType, SalePhase } from '@/models';

import { DetailCard } from './DetailCard';
import styles from './index.module.scss';
import { useNetwork } from '@/contexts/network';

export const SaleInfoPanel = () => {
const theme = useTheme();
Expand Down Expand Up @@ -98,31 +98,32 @@ export const SaleInfoPanel = () => {
}}
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>
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>
{/* https://polkadot.polkassembly.io/referenda/1172 */}
{network !== NetworkType.POLKADOT &&
{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/sales/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ interface Props {
const SaleInfoProvider = ({ children }: Props) => {
const { network } = useNetwork();
const {
state: { api: coretimeApi, isApiReady: isCoretimeReady, height, decimals, },
state: { api: coretimeApi, isApiReady: isCoretimeReady, height, decimals },
timeslicePeriod,
} = useCoretimeApi();

Expand Down

0 comments on commit ee46767

Please sign in to comment.