viem 0.0.1-alpha.33 → 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 (72) hide show
  1. package/LICENSE +1 -1
  2. package/abi/package.json +4 -0
  3. package/dist/abi.d.ts +5 -0
  4. package/dist/abi.js +11 -0
  5. package/dist/abi.js.map +1 -0
  6. package/dist/abi.mjs +11 -0
  7. package/dist/abi.mjs.map +1 -0
  8. package/dist/{chain-ce6e149c.d.ts → chain-145cfaf3.d.ts} +1 -1
  9. package/dist/{chain-4a0d4164.d.ts → chain-7b7ac245.d.ts} +2 -2
  10. package/dist/chains.d.ts +3 -3
  11. package/dist/chains.js +46 -46
  12. package/dist/chains.mjs +1 -1
  13. package/dist/{chunk-DHSIBSJL.js → chunk-3FT7UKPT.js} +75 -75
  14. package/dist/chunk-3FT7UKPT.js.map +1 -0
  15. package/dist/{chunk-BUCF6SRR.js → chunk-57YM6E7I.js} +22 -22
  16. package/dist/{chunk-KMBDF3HQ.mjs → chunk-5NDMORWZ.mjs} +3 -3
  17. package/dist/{chunk-3CEIPU6M.js → chunk-A665BF7O.js} +24 -16
  18. package/dist/chunk-A665BF7O.js.map +1 -0
  19. package/dist/{chunk-R7MSY2BH.js → chunk-HQCSH22G.js} +128 -83
  20. package/dist/chunk-HQCSH22G.js.map +1 -0
  21. package/dist/{chunk-JB4TWW3U.mjs → chunk-PBW4YL37.mjs} +89 -44
  22. package/dist/chunk-PBW4YL37.mjs.map +1 -0
  23. package/dist/{chunk-VEAHSGOL.mjs → chunk-PH5LCMME.mjs} +10 -2
  24. package/dist/chunk-PH5LCMME.mjs.map +1 -0
  25. package/dist/{chunk-RNGEVZVH.js → chunk-TJK6AFBM.js} +14 -14
  26. package/dist/{chunk-RNGEVZVH.js.map → chunk-TJK6AFBM.js.map} +1 -1
  27. package/dist/{chunk-HN7IYO7U.mjs → chunk-WQDVW7EJ.mjs} +2 -2
  28. package/dist/chunk-WQDVW7EJ.mjs.map +1 -0
  29. package/dist/{chunk-DYALOQ44.mjs → chunk-YEVW7GK5.mjs} +2 -2
  30. package/dist/{chunk-DYALOQ44.mjs.map → chunk-YEVW7GK5.mjs.map} +1 -1
  31. package/dist/contract.d.ts +7 -8
  32. package/dist/contract.js +4 -4
  33. package/dist/contract.mjs +10 -10
  34. package/dist/{createClient-8a8b4bf4.d.ts → createClient-7c715e2c.d.ts} +2 -2
  35. package/dist/{createPublicClient-b5afe6b8.d.ts → createPublicClient-4d24afb2.d.ts} +122 -9
  36. package/dist/{eip1193-8c021438.d.ts → eip1193-d538ae6d.d.ts} +11 -1
  37. package/dist/{getAbiItem-d7aa40b1.d.ts → encodeFunctionResult-f59f4cfe.d.ts} +3 -25
  38. package/dist/ens.d.ts +4 -5
  39. package/dist/ens.js +4 -4
  40. package/dist/ens.mjs +3 -3
  41. package/dist/{formatAbiItem-6e00e5e6.d.ts → formatAbiItem-89d97367.d.ts} +1 -1
  42. package/dist/getAbiItem-c9cbc8b8.d.ts +22 -0
  43. package/dist/index.d.ts +12 -13
  44. package/dist/index.js +91 -90
  45. package/dist/index.js.map +1 -1
  46. package/dist/index.mjs +24 -23
  47. package/dist/index.mjs.map +1 -1
  48. package/dist/{parseGwei-ce3a9dfa.d.ts → parseGwei-ad6d1914.d.ts} +5 -6
  49. package/dist/public.d.ts +5 -6
  50. package/dist/public.js +3 -3
  51. package/dist/public.mjs +2 -2
  52. package/dist/{test-5d5828c9.d.ts → test-8646fb50.d.ts} +9 -9
  53. package/dist/test.d.ts +5 -5
  54. package/dist/test.js +3 -3
  55. package/dist/test.mjs +2 -2
  56. package/dist/utils/index.d.ts +12 -14
  57. package/dist/utils/index.js +2 -2
  58. package/dist/utils/index.mjs +5 -5
  59. package/dist/wallet.d.ts +5 -5
  60. package/dist/wallet.js +4 -3
  61. package/dist/wallet.mjs +3 -2
  62. package/dist/window.d.ts +2 -2
  63. package/package.json +8 -2
  64. package/dist/chunk-3CEIPU6M.js.map +0 -1
  65. package/dist/chunk-DHSIBSJL.js.map +0 -1
  66. package/dist/chunk-HN7IYO7U.mjs.map +0 -1
  67. package/dist/chunk-JB4TWW3U.mjs.map +0 -1
  68. package/dist/chunk-R7MSY2BH.js.map +0 -1
  69. package/dist/chunk-VEAHSGOL.mjs.map +0 -1
  70. package/dist/wallet-5ab86528.d.ts +0 -115
  71. /package/dist/{chunk-BUCF6SRR.js.map → chunk-57YM6E7I.js.map} +0 -0
  72. /package/dist/{chunk-KMBDF3HQ.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.33",
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) {
@@ -2022,7 +2068,8 @@ function encodeTuple(value, { param }) {
2022
2068
  value: value[index]
2023
2069
  });
