Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor/substrate context #254

Open
wants to merge 23 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
fec860a
refactor: rename SubstrateContextProvider SubstrateProvider
anton-karlovskiy Aug 29, 2022
e4a84e3
chore: remove PropTypes usage in favor of TypeScript
anton-karlovskiy Aug 29, 2022
bf30bd9
chore: clean up
anton-karlovskiy Aug 29, 2022
392b53d
chore: clean up
anton-karlovskiy Aug 29, 2022
796b5cb
refactor: harmonize and improve status messages
anton-karlovskiy Aug 29, 2022
11f4fa7
refactor: configure with KeyringStatus
anton-karlovskiy Aug 29, 2022
c602b6d
chore: clean up
anton-karlovskiy Aug 29, 2022
18b32d0
refactor: configure ApiStatus
anton-karlovskiy Aug 29, 2022
22b5990
chore: clean up
anton-karlovskiy Aug 29, 2022
24cd5f9
refactor: configure with ActionType
anton-karlovskiy Aug 29, 2022
43c1de7
chore: clean up
anton-karlovskiy Aug 29, 2022
883cd26
chore: clean up
anton-karlovskiy Aug 29, 2022
a8526b1
chore: clean up
anton-karlovskiy Aug 29, 2022
41e81eb
chore: clean up
anton-karlovskiy Aug 29, 2022
23c2cde
refactor: updating SubstrateProvider
anton-karlovskiy Aug 29, 2022
6640591
refactor: update the substrate context
anton-karlovskiy Aug 30, 2022
97e2a08
chore: clean up
anton-karlovskiy Aug 30, 2022
5586e0d
refactor: rename SubstrateContext substrate-context
anton-karlovskiy Aug 30, 2022
583c270
fix: correct loadAccounts arguments
anton-karlovskiy Aug 30, 2022
195d790
chore: clean up
anton-karlovskiy Aug 30, 2022
5590a46
feat: update Main
anton-karlovskiy Sep 1, 2022
907bfb6
yarn lock fixed
nuke-web3 Dec 12, 2022
c5e4d90
yarn prettier:write
nuke-web3 Dec 12, 2022
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -93,21 +93,21 @@ keyring and the blockchain itself. Specifically it exposes this API.
state: {
socket,
keyring,
keyringState,
keyringStatus,
api,
apiState,
apiStatus,
currentAccount
}
}
```

- `socket` - The remote provider socket it is connecting to.
- `keyring` - A keyring of accounts available to the user.
- `keyringState` - One of `"READY"` or `"ERROR"` states. `keyring` is valid
only when `keyringState === "READY"`.
- `keyringStatus` - One of `"READY"` or `"ERROR"` states. `keyring` is valid
only when `keyringStatus === "READY"`.
- `api` - The remote api to the connected node.
- `apiState` - One of `"CONNECTING"`, `"READY"`, or `"ERROR"` states. `api` is valid
only when `apiState === "READY"`.
- `apiStatus` - One of `"CONNECTING"`, `"READY"`, or `"ERROR"` states. `api` is valid
only when `apiStatus === "READY"`.
- `currentAccount` - The current selected account pair in the application context.
- `setCurrentAccount` - Function to update the `currentAccount` value in the application context.

Expand Down
49 changes: 38 additions & 11 deletions src/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,12 @@ import {
} from 'semantic-ui-react'
import 'semantic-ui-css/semantic.min.css'

import { SubstrateContextProvider, useSubstrateState } from './substrate-lib'
import { SubstrateProvider, useSubstrateState } from './substrate-lib'
import {
ApiStatus,
KeyringStatus,
ActionType,
} from './substrate-lib/substrate-context'
import { DeveloperConsole } from './substrate-lib/components'

import AccountSelector from './AccountSelector'
Expand All @@ -24,14 +29,14 @@ import Transfer from './Transfer'
import Upgrade from './Upgrade'

function Main() {
const { apiState, apiError, keyringState } = useSubstrateState()
const { apiStatus, apiError, keyringStatus, keyring, api } =
useSubstrateState()

const loader = text => (
<Dimmer active>
<Loader size="small">{text}</Loader>
</Dimmer>
)

const message = errObj => (
<Grid centered columns={2} padded>
<Grid.Column>
Expand All @@ -46,13 +51,35 @@ function Main() {
</Grid>
)

if (apiState === 'ERROR') return message(apiError)
else if (apiState !== 'READY') return loader('Connecting to Substrate')
switch (apiStatus) {
case ApiStatus.Idle:
case ApiStatus.ConnectInit:
case ApiStatus.Connecting:
return loader('Connecting to Substrate')
case ApiStatus.Ready:
break
case ApiStatus.Error:
return message(apiError)
default:
throw new Error('Invalid ApiStatus!')
}

switch (keyringStatus) {
case KeyringStatus.Idle:
case KeyringStatus.Loading:
return loader(
"Loading accounts (please review any extension's authorization)"
)
case KeyringStatus.Ready:
break
case KeyringStatus.Error:
throw new Error(`${ActionType.SetKeyringError}!`)
default:
throw new Error('Invalid KeyringStatus!')
}

if (keyringState !== 'READY') {
return loader(
"Loading accounts (please review any extension's authorization)"
)
if (keyring === null || api === null) {
throw new Error('Something went wrong!')
}

const contextRef = createRef()
Expand Down Expand Up @@ -93,8 +120,8 @@ function Main() {

export default function App() {
return (
<SubstrateContextProvider>
<SubstrateProvider>
<Main />
</SubstrateContextProvider>
</SubstrateProvider>
)
}
171 changes: 0 additions & 171 deletions src/substrate-lib/SubstrateContext.js

This file was deleted.

6 changes: 3 additions & 3 deletions src/substrate-lib/components/DeveloperConsole.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@
import { useSubstrateState } from '../'

export default function DeveloperConsole(props) {
const { api, apiState, keyring, keyringState } = useSubstrateState()
if (apiState === 'READY') {
const { api, apiStatus, keyring, keyringStatus } = useSubstrateState()
if (apiStatus === 'READY') {
window.api = api
}
if (keyringState === 'READY') {
if (keyringStatus === 'READY') {
window.keyring = keyring
}
window.util = require('@polkadot/util')
Expand Down
6 changes: 3 additions & 3 deletions src/substrate-lib/index.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import {
SubstrateContextProvider,
SubstrateProvider,
useSubstrate,
useSubstrateState,
} from './SubstrateContext'
} from './substrate-context'
import utils from './utils'

export { SubstrateContextProvider, useSubstrate, useSubstrateState, utils }
export { SubstrateProvider, useSubstrate, useSubstrateState, utils }
Loading