viem 0.4.0-main.20230418T011420 → 0.4.0-main.20230419T210450

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 (127) hide show
  1. package/README.md +6 -0
  2. package/dist/cjs/actions/public/call.js +97 -10
  3. package/dist/cjs/actions/public/call.js.map +1 -1
  4. package/dist/cjs/actions/public/simulateContract.js +1 -0
  5. package/dist/cjs/actions/public/simulateContract.js.map +1 -1
  6. package/dist/cjs/chains.js +1 -24
  7. package/dist/cjs/chains.js.map +1 -1
  8. package/dist/cjs/clients/createPublicClient.js +2 -1
  9. package/dist/cjs/clients/createPublicClient.js.map +1 -1
  10. package/dist/cjs/constants/contract.js +5 -0
  11. package/dist/cjs/constants/contract.js.map +1 -0
  12. package/dist/cjs/constants/index.js +3 -1
  13. package/dist/cjs/constants/index.js.map +1 -1
  14. package/dist/cjs/errors/chain.js +13 -1
  15. package/dist/cjs/errors/chain.js.map +1 -1
  16. package/dist/cjs/errors/contract.js +1 -1
  17. package/dist/cjs/errors/contract.js.map +1 -1
  18. package/dist/cjs/errors/encoding.js +13 -1
  19. package/dist/cjs/errors/encoding.js.map +1 -1
  20. package/dist/cjs/errors/index.js +5 -2
  21. package/dist/cjs/errors/index.js.map +1 -1
  22. package/dist/cjs/errors/version.js +1 -1
  23. package/dist/cjs/index.js +5 -4
  24. package/dist/cjs/index.js.map +1 -1
  25. package/dist/cjs/utils/encoding/fromBytes.js +29 -12
  26. package/dist/cjs/utils/encoding/fromBytes.js.map +1 -1
  27. package/dist/cjs/utils/encoding/fromHex.js +32 -10
  28. package/dist/cjs/utils/encoding/fromHex.js.map +1 -1
  29. package/dist/cjs/utils/encoding/toBytes.js +31 -15
  30. package/dist/cjs/utils/encoding/toBytes.js.map +1 -1
  31. package/dist/cjs/utils/encoding/toHex.js +31 -20
  32. package/dist/cjs/utils/encoding/toHex.js.map +1 -1
  33. package/dist/cjs/utils/errors/getContractError.js +7 -5
  34. package/dist/cjs/utils/errors/getContractError.js.map +1 -1
  35. package/dist/cjs/utils/promise/createBatchScheduler.js +47 -0
  36. package/dist/cjs/utils/promise/createBatchScheduler.js.map +1 -0
  37. package/dist/cjs/utils/promise/index.js +3 -1
  38. package/dist/cjs/utils/promise/index.js.map +1 -1
  39. package/dist/esm/actions/public/call.js +91 -4
  40. package/dist/esm/actions/public/call.js.map +1 -1
  41. package/dist/esm/actions/public/simulateContract.js +1 -0
  42. package/dist/esm/actions/public/simulateContract.js.map +1 -1
  43. package/dist/esm/clients/createPublicClient.js +2 -1
  44. package/dist/esm/clients/createPublicClient.js.map +1 -1
  45. package/dist/esm/constants/contract.js +2 -0
  46. package/dist/esm/constants/contract.js.map +1 -0
  47. package/dist/esm/constants/index.js +1 -0
  48. package/dist/esm/constants/index.js.map +1 -1
  49. package/dist/esm/errors/chain.js +11 -0
  50. package/dist/esm/errors/chain.js.map +1 -1
  51. package/dist/esm/errors/contract.js +1 -1
  52. package/dist/esm/errors/contract.js.map +1 -1
  53. package/dist/esm/errors/encoding.js +11 -0
  54. package/dist/esm/errors/encoding.js.map +1 -1
  55. package/dist/esm/errors/index.js +2 -2
  56. package/dist/esm/errors/index.js.map +1 -1
  57. package/dist/esm/errors/version.js +1 -1
  58. package/dist/esm/index.js +1 -1
  59. package/dist/esm/index.js.map +1 -1
  60. package/dist/esm/utils/encoding/fromBytes.js +30 -13
  61. package/dist/esm/utils/encoding/fromBytes.js.map +1 -1
  62. package/dist/esm/utils/encoding/fromHex.js +31 -10
  63. package/dist/esm/utils/encoding/fromHex.js.map +1 -1
  64. package/dist/esm/utils/encoding/toBytes.js +31 -15
  65. package/dist/esm/utils/encoding/toBytes.js.map +1 -1
  66. package/dist/esm/utils/encoding/toHex.js +28 -17
  67. package/dist/esm/utils/encoding/toHex.js.map +1 -1
  68. package/dist/esm/utils/errors/getContractError.js +7 -5
  69. package/dist/esm/utils/errors/getContractError.js.map +1 -1
  70. package/dist/esm/utils/promise/createBatchScheduler.js +43 -0
  71. package/dist/esm/utils/promise/createBatchScheduler.js.map +1 -0
  72. package/dist/esm/utils/promise/index.js +1 -0
  73. package/dist/esm/utils/promise/index.js.map +1 -1
  74. package/dist/types/actions/public/call.d.ts +1 -0
  75. package/dist/types/actions/public/call.d.ts.map +1 -1
  76. package/dist/types/actions/public/simulateContract.d.ts +1 -1
  77. package/dist/types/actions/public/simulateContract.d.ts.map +1 -1
  78. package/dist/types/clients/createPublicClient.d.ts +15 -3
  79. package/dist/types/clients/createPublicClient.d.ts.map +1 -1
  80. package/dist/types/constants/contract.d.ts +2 -0
  81. package/dist/types/constants/contract.d.ts.map +1 -0
  82. package/dist/types/constants/index.d.ts +1 -0
  83. package/dist/types/constants/index.d.ts.map +1 -1
  84. package/dist/types/errors/chain.d.ts +4 -0
  85. package/dist/types/errors/chain.d.ts.map +1 -1
  86. package/dist/types/errors/encoding.d.ts +7 -0
  87. package/dist/types/errors/encoding.d.ts.map +1 -1
  88. package/dist/types/errors/index.d.ts +2 -2
  89. package/dist/types/errors/index.d.ts.map +1 -1
  90. package/dist/types/errors/version.d.ts +1 -1
  91. package/dist/types/index.d.ts +1 -1
  92. package/dist/types/index.d.ts.map +1 -1
  93. package/dist/types/types/eip1193.d.ts +1 -1
  94. package/dist/types/types/eip1193.d.ts.map +1 -1
  95. package/dist/types/utils/encoding/fromBytes.d.ts +95 -10
  96. package/dist/types/utils/encoding/fromBytes.d.ts.map +1 -1
  97. package/dist/types/utils/encoding/fromHex.d.ts +122 -12
  98. package/dist/types/utils/encoding/fromHex.d.ts.map +1 -1
  99. package/dist/types/utils/encoding/toBytes.d.ts +113 -9
  100. package/dist/types/utils/encoding/toBytes.d.ts.map +1 -1
  101. package/dist/types/utils/encoding/toHex.d.ts +120 -10
  102. package/dist/types/utils/encoding/toHex.d.ts.map +1 -1
  103. package/dist/types/utils/errors/getContractError.d.ts.map +1 -1
  104. package/dist/types/utils/promise/createBatchScheduler.d.ts +15 -0
  105. package/dist/types/utils/promise/createBatchScheduler.d.ts.map +1 -0
  106. package/dist/types/utils/promise/index.d.ts +1 -0
  107. package/dist/types/utils/promise/index.d.ts.map +1 -1
  108. package/package.json +1 -1
  109. package/src/actions/public/call.ts +141 -7
  110. package/src/actions/public/simulateContract.ts +2 -1
  111. package/src/clients/createPublicClient.ts +17 -1
  112. package/src/constants/contract.ts +1 -0
  113. package/src/constants/index.ts +2 -0
  114. package/src/errors/chain.ts +8 -0
  115. package/src/errors/contract.ts +1 -1
  116. package/src/errors/encoding.ts +9 -0
  117. package/src/errors/index.ts +2 -0
  118. package/src/errors/version.ts +1 -1
  119. package/src/index.ts +1 -0
  120. package/src/types/eip1193.ts +1 -1
  121. package/src/utils/encoding/fromBytes.ts +147 -18
  122. package/src/utils/encoding/fromHex.ts +162 -19
  123. package/src/utils/encoding/toBytes.ts +148 -18
  124. package/src/utils/encoding/toHex.ts +151 -26
  125. package/src/utils/errors/getContractError.ts +9 -5
  126. package/src/utils/promise/createBatchScheduler.ts +82 -0
  127. package/src/utils/promise/index.ts +1 -0
