Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
37 changed files
with
44,755 additions
and
3,647 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,25 @@ | ||
{ | ||
inputs = { | ||
nixpkgs.url = "github:NixOS/nixpkgs/nixpkgs-unstable"; | ||
flake-utils.url = "github:numtide/flake-utils"; | ||
}; | ||
|
||
outputs = { self, nixpkgs, flake-utils, ... }: | ||
flake-utils.lib.eachDefaultSystem(system: | ||
let | ||
pkgs = import nixpkgs { | ||
inherit system; | ||
}; | ||
in | ||
{ | ||
devShells.default = pkgs.mkShell { | ||
nativeBuildInputs = [ | ||
pkgs.nodejs-16_x | ||
pkgs.nodePackages.pnpm | ||
pkgs.nodePackages.typescript | ||
pkgs.nodePackages.typescript-language-server | ||
]; | ||
}; | ||
} | ||
); | ||
} |
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,13 @@ | ||
import type { Config } from 'jest' | ||
|
||
const config: Config = { | ||
verbose: true, | ||
preset: 'ts-jest', | ||
testEnvironment: 'jsdom', | ||
testMatch: ['<rootDir>/src/**/*.test.tsx'], | ||
moduleNameMapper: { | ||
'~/(.*)': '<rootDir>/src/$1', | ||
}, | ||
} | ||
|
||
export default config |
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,27 @@ | ||
import { useBlock } from './block' | ||
import { renderHook, waitFor } from '../../test/react' | ||
import { connectors, devnetProvider, compiledErc20 } from '../../test/devnet' | ||
|
||
describe('useBlock', () => { | ||
beforeAll(async () => { | ||
await devnetProvider.deployContract({ | ||
contract: compiledErc20, | ||
}) | ||
}) | ||
|
||
it('returns the latest block by default', async () => { | ||
const { result } = renderHook(() => useBlock({ watch: false }), { connectors }) | ||
|
||
await waitFor(() => { | ||
expect(result.current.data).toBeDefined() | ||
}) | ||
}) | ||
|
||
it.skip('returns an error', async () => { | ||
const { result } = renderHook(() => useBlock(), { connectors }) | ||
|
||
await waitFor(() => { | ||
expect(result.current.isError).toBeTruthy() | ||
}) | ||
}) | ||
}) |
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,56 @@ | ||
import { useQuery } from '@tanstack/react-query' | ||
import { GetBlockResponse, ProviderInterface } from 'starknet' | ||
import { BlockIdentifier } from 'starknet/provider/utils' | ||
import { useStarknet } from '~/providers' | ||
|
||
export interface StarkNetBlockResult { | ||
data?: GetBlockResponse | ||
loading?: boolean | ||
error?: string | ||
} | ||
|
||
/** | ||
* @deprecated Use `useBlock`. | ||
*/ | ||
export function useStarknetBlock(): StarkNetBlockResult { | ||
return useBlock({}) | ||
} | ||
|
||
function fetchBlock({ | ||
library, | ||
blockIdentifier, | ||
}: { | ||
library: ProviderInterface | ||
blockIdentifier: BlockIdentifier | ||
}): () => Promise<GetBlockResponse | undefined> { | ||
return async () => { | ||
return await library.getBlock(blockIdentifier) | ||
} | ||
} | ||
|
||
/** | ||
* Hook for fetching a block. | ||
*/ | ||
export function useBlock({ | ||
watch, | ||
refetchInterval: refreshInterval, | ||
blockIdentifier = 'latest', | ||
}: { | ||
watch?: boolean | ||
refetchInterval?: number | ||
blockIdentifier?: BlockIdentifier | ||
} = {}) { | ||
const { library } = useStarknet() | ||
|
||
const refetchInterval = watch ? refreshInterval ?? 5000 : false | ||
|
||
const { data, isLoading, isError } = useQuery<GetBlockResponse | undefined, string>( | ||
['block', blockIdentifier], | ||
fetchBlock({ library, blockIdentifier }), | ||
{ | ||
refetchInterval, | ||
useErrorBoundary: true, | ||
} | ||
) | ||
return { data, isLoading, isError } | ||
} |
Oops, something went wrong.