@talismn/chain-connectors 0.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +674 -0
- package/README.md +1 -0
- package/dist/declarations/src/dot/ChainConnectorDot.d.ts +79 -0
- package/dist/declarations/src/dot/ChainConnectorDotStub.d.ts +11 -0
- package/dist/declarations/src/dot/IChainConnectorDot.d.ts +10 -0
- package/dist/declarations/src/dot/Websocket.d.ts +111 -0
- package/dist/declarations/src/dot/helpers.d.ts +15 -0
- package/dist/declarations/src/dot/index.d.ts +3 -0
- package/dist/declarations/src/eth/ChainConnectorEth.d.ts +10 -0
- package/dist/declarations/src/eth/ChainConnectorEthStub.d.ts +10 -0
- package/dist/declarations/src/eth/IChainConnectorEth.d.ts +7 -0
- package/dist/declarations/src/eth/getChainFromEvmNetwork.d.ts +4 -0
- package/dist/declarations/src/eth/getEvmNetworkPublicClient.d.ts +4 -0
- package/dist/declarations/src/eth/getEvmNetworkWalletClient.d.ts +7 -0
- package/dist/declarations/src/eth/getTransportForEvmNetwork.d.ts +8 -0
- package/dist/declarations/src/eth/index.d.ts +3 -0
- package/dist/declarations/src/index.d.ts +3 -0
- package/dist/declarations/src/log.d.ts +2 -0
- package/dist/declarations/src/sol/ChainConnectorSol.d.ts +8 -0
- package/dist/declarations/src/sol/ChainConnectorSolStub.d.ts +8 -0
- package/dist/declarations/src/sol/IChainConnectorSol.d.ts +5 -0
- package/dist/declarations/src/sol/getSolConnection.d.ts +3 -0
- package/dist/declarations/src/sol/index.d.ts +3 -0
- package/dist/talismn-chain-connectors.cjs.d.ts +1 -0
- package/dist/talismn-chain-connectors.cjs.dev.js +1236 -0
- package/dist/talismn-chain-connectors.cjs.js +7 -0
- package/dist/talismn-chain-connectors.cjs.prod.js +1236 -0
- package/dist/talismn-chain-connectors.esm.js +1202 -0
- package/package.json +63 -0
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
import type { ProviderInterface, ProviderInterfaceCallback } from "@polkadot/rpc-provider/types";
|
|
2
|
+
import { DotNetworkId, IChaindataNetworkProvider } from "@talismn/chaindata-provider";
|
|
3
|
+
import { TalismanConnectionMetaDatabase } from "@talismn/connection-meta";
|
|
4
|
+
import { IChainConnectorDot } from "./IChainConnectorDot";
|
|
5
|
+
export declare class ChainConnectionError extends Error {
|
|
6
|
+
type: "CHAIN_CONNECTION_ERROR";
|
|
7
|
+
chainId: string;
|
|
8
|
+
constructor(chainId: string, options?: ErrorOptions);
|
|
9
|
+
}
|
|
10
|
+
export declare class StaleRpcError extends Error {
|
|
11
|
+
type: "STALE_RPC_ERROR";
|
|
12
|
+
chainId: string;
|
|
13
|
+
constructor(chainId: string, options?: ErrorOptions);
|
|
14
|
+
}
|
|
15
|
+
export declare class WebsocketAllocationExhaustedError extends Error {
|
|
16
|
+
type: "WEBSOCKET_ALLOCATION_EXHAUSTED_ERROR";
|
|
17
|
+
chainId: string;
|
|
18
|
+
constructor(chainId: string, options?: ErrorOptions);
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* ChainConnector provides an interface similar to WsProvider, but with three points of difference:
|
|
22
|
+
*
|
|
23
|
+
* 1. ChainConnector methods all accept a `chainId` instead of an array of RPCs. RPCs are then fetched internally from chaindata.
|
|
24
|
+
* 2. ChainConnector creates only one `WsProvider` per chain and ensures that all downstream requests to a chain share the one socket connection.
|
|
25
|
+
* 3. Subscriptions return a callable `unsubscribe` method instead of an id.
|
|
26
|
+
*
|
|
27
|
+
* Additionally, when run on the clientside of a dapp where `window.talismanSub` is available, instead of spinning up new websocket
|
|
28
|
+
* connections this class will forward all requests through to the wallet backend - where another instance of this class will
|
|
29
|
+
* handle the websocket connections.
|
|
30
|
+
*/
|
|
31
|
+
export declare class ChainConnectorDot implements IChainConnectorDot {
|
|
32
|
+
#private;
|
|
33
|
+
constructor(chaindataChainProvider: IChaindataNetworkProvider, connectionMetaDb?: TalismanConnectionMetaDatabase);
|
|
34
|
+
/**
|
|
35
|
+
* Creates a facade over this ChainConnector which conforms to the PJS ProviderInterface
|
|
36
|
+
* @example // Using a chainConnector as a Provider for an ApiPromise
|
|
37
|
+
* const provider = chainConnector.asProvider('polkadot')
|
|
38
|
+
* const api = new ApiPromise({ provider })
|
|
39
|
+
*/
|
|
40
|
+
asProvider(chainId: DotNetworkId): ProviderInterface;
|
|
41
|
+
send<T = any>(chainId: DotNetworkId, method: string, params: unknown[], isCacheable?: boolean | undefined, extraOptions?: {
|
|
42
|
+
/**
|
|
43
|
+
* Set to `true` if this query is speculative, i.e. if on some chains it's expected that it will raise a wasm unreachable error of the form:
|
|
44
|
+
*
|
|
45
|
+
* 4003: Client error: Execution failed: Execution aborted due to trap: wasm trap: wasm `unreachable` instruction executed
|
|
46
|
+
*
|
|
47
|
+
* By setting expectErrors to true, this method won't pollute the logs with errors we intend to have happen.
|
|
48
|
+
* An example use case of this is when you plan to catch the wasm unreachable error on chains that don't support the query, and then fall back
|
|
49
|
+
* to another query or perhaps an empty result.
|
|
50
|
+
*/
|
|
51
|
+
expectErrors?: boolean;
|
|
52
|
+
}): Promise<T>;
|
|
53
|
+
subscribe(chainId: DotNetworkId, subscribeMethod: string, responseMethod: string, params: unknown[], callback: ProviderInterfaceCallback, timeout?: number | false): Promise<(unsubscribeMethod: string) => void>;
|
|
54
|
+
/**
|
|
55
|
+
* Kills current websocket if any
|
|
56
|
+
* Useful after changing rpc order to make sure it's applied for futher requests
|
|
57
|
+
*/
|
|
58
|
+
reset(chainId: DotNetworkId): Promise<void>;
|
|
59
|
+
/**
|
|
60
|
+
* Wait for websocket to be ready, but don't wait forever
|
|
61
|
+
*/
|
|
62
|
+
private waitForWs;
|
|
63
|
+
/**
|
|
64
|
+
* Connect to an RPC via chainId
|
|
65
|
+
*
|
|
66
|
+
* The caller must call disconnectChainSocket with the returned SocketUserId once they are finished with it
|
|
67
|
+
*/
|
|
68
|
+
private connectChainSocket;
|
|
69
|
+
private disconnectChainSocket;
|
|
70
|
+
private addSocketUser;
|
|
71
|
+
private removeSocketUser;
|
|
72
|
+
/** continues to generate a random number until it finds one which is not present in the exclude list */
|
|
73
|
+
private getExclusiveRandomId;
|
|
74
|
+
/** generates a random number */
|
|
75
|
+
private getRandomId;
|
|
76
|
+
private getTalismanSub;
|
|
77
|
+
private updateRpcPriority;
|
|
78
|
+
private getEndpoints;
|
|
79
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { ProviderInterface, ProviderInterfaceCallback } from "@polkadot/rpc-provider/types";
|
|
2
|
+
import { DotNetwork, DotNetworkId } from "@talismn/chaindata-provider";
|
|
3
|
+
import { IChainConnectorDot } from "./IChainConnectorDot";
|
|
4
|
+
export declare class ChainConnectorDotStub implements IChainConnectorDot {
|
|
5
|
+
#private;
|
|
6
|
+
constructor(network: DotNetwork);
|
|
7
|
+
asProvider(): ProviderInterface;
|
|
8
|
+
send<T = unknown>(chainId: DotNetworkId, method: string, params: unknown[], isCacheable?: boolean): Promise<T>;
|
|
9
|
+
subscribe(chainId: DotNetworkId, subscribeMethod: string, responseMethod: string, params: unknown[], callback: ProviderInterfaceCallback, timeout?: number | false): Promise<(unsubscribeMethod: string) => void>;
|
|
10
|
+
reset(): Promise<void>;
|
|
11
|
+
}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { ProviderInterface, ProviderInterfaceCallback } from "@polkadot/rpc-provider/types";
|
|
2
|
+
import { DotNetworkId } from "@talismn/chaindata-provider";
|
|
3
|
+
export interface IChainConnectorDot {
|
|
4
|
+
asProvider(chainId: DotNetworkId): ProviderInterface;
|
|
5
|
+
send<T = unknown>(chainId: DotNetworkId, method: string, params: unknown[], isCacheable?: boolean, extraOptions?: {
|
|
6
|
+
expectErrors?: boolean;
|
|
7
|
+
}): Promise<T>;
|
|
8
|
+
subscribe(chainId: DotNetworkId, subscribeMethod: string, responseMethod: string, params: unknown[], callback: ProviderInterfaceCallback, timeout?: number | false): Promise<(unsubscribeMethod: string) => void>;
|
|
9
|
+
reset(chainId: DotNetworkId): Promise<void>;
|
|
10
|
+
}
|
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
import type { ProviderInterfaceEmitted as PjsProviderInterfaceEmitted, ProviderInterface, ProviderInterfaceCallback, ProviderInterfaceEmitCb } from "@polkadot/rpc-provider/types";
|
|
2
|
+
type ProviderInterfaceEmitted = PjsProviderInterfaceEmitted | "stale-rpcs";
|
|
3
|
+
interface SubscriptionHandler {
|
|
4
|
+
callback: ProviderInterfaceCallback;
|
|
5
|
+
type: string;
|
|
6
|
+
}
|
|
7
|
+
/**
|
|
8
|
+
* # @talismn/chain-connector/Websocket
|
|
9
|
+
*
|
|
10
|
+
* @name Websocket
|
|
11
|
+
*
|
|
12
|
+
* @description The WebSocket Provider allows sending requests using WebSocket to a WebSocket RPC server TCP port. Unlike the [[HttpProvider]], it does support subscriptions and allows listening to events such as new blocks or balance changes.
|
|
13
|
+
*
|
|
14
|
+
* @example
|
|
15
|
+
* <BR>
|
|
16
|
+
*
|
|
17
|
+
* ```javascript
|
|
18
|
+
* import { Websocket } from '@talismn/chain-connector';
|
|
19
|
+
*
|
|
20
|
+
* const provider = new Websocket('ws://127.0.0.1:9944');
|
|
21
|
+
* ```
|
|
22
|
+
*
|
|
23
|
+
* @see [[HttpProvider]]
|
|
24
|
+
*/
|
|
25
|
+
export declare class Websocket implements ProviderInterface {
|
|
26
|
+
#private;
|
|
27
|
+
/**
|
|
28
|
+
* @param {string | string[]} endpoint The endpoint url. Usually `ws://ip:9944` or `wss://ip:9944`, may provide an array of endpoint strings.
|
|
29
|
+
* @param {Record<string, string>} headers The headers provided to the underlying WebSocket
|
|
30
|
+
* @param {number} [timeout] Custom timeout value used per request . Defaults to `DEFAULT_TIMEOUT_MS`
|
|
31
|
+
*/
|
|
32
|
+
constructor(endpoint: string | string[], headers?: Record<string, string>, timeout?: number, nextBackoffInterval?: number);
|
|
33
|
+
/**
|
|
34
|
+
* @summary `true` when this provider supports subscriptions
|
|
35
|
+
*/
|
|
36
|
+
get hasSubscriptions(): boolean;
|
|
37
|
+
/**
|
|
38
|
+
* @summary `true` when this provider supports clone()
|
|
39
|
+
*/
|
|
40
|
+
get isClonable(): boolean;
|
|
41
|
+
/**
|
|
42
|
+
* @summary Whether the node is connected or not.
|
|
43
|
+
* @return {boolean} true if connected
|
|
44
|
+
*/
|
|
45
|
+
get isConnected(): boolean;
|
|
46
|
+
/**
|
|
47
|
+
* @description Promise that resolves the first time we are connected and loaded
|
|
48
|
+
*/
|
|
49
|
+
get isReady(): Promise<Websocket>;
|
|
50
|
+
get endpoint(): string;
|
|
51
|
+
/**
|
|
52
|
+
* @description Returns a clone of the object
|
|
53
|
+
*/
|
|
54
|
+
clone(): Websocket;
|
|
55
|
+
protected selectEndpointIndex(endpoints: string[]): number;
|
|
56
|
+
/**
|
|
57
|
+
* @summary Manually connect
|
|
58
|
+
* @description The [[Websocket]] connects automatically by default, however if you decided otherwise, you may
|
|
59
|
+
* connect manually using this method.
|
|
60
|
+
*/
|
|
61
|
+
connect(): Promise<void>;
|
|
62
|
+
/**
|
|
63
|
+
* @description Connect, never throwing an error, but rather forcing a retry
|
|
64
|
+
*/
|
|
65
|
+
connectWithRetry(): Promise<void>;
|
|
66
|
+
protected scheduleNextRetry(): void;
|
|
67
|
+
/**
|
|
68
|
+
* @description Manually disconnect from the connection, clearing auto-connect logic
|
|
69
|
+
*/
|
|
70
|
+
disconnect(): Promise<void>;
|
|
71
|
+
/**
|
|
72
|
+
* @summary Listens on events after having subscribed using the [[subscribe]] function.
|
|
73
|
+
* @param {ProviderInterfaceEmitted} type Event
|
|
74
|
+
* @param {ProviderInterfaceEmitCb} sub Callback
|
|
75
|
+
* @return unsubscribe function
|
|
76
|
+
*/
|
|
77
|
+
on(type: ProviderInterfaceEmitted, sub: ProviderInterfaceEmitCb): () => void;
|
|
78
|
+
/**
|
|
79
|
+
* @summary Send JSON data using WebSockets to configured HTTP Endpoint or queue.
|
|
80
|
+
* @param method The RPC methods to execute
|
|
81
|
+
* @param params Encoded parameters as applicable for the method
|
|
82
|
+
* @param subscription Subscription details (internally used)
|
|
83
|
+
*/
|
|
84
|
+
send<T = any>(method: string, params: unknown[],
|
|
85
|
+
/** @deprecated \@talismn/chain-connector doesn't implement a cache */
|
|
86
|
+
isCacheable?: boolean, subscription?: SubscriptionHandler): Promise<T>;
|
|
87
|
+
/**
|
|
88
|
+
* @name subscribe
|
|
89
|
+
* @summary Allows subscribing to a specific event.
|
|
90
|
+
*
|
|
91
|
+
* @example
|
|
92
|
+
* <BR>
|
|
93
|
+
*
|
|
94
|
+
* ```javascript
|
|
95
|
+
* const provider = new Websocket('ws://127.0.0.1:9944');
|
|
96
|
+
* const rpc = new Rpc(provider);
|
|
97
|
+
*
|
|
98
|
+
* rpc.state.subscribeStorage([[storage.system.account, <Address>]], (_, values) => {
|
|
99
|
+
* console.log(values)
|
|
100
|
+
* }).then((subscriptionId) => {
|
|
101
|
+
* console.log('balance changes subscription id: ', subscriptionId)
|
|
102
|
+
* })
|
|
103
|
+
* ```
|
|
104
|
+
*/
|
|
105
|
+
subscribe(type: string, method: string, params: unknown[], callback: ProviderInterfaceCallback): Promise<number | string>;
|
|
106
|
+
/**
|
|
107
|
+
* @summary Allows unsubscribing to subscriptions made with [[subscribe]].
|
|
108
|
+
*/
|
|
109
|
+
unsubscribe(type: string, method: string, id: number | string): Promise<boolean>;
|
|
110
|
+
}
|
|
111
|
+
export {};
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
export declare class ExponentialBackoff {
|
|
2
|
+
#private;
|
|
3
|
+
constructor(maxIntervalMs?: number, minIntervalMs?: number);
|
|
4
|
+
enable(): void;
|
|
5
|
+
disable(): void;
|
|
6
|
+
increase(): void;
|
|
7
|
+
decrease(): void;
|
|
8
|
+
reset(): void;
|
|
9
|
+
resetTo(nextInterval: number): void;
|
|
10
|
+
resetToMax(): void;
|
|
11
|
+
get isActive(): boolean;
|
|
12
|
+
get next(): number;
|
|
13
|
+
get isMin(): boolean;
|
|
14
|
+
get isMax(): boolean;
|
|
15
|
+
}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { EthNetworkId, IChaindataNetworkProvider, IChaindataTokenProvider } from "@talismn/chaindata-provider";
|
|
2
|
+
import { Account, PublicClient, WalletClient } from "viem";
|
|
3
|
+
import { IChainConnectorEth } from "./IChainConnectorEth";
|
|
4
|
+
export declare class ChainConnectorEth implements IChainConnectorEth {
|
|
5
|
+
#private;
|
|
6
|
+
constructor(chaindataProvider: IChaindataNetworkProvider & IChaindataTokenProvider);
|
|
7
|
+
getPublicClientForEvmNetwork(evmNetworkId: EthNetworkId): Promise<PublicClient | null>;
|
|
8
|
+
getWalletClientForEvmNetwork(evmNetworkId: EthNetworkId, account?: `0x${string}` | Account): Promise<WalletClient | null>;
|
|
9
|
+
clearRpcProvidersCache(evmNetworkId?: EthNetworkId): void;
|
|
10
|
+
}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { EthNetwork, EthNetworkId } from "@talismn/chaindata-provider";
|
|
2
|
+
import { Account, PublicClient, WalletClient } from "viem";
|
|
3
|
+
import { IChainConnectorEth } from "./IChainConnectorEth";
|
|
4
|
+
export declare class ChainConnectorEthStub implements IChainConnectorEth {
|
|
5
|
+
#private;
|
|
6
|
+
constructor(network: EthNetwork);
|
|
7
|
+
getPublicClientForEvmNetwork(): Promise<PublicClient | null>;
|
|
8
|
+
getWalletClientForEvmNetwork(networkId: EthNetworkId, account?: `0x${string}` | Account): Promise<WalletClient | null>;
|
|
9
|
+
clearRpcProvidersCache(): void;
|
|
10
|
+
}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { EthNetworkId } from "@talismn/chaindata-provider";
|
|
2
|
+
import { Account, PublicClient, WalletClient } from "viem";
|
|
3
|
+
export interface IChainConnectorEth {
|
|
4
|
+
getPublicClientForEvmNetwork: (evmNetworkId: EthNetworkId) => Promise<PublicClient | null>;
|
|
5
|
+
getWalletClientForEvmNetwork: (evmNetworkId: EthNetworkId, account?: `0x${string}` | Account) => Promise<WalletClient | null>;
|
|
6
|
+
clearRpcProvidersCache: (evmNetworkId?: EthNetworkId) => void;
|
|
7
|
+
}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { EthNetwork } from "@talismn/chaindata-provider";
|
|
2
|
+
import { Account, WalletClient } from "viem";
|
|
3
|
+
type WalletClientOptions = {
|
|
4
|
+
account?: `0x${string}` | Account;
|
|
5
|
+
};
|
|
6
|
+
export declare const getEvmNetworkWalletClient: (network: EthNetwork, options?: WalletClientOptions) => WalletClient;
|
|
7
|
+
export {};
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { EthNetwork } from "@talismn/chaindata-provider";
|
|
2
|
+
export type TransportOptions = {
|
|
3
|
+
batch?: boolean | {
|
|
4
|
+
batchSize?: number | undefined;
|
|
5
|
+
wait?: number | undefined;
|
|
6
|
+
};
|
|
7
|
+
};
|
|
8
|
+
export declare const getTransportForEvmNetwork: (evmNetwork: EthNetwork, options?: TransportOptions) => import("viem").FallbackTransport<import("viem").HttpTransport<undefined, false>[]>;
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { Connection } from "@solana/web3.js";
|
|
2
|
+
import { IChaindataNetworkProvider, IChaindataTokenProvider, SolNetworkId } from "@talismn/chaindata-provider";
|
|
3
|
+
import { IChainConnectorSol } from "./IChainConnectorSol";
|
|
4
|
+
export declare class ChainConnectorSol implements IChainConnectorSol {
|
|
5
|
+
#private;
|
|
6
|
+
constructor(chaindataProvider: IChaindataNetworkProvider & IChaindataTokenProvider);
|
|
7
|
+
getConnection(networkId: SolNetworkId): Promise<Connection>;
|
|
8
|
+
}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { Connection } from "@solana/web3.js";
|
|
2
|
+
import { SolNetwork } from "@talismn/chaindata-provider";
|
|
3
|
+
import { IChainConnectorSol } from "./IChainConnectorSol";
|
|
4
|
+
export declare class ChainConnectorSolStub implements IChainConnectorSol {
|
|
5
|
+
#private;
|
|
6
|
+
constructor(network: SolNetwork);
|
|
7
|
+
getConnection(): Promise<Connection>;
|
|
8
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from "./declarations/src/index";
|