viem 0.0.1-alpha.34 → 0.0.1-alpha.35

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.
Files changed (71) hide show
  1. package/abi/package.json +4 -0
  2. package/dist/abi.d.ts +5 -0
  3. package/dist/abi.js +11 -0
  4. package/dist/abi.js.map +1 -0
  5. package/dist/abi.mjs +11 -0
  6. package/dist/abi.mjs.map +1 -0
  7. package/dist/{chain-ce6e149c.d.ts → chain-145cfaf3.d.ts} +1 -1
  8. package/dist/{chain-4a0d4164.d.ts → chain-7b7ac245.d.ts} +2 -2
  9. package/dist/chains.d.ts +3 -3
  10. package/dist/chains.js +46 -46
  11. package/dist/chains.mjs +1 -1
  12. package/dist/{chunk-7ALCXZ5K.js → chunk-3FT7UKPT.js} +75 -75
  13. package/dist/chunk-3FT7UKPT.js.map +1 -0
  14. package/dist/{chunk-EJEZIHXK.js → chunk-57YM6E7I.js} +22 -22
  15. package/dist/{chunk-KQQLLKZV.mjs → chunk-5NDMORWZ.mjs} +3 -3
  16. package/dist/{chunk-IUCGWGPX.js → chunk-A665BF7O.js} +24 -16
  17. package/dist/chunk-A665BF7O.js.map +1 -0
  18. package/dist/{chunk-F76TECTY.js → chunk-HQCSH22G.js} +126 -82
  19. package/dist/chunk-HQCSH22G.js.map +1 -0
  20. package/dist/{chunk-VP57P6CD.mjs → chunk-PBW4YL37.mjs} +87 -43
  21. package/dist/chunk-PBW4YL37.mjs.map +1 -0
  22. package/dist/{chunk-TNN5X44H.mjs → chunk-PH5LCMME.mjs} +10 -2
  23. package/dist/chunk-PH5LCMME.mjs.map +1 -0
  24. package/dist/{chunk-EBLS4P77.js → chunk-TJK6AFBM.js} +14 -14
  25. package/dist/{chunk-EBLS4P77.js.map → chunk-TJK6AFBM.js.map} +1 -1
  26. package/dist/{chunk-VHZHROXW.mjs → chunk-WQDVW7EJ.mjs} +2 -2
  27. package/dist/chunk-WQDVW7EJ.mjs.map +1 -0
  28. package/dist/{chunk-NMET3ZHG.mjs → chunk-YEVW7GK5.mjs} +2 -2
  29. package/dist/{chunk-NMET3ZHG.mjs.map → chunk-YEVW7GK5.mjs.map} +1 -1
  30. package/dist/contract.d.ts +7 -8
  31. package/dist/contract.js +4 -4
  32. package/dist/contract.mjs +10 -10
  33. package/dist/{createClient-8a8b4bf4.d.ts → createClient-7c715e2c.d.ts} +2 -2
  34. package/dist/{createPublicClient-b5afe6b8.d.ts → createPublicClient-4d24afb2.d.ts} +122 -9
  35. package/dist/{eip1193-8c021438.d.ts → eip1193-d538ae6d.d.ts} +11 -1
  36. package/dist/{getAbiItem-d7aa40b1.d.ts → encodeFunctionResult-f59f4cfe.d.ts} +3 -25
  37. package/dist/ens.d.ts +4 -5
  38. package/dist/ens.js +4 -4
  39. package/dist/ens.mjs +3 -3
  40. package/dist/{formatAbiItem-6e00e5e6.d.ts → formatAbiItem-89d97367.d.ts} +1 -1
  41. package/dist/getAbiItem-c9cbc8b8.d.ts +22 -0
  42. package/dist/index.d.ts +12 -13
  43. package/dist/index.js +91 -90
  44. package/dist/index.js.map +1 -1
  45. package/dist/index.mjs +24 -23
  46. package/dist/index.mjs.map +1 -1
  47. package/dist/{parseGwei-ce3a9dfa.d.ts → parseGwei-ad6d1914.d.ts} +5 -6
  48. package/dist/public.d.ts +5 -6
  49. package/dist/public.js +3 -3
  50. package/dist/public.mjs +2 -2
  51. package/dist/{test-5d5828c9.d.ts → test-8646fb50.d.ts} +9 -9
  52. package/dist/test.d.ts +5 -5
  53. package/dist/test.js +3 -3
  54. package/dist/test.mjs +2 -2
  55. package/dist/utils/index.d.ts +12 -14
  56. package/dist/utils/index.js +2 -2
  57. package/dist/utils/index.mjs +5 -5
  58. package/dist/wallet.d.ts +5 -5
  59. package/dist/wallet.js +4 -3
  60. package/dist/wallet.mjs +3 -2
  61. package/dist/window.d.ts +2 -2
  62. package/package.json +8 -2
  63. package/dist/chunk-7ALCXZ5K.js.map +0 -1
  64. package/dist/chunk-F76TECTY.js.map +0 -1
  65. package/dist/chunk-IUCGWGPX.js.map +0 -1
  66. package/dist/chunk-TNN5X44H.mjs.map +0 -1
  67. package/dist/chunk-VHZHROXW.mjs.map +0 -1
  68. package/dist/chunk-VP57P6CD.mjs.map +0 -1
  69. package/dist/wallet-5ab86528.d.ts +0 -115
  70. /package/dist/{chunk-EJEZIHXK.js.map → chunk-57YM6E7I.js.map} +0 -0
  71. /package/dist/{chunk-KQQLLKZV.mjs.map → chunk-5NDMORWZ.mjs.map} +0 -0
