viem 0.0.1-alpha.1 → 0.0.1-alpha.10

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.
@@ -1,7 +1,13 @@
1
1
  import {
2
2
  BaseError,
3
- __publicField,
3
+ BlockNotFoundError,
4
+ InvalidGasArgumentsError,
5
+ TransactionNotFoundError,
6
+ TransactionReceiptNotFoundError,
7
+ WaitForTransactionReceiptTimeoutError,
4
8
  checksumAddress,
9
+ decodeFunctionResult,
10
+ encodeFunctionData,
5
11
  encodeHex,
6
12
  format,
7
13
  formatBlock,
@@ -12,145 +18,12 @@ import {
12
18
  formatTransactionRequest,
13
19
  getAddress,
14
20
  getCache,
21
+ getContractError,
15
22
  hexToNumber,
16
23
  numberToHex,
17
24
  wait,
18
25
  withCache
19
- } from "./chunk-Z6LRV6XI.js";
20
-
21
- // src/actions/wallet/addChain.ts
22
- async function addChain(client, chain) {
23
- const { id, name, nativeCurrency, rpcUrls, blockExplorers } = chain;
24
- await client.request({
25
- method: "wallet_addEthereumChain",
26
- params: [
27
- {
28
- chainId: numberToHex(id),
29
- chainName: name,
30
- nativeCurrency,
31
- rpcUrls: rpcUrls.default.http,
32
- blockExplorerUrls: blockExplorers ? Object.values(blockExplorers).map(({ url }) => url) : void 0
33
- }
34
- ]
35
- });
36
- }
37
-
38
- // src/actions/wallet/getAccounts.ts
39
- async function getAccounts(client) {
40
- const addresses = await client.request({ method: "eth_accounts" });
41
- return addresses.map((address) => checksumAddress(address));
42
- }
43
-
44
- // src/actions/wallet/getPermissions.ts
45
- async function getPermissions(client) {
46
- const permissions = await client.request({ method: "wallet_getPermissions" });
47
- return permissions;
48
- }
49
-
50
- // src/actions/wallet/requestAccounts.ts
51
- async function requestAccounts(client) {
52
- const addresses = await client.request({ method: "eth_requestAccounts" });
53
- return addresses.map((address) => getAddress(address));
54
- }
55
-
56
- // src/actions/wallet/requestPermissions.ts
57
- async function requestPermissions(client, permissions) {
58
- return client.request({
59
- method: "wallet_requestPermissions",
60
- params: [permissions]
61
- });
62
- }
63
-
64
- // src/actions/wallet/sendTransaction.ts
65
- async function sendTransaction(client, {
66
- chain,
67
- from,
68
- accessList,
69
- data,
70
- gas,
71
- gasPrice,
72
- maxFeePerGas,
73
- maxPriorityFeePerGas,
74
- nonce,
75
- to,
76
- value,
77
- ...rest
78
- }) {
79
- if (maxFeePerGas !== void 0 && maxPriorityFeePerGas !== void 0 && maxFeePerGas < maxPriorityFeePerGas)
80
- throw new InvalidGasArgumentsError();
81
- const request_ = format(
82
- {
83
- from,
84
- accessList,
85
- data,
86
- gas,
87
- gasPrice,
88
- maxFeePerGas,
89
- maxPriorityFeePerGas,
90
- nonce,
91
- to,
92
- value,
93
- ...rest
94
- },
95
- {
96
- formatter: chain?.formatters?.transactionRequest || formatTransactionRequest
97
- }
98
- );
99
- const hash = await client.request({
100
- method: "eth_sendTransaction",
101
- params: [request_]
102
- });
103
- return hash;
104
- }
105
- var InvalidGasArgumentsError = class extends BaseError {
106
- constructor() {
107
- super("`maxFeePerGas` cannot be less than `maxPriorityFeePerGas`");
108
- __publicField(this, "name", "InvalidGasArgumentsError");
109
- }
110
- };
111
-
112
- // src/actions/wallet/signMessage.ts
113
- async function signMessage(client, { from, data: data_ }) {
114
- let data;
115
- if (typeof data_ === "string") {
116
- if (!data_.startsWith("0x"))
117
- throw new BaseError(
118
- `data ("${data_}") must be a hex value. Encode it first to a hex with the \`encodeHex\` util.`,
119
- {
120
- docsPath: "/TODO"
121
- }
122
- );
123
- data = data_;
124
- } else {
125
- data = encodeHex(data_);
126
- }
127
- const signed = await client.request({
128
- method: "personal_sign",
129
- params: [data, from]
130
- });
131
- return signed;
132
- }
133
-
134
- // src/actions/wallet/switchChain.ts
135
- async function switchChain(client, { id }) {
136
- await client.request({
137
- method: "wallet_switchEthereumChain",
138
- params: [
139
- {
140
- chainId: numberToHex(id)
141
- }
142
- ]
143
- });
144
- }
145
-
146
- // src/actions/wallet/watchAsset.ts
147
- async function watchAsset(client, params) {
148
- const added = await client.request({
149
- method: "wallet_watchAsset",
150
- params: [params]
151
- });
152
- return added;
153
- }
26
+ } from "./chunk-6GAKRM5P.js";
154
27
 
