Skip to content

Commit

Permalink
feat: useStarknet hook returns errors
Browse files Browse the repository at this point in the history
  • Loading branch information
fracek committed Feb 4, 2022
1 parent 89aee8e commit bb0084a
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 5 deletions.
5 changes: 5 additions & 0 deletions .changeset/neat-actors-play.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@starknet-react/core': minor
---

useStarknet hook now returns an error on activation
17 changes: 13 additions & 4 deletions packages/core/src/providers/starknet/manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { StarknetState } from './model'
interface StarknetManagerState {
account?: string
library: ProviderInterface
error?: string
}

interface SetAccount {
Expand All @@ -19,7 +20,12 @@ interface SetProvider {
provider: ProviderInterface
}

type Action = SetAccount | SetProvider
interface SetError {
type: 'set_error'
error: string
}

type Action = SetAccount | SetProvider | SetError

function reducer(state: StarknetManagerState, action: Action): StarknetManagerState {
switch (action.type) {
Expand All @@ -29,6 +35,9 @@ function reducer(state: StarknetManagerState, action: Action): StarknetManagerSt
case 'set_provider': {
return { ...state, library: action.provider }
}
case 'set_error': {
return { ...state, error: action.error }
}
default: {
return state
}
Expand All @@ -41,7 +50,7 @@ export function useStarknetManager(): StarknetState {
library: defaultProvider,
})

const { account, library } = state
const { account, library, error } = state

useEffect(() => {
if (typeof window !== undefined) {
Expand All @@ -63,9 +72,9 @@ export function useStarknetManager(): StarknetState {
}
} catch (err) {
console.error(err)
// TODO: display error message to user
dispatch({ type: 'set_error', error: 'could not activate StarkNet' })
}
}, [])

return { account, hasStarknet, connectBrowserWallet, library }
return { account, hasStarknet, connectBrowserWallet, library, error }
}
1 change: 1 addition & 0 deletions packages/core/src/providers/starknet/model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ export interface StarknetState {
hasStarknet: boolean
connectBrowserWallet: () => void
library: ProviderInterface
error?: string
}

export const STARKNET_INITIAL_STATE: StarknetState = {
Expand Down
3 changes: 2 additions & 1 deletion packages/core/test/providers/block.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ describe('useStarknetBlock', () => {
)
const { result, waitForNextUpdate } = renderHook(() => useStarknetBlock(), { wrapper })
expect(result.current).toBeUndefined()
await waitForNextUpdate()
// wait up to one minute for a block
await waitForNextUpdate({ timeout: 60000 })
expect(result.current.timestamp).toBeGreaterThan(0)
expect(result.current.block_hash).not.toBeUndefined()
})
Expand Down
13 changes: 13 additions & 0 deletions packages/core/test/providers/starknet.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import React from 'react'
import { renderHook } from '@testing-library/react-hooks'
import { useStarknet, StarknetProvider } from '../../src'

describe('useStarknetBlock', () => {
it('returns the current block', async () => {
const wrapper = ({ children }) => <StarknetProvider>{children}</StarknetProvider>
const { result } = renderHook(() => useStarknet(), { wrapper })
const { account, hasStarknet } = result.current
expect(account).toBeUndefined()
expect(hasStarknet).toBeFalsy()
})
})

0 comments on commit bb0084a

Please sign in to comment.