Skip to content
This repository has been archived by the owner on Feb 9, 2024. It is now read-only.

Commit

Permalink
feat: add timestamp hooks and core funcs (#91)
Browse files Browse the repository at this point in the history
  • Loading branch information
DoubleOTheven authored Jun 14, 2023
1 parent ee11fe8 commit c50c65d
Show file tree
Hide file tree
Showing 20 changed files with 122 additions and 22 deletions.
2 changes: 1 addition & 1 deletion packages/useink/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
"React",
"hooks"
],
"version": "1.7.5",
"version": "1.8.0",
"main": "dist/index.js",
"module": "dist/index.mjs",
"description": "A React hooks library for ink! contracts",
Expand Down
5 changes: 0 additions & 5 deletions packages/useink/src/core/substrate/timestamp/getTimestamp.ts

This file was deleted.

12 changes: 12 additions & 0 deletions packages/useink/src/core/substrate/timestamp/getTimestampDate.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { unixMilliToDate } from '../../../utils';
import { ApiBase } from '../../types/index';
import { getTimestampUnix } from './getTimestampNow.ts';

export const getTimestampDate = async (
api: ApiBase<'promise'> | undefined,
): Promise<Date | undefined> => {
const now = await getTimestampUnix(api);
if (!now) return;

return unixMilliToDate(now);
};
19 changes: 11 additions & 8 deletions packages/useink/src/core/substrate/timestamp/getTimestampNow.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
import { ApiPromise, Codec } from '../../types/index';
import { getTimestamp } from './getTimestamp.ts';
import { ApiBase } from '../../types/index';
import { getTimestampQuery } from './getTimestampQuery.ts';

