diff --git a/src/components/Hooks/salePhase.tsx b/src/components/Hooks/salePhase.tsx index 252a6c0f..d2cc9c81 100644 --- a/src/components/Hooks/salePhase.tsx +++ b/src/components/Hooks/salePhase.tsx @@ -2,7 +2,11 @@ import { ApiPromise } from '@polkadot/api'; import { useRouter } from 'next/router'; import { useCallback, useEffect, useState } from 'react'; -import { getBlockTimestamp, parseHNString } from '@/utils/functions'; +import { + getBlockTime, + getBlockTimestamp, + parseHNString, +} from '@/utils/functions'; import { getCurrentPhase, getSaleEndInBlocks, @@ -34,6 +38,7 @@ const useSalePhase = () => { const router = useRouter(); const { network } = router.query; + const blockTime = getBlockTime(network); const fetchCurrentPhase = useCallback( async (api: ApiPromise) => { @@ -44,7 +49,7 @@ const useSalePhase = () => { ).lastCommittedTimeslice.toString() ); - const _saleStart = getSaleStartInBlocks(saleInfo, config); + const _saleStart = getSaleStartInBlocks(saleInfo); const _saleEnd = getSaleEndInBlocks( saleInfo, blockNumber, @@ -52,12 +57,12 @@ const useSalePhase = () => { network ); - getBlockTimestamp(api, _saleEnd).then((value: number) => - setSaleEndTimestamp(value) - ); - getBlockTimestamp(api, _saleStart).then((value: number) => + getBlockTimestamp(api, _saleStart, blockTime).then((value: number) => setSaleStartTimestamp(value) ); + getBlockTimestamp(api, _saleEnd, blockTime).then((value: number) => + setSaleEndTimestamp(value) + ); const progress = getSaleProgress( saleInfo, @@ -86,7 +91,7 @@ const useSalePhase = () => { }, ]); }, - [saleInfo, config, network] + [saleInfo, config, network, blockTime] ); useEffect(() => { diff --git a/src/components/Hooks/salePrice.tsx b/src/components/Hooks/salePrice.tsx index f216cf92..c1207de7 100644 --- a/src/components/Hooks/salePrice.tsx +++ b/src/components/Hooks/salePrice.tsx @@ -15,7 +15,7 @@ const useSalePrice = () => { const [currentPrice, setCurrentPrice] = useState(0); const fetchCurrentPrice = useCallback(async () => { - if (api && apiState == ApiState.READY) { + if (api && apiState === ApiState.READY) { const blockNumber = (await api.query.system.number()).toJSON() as number; const price = getCurrentPrice(saleInfo, blockNumber); setCurrentPrice(price); diff --git a/src/contexts/apis/common.ts b/src/contexts/apis/common.ts index 58d4b0d9..ad96b28f 100644 --- a/src/contexts/apis/common.ts +++ b/src/contexts/apis/common.ts @@ -5,8 +5,6 @@ import { ApiPromise, WsProvider } from '@polkadot/api'; import jsonrpc from '@polkadot/types/interfaces/jsonrpc'; import { DefinitionRpcExt } from '@polkadot/types/types'; -import { parseHNString } from '@/utils/functions'; - import { ApiState } from './types'; export type State = { @@ -16,7 +14,6 @@ export type State = { apiError: any; apiState: ApiState; symbol: string; - blockTime: number; // In ms. }; export const initialState: State = { @@ -27,7 +24,6 @@ export const initialState: State = { apiError: null, apiState: ApiState.DISCONNECTED, symbol: '', - blockTime: 12000, }; /// @@ -56,8 +52,6 @@ export const reducer = (state: any, action: any) => { return { ...state, apiState: ApiState.DISCONNECTED }; case 'SET_SYMBOL': return { ...state, symbol: action.payload }; - case 'SET_BLOCKTIME': - return { ...state, blockTime: action.payload }; default: throw new Error(`Unknown type: ${action.type}`); } @@ -95,15 +89,6 @@ export const connect = ( payload: symbol, }); } - if (_api.consts.aura?.slotDuration) { - const duration = parseHNString( - _api.consts.aura.slotDuration.toHuman() as string - ); - dispatch({ - type: 'SET_BLOCKTIME', - payload: duration, - }); - } }); }); _api.on('ready', () => dispatch({ type: 'CONNECT_SUCCESS' })); diff --git a/src/utils/functions.ts b/src/utils/functions.ts index f9b774cf..9def7442 100644 --- a/src/utils/functions.ts +++ b/src/utils/functions.ts @@ -117,6 +117,12 @@ export const extractRegionIdFromRaw = (rawRegionId: bigint): RegionId => { }; }; +export const getBlockTime = (network: any): number => { + // Coretime on Rococo has async backing and due to this it has a block time of 6 seconds. + const blockTime = !network || network == 'rococo' ? 6000 : 12000; + return blockTime; +}; + export const rcBlockToParachainBlock = ( network: any, blockNumber: number diff --git a/src/utils/sale/utils.test.ts b/src/utils/sale/utils.test.ts index 8f5e677b..5175b223 100644 --- a/src/utils/sale/utils.test.ts +++ b/src/utils/sale/utils.test.ts @@ -79,7 +79,7 @@ describe('Purchase page', () => { describe('getSaleStartInBlocks', () => { it('works', () => { - expect(getSaleStartInBlocks(mockSaleInfo, mockConfig)).toBe( + expect(getSaleStartInBlocks(mockSaleInfo)).toBe( mockSaleInfo.saleStart - mockConfig.interludeLength ); }); diff --git a/src/utils/sale/utils.ts b/src/utils/sale/utils.ts index ace492e3..e54fa651 100644 --- a/src/utils/sale/utils.ts +++ b/src/utils/sale/utils.ts @@ -17,13 +17,10 @@ export const getCurrentPhase = ( } }; -export const getSaleStartInBlocks = ( - saleInfo: SaleInfo, - config: SaleConfig -) => { +export const getSaleStartInBlocks = (saleInfo: SaleInfo) => { // `saleInfo.saleStart` defines the start of the leadin phase. // However, we want to account for the interlude period as well. - return saleInfo.saleStart - config.interludeLength; + return saleInfo.saleStart; }; export const getSaleEndInBlocks = ( @@ -47,7 +44,7 @@ export const getSaleProgress = ( lastCommittedTimeslice: number, network: any ): number => { - const start = getSaleStartInBlocks(saleInfo, config); + const start = getSaleStartInBlocks(saleInfo) - config.interludeLength; const end = getSaleEndInBlocks( saleInfo, blockNumber,