2024
2070
  preparedParams.push(preparedParam);
2025
- dynamic = preparedParam.dynamic;
2071
+ if (preparedParam.dynamic)
2072
+ dynamic = true;
2026
2073
  }
2027
2074
  return {
2028
2075
  dynamic,
@@ -2037,19 +2084,16 @@ function getArrayComponents(type) {
2037
2084
  ) : void 0;
2038
2085
  }
2039
2086
 
2040
- // src/utils/abi/decodeAbi.ts
2041
- function decodeAbi({ data, params }) {
2087
+ // src/utils/abi/decodeAbiParameters.ts
2088
+ function decodeAbiParameters(params, data) {
2042
2089
  if (data === "0x" && params.length > 0)
2043
2090
  throw new AbiDecodingZeroDataError();
2044
2091
  if (size(data) % 32 !== 0)
2045
2092
  throw new AbiDecodingDataSizeInvalidError(size(data));
2046
- const values = decodeParams({
2093
+ return decodeParams({
2047
2094
  data,
2048
2095
  params
2049
2096
  });
2050
- if (values.length === 0)
2051
- return void 0;
2052
- return values;
2053
2097
  }
2054
2098
  function decodeParams({
2055
2099
  data,
@@ -2086,7 +2130,7 @@ function decodeParam({
2086
2130
  return decodeString(data, { position });
2087
2131
  }
2088
2132
  if (param.type.startsWith("bytes")) {
2089
- return fromBytes2(data, { param, position });
2133
+ return decodeBytes(data, { param, position });
2090
2134
  }
2091
2135
  let value = slice(data, position, position + 32);
2092
2136
  if (param.type.startsWith("uint") || param.type.startsWith("int")) {
@@ -2099,7 +2143,7 @@ function decodeParam({
2099
2143
  return decodeBool(value);
2100
2144
  }
2101
2145
  throw new InvalidAbiDecodingTypeError(param.type, {
2102
- docsPath: "/docs/contract/decodeAbi"
2146
+ docsPath: "/docs/contract/decodeAbiParameters"
2103
2147
  });
2104
2148
  }
2105
2149
  function decodeAddress(value) {
@@ -2159,7 +2203,7 @@ function decodeArray(data, {
2159
2203
  function decodeBool(value) {
2160
2204
  return { consumed: 32, value: hexToBool(value) };
2161
2205
  }
2162
- function fromBytes2(data, { param, position }) {
2206
+ function decodeBytes(data, { param, position }) {
2163
2207
  const [_, size2] = param.type.split("bytes");
2164
2208
  if (!size2) {
2165
2209
  const offset = hexToNumber(slice(data, position, position + 32));
@@ -2271,7 +2315,7 @@ function decodeErrorResult({
2271
2315
  });
2272
2316
  return {
2273
2317
  abiItem,
2274
- 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,
2275
2319
  errorName: abiItem.name
2276
2320
  };
2277
2321
  }
@@ -2308,7 +2352,7 @@ function decodeEventLog({
2308
2352
  }
2309
2353
  if (data && data !== "0x") {
2310
2354
  const params = inputs.filter((x) => !("indexed" in x && x.indexed));
2311
- const decodedData = decodeAbi({ params, data });
2355
+ const decodedData = decodeAbiParameters(params, data);
2312
2356
  if (decodedData) {
2313
2357
  if (isUnnamed)
2314
2358
  args = [...args, ...decodedData];
@@ -2327,7 +2371,7 @@ function decodeEventLog({
2327
2371
  function decodeTopic({ param, value }) {
2328
2372
  if (param.type === "string" || param.type === "bytes" || param.type === "tuple" || param.type.match(/^(.*)\[(\d+)?\]$/))
2329
2373
  return value;
2330
- const decodedArg = decodeAbi({ params: [param], data: value }) || [];
2374
+ const decodedArg = decodeAbiParameters([param], value) || [];
2331
2375
  return decodedArg[0];
2332
2376
  }
2333
2377
 
@@ -2343,7 +2387,7 @@ function decodeFunctionData({ abi, data }) {
2343
2387
  });
2344
2388
  return {
2345
2389
  functionName: description.name,
2346
- 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
2347
2391
  };
2348
2392
  }
2349
2393
 
@@ -2436,7 +2480,7 @@ function decodeFunctionResult({
2436
2480
  throw new AbiFunctionNotFoundError(functionName, { docsPath });
2437
2481
  if (!("outputs" in description))
2438
2482
  throw new AbiFunctionOutputsNotFoundError(functionName, { docsPath });
2439
- const values = decodeAbi({ data, params: description.outputs });
2483
+ const values = decodeAbiParameters(description.outputs, data);
2440
2484
  if (values && values.length > 1)
2441
2485
  return values;
2442
2486
  if (values && values.length === 1)
@@ -2462,10 +2506,10 @@ function encodeDeployData({
2462
2506
  throw new AbiConstructorParamsNotFoundError({ docsPath: docsPath2 });
2463
2507
  if (!description.inputs || description.inputs.length === 0)
2464
2508
  throw new AbiConstructorParamsNotFoundError({ docsPath: docsPath2 });
2465
- const data = encodeAbi({
2466
- params: description.inputs,
2467
- values: args
2468
- });
2509
+ const data = encodeAbiParameters(
2510
+ description.inputs,
2511
+ args
2512
+ );
2469
2513
  return concatHex([bytecode, data]);
2470
2514
  }
2471
2515
 
@@ -2485,7 +2529,7 @@ function encodeErrorResult({ abi, errorName, args }) {
2485
2529
  if (args && args.length > 0) {
2486
2530
  if (!("inputs" in description && description.inputs))
2487
2531
  throw new AbiErrorInputsNotFoundError(errorName, { docsPath: docsPath3 });
2488
- data = encodeAbi({ params: description.inputs, values: args });
2532
+ data = encodeAbiParameters(description.inputs, args);
2489
2533
  }
2490
2534
  return concatHex([signature, data]);
2491
2535
  }
@@ -2518,7 +2562,7 @@ function encodeArg({
2518
2562
  return keccak256(toBytes(value));
2519
2563
  if (param.type === "tuple" || param.type.match(/^(.*)\[(\d+)?\]$/))
2520
2564
  throw new FilterTypeNotSupportedError(param.type);
2521
- return encodeAbi({ params: [param], values: [value] });
2565
+ return encodeAbiParameters([param], [value]);
2522
2566
  }
2523
2567
 
2524
2568
  // src/utils/abi/encodeFunctionData.ts
@@ -2534,10 +2578,10 @@ function encodeFunctionData({ abi, args, functionName }) {
2534
2578
  });
2535
2579
  const definition = formatAbiItem(description);
2536
2580
  const signature = getFunctionSelector(definition);
2537
- const data = "inputs" in description && description.inputs ? encodeAbi({
2538
- params: description.inputs,
2539
- values: args ?? []
2540
- }) : void 0;
2581
+ const data = "inputs" in description && description.inputs ? encodeAbiParameters(
2582
+ description.inputs,
2583
+ args ?? []
2584
+ ) : void 0;
2541
2585
  return concatHex([signature, data ?? "0x"]);
2542
2586
  }
2543
2587
 
@@ -2558,7 +2602,7 @@ function encodeFunctionResult({
2558
2602
  let values = Array.isArray(result) ? result : [result];
2559
2603
  if (description.outputs.length === 0 && !values[0])
2560
2604
  values = [];
2561
- return encodeAbi({ params: description.outputs, values });
2605
+ return encodeAbiParameters(description.outputs, values);
2562
2606
  }
2563
2607
 
2564
2608
  // src/utils/abi/formatAbiItemWithArgs.ts
@@ -3308,6 +3352,7 @@ export {
3308
3352
  InvalidAddressError,
3309
3353
  BlockNotFoundError,
3310
3354
  ChainDoesNotSupportContract,
3355
+ ChainMismatchError,
3311
3356
  multicall3Abi,
3312
3357
  panicReasons,
3313
3358
  TransactionExecutionError,
@@ -3378,8 +3423,8 @@ export {
3378
3423
  toHex,
3379
3424
  numberToHex,
3380
3425
  stringToHex,
3381
- boolToBytes,
3382
3426
  toBytes,
3427
+ boolToBytes,
3383
3428
  hexToBytes,
3384
3429
  numberToBytes,
3385
3430
  stringToBytes,
@@ -3409,8 +3454,8 @@ export {
3409
3454
  getCreateAddress,
3410
3455
  getCreate2Address,
3411
3456
  isAddressEqual,
3412
- encodeAbi,
3413
- decodeAbi,
3457
+ encodeAbiParameters,
3458
+ decodeAbiParameters,
3414
3459
  formatAbiItem,
3415
3460
  decodeErrorResult,
3416
3461
  decodeEventLog,
@@ -3463,4 +3508,4 @@ export {
3463
3508
  parseEther,
3464
3509
  parseGwei
3465
3510
  };
3466
- //# sourceMappingURL=chunk-JB4TWW3U.mjs.map
3511
+ //# sourceMappingURL=chunk-PBW4YL37.mjs.map