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: Ledger support improvements + bug fixes #1594

Merged
merged 45 commits into from
Nov 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
45 commits
Select commit Hold shift + click to select a range
15b1138
refactor error handling
Nov 3, 2023
5e12cd1
reorganise some context syntax
Nov 3, 2023
7cb12a8
more ledger context tidy up
Nov 3, 2023
e49dd1f
accept one task, minimise executeLedgerLoop
Nov 3, 2023
47eeaee
expand chainState
Nov 3, 2023
3799ce3
import
Nov 3, 2023
84861f5
add runtimesInconsistent ref, checkRuntimeVersion
Nov 3, 2023
76072fc
rm getRuntimeVersion from sign_tx
Nov 3, 2023
f89e895
add integrityChecked
Nov 4, 2023
7dc1769
don't reset txFees on mount
Nov 4, 2023
12dfcd9
setIntegrityChecked on tx reset
Nov 4, 2023
1d9d06a
refactor Ledger submit, inner Tx element
Nov 4, 2023
0705c91
disabled as prop, LedgerSubmitProps
Nov 4, 2023
36aaef2
work on ledger feedback
Nov 4, 2023
55948e6
simplify handleGetAddress
Nov 4, 2023
7df0eec
refactor remaining calls
Nov 4, 2023
0fe5156
rm ensureTransportClosed
Nov 4, 2023
df27125
rm deprecated state
Nov 4, 2023
4448ea0
use handleResetLedgerTx
Nov 4, 2023
7de3366
simplify handleUnmount
Nov 4, 2023
a082a11
reformat status codes
Nov 4, 2023
1e95634
fixes
Nov 4, 2023
912381d
separate ledger accounts from hardware management
Nov 5, 2023
c7c3229
mv useEffect over to ledger accounts
Nov 5, 2023
9f86b43
some tidy up
Nov 5, 2023
e9b2c76
rm duplicate isExecuting
Nov 5, 2023
865670b
only use latest status code
Nov 5, 2023
70e34d4
improvements to syntax
Nov 5, 2023
1aa44a8
Merge branch 'main' into rb-ledger-version
Nov 5, 2023
8dcacd4
remove unneeded isMounted code
Nov 5, 2023
14d683b
Merge branch 'rb-ledger-version' of https://github.com/paritytech/pol…
Nov 5, 2023
8ef82fd
fix duplicate number
Nov 6, 2023
9099b6e
add ledger import notifications
Nov 6, 2023
d00757d
add translation keys
Nov 6, 2023
920c629
mv error code to correct key
Nov 6, 2023
ad6f7ec
tweak
Nov 6, 2023
e6e6c83
tweak
Nov 6, 2023
128cf1c
update
Nov 6, 2023
e1d0893
translations
TingALin Nov 6, 2023
aae2476
use transferrable balance
Nov 6, 2023
b542b0b
Merge branch 'rb-ledger-version' of https://github.com/paritytech/pol…
Nov 6, 2023
d864198
fix
Nov 6, 2023
4b6753d
factor ed into free
Nov 6, 2023
6ebb03f
fix
Nov 6, 2023
ecd05a5
tweak
Nov 6, 2023
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
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@
"@fortawesome/free-solid-svg-icons": "^6.4.2",
"@fortawesome/react-fontawesome": "^0.2.0",
"@ledgerhq/hw-transport-webhid": "^6.27.19",
"@polkadot-cloud/assets": "0.1.34",
"@polkadot-cloud/core": "^1.0.34",
"@polkadot-cloud/react": "^0.1.108",
"@polkadot-cloud/assets": "^0.1.34",
"@polkadot-cloud/core": "^1.0.42",
"@polkadot-cloud/react": "^0.1.118",
"@polkadot-cloud/utils": "^0.0.25",
"@polkadot/api": "^10.10.1",
"@polkadot/keyring": "^12.1.1",
Expand Down
4 changes: 3 additions & 1 deletion src/Providers.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import {
import { ExtrinsicsProvider } from 'contexts/Extrinsics';
import { FastUnstakeProvider } from 'contexts/FastUnstake';
import { FiltersProvider } from 'contexts/Filters';
import { LedgerHardwareProvider } from 'contexts/Hardware/Ledger';
import { LedgerHardwareProvider } from 'contexts/Hardware/Ledger/LedgerHardware';
import { VaultHardwareProvider } from 'contexts/Hardware/Vault';
import { HelpProvider } from 'contexts/Help';
import { IdentitiesProvider } from 'contexts/Identities';
Expand Down Expand Up @@ -49,6 +49,7 @@ import { useActiveAccounts } from 'contexts/ActiveAccounts';
import { DappName } from 'consts';
import { ImportedAccountsProvider } from 'contexts/Connect/ImportedAccounts';
import { PoolPerformanceProvider } from 'contexts/Pools/PoolPerformance';
import { LedgerAccountsProvider } from 'contexts/Hardware/Ledger/LedgerAccounts';

// Embed providers from hook.
export const Providers = () => {
Expand All @@ -72,6 +73,7 @@ export const Providers = () => {
{ dappName: DappName, network, ss58, activeAccount, setActiveAccount },
],
OtherAccountsProvider,
LedgerAccountsProvider,
ImportedAccountsProvider,
HelpProvider,
NetworkMetricsProvider,
Expand Down
2 changes: 1 addition & 1 deletion src/config/ledger.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Copyright 2023 @paritytech/polkadot-staking-dashboard authors & contributors
// SPDX-License-Identifier: GPL-3.0-only

import type { LedgerApp } from 'contexts/Hardware/types';
import type { LedgerApp } from 'contexts/Hardware/Ledger/types';
import KusamaSVG from 'img/appIcons/kusama.svg?react';
import PolkadotSVG from 'img/appIcons/polkadot.svg?react';

Expand Down
20 changes: 16 additions & 4 deletions src/contexts/Api/defaults.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,13 @@

import { stringToU8a } from '@polkadot/util';
import BigNumber from 'bignumber.js';
import type { APIConstants, APIContextInterface } from 'contexts/Api/types';
import type {
APIChainState,
APIConstants,
APIContextInterface,
} from 'contexts/Api/types';

export const consts: APIConstants = {
export const defaultConsts: APIConstants = {
bondDuration: new BigNumber(0),
maxNominations: new BigNumber(0),
sessionsPerEra: new BigNumber(0),
Expand All @@ -20,10 +24,18 @@ export const consts: APIConstants = {
poolsPalletId: stringToU8a('0'),
};

export const defaultChainState: APIChainState = {
chain: null,
version: {
specVersion: 0,
},
ss58Prefix: 0,
};

export const defaultApiContext: APIContextInterface = {
api: null,
consts,
chainState: undefined,
consts: defaultConsts,
chainState: defaultChainState,
isReady: false,
apiStatus: 'disconnected',
isLightClient: false,
Expand Down
30 changes: 14 additions & 16 deletions src/contexts/Api/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,11 @@ import type {
} from 'contexts/Api/types';
import type { AnyApi } from 'types';
import { useEffectIgnoreInitial } from '@polkadot-cloud/react/hooks';
import * as defaults from './defaults';
import {
defaultApiContext,
defaultChainState,
defaultConsts,
} from './defaults';

export const APIProvider = ({ children, network }: APIProviderProps) => {
// Store povider instance.
Expand All @@ -34,7 +38,8 @@ export const APIProvider = ({ children, network }: APIProviderProps) => {
);

// Store chain state.
const [chainState, setchainState] = useState<APIChainState>(undefined);
const [chainState, setchainState] =
useState<APIChainState>(defaultChainState);

// Store the active RPC provider.
const initialRpcEndpoint = () => {
Expand All @@ -61,7 +66,7 @@ export const APIProvider = ({ children, network }: APIProviderProps) => {
const [api, setApi] = useState<ApiPromise | null>(null);

// Store network constants.
const [consts, setConsts] = useState<APIConstants>(defaults.consts);
const [consts, setConsts] = useState<APIConstants>(defaultConsts);

// Store API connection status.
const [apiStatus, setApiStatus] = useState<ApiStatus>('disconnected');
Expand All @@ -88,8 +93,8 @@ export const APIProvider = ({ children, network }: APIProviderProps) => {

const handleApiSwitch = () => {
setApi(null);
setConsts(defaults.consts);
setchainState(undefined);
setConsts(defaultConsts);
setchainState(defaultChainState);
};

// Handle connect to API.
Expand Down Expand Up @@ -134,22 +139,17 @@ export const APIProvider = ({ children, network }: APIProviderProps) => {

const newChainState = await Promise.all([
newApi.rpc.system.chain(),
newApi.rpc.system.version(),
newApi.consts.system.version,
newApi.consts.system.ss58Prefix,
]);

// check that chain values have been fetched before committing to state.
// could be expanded to check supported chains.
if (
newChainState.every((c) => {
return !!c?.toHuman();
})
) {
if (newChainState.every((c) => !!c?.toHuman())) {
const chain = newChainState[0]?.toString();
const version = newChainState[1]?.toString();
const version = newChainState[1]?.toJSON();
const ss58Prefix = Number(newChainState[2]?.toString());

// set fetched chain state in storage.
setchainState({ chain, version, ss58Prefix });
}

Expand Down Expand Up @@ -301,8 +301,6 @@ export const APIProvider = ({ children, network }: APIProviderProps) => {
);
};

export const APIContext = createContext<APIContextInterface>(
defaults.defaultApiContext
);
export const APIContext = createContext<APIContextInterface>(defaultApiContext);

export const useApi = () => useContext(APIContext);
14 changes: 6 additions & 8 deletions src/contexts/Api/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import type { ApiPromise } from '@polkadot/api';
import type { U8aLike } from '@polkadot/util/types';
import type BigNumber from 'bignumber.js';
import type { ReactNode } from 'react';
import type { Network, NetworkName } from '../../types';
import type { AnyJson, Network, NetworkName } from '../../types';

export type ApiStatus = 'connecting' | 'connected' | 'disconnected';

Expand All @@ -32,13 +32,11 @@ export interface APIConstants {
poolsPalletId: U8aLike;
}

export type APIChainState =
| {
chain: string;
version: string;
ss58Prefix: number;
}
| undefined;
export type APIChainState = {
chain: string | null;
version: AnyJson;
ss58Prefix: number;
};

export interface APIContextInterface {
api: ApiPromise | null;
Expand Down
Loading
Loading