@@ -1 +1 @@
1
- {"version":3,"file":"fromHex.d.ts","sourceRoot":"","sources":["../../../../src/utils/encoding/fromHex.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,SAAS,EAAE,GAAG,EAAE,MAAM,sBAAsB,CAAA;AAI1D,KAAK,iBAAiB,CAAC,GAAG,IAAI,GAAG,SAAS,QAAQ,GAC9C,MAAM,GACN,GAAG,SAAS,QAAQ,GACpB,MAAM,GACN,GAAG,SAAS,QAAQ,GACpB,MAAM,GACN,GAAG,SAAS,OAAO,GACnB,SAAS,GACT,GAAG,SAAS,SAAS,GACrB,OAAO,GACP,KAAK,CAAA;AAET;;GAEG;AACH,wBAAgB,OAAO,CACrB,GAAG,SAAS,QAAQ,GAAG,QAAQ,GAAG,QAAQ,GAAG,OAAO,GAAG,SAAS,EAChE,GAAG,EAAE,GAAG,EAAE,EAAE,EAAE,GAAG,GAAG,iBAAiB,CAAC,GAAG,CAAC,CAM3C;AAED,MAA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;;;;;;;;;;;;;;GA2BG;AACH,wBAAgB,OAAO,CACrB,GAAG,SAAS,QAAQ,GAAG,QAAQ,GAAG,QAAQ,GAAG,OAAO,GAAG,SAAS,EAChE,GAAG,EAAE,GAAG,EAAE,QAAQ,EAAE,iBAAiB,CAAC,GAAG,CAAC,GAAG,iBAAiB,CAAC,GAAG,CAAC,CASpE;AAED,MAAM,MAAM,eAAe,GAAG;IAC5B,4DAA4D;IAC5D,MAAM,CAAC,EAAE,OAAO,CAAA;IAChB,wCAAwC;IACxC,IAAI,CAAC,EAAE,MAAM,CAAA;CACd,CAAA;AAED;;;;;;;;;;;;;;;;;;GAkBG;AACH,wBAAgB,WAAW,CAAC,GAAG,EAAE,GAAG,EAAE,IAAI,GAAE,eAAoB,GAAG,MAAM,CAaxE;AAED,MAAM,MAAM,aAAa,GAAG;IAC1B,wCAAwC;IACxC,IAAI,CAAC,EAAE,MAAM,CAAA;CACd,CAAA;AAED;;;;;;;;;;;;;;;;;;GAkBG;AACH,wBAAgB,SAAS,CAAC,IAAI,EAAE,GAAG,EAAE,IAAI,GAAE,aAAkB,GAAG,OAAO,CAStE;AAED,MAAM,MAAM,eAAe,GAAG,eAAe,CAAA;AAE7C;;;;;;;;;;;;;;;;;;GAkBG;AACH,wBAAgB,WAAW,CAAC,GAAG,EAAE,GAAG,EAAE,IAAI,GAAE,eAAoB,GAAG,MAAM,CAExE;AAED,MAAM,MAAM,eAAe,GAAG;IAC5B,wCAAwC;IACxC,IAAI,CAAC,EAAE,MAAM,CAAA;CACd,CAAA;AAED;;;;;;;;;;;;;;;;;;;;GAoBG;AACH,wBAAgB,WAAW,CAAC,GAAG,EAAE,GAAG,EAAE,IAAI,GAAE,eAAoB,GAAG,MAAM,CAOxE"}
@@ -1,21 +1,125 @@
1
1
  import type { ByteArray, Hex } from '../../types/index.js';
2
2
  import type { NumberToHexOpts } from './toHex.js';
3
- /** @description Encodes a UTF-8 string, hex value, bigint, number or boolean to a byte array. */
4
- export declare function toBytes(value: string | bigint | number | boolean | Hex): ByteArray;
3
+ export type ToBytesParameters = {
4
+ /** Size of the output bytes. */
5
+ size?: number;
6
+ };
5
7
  /**
6
- * @description Encodes a boolean into a byte array.
8
+ * Encodes a UTF-8 string, hex value, bigint, number or boolean to a byte array.
9
+ *
10
+ * - Docs: https://viem.sh/docs/utilities/toBytes.html
11
+ * - Example: https://viem.sh/docs/utilities/toBytes.html#usage
12
+ *
13
+ * @param value Value to encode.
14
+ * @param opts Options.
15
+ * @returns Byte array value.
16
+ *
17
+ * @example
18
+ * import { toBytes } from 'viem'
19
+ * const data = toBytes('Hello world')
20
+ * // Uint8Array([72, 101, 108, 108, 111, 32, 87, 111, 114, 108, 100, 33])
21
+ *
22
+ * @example
23
+ * import { toBytes } from 'viem'
24
+ * const data = toBytes(420)
25
+ * // Uint8Array([1, 164])
26
+ *
27
+ * @example
28
+ * import { toBytes } from 'viem'
29
+ * const data = toBytes(420, { size: 4 })
30
+ * // Uint8Array([0, 0, 1, 164])
7
31
  */
8
- export declare function boolToBytes(value: boolean): Uint8Array;
32
+ export declare function toBytes(value: string | bigint | number | boolean | Hex, opts?: ToBytesParameters): ByteArray;
33
+ export type BoolToHexOpts = {
34
+ /** Size of the output bytes. */
35
+ size?: number;
36
+ };
9
37
  /**
10
- * @description Encodes a hex string into a byte array.
38
+ * Encodes a boolean into a byte array.
39
+ *
40
+ * - Docs: https://viem.sh/docs/utilities/toBytes.html#booltobytes
41
+ *
42
+ * @param value Boolean value to encode.
43
+ * @param opts Options.
44
+ * @returns Byte array value.
45
+ *
46
+ * @example
47
+ * import { boolToBytes } from 'viem'
48
+ * const data = boolToBytes(true)
49
+ * // Uint8Array([1])
50
+ *
51
+ * @example
52
+ * import { boolToBytes } from 'viem'
53
+ * const data = boolToBytes(true, { size: 32 })
54
+ * // Uint8Array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1])
11
55
  */
12
- export declare function hexToBytes(hex_: Hex): ByteArray;
56
+ export declare function boolToBytes(value: boolean, opts?: BoolToHexOpts): Uint8Array;
57
+ export type HexToBytesOpts = {
58
+ /** Size of the output bytes. */
59
+ size?: number;
60
+ };
13
61
  /**
14
- * @description Encodes a number into a byte array.
62
+ * Encodes a hex string into a byte array.
63
+ *
64
+ * - Docs: https://viem.sh/docs/utilities/toBytes.html#hextobytes
65
+ *
66
+ * @param hex Hex string to encode.
67
+ * @param opts Options.
68
+ * @returns Byte array value.
69
+ *
70
+ * @example
71
+ * import { hexToBytes } from 'viem'
72
+ * const data = hexToBytes('0x48656c6c6f20776f726c6421')
73
+ * // Uint8Array([72, 101, 108, 108, 111, 32, 87, 111, 114, 108, 100, 33])
74
+ *
75
+ * @example
76
+ * import { hexToBytes } from 'viem'
77
+ * const data = hexToBytes('0x48656c6c6f20776f726c6421', { size: 32 })
78
+ * // Uint8Array([72, 101, 108, 108, 111, 32, 87, 111, 114, 108, 100, 33, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0])
79
+ */
80
+ export declare function hexToBytes(hex_: Hex, opts?: HexToBytesOpts): ByteArray;
81
+ /**
82
+ * Encodes a number into a byte array.
83
+ *
84
+ * - Docs: https://viem.sh/docs/utilities/toBytes.html#numbertobytes
85
+ *
86
+ * @param value Number to encode.
87
+ * @param opts Options.
88
+ * @returns Byte array value.
89
+ *
90
+ * @example
91
+ * import { numberToBytes } from 'viem'
92
+ * const data = numberToBytes(420)
93
+ * // Uint8Array([1, 164])
94
+ *
95
+ * @example
96
+ * import { numberToBytes } from 'viem'
97
+ * const data = numberToBytes(420, { size: 4 })
98
+ * // Uint8Array([0, 0, 1, 164])
15
99
  */
16
100
  export declare function numberToBytes(value: bigint | number, opts?: NumberToHexOpts): Uint8Array;
101
+ export type StringToBytesOpts = {
102
+ /** Size of the output bytes. */
103
+ size?: number;
104
+ };
17
105
  /**
18
- * @description Encodes a UTF-8 string into a byte array.
106
+ * Encodes a UTF-8 string into a byte array.
107
+ *
108
+ * - Docs: https://viem.sh/docs/utilities/toBytes.html#stringtobytes
109
+ *
110
+ * @param value String to encode.
111
+ * @param opts Options.
112
+ * @returns Byte array value.
113
+ *
114
+ * @example
115
+ * import { stringToBytes } from 'viem'
116
+ * const data = stringToBytes('Hello world!')
117
+ * // Uint8Array([72, 101, 108, 108, 111, 32, 119, 111, 114, 108, 100, 33])
118
+ *
119
+ * @example
120
+ * import { stringToBytes } from 'viem'
121
+ * const data = stringToBytes('Hello world!', { size: 32 })
122
+ * // Uint8Array([72, 101, 108, 108, 111, 32, 87, 111, 114, 108, 100, 33, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0])
19
123
  */
20
- export declare function stringToBytes(value: string): ByteArray;
124
+ export declare function stringToBytes(value: string, opts?: StringToBytesOpts): ByteArray;
21
125
  //# sourceMappingURL=toBytes.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"toBytes.d.ts","sourceRoot":"","sources":["../../../../src/utils/encoding/toBytes.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,SAAS,EAAE,GAAG,EAAE,MAAM,sBAAsB,CAAA;AAE1D,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,YAAY,CAAA;AAKjD,iGAAiG;AACjG,wBAAgB,OAAO,CACrB,KAAK,EAAE,MAAM,GAAG,MAAM,GAAG,MAAM,GAAG,OAAO,GAAG,GAAG,GAC9C,SAAS,CAMX;AAED;;GAEG;AACH,wBAAgB,WAAW,CAAC,KAAK,EAAE,OAAO,cAIzC;AAED;;GAEG;AACH,wBAAgB,UAAU,CAAC,IAAI,EAAE,GAAG,GAAG,SAAS,CAe/C;AAED;;GAEG;AACH,wBAAgB,aAAa,CAAC,KAAK,EAAE,MAAM,GAAG,MAAM,EAAE,IAAI,CAAC,EAAE,eAAe,cAG3E;AAED;;GAEG;AACH,wBAAgB,aAAa,CAAC,K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,IAAI,GAAE,iBAAsB,GAC3B,SAAS,CAOX"}
@@ -1,29 +1,139 @@
1
1
  import type { ByteArray, Hex } from '../../types/index.js';
2
+ export type ToHexParameters = {
3
+ /** The size (in bytes) of the output hex value. */
4
+ size?: number;
5
+ };
2
6
  /**
3
- * @description Encodes a boolean into a hex string
7
+ * Encodes a string, number, bigint, or ByteArray into a hex string
8
+ *
9
+ * - Docs: https://viem.sh/docs/utilities/toHex.html
10
+ * - Example: https://viem.sh/docs/utilities/toHex.html#usage
11
+ *
12
+ * @param value Value to encode.
13
+ * @param opts Options.
14
+ * @returns Hex value.
15
+ *
16
+ * @example
17
+ * import { toHex } from 'viem'
18
+ * const data = toHex('Hello world')
19
+ * // '0x48656c6c6f20776f726c6421'
20
+ *
21
+ * @example
22
+ * import { toHex } from 'viem'
23
+ * const data = toHex(420)
24
+ * // '0x1a4'
25
+ *
26
+ * @example
27
+ * import { toHex } from 'viem'
28
+ * const data = toHex('Hello world', { size: 32 })
29
+ * // '0x48656c6c6f20776f726c64210000000000000000000000000000000000000000'
4
30
  */
5
- export declare function boolToHex(value: boolean): Hex;
31
+ export declare function toHex(value: string | number | bigint | boolean | ByteArray, opts?: ToHexParameters): Hex;
32
+ export type BoolToHexOpts = {
33
+ /** The size (in bytes) of the output hex value. */
34
+ size?: number;
35
+ };
6
36
  /**
7
- * @description Encodes a bytes array into a hex string
37
+ * Encodes a boolean into a hex string
38
+ *
39
+ * - Docs: https://viem.sh/docs/utilities/toHex.html#booltohex
40
+ *
41
+ * @param value Value to encode.
42
+ * @param opts Options.
43
+ * @returns Hex value.
44
+ *
45
+ * @example
46
+ * import { boolToHex } from 'viem'
47
+ * const data = boolToHex(true)
48
+ * // '0x1'
49
+ *
50
+ * @example
51
+ * import { boolToHex } from 'viem'
52
+ * const data = boolToHex(false)
53
+ * // '0x0'
54
+ *
55
+ * @example
56
+ * import { boolToHex } from 'viem'
57
+ * const data = boolToHex(true, { size: 32 })
58
+ * // '0x0000000000000000000000000000000000000000000000000000000000000001'
8
59
  */
9
- export declare function bytesToHex(value: ByteArray): Hex;
60
+ export declare function boolToHex(value: boolean, opts?: BoolToHexOpts): Hex;
61
+ export type BytesToHexOpts = {
62
+ /** The size (in bytes) of the output hex value. */
63
+ size?: number;
64
+ };
10
65
  /**
11
- * @description Encodes a string, number, bigint, or ByteArray into a hex string
66
+ * Encodes a bytes array into a hex string
67
+ *
68
+ * - Docs: https://viem.sh/docs/utilities/toHex.html#bytestohex
69
+ *
70
+ * @param value Value to encode.
71
+ * @param opts Options.
72
+ * @returns Hex value.
73
+ *
74
+ * @example
75
+ * import { bytesToHex } from 'viem'
76
+ * const data = bytesToHex(Uint8Array.from([72, 101, 108, 108, 111, 32, 87, 111, 114, 108, 100, 33])
77
+ * // '0x48656c6c6f20576f726c6421'
78
+ *
79
+ * @example
80
+ * import { bytesToHex } from 'viem'
81
+ * const data = bytesToHex(Uint8Array.from([72, 101, 108, 108, 111, 32, 87, 111, 114, 108, 100, 33]), { size: 32 })
82
+ * // '0x48656c6c6f20576f726c64210000000000000000000000000000000000000000'
12
83
  */
13
- export declare function toHex(value: string | number | bigint | boolean | ByteArray): Hex;
84
+ export declare function bytesToHex(value: ByteArray, opts?: BytesToHexOpts): Hex;
14
85
  export type NumberToHexOpts = {
86
+ /** Whether or not the number of a signed representation. */
15
87
  signed?: boolean;
88
+ /** The size (in bytes) of the output hex value. */
16
89
  size: number;
17
90
  } | {
18
91
  signed?: never;
19
- size?: never;
92
+ /** The size (in bytes) of the output hex value. */
93
+ size?: number;
20
94
  };
21
95
  /**
22
- * @description Encodes a number or bigint into a hex string
96
+ * Encodes a number or bigint into a hex string
97
+ *
98
+ * - Docs: https://viem.sh/docs/utilities/toHex.html#numbertohex
99
+ *
100
+ * @param value Value to encode.
101
+ * @param opts Options.
102
+ * @returns Hex value.
103
+ *
104
+ * @example
105
+ * import { numberToHex } from 'viem'
106
+ * const data = numberToHex(420)
107
+ * // '0x1a4'
108
+ *
109
+ * @example
110
+ * import { numberToHex } from 'viem'
111
+ * const data = numberToHex(420, { size: 32 })
112
+ * // '0x00000000000000000000000000000000000000000000000000000000000001a4'
23
113
  */
24
114
  export declare function numberToHex(value_: number | bigint, opts?: NumberToHexOpts): Hex;
115
+ export type StringToHexOpts = {
116
+ /** The size (in bytes) of the output hex value. */
117
+ size?: number;
118
+ };
25
119
  /**
26
- * @description Encodes a UTF-8 string into a hex string
120
+ * Encodes a UTF-8 string into a hex string
121
+ *
122
+ * - Docs: https://viem.sh/docs/utilities/toHex.html#stringtohex
123
+ *
124
+ * @param value Value to encode.
125
+ * @param opts Options.
126
+ * @returns Hex value.
127
+ *
128
+ * @example
129
+ * import { stringToHex } from 'viem'
130
+ * const data = stringToHex('Hello World!')
131
+ * // '0x48656c6c6f20576f726c6421'
132
+ *
133
+ * @example
134
+ * import { stringToHex } from 'viem'
135
+ * const data = stringToHex('Hello World!', { size: 32 })
136
+ * // '0x48656c6c6f20576f726c64210000000000000000000000000000000000000000'
27
137
  */
28
- export declare function stringToHex(value_: string): Hex;
138
+ export declare function stringToHex(value_: string, opts?: StringToHexOpts): Hex;
29
139
  //# sourceMappingURL=toHex.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"toHex.d.ts","sourceRoot":"","sources":["../../../../src/utils/encoding/toHex.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,SAAS,EAAE,GAAG,EAAE,MAAM,sBAAsB,CAAA;AAO1D;;GAEG;AACH,wBAAgB,SAAS,CAAC,KAAK,EAAE,OAAO,GAAG,GAAG,CAE7C;AAED;;GAEG;AACH,wBAAgB,UAAU,CAAC,KAAK,EAAE,SAAS,GAAG,GAAG,CAMhD;AAED;;GAEG;AACH,wBAAgB,KAAK,CACnB,KAAK,EAAE,MAAM,GAAG,MAAM,GAAG,MAAM,GAAG,OAAO,GAAG,SAAS,GACpD,GAAG,CAQL;AAED,MAAM,MAAM,eAAe,GACvB;IAEE,MAAM,CAAC,EAAE,OAAO,CAAA;IAEhB,IAAI,E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DAAmD;IACnD,IAAI,EAAE,MAAM,CAAA;CACb,GACD;IACE,MAAM,CAAC,EAAE,KAAK,CAAA;IACd,mDAAmD;IACnD,IAAI,CAAC,EAAE,MAAM,CAAA;CACd,CAAA;AAEL;;;;;;;;;;;;;;;;;;GAkBG;AACH,wBAAgB,WAAW,CACzB,MAAM,EAAE,MAAM,GAAG,MAAM,EACvB,IAAI,GAAE,eAAoB,GACzB,GAAG,CAgCL;AAED,MAAM,MAAM,eAAe,GAAG;IAC5B,mDAAmD;IACnD,IAAI,CAAC,EAAE,MAAM,CAAA;CACd,CAAA;AAID;;;;;;;;;;;;;;;;;;GAkBG;AACH,wBAAgB,WAAW,CAAC,MAAM,EAAE,MAAM,EAAE,IAAI,GAAE,eAAoB,GAAG,GAAG,CAG3E"}
@@ -1 +1 @@
1
- {"version":3,"file":"getContractError.d.ts","sourceRoot":"","sources":["../../../../src/utils/errors/getContractError.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,GAAG,EAAE,MAAM,SAAS,CAAA;AAClC,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,uBAAuB,CAAA;AACtD,OAAO,EAEL,8BAA8B,EAG/B,MAAM,uBAAuB,CAAA;AAM9B,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,sBAAsB,CAAA;AAInD,wBAAgB,gBAAgB,CAC9B,GAAG,EAAE,SAAS,EACd,EACE,GAAG,EACH,OAAO,EACP,IAAI,EACJ,QAAQ,EACR,YAAY,EACZ,MAAM,GACP,EAAE;IACD,GAAG,EAAE,GAAG,CAAA;IACR,IAAI,EAAE,GAAG,CAAA;IACT,OAAO,CAAC,EAAE,OAAO,CAAA;IACjB,QAAQ,CAAC,EAAE,MAAM,CAAA;IACjB,YAAY,EAAE,MAAM,CAAA;IACpB,MAAM,CAAC,EAAE,OAAO,CAAA;CACjB,kCA+BF"}
1
+ {"version":3,"file":"getContractError.d.ts","sourceRoot":"","sources":["../../../../src/utils/errors/getContractError.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,GAAG,EAAE,MAAM,SAAS,CAAA;AAClC,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,uBAAuB,CAAA;AACtD,OAAO,EAEL,8BAA8B,EAG/B,MAAM,uBAAuB,CAAA;AAM9B,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,sBAAsB,CAAA;AAInD,wBAAgB,gBAAgB,CAC9B,GAAG,EAAE,SAAS,EACd,EACE,GAAG,EACH,OAAO,EACP,IAAI,EACJ,QAAQ,EACR,YAAY,EACZ,MAAM,GACP,EAAE;IACD,GAAG,EAAE,GAAG,CAAA;IACR,IAAI,EAAE,GAAG,CAAA;IACT,OAAO,CAAC,EAAE,OAAO,CAAA;IACjB,QAAQ,CAAC,EAAE,MAAM,CAAA;IACjB,YAAY,EAAE,MAAM,CAAA;IACpB,MAAM,CAAC,EAAE,OAAO,CAAA;CACjB,kCAmCF"}
@@ -0,0 +1,15 @@
1
+ type Resolved<TReturnType extends readonly unknown[] = any> = [
2
+ result: TReturnType[number],
3
+ results: TReturnType
4
+ ];
5
+ export declare function createBatchScheduler<TParameters, TReturnType extends readonly unknown[]>({ fn, id, shouldSplitBatch, wait, }: {
6
+ fn: (args: TParameters[]) => Promise<TReturnType>;
7
+ id: number | string;
8
+ shouldSplitBatch?: (args: TParameters[]) => boolean;
9
+ wait?: number;
10
+ }): {
11
+ flush: () => boolean;
12
+ schedule(args: TParameters): Promise<Resolved<TReturnType>>;
13
+ };
14
+ export {};
15
+ //# sourceMappingURL=createBatchScheduler.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"createBatchScheduler.d.ts","sourceRoot":"","sources":["../../../../src/utils/promise/createBatchScheduler.ts"],"names":[],"mappings":"AAAA,KAAK,QAAQ,CAAC,WAAW,SAAS,SAAS,OAAO,EAAE,GAAG,GAAG,IAAI;IAC5D,MAAM,EAAE,WAAW,CAAC,MAAM,CAAC;IAC3B,OAAO,EAAE,WAAW;CACrB,CAAA;AAWD,wBAAgB,oBAAoB,CAClC,WAAW,EACX,WAAW,SAAS,SAAS,OAAO,EAAE,EACtC,EACA,EAAE,EACF,EAAE,EACF,gBAAgB,EAChB,IAAQ,GACT,EAAE;IACD,EAAE,EAAE,CAAC,IAAI,EAAE,WAAW,EAAE,KAAK,OAAO,CAAC,WAAW,CAAC,CAAA;IACjD,EAAE,EAAE,MAAM,GAAG,MAAM,CAAA;IACnB,gBAAgB,CAAC,EAAE,CAAC,IAAI,EAAE,WAAW,EAAE,KAAK,OAAO,CAAA;IACnD,IAAI,CAAC,EAAE,MAAM,CAAA;CACd;;mBAgCwB,WAAW;EAsBnC"}
@@ -1,3 +1,4 @@
1
+ export { createBatchScheduler } from './createBatchScheduler.js';
1
2
  export { getCache, withCache } from './withCache.js';
2
3
  export { withRetry } from './withRetry.js';
3
4
  export { withTimeout } from './withTimeout.js';
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../../src/utils/promise/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,SAAS,EAAE,MAAM,gBAAgB,CAAA;AACpD,OAAO,EAAE,SAAS,EAAE,MAAM,gBAAgB,CAAA;AAC1C,OAAO,EAAE,WAAW,EAAE,MAAM,kBAAkB,CAAA"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../../src/utils/promise/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,oBAAoB,EAAE,MAAM,2BAA2B,CAAA;AAChE,OAAO,EAAE,QAAQ,EAAE,SAAS,EAAE,MAAM,gBAAgB,CAAA;AACpD,OAAO,EAAE,SAAS,EAAE,MAAM,gBAAgB,CAAA;AAC1C,OAAO,EAAE,WAAW,EAAE,MAAM,kBAAkB,CAAA"}
package/package.json CHANGED
@@ -12,7 +12,7 @@
12
12
  "isomorphic-ws": "5.0.0",
13
13
  "ws": "8.12.0"
14
14
  },
15
- "version": "0.4.0-main.20230418T011420",
15
+ "version": "0.4.0-main.20230419T210450",
16
16
  "files": [
17
17
  "dist",
18
18
  "!dist/**/*.tsbuildinfo",
@@ -1,5 +1,11 @@
1
1
  import type { PublicClient, Transport } from '../../clients/index.js'
2
+ import { aggregate3Signature, multicall3Abi } from '../../constants/index.js'
2
3
  import type { BaseError } from '../../errors/index.js'
4
+ import {
5
+ ChainDoesNotSupportContract,
6
+ ClientChainNotConfiguredError,
7
+ RawContractError,
8
+ } from '../../errors/index.js'
3
9
  import type {
4
10
  Account,
5
11
  Address,
@@ -10,19 +16,23 @@ import type {
10
16
  MergeIntersectionProperties,
11
17
  TransactionRequest,
12
18
  } from '../../types/index.js'
19
+ import type {
20
+ Formatted,
21
+ TransactionRequestFormatter,
22
+ } from '../../utils/index.js'
13
23
  import {
14
24
  assertRequest,
25
+ decodeFunctionResult,
26
+ encodeFunctionData,
15
27
  extract,
16
28
  format,
17
29
  formatTransactionRequest,
18
30
  getCallError,
31
+ getChainContractAddress,
19
32
  numberToHex,
20
33
  parseAccount,
21
34
  } from '../../utils/index.js'
22
- import type {
23
- Formatted,
24
- TransactionRequestFormatter,
25
- } from '../../utils/index.js'
35
+ import { createBatchScheduler } from '../../utils/promise/createBatchScheduler.js'
26
36
 
27
37
  export type FormattedCall<
28
38
  TFormatter extends Formatter | undefined = Formatter,
@@ -35,6 +45,7 @@ export type CallParameters<
35
45
  TChain extends Chain | undefined = Chain | undefined,
36
46
  > = FormattedCall<TransactionRequestFormatter<TChain>> & {
37
47
  account?: Account | Address
48
+ batch?: boolean
38
49
  } & (
39
50
  | {
40
51
  /** The balance of the account at a block number. */
@@ -84,6 +95,7 @@ export async function call<TChain extends Chain | undefined>(
84
95
  ): Promise<CallReturnType> {
85
96
  const {
86
97
  account: account_,
98
+ batch = Boolean(client.batch?.multicall),
87
99
  blockNumber,
88
100
  blockTag = 'latest',
89
101
  accessList,
@@ -104,7 +116,7 @@ export async function call<TChain extends Chain | undefined>(
104
116
 
105
117
  const blockNumberHex = blockNumber ? numberToHex(blockNumber) : undefined
106
118
  const formatter = client.chain?.formatters?.transactionRequest
107
- const request_ = format(
119
+ const request = format(
108
120
  {
109
121
  from: account?.address,
110
122
  accessList,
@@ -122,11 +134,27 @@ export async function call<TChain extends Chain | undefined>(
122
134
  {
123
135
  formatter: formatter || formatTransactionRequest,
124
136
  },
125
- )
137
+ ) as TransactionRequest
138
+
139
+ if (batch && shouldPerformMulticall({ request })) {
140
+ try {
141
+ return await scheduleMulticall(client, {
142
+ ...request,
143
+ blockNumber,
144
+ blockTag,
145
+ } as unknown as ScheduleMulticallParameters<TChain>)
146
+ } catch (err) {
147
+ if (
148
+ !(err instanceof ClientChainNotConfiguredError) &&
149
+ !(err instanceof ChainDoesNotSupportContract)
150
+ )
151
+ throw err
152
+ }
153
+ }
126
154
 
127
155
  const response = await client.request({
128
156
  method: 'eth_call',
129
- params: [request_, blockNumberHex || blockTag],
157
+ params: [request as any, blockNumberHex || blockTag],
130
158
  })
131
159
  if (response === '0x') return { data: undefined }
132
160
  return { data: response }
@@ -138,3 +166,109 @@ export async function call<TChain extends Chain | undefined>(
138
166
  })
139
167
  }
140
168
  }
169
+
170
+ // We only want to perform a scheduled multicall if:
171
+ // - The request has calldata,
172
+ // - The request has a target address,
173
+ // - The target address is not already the aggregate3 signature,
174
+ // - The request has no other properties (`nonce`, `gas`, etc cannot be sent with a multicall).
175
+ function shouldPerformMulticall({ request }: { request: TransactionRequest }) {
176
+ const { data, to, ...request_ } = request
177
+ if (!data) return false
178
+ if (data.startsWith(aggregate3Signature)) return false
179
+ if (!to) return false
180
+ if (
181
+ Object.values(request_).filter((x) => typeof x !== 'undefined').length > 0
182
+ )
183
+ return false
184
+ return true
185
+ }
186
+
187
+ type ScheduleMulticallParameters<TChain extends Chain | undefined> = Pick<
188
+ CallParameters<TChain>,
189
+ 'blockNumber' | 'blockTag'
190
+ > & {
191
+ data: Hex
192
+ multicallAddress?: Address
193
+ to: Address
194
+ }
195
+
196
+ async function scheduleMulticall<TChain extends Chain | undefined>(
197
+ client: PublicClient<Transport, TChain>,
198
+ args: ScheduleMulticallParameters<TChain>,
199
+ ) {
200
+ const { batchSize = 1024, wait = 0 } =
201
+ typeof client.batch?.multicall === 'object' ? client.batch?.multicall : {}
202
+ const {
203
+ blockNumber,
204
+ blockTag = 'latest',
205
+ data,
206
+ multicallAddress: multicallAddress_,
207
+ to,
208
+ } = args
209
+
210
+ let multicallAddress = multicallAddress_
211
+ if (!multicallAddress) {
212
+ if (!client.chain) throw new ClientChainNotConfiguredError()
213
+
214
+ multicallAddress = getChainContractAddress({
215
+ blockNumber,
216
+ chain: client.chain,
217
+ contract: 'multicall3',
218
+ })
219
+ }
220
+
221
+ const blockNumberHex = blockNumber ? numberToHex(blockNumber) : undefined
222
+ const block = blockNumberHex || blockTag
223
+
224
+ const { schedule } = createBatchScheduler({
225
+ id: `${client.uid}.${block}`,
226
+ wait,
227
+ shouldSplitBatch(args) {
228
+ const size = args.reduce((size, { data }) => size + (data.length - 2), 0)
229
+ return size > batchSize * 2
230
+ },
231
+ fn: async (
232
+ requests: {
233
+ data: Hex
234
+ to: Address
235
+ }[],
236
+ ) => {
237
+ const calls = requests.map((request) => ({
238
+ allowFailure: true,
239
+ callData: request.data,
240
+ target: request.to,
241
+ }))
242
+
243
+ const calldata = encodeFunctionData({
244
+ abi: multicall3Abi,
245
+ args: [calls],
246
+ functionName: 'aggregate3',
247
+ })
248
+
249
+ const data = await client.request({
250
+ method: 'eth_call',
251
+ params: [
252
+ {
253
+ data: calldata,
254
+ to: multicallAddress,
255
+ },
256
+ block,
257
+ ],
258
+ })
259
+
260
+ return decodeFunctionResult({
261
+ abi: multicall3Abi,
262
+ args: [calls],
263
+ functionName: 'aggregate3',
264
+ data: data || '0x',
265
+ })
266
+ },
267
+ })
268
+
269
+ const [{ returnData, success }] = await schedule({ data, to })
270
+
271
+ if (!success) throw new RawContractError({ data: returnData })
272
+ if (returnData === '0x') return { data: undefined }
273
+ return { data: returnData }
274
+ }
@@ -32,7 +32,7 @@ export type SimulateContractParameters<
32
32
  } & ContractFunctionConfig<TAbi, TFunctionName, 'payable' | 'nonpayable'> &
33
33
  Omit<
34
34
  CallParameters<TChainOverride extends Chain ? TChainOverride : TChain>,
35
- 'to' | 'data' | 'value'
35
+ 'batch' | 'to' | 'data' | 'value'
36
36
  > &
37
37
  GetValue<TAbi, TFunctionName, CallParameters<TChain>['value']>
38
38
 
@@ -115,6 +115,7 @@ export async function simulateContract<
115
115
  } as unknown as EncodeFunctionDataParameters<TAbi, TFunctionName>)
116
116
  try {
117
117
  const { data } = await call(client, {
118
+ batch: false,
118
119
  data: calldata,
119
120
  to: address,
120
121
  ...callRequest,
@@ -6,13 +6,26 @@ import { publicActions } from './decorators/index.js'
6
6
  import type { PublicActions } from './decorators/index.js'
7
7
  import type { Chain, Prettify } from '../types/index.js'
8
8
 
9
+ export type MulticallBatchOptions = {
10
+ /** The maximum size (in bytes) for each calldata chunk. @default 1_024 */
11
+ batchSize?: number
12
+ /** The maximum number of milliseconds to wait before sending a batch. @default 16 */
13
+ wait?: number
14
+ }
15
+
9
16
  export type PublicClientConfig<
10
17
  TTransport extends Transport = Transport,
11
18
  TChain extends Chain | undefined = Chain | undefined,
12
19
  > = Pick<
13
20
  ClientConfig<TTransport, TChain>,
14
21
  'chain' | 'key' | 'name' | 'pollingInterval' | 'transport'
15
- >
22
+ > & {
23
+ /** Flags for batch settings. */
24
+ batch?: {
25
+ /** Toggle to enable `eth_call` multicall aggregation. */
26
+ multicall?: boolean | MulticallBatchOptions
27
+ }
28
+ }
16
29
 
17
30
  export type PublicClient<
18
31
  TTransport extends Transport = Transport,
@@ -20,6 +33,7 @@ export type PublicClient<
20
33
  TIncludeActions extends boolean = true,
21
34
  > = Prettify<
22
35
  Client<TTransport, PublicRequests, TChain> &
36
+ Pick<PublicClientConfig, 'batch'> &
23
37
  (TIncludeActions extends true ? PublicActions<TTransport, TChain> : unknown)
24
38
  >
25
39
 
@@ -46,6 +60,7 @@ export function createPublicClient<
46
60
  TTransport extends Transport,
47
61
  TChain extends Chain | undefined = undefined,
48
62
  >({
63
+ batch,
49
64
  chain,
50
65
  key = 'public',
51
66
  name = 'Public Client',
@@ -65,6 +80,7 @@ export function createPublicClient<
65
80
  type: 'publicClient',
66
81
  }) as PublicClient<TTransport, TChain>
67
82
  return {
83
+ batch,
68
84
  ...client,
69
85
  ...publicActions(client),
70
86
  }
@@ -0,0 +1 @@
1
+ export const aggregate3Signature = '0x82ad56cb'
@@ -1,5 +1,7 @@
1
1
  export { multicall3Abi } from './abis.js'
2
2
 
3
+ export { aggregate3Signature } from './contract.js'
4
+
3
5
  export { panicReasons, solidityError, solidityPanic } from './solidity.js'
4
6
 
5
7
  export { etherUnits, gweiUnits, weiUnits } from './unit.js'
@@ -67,6 +67,14 @@ export class ChainNotFoundError extends BaseError {
67
67
  }
68
68
  }
69
69
 
70
+ export class ClientChainNotConfiguredError extends BaseError {
71
+ override name = 'ClientChainNotConfiguredError'
72
+
73
+ constructor() {
74
+ super('No chain was provided to the Client.')
75
+ }
76
+ }
77
+
70
78
  export class InvalidChainIdError extends BaseError {
71
79
  override name = 'InvalidChainIdError'
72
80
 
@@ -194,7 +194,7 @@ export class ContractFunctionRevertedError extends BaseError {
194
194
  } else if (message) reason = message
195
195
 
196
196
  super(
197
- reason
197
+ reason && reason !== 'execution reverted'
198
198
  ? [
199
199
  `The contract function "${functionName}" reverted with the following reason:`,
200
200
  reason,