155
28
  // src/actions/public/call.ts
156
29
  async function call(client, {
@@ -199,6 +72,41 @@ async function call(client, {
199
72
  return { data: response };
200
73
  }
201
74
 
75
+ // src/actions/public/callContract.ts
76
+ async function callContract(client, {
77
+ abi,
78
+ address,
79
+ args,
80
+ functionName,
81
+ ...callRequest
82
+ }) {
83
+ const calldata = encodeFunctionData({
84
+ abi,
85
+ args,
86
+ functionName
87
+ });
88
+ try {
89
+ const { data } = await call(client, {
90
+ data: calldata,
91
+ to: address,
92
+ ...callRequest
93
+ });
94
+ return decodeFunctionResult({
95
+ abi,
96
+ functionName,
97
+ data: data || "0x"
98
+ });
99
+ } catch (err) {
100
+ throw getContractError(err, {
101
+ abi,
102
+ address,
103
+ args,
104
+ functionName,
105
+ sender: callRequest.from
106
+ });
107
+ }
108
+ }
109
+
202
110
  // src/actions/public/createPendingTransactionFilter.ts
203
111
  async function createPendingTransactionFilter(client) {
204
112
  const id = await client.request({
@@ -282,20 +190,6 @@ async function getBlock(client, {
282
190
  formatter: client.chain?.formatters?.block || formatBlock
283
191
  });
284
192
  }
285
- var BlockNotFoundError = class extends BaseError {
286
- constructor({
287
- blockHash,
288
- blockNumber
289
- }) {
290
- let identifier = "Block";
291
- if (blockHash)
292
- identifier = `Block at hash "${blockHash}"`;
293
- if (blockNumber)
294
- identifier = `Block at number "${blockNumber}"`;
295
- super(`${identifier} could not be found.`);
296
- __publicField(this, "name", "BlockNotFoundError");
297
- }
298
- };
299
193
 
300
194
  // src/actions/public/getBlockNumber.ts
301
195
  var cacheKey = (id) => `blockNumber.${id}`;
@@ -425,27 +319,6 @@ async function getTransaction(client, {
425
319
  formatter: client.chain?.formatters?.transaction || formatTransaction
426
320
  });
427
321
  }
428
- var TransactionNotFoundError = class extends BaseError {
429
- constructor({
430
- blockHash,
431
- blockNumber,
432
- blockTag,
433
- hash,
434
- index
435
- }) {
436
- let identifier = "Transaction";
437
- if (blockTag && index !== void 0)
438
- identifier = `Transaction at block time "${blockTag}" at index "${index}"`;
439
- if (blockHash && index !== void 0)
440
- identifier = `Transaction at block hash "${blockHash}" at index "${index}"`;
441
- if (blockNumber && index !== void 0)
442
- identifier = `Transaction at block number "${blockNumber}" at index "${index}"`;
443
- if (hash)
444
- identifier = `Transaction with hash "${hash}"`;
445
- super(`${identifier} could not be found.`);
446
- __publicField(this, "name", "TransactionNotFoundError");
447
- }
448
- };
449
322
 
450
323
  // src/actions/public/getTransactionConfirmations.ts
451
324
  async function getTransactionConfirmations(client, { hash, transactionReceipt }) {
@@ -465,7 +338,7 @@ async function getTransactionCount(client, { address, blockTag = "latest", block
465
338
  method: "eth_getTransactionCount",
466
339
  params: [address, blockNumber ? numberToHex(blockNumber) : blockTag]
467
340
  });
468
- return hexToNumber(count ?? "0x0");
341
+ return hexToNumber(count);
469
342
  }
470
343
 
471
344
  // src/actions/public/getTransactionReceipt.ts
@@ -480,14 +353,6 @@ async function getTransactionReceipt(client, { hash }) {
480
353
  formatter: client.chain?.formatters?.transactionReceipt || formatTransactionReceipt
481
354
  });
482
355
  }
483
- var TransactionReceiptNotFoundError = class extends BaseError {
484
- constructor({ hash }) {
485
- super(
486
- `Transaction receipt with hash "${hash}" could not be found. The Transaction may not be processed on a block yet.`
487
- );
488
- __publicField(this, "name", "TransactionReceiptNotFoundError");
489
- }
490
- };
491
356
 
492
357
  // src/actions/public/uninstallFilter.ts
493
358
  async function uninstallFilter(client, { filter }) {
@@ -630,14 +495,6 @@ async function waitForTransactionReceipt(client, {
630
495
  );
631
496
  });
632
497
  }
633
- var WaitForTransactionReceiptTimeoutError = class extends BaseError {
634
- constructor({ hash }) {
635
- super(
636
- `Timed out while waiting for transaction with hash "${hash}" to be confirmed.`
637
- );
638
- __publicField(this, "name", "WaitForTransactionReceiptTimeoutError");
639
- }
640
- };
641
498
 
642
499
  // src/utils/poll.ts
643
500
  function poll(fn, { emitOnBegin, initialWaitTime, interval }) {
@@ -1035,17 +892,137 @@ async function stopImpersonatingAccount(client, { address }) {
1035
892
  });
1036
893
  }