@@ -9,7 +9,7 @@ var __publicField = (obj, key, value) => {
9
9
  var package_default = {
10
10
  name: "viem",
11
11
  description: "TypeScript Interface for Ethereum",
12
- version: "0.0.1-alpha.34",
12
+ version: "0.0.1-alpha.35",
13
13
  scripts: {
14
14
  anvil: "source .env && anvil --fork-url $VITE_ANVIL_FORK_URL --fork-block-number $VITE_ANVIL_BLOCK_NUMBER --block-time $VITE_ANVIL_BLOCK_TIME",
15
15
  bench: "vitest bench --no-threads",
@@ -40,6 +40,7 @@ var package_default = {
40
40
  tsup: {
41
41
  entry: [
42
42
  "src/index.ts",
43
+ "src/abi.ts",
43
44
  "src/chains.ts",
44
45
  "src/contract.ts",
45
46
  "src/ens.ts",
@@ -51,6 +52,7 @@ var package_default = {
51
52
  ]
52
53
  },
53
54
  files: [
55
+ "/abi",
54
56
  "/dist",
55
57
  "/chains",
56
58
  "/contract",
@@ -67,6 +69,11 @@ var package_default = {
67
69
  module: "./dist/index.mts",
68
70
  default: "./dist/index.js"
69
71
  },
72
+ "./abi": {
73
+ types: "./dist/abi.d.ts",
74
+ module: "./dist/abi.mts",
75
+ default: "./dist/abi.js"
76
+ },
70
77
  "./chains": {
71
78
  types: "./dist/chains.d.ts",
72
79
  module: "./dist/chains.mts",
@@ -116,7 +123,7 @@ var package_default = {
116
123
  dependencies: {
117
124
  "@noble/hashes": "^1.1.2",
118
125
  "@wagmi/chains": "~0.2.8",
119
- abitype: "~0.3.0",
126
+ abitype: "~0.5.0",
120
127
  "idna-uts46-hx": "^4.1.2",
121
128
  "isomorphic-ws": "^5.0.0",
122
129
  ws: "^8.12.0"
@@ -268,6 +275,16 @@ var AbiEncodingArrayLengthMismatchError = class extends BaseError {
268
275
  __publicField(this, "name", "AbiEncodingArrayLengthMismatchError");
269
276
  }
270
277
  };
278
+ var AbiEncodingBytesSizeMismatchError = class extends BaseError {
279
+ constructor({ expectedSize, value }) {
280
+ super(
281
+ `Size of bytes "${value}" (bytes${size(
282
+ value
283
+ )}) does not match expected size (bytes${expectedSize}).`
284
+ );
285
+ __publicField(this, "name", "AbiEncodingBytesSizeMismatchError");
286
+ }
287
+ };
271
288
  var AbiEncodingLengthMismatchError = class extends BaseError {
272
289
  constructor({
273
290
  expectedLength,
@@ -489,6 +506,23 @@ var ChainDoesNotSupportContract = class extends BaseError {
489
506
  __publicField(this, "name", "ChainDoesNotSupportContract");
490
507
  }
491
508
  };
509
+ var ChainMismatchError = class extends BaseError {
510
+ constructor({
511
+ chain,
512
+ currentChainId
513
+ }) {
514
+ super(
515
+ `The current chain (id: ${currentChainId}) does not match the chain passed to the request (id: ${chain.id} \u2013 ${chain.name}).`,
516
+ {
517
+ metaMessages: [
518
+ `Current Chain ID: ${currentChainId}`,
519
+ `Expected Chain ID: ${chain.id} \u2013 ${chain.name}`
520
+ ]
521
+ }
522
+ );
523
+ __publicField(this, "name", "ChainMismatchError");
524
+ }
525
+ };
492
526
 
493
527
  // src/constants/abis.ts
494
528
  var multicall3Abi = [
@@ -593,6 +627,7 @@ var TransactionExecutionError = class extends BaseError {
593
627
  value
594
628
  }) {
595
629
  const prettyArgs = prettyPrint({
630
+ chain: chain && `${chain?.name} (id: ${chain?.id})`,
596
631
  from,
597
632
  to,
598
633
  value: typeof value !== "undefined" && `${formatEther(value)} ${chain?.nativeCurrency.symbol || "ETH"}`,
@@ -1517,11 +1552,6 @@ function stringToHex(value) {
1517
1552
 
1518
1553
  // src/utils/encoding/toBytes.ts
1519
1554
  var encoder = new TextEncoder();
1520
- function boolToBytes(value) {
1521
- const bytes = new Uint8Array(1);
1522
- bytes[0] = Number(value);
1523
- return bytes;
1524
- }
1525
1555
  function toBytes(value) {
1526
1556
  if (typeof value === "number" || typeof value === "bigint")
1527
1557
  return numberToBytes(value);
@@ -1531,6 +1561,11 @@ function toBytes(value) {
1531
1561
  return hexToBytes(value);
1532
1562
  return stringToBytes(value);
1533
1563
  }
1564
+ function boolToBytes(value) {
1565
+ const bytes = new Uint8Array(1);
1566
+ bytes[0] = Number(value);
1567
+ return bytes;
1568
+ }
1534
1569
  function hexToBytes(hex_) {
1535
1570
  let hex = hex_.slice(2);
1536
1571
  if (hex.length % 2)
@@ -1541,7 +1576,7 @@ function hexToBytes(hex_) {
1541
1576
  const hexByte = hex.slice(start, start + 2);
1542
1577
  const byte = Number.parseInt(hexByte, 16);
1543
1578
  if (Number.isNaN(byte) || byte < 0)
1544
- throw new Error("Invalid byte sequence");
1579
+ throw new BaseError(`Invalid byte sequence ("${hexByte}" in "${hex}").`);
1545
1580
  bytes[index] = byte;
1546
1581
  }
1547
1582
  return bytes;
@@ -1847,8 +1882,8 @@ function isAddressEqual(a, b) {
1847
1882
  return a.toLowerCase() === b.toLowerCase();
1848
1883
  }
1849
1884
 
1850
- // src/utils/abi/encodeAbi.ts
1851
- function encodeAbi({ params, values }) {
1885
+ // src/utils/abi/encodeAbiParameters.ts
1886
+ function encodeAbiParameters(params, values) {
1852
1887
  if (params.length !== values.length)
1853
1888
  throw new AbiEncodingLengthMismatchError({
1854
1889
  expectedLength: params.length,
@@ -1898,13 +1933,13 @@ function prepareParam({
1898
1933
  return encodeNumber(value, { signed });
1899
1934
  }
1900
1935
  if (param.type.startsWith("bytes")) {
1901
- return toBytes2(value, { param });
1936
+ return encodeBytes(value, { param });
1902
1937
  }
1903
1938
  if (param.type === "string") {
1904
1939
  return encodeString(value);
1905
1940
  }
1906
1941
  throw new InvalidAbiEncodingTypeError(param.type, {
1907
- docsPath: "/docs/contract/encodeAbi"
1942
+ docsPath: "/docs/contract/encodeAbiParameters"
1908
1943
  });
1909
1944
  }
1910
1945
  function encodeParams(preparedParams) {
@@ -1932,6 +1967,8 @@ function encodeParams(preparedParams) {
1932
1967
  return concat([...staticParams, ...dynamicParams]);
1933
1968
  }
1934
1969
  function encodeAddress(value) {
1970
+ if (!isAddress(value))
1971
+ throw new InvalidAddressError({ address: value });
1935
1972
  return { dynamic: false, encoded: padHex(value.toLowerCase()) };
1936
1973
  }
1937
1974
  function encodeArray(value, {
@@ -1972,13 +2009,17 @@ function encodeArray(value, {
1972
2009
  encoded: concat(preparedParams.map(({ encoded }) => encoded))
1973
2010
  };
1974
2011
  }
1975
- function toBytes2(value, { param }) {
2012
+ function encodeBytes(value, { param }) {
1976
2013
  const [_, size_] = param.type.split("bytes");
1977
2014
  if (!size_) {
1978
2015
  const partsLength = Math.floor(size(value) / 32);
1979
2016
  const parts = [];
1980
2017
  for (let i = 0; i < partsLength + 1; i++) {
1981
- parts.push(padHex(slice(value, i * 32, (i + 1) * 32), { dir: "right" }));
2018
+ parts.push(
2019
+ padHex(slice(value, i * 32, (i + 1) * 32), {
2020
+ dir: "right"
2021
+ })
2022
+ );
1982
2023
  }
1983
2024
  return {
1984
2025
  dynamic: true,
@@ -1988,6 +2029,11 @@ function toBytes2(value, { param }) {
1988
2029
  ])
1989
2030
  };
1990
2031
  }
2032
+ if (size(value) !== parseInt(size_))
2033
+ throw new AbiEncodingBytesSizeMismatchError({
2034
+ expectedSize: parseInt(size_),
2035
+ value
2036
+ });
1991
2037
  return { dynamic: false, encoded: padHex(value, { dir: "right" }) };
1992
2038
  }
1993
2039
  function encodeBool(value) {
@@ -2038,19 +2084,16 @@ function getArrayComponents(type) {
2038
2084
  ) : void 0;
2039
2085
  }
2040
2086
 
2041
- // src/utils/abi/decodeAbi.ts
2042
- function decodeAbi({ data, params }) {
2087
+ // src/utils/abi/decodeAbiParameters.ts
2088
+ function decodeAbiParameters(params, data) {
2043
2089
  if (data === "0x" && params.length > 0)
2044
2090
  throw new AbiDecodingZeroDataError();
2045
2091
  if (size(data) % 32 !== 0)
2046
2092
  throw new AbiDecodingDataSizeInvalidError(size(data));
2047
- const values = decodeParams({
2093
+ return decodeParams({
2048
2094
  data,
2049
2095
  params
2050
2096
  });
2051
- if (values.length === 0)
2052
- return void 0;
2053
- return values;
2054
2097
  }
2055
2098
  function decodeParams({
2056
2099
  data,
@@ -2087,7 +2130,7 @@ function decodeParam({
2087
2130
  return decodeString(data, { position });
2088
2131
  }
2089
2132
  if (param.type.startsWith("bytes")) {
2090
- return fromBytes2(data, { param, position });
2133
+ return decodeBytes(data, { param, position });
2091
2134
  }
2092
2135
  let value = slice(data, position, position + 32);
2093
2136
  if (param.type.startsWith("uint") || param.type.startsWith("int")) {
@@ -2100,7 +2143,7 @@ function decodeParam({
2100
2143
  return decodeBool(value);
2101
2144
  }
2102
2145
  throw new InvalidAbiDecodingTypeError(param.type, {
2103
- docsPath: "/docs/contract/decodeAbi"
2146
+ docsPath: "/docs/contract/decodeAbiParameters"
2104
2147
  });
2105
2148
  }
2106
2149
  function decodeAddress(value) {
@@ -2160,7 +2203,7 @@ function decodeArray(data, {
2160
2203
  function decodeBool(value) {
2161
2204
  return { consumed: 32, value: hexToBool(value) };
2162
2205
  }
2163
- function fromBytes2(data, { param, position }) {
2206
+ function decodeBytes(data, { param, position }) {
2164
2207
  const [_, size2] = param.type.split("bytes");
2165
2208
  if (!size2) {
2166
2209
  const offset = hexToNumber(slice(data, position, position + 32));
@@ -2272,7 +2315,7 @@ function decodeErrorResult({
2272
2315
  });
2273
2316
  return {
2274
2317
  abiItem,
2275
- args: "inputs" in abiItem && abiItem.inputs && abiItem.inputs.length > 0 ? decodeAbi({ data: slice(data, 4), params: abiItem.inputs }) : void 0,
2318
+ args: "inputs" in abiItem && abiItem.inputs && abiItem.inputs.length > 0 ? decodeAbiParameters(abiItem.inputs, slice(data, 4)) : void 0,
2276
2319
  errorName: abiItem.name
2277
2320
  };
2278
2321
  }
@@ -2309,7 +2352,7 @@ function decodeEventLog({
2309
2352
  }
2310
2353
  if (data && data !== "0x") {
2311
2354
  const params = inputs.filter((x) => !("indexed" in x && x.indexed));
2312
- const decodedData = decodeAbi({ params, data });
2355
+ const decodedData = decodeAbiParameters(params, data);
2313
2356
  if (decodedData) {
2314
2357
  if (isUnnamed)
2315
2358
  args = [...args, ...decodedData];
@@ -2328,7 +2371,7 @@ function decodeEventLog({
2328
2371
  function decodeTopic({ param, value }) {
2329
2372
  if (param.type === "string" || param.type === "bytes" || param.type === "tuple" || param.type.match(/^(.*)\[(\d+)?\]$/))
2330
2373
  return value;
2331
- const decodedArg = decodeAbi({ params: [param], data: value }) || [];
2374
+ const decodedArg = decodeAbiParameters([param], value) || [];
2332
2375
  return decodedArg[0];
2333
2376
  }
2334
2377
 
@@ -2344,7 +2387,7 @@ function decodeFunctionData({ abi, data }) {
2344
2387
  });
2345
2388
  return {
2346
2389
  functionName: description.name,
2347
- args: "inputs" in description && description.inputs && description.inputs.length > 0 ? decodeAbi({ data: slice(data, 4), params: description.inputs }) : void 0
2390
+ args: "inputs" in description && description.inputs && description.inputs.length > 0 ? decodeAbiParameters(description.inputs, slice(data, 4)) : void 0
2348
2391
  };
2349
2392
  }
2350
2393
 
@@ -2437,7 +2480,7 @@ function decodeFunctionResult({
2437
2480
  throw new AbiFunctionNotFoundError(functionName, { docsPath });
2438
2481
  if (!("outputs" in description))
2439
2482
  throw new AbiFunctionOutputsNotFoundError(functionName, { docsPath });
2440
- const values = decodeAbi({ data, params: description.outputs });
2483
+ const values = decodeAbiParameters(description.outputs, data);
2441
2484
  if (values && values.length > 1)
2442
2485
  return values;
2443
2486
  if (values && values.length === 1)
@@ -2463,10 +2506,10 @@ function encodeDeployData({
2463
2506
  throw new AbiConstructorParamsNotFoundError({ docsPath: docsPath2 });
2464
2507
  if (!description.inputs || description.inputs.length === 0)
2465
2508
  throw new AbiConstructorParamsNotFoundError({ docsPath: docsPath2 });
2466
- const data = encodeAbi({
2467
- params: description.inputs,
2468
- values: args
2469
- });
2509
+ const data = encodeAbiParameters(
2510
+ description.inputs,
2511
+ args
2512
+ );
2470
2513
  return concatHex([bytecode, data]);
2471
2514
  }
2472
2515
 
@@ -2486,7 +2529,7 @@ function encodeErrorResult({ abi, errorName, args }) {
2486
2529
  if (args && args.length > 0) {
2487
2530
  if (!("inputs" in description && description.inputs))
2488
2531
  throw new AbiErrorInputsNotFoundError(errorName, { docsPath: docsPath3 });
2489
- data = encodeAbi({ params: description.inputs, values: args });
2532
+ data = encodeAbiParameters(description.inputs, args);
2490
2533
  }
2491
2534
  return concatHex([signature, data]);
2492
2535
  }
@@ -2519,7 +2562,7 @@ function encodeArg({
2519
2562
  return keccak256(toBytes(value));
2520
2563
  if (param.type === "tuple" || param.type.match(/^(.*)\[(\d+)?\]$/))
2521
2564
  throw new FilterTypeNotSupportedError(param.type);
2522
- return encodeAbi({ params: [param], values: [value] });
2565
+ return encodeAbiParameters([param], [value]);
2523
2566
  }
2524
2567
 
2525
2568
  // src/utils/abi/encodeFunctionData.ts
@@ -2535,10 +2578,10 @@ function encodeFunctionData({ abi, args, functionName }) {
2535
2578
  });
2536
2579
  const definition = formatAbiItem(description);
2537
2580
  const signature = getFunctionSelector(definition);
2538
- const data = "inputs" in description && description.inputs ? encodeAbi({
2539
- params: description.inputs,
2540
- values: args ?? []
2541
- }) : void 0;
2581
+ const data = "inputs" in description && description.inputs ? encodeAbiParameters(
2582
+ description.inputs,
2583
+ args ?? []
2584
+ ) : void 0;
2542
2585
  return concatHex([signature, data ?? "0x"]);
2543
2586
  }
2544
2587
 
@@ -2559,7 +2602,7 @@ function encodeFunctionResult({
2559
2602
  let values = Array.isArray(result) ? result : [result];
2560
2603
  if (description.outputs.length === 0 && !values[0])
2561
2604
  values = [];
2562
- return encodeAbi({ params: description.outputs, values });
2605
+ return encodeAbiParameters(description.outputs, values);
2563
2606
  }
2564
2607
 
2565
2608
  // src/utils/abi/formatAbiItemWithArgs.ts
@@ -3309,6 +3352,7 @@ export {
3309
3352
  InvalidAddressError,
3310
3353
  BlockNotFoundError,
3311
3354
  ChainDoesNotSupportContract,
3355
+ ChainMismatchError,
3312
3356
  multicall3Abi,
3313
3357
  panicReasons,
3314
3358
  TransactionExecutionError,
@@ -3379,8 +3423,8 @@ export {
3379
3423
  toHex,
3380
3424
  numberToHex,
3381
3425
  stringToHex,
3382
- boolToBytes,
3383
3426
  toBytes,
3427
+ boolToBytes,
3384
3428
  hexToBytes,
3385
3429
  numberToBytes,
3386
3430
  stringToBytes,
@@ -3410,8 +3454,8 @@ export {
3410
3454
  getCreateAddress,
3411
3455
  getCreate2Address,
3412
3456
  isAddressEqual,
3413
- encodeAbi,
3414
- decodeAbi,
3457
+ encodeAbiParameters,
3458
+ decodeAbiParameters,
3415
3459
  formatAbiItem,
3416
3460
  decodeErrorResult,
3417
3461
  decodeEventLog,
@@ -3464,4 +3508,4 @@ export {
3464
3508
  parseEther,
3465
3509
  parseGwei
3466
3510
  };
3467
- //# sourceMappingURL=chunk-VP57P6CD.mjs.map
3511
+ //# sourceMappingURL=chunk-PBW4YL37.mjs.map