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

feat: add light-client example #80

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
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
5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,18 @@
"lint:ci": "rome ci ."
},
"dependencies": {
"@polkadot/api": "^10.9.1",
"@polkadot/api-contract": "^10.9.1",
"@polkadot/api-derive": "^10.9.1",
"@polkadot/api": "^10.9.1",
"@polkadot/extension-dapp": "^0.46.5",
"@polkadot/extension-inject": "^0.46.5",
"@polkadot/rpc-provider": "^10.9.1",
"@polkadot/util": "^12.3.2",
"@substrate/connect": "^0.7.30",
"@talismn/connect-wallets": "^1.2.3"
},
"devDependencies": {
"@types/node": "18.15.6",
"rome": "12.1.3"
},
"packageManager": "[email protected]"
Expand Down
1 change: 1 addition & 0 deletions packages/useink/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
"@changesets/cli": "^2.26.1",
"@polkadot/types": "^10.9.1",
"@polkadot/types-codec": "^10.9.1",
"@types/node": "18.15.6",
"@types/react": "^18.2.7",
"tsup": "^6.7.0",
"typescript": "^5.0.4"
Expand Down
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';
21 changes: 18 additions & 3 deletions packages/useink/src/react/providers/api/model.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,26 @@
import { ChainId } from '../../../chains/index';
import { ApiPromise, WsProvider } from '../../../core/index';
import { ApiPromise, ScProvider, WsProvider } from '../../../core/index';

export interface IApiProvider {
api: ApiPromise;
interface TrustedConnection {
connection: 'trusted';
provider: WsProvider;
}

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

export type ConnectionType =
| TrustedConnection['connection']
| LightClientConnection['connection'];

export type ProviderConnection = TrustedConnection | LightClientConnection;

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

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

export interface API {
Expand Down
54 changes: 41 additions & 13 deletions packages/useink/src/react/providers/api/provider.tsx
Original file line number Diff line number Diff line change
@@ -1,30 +1,58 @@
import { ApiPromise, WsProvider } from '../../../core/index';
import { ChainId } from '../../../chains/types.ts';
import { ApiPromise, Sc, ScProvider, WsProvider } from '../../../core/index';
import { useChains } from '../../hooks/index';
import { useConfig } from '../../index';
import { ProviderConnection, useConfig } from '../../index';
import { ProviderConnectionConfig } from '../config/index.ts';
import { APIContext } from './context.ts';
import { apiProvidersReducer } from './reducer.ts';
import React, { useEffect, useReducer } from 'react';

const isTrusted = (
chainId: ChainId,
apiConfig?: ProviderConnectionConfig,
): boolean => {
const chainConfig = apiConfig?.[chainId];
if (chainConfig !== undefined) return chainConfig === 'trusted';

const defaultConfig = apiConfig?.default;
if (defaultConfig !== undefined) return defaultConfig === 'trusted';

return false;
};

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

useEffect(() => {
chains.forEach((chain) => {
const provider = new WsProvider(chainRpcs[chain.id] || chain.rpcs[0]);

ApiPromise.create({ provider }).then((api) => {
dispatch({
type: 'ADD_API_PROVIDER',
chainId: chain.id,
apiProvider: { api, provider },
});
});
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'),
};

console.log('conn', connection);

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

return <APIContext.Provider value={{ apis }}>{children}</APIContext.Provider>;
};
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
3 changes: 3 additions & 0 deletions playground/src/pages/_app.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ function App({ Component, pageProps }: AppProps) {
caller: {
default: '5EyR7vEk7DtvEWeefGcXXMV6hKwB8Ex5uvjHufm466mbjJkR',
},
api: {
default: 'light-client',
},
}}
>
<NotificationsProvider>
Expand Down
7 changes: 5 additions & 2 deletions playground/src/pages/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,11 @@ export default function Index() {
return (
<>
<Head>
<title>Playground</title>
<meta name='description' content='useink Kitchen Sink' />
<title>useink Playground</title>
<meta
name='description'
content='Playground is a sandbox for useink development and showing quick examples.'
/>
<meta name='viewport' content='width=device-width, initial-scale=1' />
<link rel='icon' href='/squink.svg' />
</Head>
Expand Down
39 changes: 35 additions & 4 deletions pnpm-lock.yaml

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