|
1 | 1 | import { ApiPromise, Keyring, WsProvider } from '@polkadot/api' |
2 | 2 | import type { AccountId } from '@polkadot/types/interfaces' |
| 3 | +import type { HexString } from '@polkadot/util/types' |
3 | 4 | import { cryptoWaitReady } from '@polkadot/util-crypto' |
4 | 5 | import { PinkContractPromise } from './contracts/PinkContract' |
5 | 6 | import { PinkLoggerContractPromise } from './contracts/PinkLoggerContract' |
6 | 7 | import { type CreateOptions, OnChainRegistry } from './OnChainRegistry' |
7 | 8 | import { options } from './options' |
8 | 9 | import createPruntimeClient from './pruntime/createPruntimeClient' |
9 | 10 | import type { AbiLike } from './types' |
| 11 | +import { type LiteralRpc, fetchMetadata } from './utils/fetchMetadata' |
10 | 12 |
|
11 | 13 | export type GetClientOptions = { |
12 | | - transport: string | WsProvider |
| 14 | + transport: LiteralRpc | WsProvider |
| 15 | + |
| 16 | + // Provides metadata instead loading via RPC when initializing the client. |
| 17 | + // It's optional since if the RPC under the phala.network domain, we will |
| 18 | + // try to preload the metadata via HTTP unless the `noPreloadMetadata` is |
| 19 | + // set to true. |
| 20 | + metadata?: Record<string, HexString> |
| 21 | + noPreloadMetadata?: boolean |
13 | 22 | } & CreateOptions |
14 | 23 |
|
15 | 24 | export async function getClient(opts: GetClientOptions): Promise<OnChainRegistry> { |
16 | | - const { transport, ...rest } = opts |
| 25 | + const { transport, metadata: _metadata, noPreloadMetadata, ...rest } = opts |
17 | 26 | const provider = typeof transport === 'string' ? new WsProvider(transport) : transport |
18 | | - const api = await ApiPromise.create(options({ provider, noInitWarn: true })) |
| 27 | + let metadata = _metadata |
| 28 | + if (typeof transport === 'string' && !metadata && transport.indexOf('phala.network/') !== -1 && !noPreloadMetadata) { |
| 29 | + metadata = await fetchMetadata(transport) |
| 30 | + } |
| 31 | + const api = await ApiPromise.create(options({ provider, noInitWarn: true, metadata })) |
19 | 32 | return await OnChainRegistry.create(api, rest) |
20 | 33 | } |
21 | 34 |
|
|
0 commit comments