Skip to content

Commit

Permalink
Fix sale related data (#213)
Browse files Browse the repository at this point in the history
* fix sale end

* cleanup
  • Loading branch information
Szegoo authored Aug 11, 2024
1 parent 707e95e commit 7174de3
Show file tree
Hide file tree
Showing 12 changed files with 33 additions and 36 deletions.
11 changes: 5 additions & 6 deletions src/components/Panels/SaleInfoPanel/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export const SaleInfoPanel = () => {
} = useCoretimeApi();

const {
phase: { currentPhase, currentPrice, saleStartTimestamp, saleEndTimestamp },
phase: { currentPhase, currentPrice, endpoints: saleEndpoints },
saleInfo,
} = useSaleInfo();

Expand Down Expand Up @@ -57,13 +57,12 @@ export const SaleInfoPanel = () => {
title='Sale details'
items={{
left: {
label:
saleStartTimestamp < Date.now() ? 'Started at' : 'Starts at:',
value: getTimeStringShort(saleStartTimestamp),
label: 'Leadin start',
value: getTimeStringShort(saleEndpoints.leadin.start),
},
right: {
label: saleEndTimestamp > Date.now() ? 'Ends at' : 'Ended at:',
value: getTimeStringShort(saleEndTimestamp),
label: 'Sale end',
value: getTimeStringShort(saleEndpoints.fixed.end),
},
}}
/>
Expand Down
4 changes: 2 additions & 2 deletions src/components/Regions/Modals/Price/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export const PriceModal = ({ open, onClose }: PriceModalProps) => {
const theme = useTheme();

const {
phase: { currentPhase, saleStartTimestamp },
phase: { currentPhase, endpoints: saleEndpoints },
} = useSaleInfo();

return (
Expand All @@ -49,7 +49,7 @@ export const PriceModal = ({ open, onClose }: PriceModalProps) => {
>
<Typography>Sale starts in:</Typography>
<CountDown
remainingTime={(saleStartTimestamp - Date.now()) / 1000}
remainingTime={(saleEndpoints.fixed.end - Date.now()) / 1000}
/>
</Stack>
</Box>
Expand Down
4 changes: 2 additions & 2 deletions src/contexts/account/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -122,8 +122,8 @@ const AccountProvider = ({ children }: Props) => {
if (accounts.length) {
const activeAccount = localStorage.getItem(LOCAL_STORAGE_ACTIVE_ACCOUNT);
const account: InjectedAccountWithMeta = activeAccount
? accounts.find((acc: any) => acc.address == activeAccount) ??
accounts[0]
? (accounts.find((acc: any) => acc.address == activeAccount) ??
accounts[0])
: accounts[0];

setActiveAccount(account);
Expand Down
9 changes: 4 additions & 5 deletions src/contexts/regions/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -118,10 +118,9 @@ const RegionDataProvider = ({ children }: Props) => {
const tasks = await fetchWorkplan();

const ctRegions = await collectCoretimeRegions(tasks);
const rxRegions =
enableRegionX(network)
? await collectRegionXRegions(tasks)
: [];
const rxRegions = enableRegionX(network)
? await collectRegionXRegions(tasks)
: [];

setRegions(ctRegions.concat(rxRegions));
setStatus(ContextStatus.LOADED);
Expand Down Expand Up @@ -177,7 +176,7 @@ const RegionDataProvider = ({ children }: Props) => {
// Only user owned non-expired regions.
if (
encodeAddress(region.getOwner(), 42) !==
encodeAddress(activeAccount.address, 42) ||
encodeAddress(activeAccount.address, 42) ||
region.consumed({ timeslicePeriod, relayBlockNumber }) > 1
)
return null;
Expand Down
19 changes: 8 additions & 11 deletions src/contexts/sales/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { getCorePriceAt, getCurrentPhase } from '@/utils/sale';
import {
ContextStatus,
PhaseEndpoints,
RELAY_CHAIN_BLOCK_TIME,
SaleConfig,
SaleInfo,
SalePhase,
Expand Down Expand Up @@ -57,8 +58,6 @@ const defaultEndpoints = {
const defaultSalePhase = {
currentPhase: SalePhase.Interlude,
currentPrice: undefined,
saleStartTimestamp: 0,
saleEndTimestamp: 0,
endpoints: defaultEndpoints,
};

Expand Down Expand Up @@ -98,8 +97,6 @@ const SaleInfoProvider = ({ children }: Props) => {
);
const [at, setAt] = useState(0);
const [currentPrice, setCurrentPrice] = useState<number | undefined>();
const [saleStartTimestamp, setSaleStartTimestamp] = useState(0);
const [saleEndTimestamp, setSaleEndTimestamp] = useState(0);
const [endpoints, setEndpoints] = useState<PhaseEndpoints>(defaultEndpoints);

useEffect(() => {
Expand Down Expand Up @@ -139,19 +136,21 @@ const SaleInfoProvider = ({ children }: Props) => {
setConfig(config);

const saleStart = saleInfo.saleStart;
const saleEnd = saleInfo.regionBegin * timeslicePeriod;
// Sale start != bulk phase start. sale_start = bulk_phase_start + interlude_length.
const saleStartTimestamp = await getBlockTimestamp(
coretimeApi,
saleStart,
network
);
const saleEndTimestamp = await getBlockTimestamp(relayApi, saleEnd);

setSaleStartTimestamp(saleStartTimestamp);
setSaleEndTimestamp(saleEndTimestamp);

const regionDuration = saleInfo.regionEnd - saleInfo.regionBegin;
const blockTime = getBlockTime(network); // Block time on the coretime chain

const saleEndTimestamp =
saleStartTimestamp -
config.interludeLength * blockTime +
regionDuration * timeslicePeriod * RELAY_CHAIN_BLOCK_TIME;

const _endpoints = {
interlude: {
start: saleStartTimestamp - config.interludeLength * blockTime,
Expand Down Expand Up @@ -198,8 +197,6 @@ const SaleInfoProvider = ({ children }: Props) => {
phase: {
currentPhase,
currentPrice,
saleStartTimestamp,
saleEndTimestamp,
endpoints,
},
fetchSaleInfo,
Expand Down
2 changes: 1 addition & 1 deletion src/hooks/accountExtrinsics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ export const useAccountExtrinsics = (
call: item.call,
timestamp: new Date(`${item.timestamp}Z`),
success: item.success,
} as AccountTxHistoryItem)
}) as AccountTxHistoryItem
)
);
}
Expand Down
2 changes: 1 addition & 1 deletion src/hooks/sale/purchaseHistory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ export const usePurchaseHistory = (
timestamp: new Date(`${timestamp}Z`),
price: parseInt(price),
type: purchaseType,
} as PurchaseHistoryItem)
}) as PurchaseHistoryItem
)
);
}
Expand Down
2 changes: 1 addition & 1 deletion src/hooks/sale/saleDetails.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ export const useSaleDetails = (network: NetworkType, saleCycle: number) => {
timestamp: new Date(`${timestamp}Z`),
price: parseInt(price),
type: purchaseType,
} as PurchaseHistoryItem)
}) as PurchaseHistoryItem
)
);
}
Expand Down
2 changes: 1 addition & 1 deletion src/hooks/sale/salesHistory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ export const useSalesHistory = (network: NetworkType) => {
endTimestamp: Date.parse(`${tsSaleEnd}Z`),
startPrice,
endPrice,
} as SalesHistoryItem)
}) as SalesHistoryItem
)
);
}
Expand Down
2 changes: 0 additions & 2 deletions src/models/regions/sale.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,6 @@ export type PhaseEndpoints = {
export type SalePhaseInfo = {
currentPhase: SalePhase;
currentPrice?: number;
saleStartTimestamp: number;
saleEndTimestamp: number;
endpoints: PhaseEndpoints;
};

Expand Down
6 changes: 3 additions & 3 deletions src/pages/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -49,15 +49,15 @@ const Home = () => {
usePurchaseHistory(network, regionBegin);

const renewals = purchaseHistoryData.filter(
(item) => item.type === PurchaseType.RENEWAL,
(item) => item.type === PurchaseType.RENEWAL
);
const numRenewals = renewals.length;
const renewalCost =
numRenewals === 0
? 0
: Math.floor(
renewals.reduce((sum, item) => sum + item.price, 0) / numRenewals
);
renewals.reduce((sum, item) => sum + item.price, 0) / numRenewals
);

const {
currentBurn,
Expand Down
6 changes: 5 additions & 1 deletion src/pages/transfer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,11 @@ const TransferPage = () => {
originChain === ChainType.REGIONX &&
destinationChain === ChainType.CORETIME
) {
if (!enableRegionX(network) || !regionXApi || regionxApiState !== ApiState.READY) {
if (
!enableRegionX(network) ||
!regionXApi ||
regionxApiState !== ApiState.READY
) {
toastWarning('Currently not supported');
return;
}
Expand Down

0 comments on commit 7174de3

Please sign in to comment.