Skip to content

Commit

Permalink
fetch token symbol from the chain (#74)
Browse files Browse the repository at this point in the history
  • Loading branch information
TopETH authored Apr 19, 2024
1 parent a8fc0a5 commit da90c1f
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 13 deletions.
6 changes: 3 additions & 3 deletions src/components/Elements/ListingCard/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ const ListingCardInner = ({
const [endTimestamp, setEndTimestamp] = useState(0);

const {
state: { api, apiState },
state: { api, apiState, symbol },
} = useRelayApi();
const { timeslicePeriod } = useCommon();

Expand Down Expand Up @@ -196,7 +196,7 @@ const ListingCardInner = ({
>
<Typography fontSize={'1rem'}>Price/timeslice:</Typography>
<Typography variant='h2'>
{formatBalance(listing.timeslicePrice.toString(), true)} ROC
{`${formatBalance(listing.timeslicePrice.toString(),true)} ${symbol}`}
</Typography>
</Box>
<Box
Expand All @@ -206,7 +206,7 @@ const ListingCardInner = ({
>
<Typography fontSize={'1rem'}>Total:</Typography>
<Typography variant='h2'>
{formatBalance(listing.currentPrice.toString(), true)} ROC
{`${formatBalance(listing.currentPrice.toString(), true)} ${symbol}`}
</Typography>
</Box>
</Box>
Expand Down
8 changes: 3 additions & 5 deletions src/components/Elements/SaleInfo/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export const SaleInfoGrid = ({
const [saleStartTimestamp, setSaleStartTimestamp] = useState(0);
const [saleEndTimestamp, setSaleEndTimestamp] = useState(0);
const {
state: { api, apiState },
state: { api, apiState, symbol },
} = useCoretimeApi();

const nextPhase = (): SalePhase => {
Expand Down Expand Up @@ -79,12 +79,10 @@ export const SaleInfoGrid = ({
</Box>
<Box className={styles.gridItem}>
<Typography variant='h6'>
{`Current price: ${formatBalance(currentPrice.toString(), false)}`}{' '}
ROC
{`Current price: ${formatBalance(currentPrice.toString(), false)} ${symbol}`}
</Typography>
<Typography>
{`Floor price: ${formatBalance(saleInfo.price.toString(), false)}`}{' '}
ROC
{`Floor price: ${formatBalance(saleInfo.price.toString(), false)} ${symbol}`}
</Typography>
</Box>
<Box>
Expand Down
4 changes: 3 additions & 1 deletion src/components/Modals/Sell/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import { useEffect, useState } from 'react';
import { AmountInput, RegionCard } from '@/components/Elements';
import { RecipientSelector } from '@/components/Elements/Selectors/RecipientSelector';

import { useCoretimeApi } from '@/contexts/apis';
import { useRegions } from '@/contexts/regions';
import { useToast } from '@/contexts/toast';
import { RegionMetadata } from '@/models';
Expand All @@ -31,6 +32,7 @@ export const SellModal = ({
regionMetadata,
}: SellModalProps) => {
const { activeAccount, api } = useInkathon();
const {state: {symbol}} = useCoretimeApi();

const { fetchRegions } = useRegions();
const { toastError, toastSuccess } = useToast();
Expand Down Expand Up @@ -112,7 +114,7 @@ export const SellModal = ({
amount={regionPrice}
title='Region price'
caption='The price for the entire region'
currency='ROC'
currency={symbol}
setAmount={setRegionPrice}
/>
</Stack>
Expand Down
16 changes: 15 additions & 1 deletion src/contexts/apis/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export type State = {
api: ApiPromise | null;
apiError: any;
apiState: ApiState;
symbol: string;
};

export const initialState: State = {
Expand All @@ -20,6 +21,7 @@ export const initialState: State = {
api: null,
apiError: null,
apiState: ApiState.DISCONNECTED,
symbol: '',
};

///
Expand All @@ -37,6 +39,8 @@ export const reducer = (state: any, action: any) => {
return { ...state, apiState: ApiState.ERROR, apiError: action.payload };
case 'DISCONNECTED':
return { ...state, apiState: ApiState.DISCONNECTED };
case 'SET_SYMBOL':
return { ...state, symbol: action.payload };
default:
throw new Error(`Unknown type: ${action.type}`);
}
Expand Down Expand Up @@ -66,7 +70,17 @@ export const connect = (
_api.on('connected', () => {
dispatch({ type: 'CONNECT', payload: _api });
// `ready` event is not emitted upon reconnection and is checked explicitly here.
_api.isReady.then(() => dispatch({ type: 'CONNECT_SUCCESS' }));
_api.isReady.then(() => {
dispatch({ type: 'CONNECT_SUCCESS' });
const chainInfo = _api.registry.getChainProperties();
if (chainInfo?.tokenSymbol.isSome) {
const [symbol] = chainInfo.tokenSymbol.toHuman() as [string];
dispatch({
type: 'SET_SYMBOL',
payload: symbol,
});
}
});
});
_api.on('ready', () => dispatch({ type: 'CONNECT_SUCCESS' }));
_api.on('error', (err) => dispatch({ type: 'CONNECT_ERROR', payload: err }));
Expand Down
6 changes: 3 additions & 3 deletions src/pages/purchase.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ const Purchase = () => {
const [saleEndTimestamp, setSaleEndTimestamp] = useState(0);
const { saleInfo, config, loading } = useSaleInfo();
const {
state: { api, apiState },
state: { api, apiState, symbol },
} = useCoretimeApi();

const { fetchRegions } = useRegions();
Expand All @@ -71,7 +71,7 @@ const Purchase = () => {

if (balance == 0) {
toastWarning(
'The selected account does not have any ROC tokens on the Coretime chain.'
`The selected account does not have any ${symbol} tokens on the Coretime chain.`
);
}
},
Expand Down Expand Up @@ -208,7 +208,7 @@ const Purchase = () => {
</Typography>
</Box>
<Typography variant='h6' sx={{ color: theme.palette.text.primary }}>
{`Your balance: ${formatBalance(balance.toString(), false)} ROC`}
{`Your balance: ${formatBalance(balance.toString(), false)} ${symbol}`}
</Typography>
</Box>
<Box>
Expand Down

0 comments on commit da90c1f

Please sign in to comment.