Skip to content
This repository has been archived by the owner on Feb 9, 2024. It is now read-only.

Commit

Permalink
feat: add light-client example
Browse files Browse the repository at this point in the history
  • Loading branch information
DoubleOTheven committed Jul 24, 2023
1 parent f93af9a commit a90e21d
Show file tree
Hide file tree
Showing 7 changed files with 116 additions and 4 deletions.
2 changes: 2 additions & 0 deletions packages/useink/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,8 @@
}
},
"dependencies": {
"@polkadot/rpc-provider": "^10.9.1",
"@substrate/connect": "^0.7.30",
"nanoid": "3"
}
}
1 change: 1 addition & 0 deletions packages/useink/src/core/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ export * from './api-contract.ts';
export * from './array.ts';
export * from './contracts.ts';
export * from './result.ts';
export * from './substrate-connect.ts';
export * from './substrate.ts';
export * from './talisman-connect-wallets.ts';
export * from './unsub.ts';
2 changes: 2 additions & 0 deletions packages/useink/src/core/types/substrate-connect.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export { ScProvider } from '@polkadot/rpc-provider';
export * as Sc from '@substrate/connect';
14 changes: 11 additions & 3 deletions packages/useink/src/react/providers/config/model.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,25 @@
import { Chain, ChainId, RococoContractsTestnet } from '../../../chains/index';
import { ArrayOneOrMore } from '../../../core/index';
import { FIVE_SECONDS, HALF_A_SECOND } from '../../constants.ts';
import { ConnectionType } from '../../providers/api/model.ts';

export type ChainRPCs = Partial<Record<ChainId, string>>;

export type CallerAddress = string;

export type ProviderConnectionConfig = Partial<
Record<ChainId, ConnectionType>
> & { default?: ConnectionType };

export type DefaultCallerConfig = Partial<Record<ChainId, CallerAddress>> & {
default?: CallerAddress;
};

export type ConfigProps = {
dappName?: string;
chains: ArrayOneOrMore<Chain>;
caller?: {
default?: CallerAddress;
} & Partial<Record<ChainId, CallerAddress>>;
api?: ProviderConnectionConfig;
caller?: DefaultCallerConfig;
events?: {
expiration?: number;
checkInterval?: number;
Expand Down
27 changes: 26 additions & 1 deletion pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

28 changes: 28 additions & 0 deletions react/providers/api/model.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { ChainId } from '../../../chains/mod.ts';
import { ApiPromise, ScProvider, WsProvider } from '../../../core/mod.ts';

interface TrustedConnection {
connection: 'trusted';
provider: WsProvider;
}

interface LightClientConnection {
connection: 'light-client';
provider: ScProvider;
}

export type ProviderConnection = TrustedConnection | LightClientConnection;

export type IApiProvider = {
api: ApiPromise;
} & ProviderConnection;

export type IApiProviders = Partial<
Record<ChainId, IApiProvider>
>;

export interface API {
apis?: IApiProviders;
}

export type ConnectionType = 'trusted' | 'light-client';
46 changes: 46 additions & 0 deletions react/providers/api/provider.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import React, { useEffect, useReducer } from 'react'
import { useChains } from '../../hooks/mod.ts'
import { APIContext } from './context.ts'
import { apiProvidersReducer } from './reducer.ts'
import { ProviderConnection, useConfig } from '../../mod.ts'
import { ApiPromise, Sc, ScProvider, WsProvider } from "../../../core/mod.ts";
import { ChainId } from '../../../chains/types.ts'
import { ProviderConnectionConfig } from '../config/mod.ts'

const isTrusted = (chainId: ChainId, apiConfig?: ProviderConnectionConfig): boolean => {
if (!apiConfig) return true;
if (apiConfig?.[chainId] === 'light-client') return false;
if (!apiConfig?.[chainId] && apiConfig.default === 'light-client') return false;

return true
}

export const APIProvider: React.FC<React.PropsWithChildren<any>> = ({
children,
}) => {
const chains = useChains()
const c = useConfig()
const [apis, dispatch] = useReducer(apiProvidersReducer, {})

useEffect(() => {
chains.forEach((chain) => {
const connection: ProviderConnection = isTrusted(chain.id, c.api) ? ({
connection: 'trusted',
provider: new WsProvider(c.chainRpcs[chain.id] || chain.rpcs[0]),
}) : ({
connection: 'light-client',
provider: new ScProvider(Sc, "all"),
})

ApiPromise.create({ provider: connection.provider }).then((api) => {
dispatch({
type: 'ADD_API_PROVIDER',
chainId: chain.id,
apiProvider: { api, ...connection },
})
})
})
}, [chains, c.chainRpcs, c.api])

return <APIContext.Provider value={{ apis }} children={children} />
}

0 comments on commit a90e21d

Please sign in to comment.