Skip to content

Commit

Permalink
sale status
Browse files Browse the repository at this point in the history
  • Loading branch information
Szegoo committed Aug 19, 2024
1 parent c5c36ad commit bec562d
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 20 deletions.
2 changes: 1 addition & 1 deletion src/contexts/orders/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
useState,
} from 'react';

import { fetchOrders as fetchOrdersApi,fetchUserContribution } from '@/apis';
import { fetchOrders as fetchOrdersApi, fetchUserContribution } from '@/apis';
import { ApiResponse, ContextStatus, Order } from '@/models';

import { useAccounts } from '../account';
Expand Down
43 changes: 24 additions & 19 deletions src/contexts/sales/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ const defaultSaleStatus: BrokerStatus = {
lastCommittedTimeslice: 0,
lastTimeslice: 0,
privatePoolSize: 0,
systemPoolSize: 0
systemPoolSize: 0,
};

const defaultSaleData: SaleData = {
Expand Down Expand Up @@ -100,7 +100,9 @@ const SaleInfoProvider = ({ children }: Props) => {
} = useRelayApi();

const [saleInfo, setSaleInfo] = useState<SaleInfo>(defaultSaleData.saleInfo);
const [saleStatus, setSaleStatus] = useState<BrokerStatus>(defaultSaleData.saleStatus);
const [saleStatus, setSaleStatus] = useState<BrokerStatus>(
defaultSaleData.saleStatus
);
const [config, setConfig] = useState<SaleConfig>(defaultSaleData.config);

const [status, setStatus] = useState(ContextStatus.UNINITIALIZED);
Expand All @@ -126,27 +128,37 @@ const SaleInfoProvider = ({ children }: Props) => {
const fetchSaleInfo = async () => {
try {
setStatus(ContextStatus.LOADING);
if (
!coretimeApi ||
coretimeApiState !== ApiState.READY ||
!relayApi ||
relayApiState !== ApiState.READY ||
!coretimeApi.query.broker
) {
if (!coretimeApi || coretimeApiState !== ApiState.READY) {
setStatus(ContextStatus.UNINITIALIZED);
return;
}

const saleInfoRaw = await coretimeApi.query.broker.saleInfo();
const [brokerStatusRaw, saleInfoRaw, configRaw] = (await new Promise(
(resolve, _reject) => {
coretimeApi.queryMulti(
[
coretimeApi.query.broker.status,
coretimeApi.query.broker.saleInfo,
coretimeApi.query.broker.configuration,
],
(result) => {
resolve(result);
}
);
}
)) as Array<any>;

const saleInfo = saleInfoRaw.toJSON() as SaleInfo;
// On Rococo we have `endPrice` while on Kusama we still have `price`.
saleInfo.price = saleInfo.price || (saleInfo as any).endPrice;
setSaleInfo(saleInfo);

const configRaw = await coretimeApi.query.broker.configuration();
const config = configRaw.toJSON() as SaleConfig;
setConfig(config);

const brokerStatus = brokerStatusRaw.toJSON() as BrokerStatus;
setSaleStatus(brokerStatus);

const saleStart = saleInfo.saleStart;
// Sale start != bulk phase start. sale_start = bulk_phase_start + interlude_length.
const saleStartTimestamp = await getBlockTimestamp(
Expand Down Expand Up @@ -186,14 +198,7 @@ const SaleInfoProvider = ({ children }: Props) => {

useEffect(() => {
fetchSaleInfo();
}, [
network,
coretimeApi,
coretimeApiState,
relayApi,
relayApiState,
timeslicePeriod,
]);
}, [network, coretimeApi, coretimeApiState, timeslicePeriod]);

useEffect(() => {
if (height === 0) return;
Expand Down

0 comments on commit bec562d

Please sign in to comment.