This repository has been archived by the owner on Feb 9, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add timestamp hooks and core funcs (#91)
- Loading branch information
1 parent
ee11fe8
commit c50c65d
Showing
20 changed files
with
122 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
12 changes: 12 additions & 0 deletions
12
packages/useink/src/core/substrate/timestamp/getTimestampDate.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
19
packages/useink/src/core/substrate/timestamp/getTimestampNow.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
}; |
5 changes: 5 additions & 0 deletions
5
packages/useink/src/core/substrate/timestamp/getTimestampQuery.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from './useUnixMilliToDate'; |
7 changes: 7 additions & 0 deletions
7
packages/useink/src/react/hooks/helpers/useUnixMilliToDate.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from './useBalance'; |
12 changes: 6 additions & 6 deletions
12
...k/src/react/hooks/substrate/useBalance.ts → ...act/hooks/substrate/balance/useBalance.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export * from './useTimestampDate'; | ||
export * from './useTimestampNow'; | ||
export * from './useTimestampQuery'; |
9 changes: 9 additions & 0 deletions
9
packages/useink/src/react/hooks/substrate/timestamp/useTimestampDate.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}; |
18 changes: 18 additions & 0 deletions
18
packages/useink/src/react/hooks/substrate/timestamp/useTimestampNow.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
}; |
19 changes: 19 additions & 0 deletions
19
packages/useink/src/react/hooks/substrate/timestamp/useTimestampQuery.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -195,6 +195,7 @@ matklad | |
merkle | ||
metaverse | ||
micnncim | ||
milli | ||
minix | ||
miterlimit | ||
mogiway | ||
|