export const getTimestampNow = async (
api: ApiPromise | undefined,
): Promise<Codec | undefined> => {
const timestamp = getTimestamp(api);
if (!timestamp?.now) return;
export const getTimestampUnix = async (
api: ApiBase<'promise'> | undefined,
): Promise<number | undefined> => {
const query = getTimestampQuery(api);
if (!query?.now) return;

return await timestamp.now();
const t = await query.now();
const stringWithoutCommas = t.toHuman()?.toString().split(',').join('');

return stringWithoutCommas ? parseInt(stringWithoutCommas) : undefined;
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { ApiBase, QueryableModuleCalls } from '../../types/index';

export const getTimestampQuery = (
api: ApiBase<'promise'> | undefined,
): QueryableModuleCalls<'promise'> | undefined => api?.query?.timestamp;
3 changes: 2 additions & 1 deletion packages/useink/src/core/substrate/timestamp/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export * from './getTimestamp.ts';
export * from './getTimestampDate.ts';
export * from './getTimestampNow.ts';
export * from './getTimestampQuery.ts';
1 change: 1 addition & 0 deletions packages/useink/src/react/hooks/helpers/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './useUnixMilliToDate';
7 changes: 7 additions & 0 deletions packages/useink/src/react/hooks/helpers/useUnixMilliToDate.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { unixMilliToDate } from '../../../utils/helpers/unixMilliToDate';
import { useMemo } from 'react';

export const useUnixMilliToDate = (
unixInMilliSeconds: number | undefined,
): Date | undefined =>
useMemo(() => unixMilliToDate(unixInMilliSeconds), [unixInMilliSeconds]);
1 change: 1 addition & 0 deletions packages/useink/src/react/hooks/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export * from './config/index';
export * from './contracts/index';
export * from './helpers/index';
export * from './substrate/index';
export * from './wallets/index';
1 change: 1 addition & 0 deletions packages/useink/src/react/hooks/substrate/balance/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './useBalance';
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { ChainId } from '../../../chains/index';
import { DeriveBalancesAccount, WithAddress } from '../../../core/index';
import { getBalance } from '../../../core/index';
import { useChain } from '../index';
import { useApi } from '../substrate/useApi.ts';
import { useBlockHeader } from './useBlockHeader.ts';
import { ChainId } from '../../../../chains/index';
import { DeriveBalancesAccount, WithAddress } from '../../../../core/index';
import { getBalance } from '../../../../core/index';
import { useChain } from '../../index';
import { useApi } from '../useApi.ts';
import { useBlockHeader } from '../useBlockHeader.ts';
import { useEffect, useState } from 'react';

export const useBalance = (
Expand Down
3 changes: 2 additions & 1 deletion packages/useink/src/react/hooks/substrate/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
export * from './balance';
export * from './timestamp';
export * from './useApi.ts';
export * from './useBalance.ts';
export * from './useBlockHeader.ts';
export * from './useChainDecimals.ts';
export * from './useTokenSymbol.ts';
Expand Down
3 changes: 3 additions & 0 deletions packages/useink/src/react/hooks/substrate/timestamp/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export * from './useTimestampDate';
export * from './useTimestampNow';
export * from './useTimestampQuery';
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { ChainId } from '../../../../chains';
import { useUnixMilliToDate } from '../../helpers';
import { useTimestampNow } from './useTimestampNow';

export const useTimestampDate = (chainId?: ChainId): Date | undefined => {
const unix = useTimestampNow(chainId);

return useUnixMilliToDate(unix);
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { ChainId } from '../../../../chains';
import { getTimestampUnix } from '../../../../core';
import { useApi } from '../useApi';
import { useBlockHeader } from '../useBlockHeader';
import { useEffect, useState } from 'react';

// Get the current timestamp in milliseconds
export const useTimestampNow = (chainId?: ChainId): number | undefined => {
const [now, setNow] = useState<number>();
const b = useBlockHeader(chainId);
const chainApi = useApi(chainId);

useEffect(() => {
getTimestampUnix(chainApi?.api).then(setNow).catch();
}, [b?.blockNumber]);

return now;
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { ChainId } from '../../../../chains';
import { QueryableModuleCalls, getTimestampQuery } from '../../../../core';
import { useApi } from '../useApi';
import { useEffect, useState } from 'react';

// Get a queryable function that can then be used to call a chain: `await timestampQuery.now()`
export const useTimestampQuery = (
chainId?: ChainId,
): QueryableModuleCalls<'promise'> | undefined => {
const chainApi = useApi(chainId);
const [timestamp, setTimestamp] = useState<QueryableModuleCalls<'promise'>>();

useEffect(() => {
const t = getTimestampQuery(chainApi?.api);
setTimestamp(t);
}, [chainApi?.api]);

return timestamp;
};
1 change: 1 addition & 0 deletions packages/useink/src/utils/helpers/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export * from './getExpiredItem.ts';
export * from './parseUnits';
export * from './pseudoRandomId.ts';
export * from './unixMilliToDate.ts';
4 changes: 4 additions & 0 deletions packages/useink/src/utils/helpers/unixMilliToDate.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export const unixMilliToDate = (
unixInMilliSeconds: number | undefined,
): Date | undefined =>
unixInMilliSeconds ? new Date(unixInMilliSeconds) : undefined;
18 changes: 18 additions & 0 deletions playground/src/components/pg-home/HomePage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ import {
useEventSubscription,
useEvents,
useInstalledWallets,
useTimestampDate,
useTimestampNow,
useTokenSymbol,
useTx,
useTxPaymentInfo,
Expand Down Expand Up @@ -77,6 +79,8 @@ export const HomePage: React.FC = () => {
const panic = useCall<boolean>(cRococoContract, 'panic');
const assertBoom = useCall<boolean>(cRococoContract, 'assertBoom');
const mood = useCall<MoodResult>(cRococoContract, 'mood');
const phalaTimestamp = useTimestampNow('phala');
const phalaDate = useTimestampDate('phala');
const shibuyaContract = useContract(
SHIBUYA_CONTRACT_ADDRESS,
metadata,
Expand Down Expand Up @@ -271,6 +275,7 @@ export const HomePage: React.FC = () => {
{planckToDecimalFormatted(
balance?.freeBalance,
cRococoContract.contract.api,
{ decimals: 4 },
)}
</span>
</li>
Expand Down Expand Up @@ -336,6 +341,19 @@ export const HomePage: React.FC = () => {
</ul>
</li>

<li>
<b>
Phala&apos;s current timestamp:{' '}
<code className='p-1 rounded-md bg-slate-500'>
{phalaTimestamp}
</code>
</b>

<p className='text-sm'>
Phala&apos;s last block time: {phalaDate?.toLocaleTimeString()}
</p>
</li>

<li className='flex items-center gap-4'>
<button
type='button'
Expand Down
1 change: 1 addition & 0 deletions words.txt
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,7 @@ matklad
merkle
metaverse
micnncim
milli
minix
miterlimit
mogiway
Expand Down

0 comments on commit c50c65d

Please sign in to comment.