viem 0.0.0 → 0.0.1-alpha.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.
@@ -0,0 +1,251 @@
1
+ import {
2
+ BaseError,
3
+ buildRequest,
4
+ getSocket,
5
+ rpc
6
+ } from "./chunk-GI67STNV.js";
7
+
8
+ // src/clients/transports/createTransport.ts
9
+ function createTransport(config, value) {
10
+ return {
11
+ config,
12
+ value
13
+ };
14
+ }
15
+
16
+ // src/clients/transports/errors.ts
17
+ var UrlRequiredError = class extends BaseError {
18
+ constructor() {
19
+ super(
20
+ "No URL was provided to the Transport. Please provide a valid RPC URL to the Transport.",
21
+ {
22
+ docsPath: "/TODO"
23
+ }
24
+ );
25
+ }
26
+ };
27
+
28
+ // src/clients/transports/ethereumProvider.ts
29
+ function ethereumProvider({
30
+ key = "ethereumProvider",
31
+ name = "Ethereum Provider",
32
+ provider
33
+ }) {
34
+ return () => createTransport({
35
+ key,
36
+ name,
37
+ request: provider.request.bind(provider),
38
+ type: "ethereumProvider"
39
+ });
40
+ }
41
+
42
+ // src/clients/transports/http.ts
43
+ function http({
44
+ key = "http",
45
+ name = "HTTP JSON-RPC",
46
+ url
47
+ } = {}) {
48
+ return ({ chain }) => {
49
+ const url_ = url || chain?.rpcUrls.default.http[0];
50
+ if (!url_)
51
+ throw new UrlRequiredError();
52
+ return createTransport(
53
+ {
54
+ key,
55
+ name,
56
+ async request({ method, params }) {
57
+ const { result } = await rpc.http(url_, {
58
+ body: {
59
+ method,
60
+ params
61
+ }
62
+ });
63
+ return result;
64
+ },
65
+ type: "http"
66
+ },
67
+ {
68
+ url
69
+ }
70
+ );
71
+ };
72
+ }
73
+
74
+ // src/clients/transports/webSocket.ts
75
+ function webSocket({
76
+ key = "webSocket",
77
+ name = "WebSocket JSON-RPC",
78
+ url
79
+ } = {}) {
80
+ return ({ chain }) => {
81
+ const url_ = url || chain?.rpcUrls.default.webSocket?.[0];
82
+ if (!url_)
83
+ throw new UrlRequiredError();
84
+ return createTransport(
85
+ {
86
+ key,
87
+ name,
88
+ async request({ method, params }) {
89
+ const socket = await getSocket(url_);
90
+ const { result } = await rpc.webSocketAsync(socket, {
91
+ body: { method, params }
92
+ });
93
+ return result;
94
+ },
95
+ type: "webSocket"
96
+ },
97
+ {
98
+ getSocket() {
99
+ return getSocket(url_);
100
+ },
101
+ async subscribe({ params, onData, onError }) {
102
+ const socket = await getSocket(url_);
103
+ const { result: subscriptionId } = await new Promise(
104
+ (resolve, reject) => rpc.webSocket(socket, {
105
+ body: {
106
+ method: "eth_subscribe",
107
+ params
108
+ },
109
+ onData: (data) => {
110
+ if (typeof data.id === "number") {
111
+ resolve(data);
112
+ return;
113
+ }
114
+ onData(data);
115
+ },
116
+ onError: (error) => {
117
+ reject(error);
118
+ onError?.(error);
119
+ }
120
+ })
121
+ );
122
+ return {
123
+ subscriptionId,
124
+ async unsubscribe() {
125
+ return new Promise(
126
+ (resolve, reject) => rpc.webSocket(socket, {
127
+ body: {
128
+ method: "eth_unsubscribe",
129
+ params: [subscriptionId]
130
+ },
131
+ onData: resolve,
132
+ onError: reject
133
+ })
134
+ );
135
+ }
136
+ };
137
+ }
138
+ }
139
+ );
140
+ };
141
+ }
142
+
143
+ // src/utils/uid.ts
144
+ var size = 256;
145
+ var index = size;
146
+ var buffer;
147
+ function uid(length = 11) {
148
+ if (!buffer || index + length > size * 2) {
149
+ buffer = "";
150
+ index = 0;
151
+ for (let i = 0; i < size; i++) {
152
+ buffer += (256 + Math.random() * 256 | 0).toString(16).substring(1);
153
+ }
154
+ }
155
+ return buffer.substring(index, index++ + length);
156
+ }
157
+
158
+ // src/clients/createClient.ts
159
+ function createClient({
160
+ chain,
161
+ key = "base",
162
+ name = "Base Client",
163
+ pollingInterval = 4e3,
164
+ transport,
165
+ type = "base"
166
+ }) {
167
+ const { config, value } = transport({ chain });
168
+ return {
169
+ chain,
170
+ key,
171
+ name,
172
+ pollingInterval,
173
+ request: buildRequest(config.request),
174
+ transport: { ...config, ...value },
175
+ type,
176
+ uid: uid()
177
+ };
178
+ }
179
+
180
+ // src/clients/createPublicClient.ts
181
+ function createPublicClient({
182
+ chain,
183
+ key = "public",
184
+ name = "Public Client",
185
+ transport,
186
+ pollingInterval
187
+ }) {
188
+ chain;
189
+ return {
190
+ ...createClient({
191
+ chain,
192
+ key,
193
+ name,
194
+ pollingInterval,
195
+ transport,
196
+ type: "publicClient"
197
+ })
198
+ };
199
+ }
200
+
201
+ // src/clients/createTestClient.ts
202
+ function createTestClient({
203
+ chain,
204
+ key = "test",
205
+ name = "Test Client",
206
+ mode,
207
+ pollingInterval,
208
+ transport
209
+ }) {
210
+ return {
211
+ ...createClient({
212
+ chain,
213
+ key,
214
+ name,
215
+ pollingInterval,
216
+ transport,
217
+ type: "testClient"
218
+ }),
219
+ mode
220
+ };
221
+ }
222
+
223
+ // src/clients/createWalletClient.ts
224
+ function createWalletClient({
225
+ transport,
226
+ key = "wallet",
227
+ name = "Wallet Client",
228
+ pollingInterval
229
+ }) {
230
+ return {
231
+ ...createClient({
232
+ key,
233
+ name,
234
+ pollingInterval,
235
+ transport,
236
+ type: "walletClient"
237
+ })
238
+ };
239
+ }
240
+
241
+ export {
242
+ createTransport,
243
+ UrlRequiredError,
244
+ ethereumProvider,
245
+ http,
246
+ webSocket,
247
+ createClient,
248
+ createPublicClient,
249
+ createTestClient,
250
+ createWalletClient
251
+ };
@@ -0,0 +1,8 @@
1
+ export { C as Client, a as ClientConfig, P as PublicClient, b as PublicClientConfig, T as TestClient, c as TestClientConfig, d as Transport, e as TransportConfig, W as WalletClient, f as WalletClientConfig, g as createClient, h as createPublicClient, i as createTestClient, j as createTransport, k as createWalletClient } from '../createWalletClient-c40fef16.js';
2
+ export { E as EthereumProviderTransport, a as EthereumProviderTransportConfig, H as HttpTransport, b as HttpTransportConfig, U as UrlRequiredError, W as WebSocketTransport, c as WebSocketTransportConfig, e as ethereumProvider, h as http, w as webSocket } from '../webSocket-14584a7e.js';
3
+ import '../chains.js';
4
+ import '../rpc-655c0ba4.js';
5
+ import '@wagmi/chains';
6
+ import '../eip1193-8f7c22ce.js';
7
+ import '../BaseError-7688f84e.js';
8
+ import '../rpc-3c0e3985.js';
@@ -0,0 +1,23 @@
1
+ import {
2
+ UrlRequiredError,
3
+ createClient,
4
+ createPublicClient,
5
+ createTestClient,
6
+ createTransport,
7
+ createWalletClient,
8
+ ethereumProvider,
9
+ http,
10
+ webSocket
11
+ } from "../chunk-OPR6LKYX.js";
12
+ import "../chunk-GI67STNV.js";
13
+ export {
14
+ UrlRequiredError,
15
+ createClient,
16
+ createPublicClient,
17
+ createTestClient,
18
+ createTransport,
19
+ createWalletClient,
20
+ ethereumProvider,
21
+ http,
22
+ webSocket
23
+ };
@@ -0,0 +1,130 @@
1
+ import { Chain } from './chains.js';
2
+ import { R as Requests, P as PublicRequests, T as TestRequests, S as SignableRequests, b as WalletRequests } from './eip1193-8f7c22ce.js';
3
+
4
+ type BaseRpcRequests = {
5
+ request(...args: any): Promise<any>;
6
+ };
7
+ type TransportConfig<TType extends string = string, TRequests extends BaseRpcRequests['request'] = Requests['request']> = {
8
+ /** The name of the transport. */
9
+ name: string;
10
+ /** The key of the transport. */
11
+ key: string;
12
+ /** The JSON-RPC request function that matches the EIP-1193 request spec. */
13
+ request: TRequests;
14
+ /** The type of the transport. */
15
+ type: TType;
16
+ };
17
+ type Transport<TType extends string = string, TRpcAttributes = Record<string, any>> = <TChain extends Chain = Chain>({ chain, }: {
18
+ chain?: TChain;
19
+ }) => {
20
+ config: TransportConfig<TType>;
21
+ value?: TRpcAttributes;
22
+ };
23
+ /**
24
+ * @description Creates an transport intended to be used with a client.
25
+ */
26
+ declare function createTransport<TType extends string = string, TRpcAttributes = any>(config: TransportConfig<TType>, value?: TRpcAttributes): ReturnType<Transport<TType, TRpcAttributes>>;
27
+
28
+ type Client<TTransport extends Transport = Transport, TChain extends Chain = Chain, TRequests extends BaseRpcRequests = Requests> = {
29
+ /** Chain for the client. */
30
+ chain?: TChain;
31
+ /** A key for the client. */
32
+ key: string;
33
+ /** A name for the client. */
34
+ name: string;
35
+ /** Frequency (in ms) for polling enabled actions & events. Defaults to 4_000 milliseconds. */
36
+ pollingInterval: number;
37
+ /** Request function wrapped with friendly error handling */
38
+ request: TRequests['request'];
39
+ /** The RPC transport (http, webSocket, ethereumProvider, etc) */
40
+ transport: ReturnType<TTransport>['config'] & ReturnType<TTransport>['value'];
41
+ /** The type of client. */
42
+ type: string;
43
+ /** A unique ID for the client. */
44
+ uid: string;
45
+ };
46
+ type ClientConfig<TTransport extends Transport = Transport, TChain extends Chain = Chain, TRequests extends BaseRpcRequests = Requests> = Partial<Pick<Client<TTransport, TChain, TRequests>, 'chain' | 'key' | 'name' | 'pollingInterval' | 'type'>> & {
47
+ transport: TTransport;
48
+ };
49
+ /**
50
+ * @description Creates a base RPC client with the given transport.
51
+ *
52
+ * - Intended to be used as a base for other RPC clients.
53
+ * - Has access to _all_ EIP-1474 RPC methods.
54
+ *
55
+ * @example
56
+ * import { mainnet } from 'viem/chains'
57
+ * import { createClient, http } from 'viem/clients'
58
+ * const client = createClient(http({ chain: mainnet }))
59
+ */
60
+ declare function createClient<TTransport extends Transport, TChain extends Chain, TRequests extends BaseRpcRequests>({ chain, key, name, pollingInterval, transport, type, }: ClientConfig<TTransport, TChain, TRequests>): Client<TTransport, TChain, TRequests>;
61
+
62
+ type PublicClientConfig<TTransport extends Transport = Transport, TChain extends Chain = Chain> = Pick<ClientConfig<TTransport, TChain>, 'chain' | 'key' | 'name' | 'pollingInterval' | 'transport'>;
63
+ type PublicClient<TTransport extends Transport = Transport, TChain extends Chain = Chain> = Client<TTransport, TChain, PublicRequests>;
64
+ /**
65
+ * @description Creates a public client with a given transport.
66
+ *
67
+ * - Only has access to "public" EIP-1474 RPC methods (ie. `eth_blockNumber`, etc).
68
+ *
69
+ * @example
70
+ * import { mainnet } from 'viem/chains'
71
+ * import { createPublicClient, http } from 'viem/clients'
72
+ * const client = createPublicClient(http({ chain: mainnet }))
73
+ */
74
+ declare function createPublicClient<TTransport extends Transport, TChain extends Chain>({ chain, key, name, transport, pollingInterval, }: PublicClientConfig<TTransport, TChain>): PublicClient<TTransport, TChain>;
75
+
76
+ type TestClientModes = 'anvil' | 'hardhat';
77
+ type TestClientConfig<TTransport extends Transport = Transport, TChain extends Chain = Chain, TMode extends TestClientModes = TestClientModes> = {
78
+ chain?: ClientConfig<TTransport, TChain>['chain'];
79
+ /** The key of the client. */
80
+ key?: ClientConfig['key'];
81
+ /** Mode of the test client. Available: "anvil" | "hardhat" */
82
+ mode: TMode;
83
+ /** The name of the client. */
84
+ name?: ClientConfig['name'];
85
+ /** Frequency (in ms) for polling enabled actions & events. Defaults to 4_000 milliseconds. */
86
+ pollingInterval?: ClientConfig['pollingInterval'];
87
+ transport: ClientConfig<TTransport, TChain>['transport'];
88
+ };
89
+ type TestClient<TTransport extends Transport = Transport, TChain extends Chain = Chain, TMode extends TestClientModes = TestClientModes> = Client<TTransport, TChain, TestRequests<TMode>> & {
90
+ mode: TMode;
91
+ };
92
+ /**
93
+ * @description Creates a test client with a given transport.
94
+ *
95
+ * - Only has access to "test" RPC methods (ie. `anvil_setBalance`,
96
+ * `evm_mine`, etc).
97
+ *
98
+ * @example
99
+ * import { local } from 'viem/chains'
100
+ * import { createTestClient, http } from 'viem/clients'
101
+ * const client = createTestClient(http({ chain: local }), { mode: 'anvil' })
102
+ */
103
+ declare function createTestClient<TTransport extends Transport, TChain extends Chain, TMode extends TestClientModes>({ chain, key, name, mode, pollingInterval, transport, }: TestClientConfig<TTransport, TChain, TMode>): TestClient<TTransport, TChain, TMode>;
104
+
105
+ type WalletClientConfig<TTransport extends Transport = Transport, TChain extends Chain = Chain> = {
106
+ chain?: ClientConfig<TTransport, TChain>['chain'];
107
+ /** The key of the Wallet Client. */
108
+ key?: ClientConfig['key'];
109
+ /** The name of the Wallet Client. */
110
+ name?: ClientConfig['name'];
111
+ /** Frequency (in ms) for polling enabled actions & events. Defaults to 4_000 milliseconds. */
112
+ pollingInterval?: ClientConfig['pollingInterval'];
113
+ transport: ClientConfig<TTransport, TChain>['transport'];
114
+ };
115
+ type WalletClient<TTransport extends Transport = Transport, TChain extends Chain = Chain> = Client<TTransport, TChain, SignableRequests & WalletRequests>;
116
+ /**
117
+ * @description Creates a wallet client with a given transport.
118
+ *
119
+ * - Only has access to "wallet" & "signable" EIP-1474 RPC methods
120
+ * (ie. `eth_sendTransaction`, `eth_requestAccounts`, etc).
121
+ *
122
+ * @example
123
+ * import { createWalletClient, ethereumProvider } from 'viem/clients'
124
+ * const client = createWalletClient(
125
+ * ethereumProvider({ provider: window.ethereum })
126
+ * )
127
+ */
128
+ declare function createWalletClient<TTransport extends Transport, TChain extends Chain>({ transport, key, name, pollingInterval, }: WalletClientConfig<TTransport, TChain>): WalletClient<TTransport, TChain>;
129
+
130
+ export { BaseRpcRequests as B, Client as C, PublicClient as P, TestClient as T, WalletClient as W, ClientConfig as a, PublicClientConfig as b, TestClientConfig as c, Transport as d, TransportConfig as e, WalletClientConfig as f, createClient as g, createPublicClient as h, createTestClient as i, createTransport as j, createWalletClient as k };