1037
894
 
895
+ // src/actions/wallet/addChain.ts
896
+ async function addChain(client, chain) {
897
+ const { id, name, nativeCurrency, rpcUrls, blockExplorers } = chain;
898
+ await client.request({
899
+ method: "wallet_addEthereumChain",
900
+ params: [
901
+ {
902
+ chainId: numberToHex(id),
903
+ chainName: name,
904
+ nativeCurrency,
905
+ rpcUrls: rpcUrls.default.http,
906
+ blockExplorerUrls: blockExplorers ? Object.values(blockExplorers).map(({ url }) => url) : void 0
907
+ }
908
+ ]
909
+ });
910
+ }
911
+
912
+ // src/actions/wallet/getAccounts.ts
913
+ async function getAccounts(client) {
914
+ const addresses = await client.request({ method: "eth_accounts" });
915
+ return addresses.map((address) => checksumAddress(address));
916
+ }
917
+
918
+ // src/actions/wallet/getPermissions.ts
919
+ async function getPermissions(client) {
920
+ const permissions = await client.request({ method: "wallet_getPermissions" });
921
+ return permissions;
922
+ }
923
+
924
+ // src/actions/wallet/requestAccounts.ts
925
+ async function requestAccounts(client) {
926
+ const addresses = await client.request({ method: "eth_requestAccounts" });
927
+ return addresses.map((address) => getAddress(address));
928
+ }
929
+
930
+ // src/actions/wallet/requestPermissions.ts
931
+ async function requestPermissions(client, permissions) {
932
+ return client.request({
933
+ method: "wallet_requestPermissions",
934
+ params: [permissions]
935
+ });
936
+ }
937
+
938
+ // src/actions/wallet/sendTransaction.ts
939
+ async function sendTransaction(client, {
940
+ chain,
941
+ from,
942
+ accessList,
943
+ data,
944
+ gas,
945
+ gasPrice,
946
+ maxFeePerGas,
947
+ maxPriorityFeePerGas,
948
+ nonce,
949
+ to,
950
+ value,
951
+ ...rest
952
+ }) {
953
+ if (maxFeePerGas !== void 0 && maxPriorityFeePerGas !== void 0 && maxFeePerGas < maxPriorityFeePerGas)
954
+ throw new InvalidGasArgumentsError();
955
+ const request_ = format(
956
+ {
957
+ from,
958
+ accessList,
959
+ data,
960
+ gas,
961
+ gasPrice,
962
+ maxFeePerGas,
963
+ maxPriorityFeePerGas,
964
+ nonce,
965
+ to,
966
+ value,
967
+ ...rest
968
+ },
969
+ {
970
+ formatter: chain?.formatters?.transactionRequest || formatTransactionRequest
971
+ }
972
+ );
973
+ const hash = await client.request({
974
+ method: "eth_sendTransaction",
975
+ params: [request_]
976
+ });
977
+ return hash;
978
+ }
979
+
980
+ // src/actions/wallet/signMessage.ts
981
+ async function signMessage(client, { from, data: data_ }) {
982
+ let data;
983
+ if (typeof data_ === "string") {
984
+ if (!data_.startsWith("0x"))
985
+ throw new BaseError(
986
+ `data ("${data_}") must be a hex value. Encode it first to a hex with the \`encodeHex\` util.`,
987
+ {
988
+ docsPath: "/TODO"
989
+ }
990
+ );
991
+ data = data_;
992
+ } else {
993
+ data = encodeHex(data_);
994
+ }
995
+ const signed = await client.request({
996
+ method: "personal_sign",
997
+ params: [data, from]
998
+ });
999
+ return signed;
1000
+ }
1001
+
1002
+ // src/actions/wallet/switchChain.ts
1003
+ async function switchChain(client, { id }) {
1004
+ await client.request({
1005
+ method: "wallet_switchEthereumChain",
1006
+ params: [
1007
+ {
1008
+ chainId: numberToHex(id)
1009
+ }
1010
+ ]
1011
+ });
1012
+ }
1013
+
1014
+ // src/actions/wallet/watchAsset.ts
1015
+ async function watchAsset(client, params) {
1016
+ const added = await client.request({
1017
+ method: "wallet_watchAsset",
1018
+ params: [params]
1019
+ });
1020
+ return added;
1021
+ }
1022
+
1038
1023
  export {
1039
- addChain,
1040
- getAccounts,
1041
- getPermissions,
1042
- requestAccounts,
1043
- requestPermissions,
1044
- sendTransaction,
1045
- signMessage,
1046
- switchChain,
1047
- watchAsset,
1048
1024
  call,
1025
+ callContract,
1049
1026
  createPendingTransactionFilter,
1050
1027
  createBlockFilter,
1051
1028
  estimateGas,
@@ -1094,5 +1071,14 @@ export {
1094
1071
  setNonce,
1095
1072
  setStorageAt,
1096
1073
  snapshot,
1097
- stopImpersonatingAccount
1074
+ stopImpersonatingAccount,
1075
+ addChain,
1076
+ getAccounts,
1077
+ getPermissions,
1078
+ requestAccounts,
1079
+ requestPermissions,
1080
+ sendTransaction,
1081
+ signMessage,
1082
+ switchChain,
1083
+ watchAsset
1098
1084
  };
@@ -1,8 +1,7 @@
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-915223f3.js';
2
- export { C as CustomTransport, a as CustomTransportConfig, F as FallbackTransport, b as FallbackTransportConfig, H as HttpTransport, c as HttpTransportConfig, U as UrlRequiredError, W as WebSocketTransport, d as WebSocketTransportConfig, e as custom, f as fallback, h as http, w as webSocket } from '../webSocket-c6e0d26f.js';
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-d612fe08.js';
2
+ export { C as CustomTransport, a as CustomTransportConfig, F as FallbackTransport, b as FallbackTransportConfig, H as HttpTransport, c as HttpTransportConfig, W as WebSocketTransport, d as WebSocketTransportConfig, e as custom, f as fallback, h as http, w as webSocket } from '../webSocket-7f88e9e0.js';
3
3
  import '../chains.js';
4
- import '../rpc-655c0ba4.js';
4
+ import '../rpc-b77c5aee.js';
5
5
  import '@wagmi/chains';
6
- import '../eip1193-8f7c22ce.js';
7
- import '../BaseError-7688f84e.js';
8
- import '../rpc-3c0e3985.js';
6
+ import '../eip1193-020a6f13.js';
7
+ import '../rpc-26932bae.js';
@@ -1,5 +1,4 @@
1
1
  import {
2
- UrlRequiredError,
3
2
  createClient,
4
3
  createPublicClient,
5
4
  createTestClient,
@@ -9,10 +8,9 @@ import {
9
8
  fallback,
10
9
  http,
11
10
  webSocket
12
- } from "../chunk-OQTFTQTO.js";
13
- import "../chunk-Z6LRV6XI.js";
11
+ } from "../chunk-3TSTZHVO.js";
12
+ import "../chunk-6GAKRM5P.js";
14
13
  export {
15
- UrlRequiredError,
16
14
  createClient,
17
15
  createPublicClient,
18
16
  createTestClient,
@@ -1,5 +1,5 @@
1
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';
2
+ import { R as Requests, P as PublicRequests, T as TestRequests, S as SignableRequests, b as WalletRequests } from './eip1193-020a6f13.js';
3
3
 
4
4
  type BaseRpcRequests = {
5
5
  request(...args: any): Promise<any>;
@@ -1,4 +1,4 @@
1
- import { A as Address, k as Hash, Q as Quantity, s as RpcTransactionRequest, m as RpcBlockNumber, d as BlockTag, l as RpcBlockIdentifier, H as Hex, P as RpcEstimateGasParameters, n as RpcFeeHistory, R as RpcBlock, p as RpcLog, q as RpcTransaction, r as RpcTransactionReceipt, u as RpcUncle } from './rpc-655c0ba4.js';
1
+ import { A as Address, a as Hash, Q as Quantity, s as RpcTransactionRequest, m as RpcBlockNumber, b as BlockTag, l as RpcBlockIdentifier, H as Hex, P as RpcEstimateGasParameters, n as RpcFeeHistory, R as RpcBlock, p as RpcLog, q as RpcTransaction, r as RpcTransactionReceipt, u as RpcUncle } from './rpc-b77c5aee.js';
2
2
 
3
3
  declare class RpcError extends Error {
4
4
  code: number;
@@ -405,7 +405,7 @@ type PublicRequests = {
405
405
  * */
406
406
  method: 'eth_getTransactionCount';
407
407
  params: [address: Address, block: RpcBlockNumber | BlockTag | RpcBlockIdentifier];
408
- }): Promise<Quantity | null>;
408
+ }): Promise<Quantity>;
409
409
  request(args: {
410
410
  /**
411
411
  * @description Returns the receipt of a transaction specified by hash