Skip to content

Commit

Permalink
..
Browse files Browse the repository at this point in the history
  • Loading branch information
Szegoo committed Apr 22, 2024
1 parent 187d861 commit 1d4d05a
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 30 deletions.
19 changes: 12 additions & 7 deletions src/components/Hooks/salePhase.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -34,6 +38,7 @@ const useSalePhase = () => {

const router = useRouter();
const { network } = router.query;
const blockTime = getBlockTime(network);

const fetchCurrentPhase = useCallback(
async (api: ApiPromise) => {
Expand All @@ -44,20 +49,20 @@ const useSalePhase = () => {
).lastCommittedTimeslice.toString()
);

const _saleStart = getSaleStartInBlocks(saleInfo, config);
const _saleStart = getSaleStartInBlocks(saleInfo);
const _saleEnd = getSaleEndInBlocks(
saleInfo,
blockNumber,
lastCommittedTimeslice,
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,
Expand Down Expand Up @@ -86,7 +91,7 @@ const useSalePhase = () => {
},
]);
},
[saleInfo, config, network]
[saleInfo, config, network, blockTime]
);

useEffect(() => {
Expand Down
2 changes: 1 addition & 1 deletion src/components/Hooks/salePrice.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
15 changes: 0 additions & 15 deletions src/contexts/apis/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand All @@ -16,7 +14,6 @@ export type State = {
apiError: any;
apiState: ApiState;
symbol: string;
blockTime: number; // In ms.
};

export const initialState: State = {
Expand All @@ -27,7 +24,6 @@ export const initialState: State = {
apiError: null,
apiState: ApiState.DISCONNECTED,
symbol: '',
blockTime: 12000,
};

///
Expand Down Expand Up @@ -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}`);
}
Expand Down Expand Up @@ -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' }));
Expand Down
6 changes: 6 additions & 0 deletions src/utils/functions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/utils/sale/utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ describe('Purchase page', () => {

describe('getSaleStartInBlocks', () => {
it('works', () => {
expect(getSaleStartInBlocks(mockSaleInfo, mockConfig)).toBe(
expect(getSaleStartInBlocks(mockSaleInfo)).toBe(
mockSaleInfo.saleStart - mockConfig.interludeLength
);
});
Expand Down
9 changes: 3 additions & 6 deletions src/utils/sale/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = (
Expand All @@ -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,
Expand Down

0 comments on commit 1d4d05a

Please sign in to comment.