viem 0.0.0-main.20230518T142520 → 0.0.0-portable-types-20230518183529

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 (111) hide show
  1. package/LICENSE +1 -1
  2. package/dist/cjs/actions/ens/getEnsAddress.js +1 -1
  3. package/dist/cjs/actions/ens/getEnsAddress.js.map +1 -1
  4. package/dist/cjs/actions/public/waitForTransactionReceipt.js +7 -21
  5. package/dist/cjs/actions/public/waitForTransactionReceipt.js.map +1 -1
  6. package/dist/cjs/errors/abi.js +2 -0
  7. package/dist/cjs/errors/abi.js.map +1 -1
  8. package/dist/cjs/errors/data.js +2 -2
  9. package/dist/cjs/errors/data.js.map +1 -1
  10. package/dist/cjs/errors/version.js +1 -1
  11. package/dist/cjs/errors/version.js.map +1 -1
  12. package/dist/cjs/index.js.map +1 -1
  13. package/dist/cjs/utils/abi/decodeAbiParameters.js +14 -22
  14. package/dist/cjs/utils/abi/decodeAbiParameters.js.map +1 -1
  15. package/dist/cjs/utils/abi/decodeEventLog.js +5 -5
  16. package/dist/cjs/utils/abi/decodeEventLog.js.map +1 -1
  17. package/dist/cjs/utils/data/isHex.js +2 -2
  18. package/dist/cjs/utils/data/isHex.js.map +1 -1
  19. package/dist/cjs/utils/data/size.js +1 -1
  20. package/dist/cjs/utils/data/size.js.map +1 -1
  21. package/dist/cjs/utils/data/slice.js +12 -36
  22. package/dist/cjs/utils/data/slice.js.map +1 -1
  23. package/dist/cjs/utils/data/trim.js +1 -1
  24. package/dist/cjs/utils/data/trim.js.map +1 -1
  25. package/dist/cjs/utils/encoding/fromHex.js +2 -2
  26. package/dist/cjs/utils/encoding/fromHex.js.map +1 -1
  27. package/dist/cjs/utils/hash/keccak256.js +1 -1
  28. package/dist/cjs/utils/hash/keccak256.js.map +1 -1
  29. package/dist/cjs/utils/promise/createBatchScheduler.js.map +1 -1
  30. package/dist/cjs/utils/rpc.js +40 -49
  31. package/dist/cjs/utils/rpc.js.map +1 -1
  32. package/dist/cjs/utils/transaction/serializeTransaction.js +1 -2
  33. package/dist/cjs/utils/transaction/serializeTransaction.js.map +1 -1
  34. package/dist/esm/actions/ens/getEnsAddress.js +1 -1
  35. package/dist/esm/actions/ens/getEnsAddress.js.map +1 -1
  36. package/dist/esm/actions/public/waitForTransactionReceipt.js +7 -21
  37. package/dist/esm/actions/public/waitForTransactionReceipt.js.map +1 -1
  38. package/dist/esm/errors/abi.js +2 -0
  39. package/dist/esm/errors/abi.js.map +1 -1
  40. package/dist/esm/errors/data.js +2 -2
  41. package/dist/esm/errors/data.js.map +1 -1
  42. package/dist/esm/errors/version.js +1 -1
  43. package/dist/esm/errors/version.js.map +1 -1
  44. package/dist/esm/index.js.map +1 -1
  45. package/dist/esm/utils/abi/decodeAbiParameters.js +15 -23
  46. package/dist/esm/utils/abi/decodeAbiParameters.js.map +1 -1
  47. package/dist/esm/utils/abi/decodeEventLog.js +5 -5
  48. package/dist/esm/utils/abi/decodeEventLog.js.map +1 -1
  49. package/dist/esm/utils/data/isHex.js +2 -2
  50. package/dist/esm/utils/data/isHex.js.map +1 -1
  51. package/dist/esm/utils/data/size.js +1 -1
  52. package/dist/esm/utils/data/size.js.map +1 -1
  53. package/dist/esm/utils/data/slice.js +12 -36
  54. package/dist/esm/utils/data/slice.js.map +1 -1
  55. package/dist/esm/utils/data/trim.js +1 -1
  56. package/dist/esm/utils/data/trim.js.map +1 -1
  57. package/dist/esm/utils/encoding/fromHex.js +2 -2
  58. package/dist/esm/utils/encoding/fromHex.js.map +1 -1
  59. package/dist/esm/utils/hash/keccak256.js +1 -1
  60. package/dist/esm/utils/hash/keccak256.js.map +1 -1
  61. package/dist/esm/utils/promise/createBatchScheduler.js.map +1 -1
  62. package/dist/esm/utils/rpc.js +40 -49
  63. package/dist/esm/utils/rpc.js.map +1 -1
  64. package/dist/esm/utils/transaction/serializeTransaction.js +1 -2
  65. package/dist/esm/utils/transaction/serializeTransaction.js.map +1 -1
  66. package/dist/types/actions/public/getTransactionReceipt.d.ts +1 -1
  67. package/dist/types/actions/public/waitForTransactionReceipt.d.ts.map +1 -1
  68. package/dist/types/errors/abi.d.ts.map +1 -1
  69. package/dist/types/errors/data.d.ts +1 -2
  70. package/dist/types/errors/data.d.ts.map +1 -1
  71. package/dist/types/errors/version.d.ts +1 -1
  72. package/dist/types/errors/version.d.ts.map +1 -1
  73. package/dist/types/index.d.ts +2 -2
  74. package/dist/types/index.d.ts.map +1 -1
  75. package/dist/types/types/contract.d.ts +33 -7
  76. package/dist/types/types/contract.d.ts.map +1 -1
  77. package/dist/types/types/log.d.ts +2 -0
  78. package/dist/types/types/log.d.ts.map +1 -1
  79. package/dist/types/utils/abi/decodeAbiParameters.d.ts.map +1 -1
  80. package/dist/types/utils/data/isHex.d.ts +1 -3
  81. package/dist/types/utils/data/isHex.d.ts.map +1 -1
  82. package/dist/types/utils/data/slice.d.ts +3 -9
  83. package/dist/types/utils/data/slice.d.ts.map +1 -1
  84. package/dist/types/utils/data/trim.d.ts.map +1 -1
  85. package/dist/types/utils/encoding/fromHex.d.ts +1 -1
  86. package/dist/types/utils/hash/keccak256.d.ts.map +1 -1
  87. package/dist/types/utils/promise/createBatchScheduler.d.ts +4 -6
  88. package/dist/types/utils/promise/createBatchScheduler.d.ts.map +1 -1
  89. package/dist/types/utils/rpc.d.ts.map +1 -1
  90. package/dist/types/utils/transaction/serializeTransaction.d.ts.map +1 -1
  91. package/package.json +2 -2
  92. package/src/actions/ens/getEnsAddress.ts +1 -1
  93. package/src/actions/public/getTransactionReceipt.ts +1 -1
  94. package/src/actions/public/waitForTransactionReceipt.ts +6 -31
  95. package/src/errors/abi.ts +2 -0
  96. package/src/errors/data.ts +2 -8
  97. package/src/errors/version.ts +1 -1
  98. package/src/index.ts +23 -2
  99. package/src/types/contract.ts +87 -11
  100. package/src/types/log.ts +2 -2
  101. package/src/utils/abi/decodeAbiParameters.ts +15 -36
  102. package/src/utils/abi/decodeEventLog.ts +5 -5
  103. package/src/utils/data/isHex.ts +2 -5
  104. package/src/utils/data/size.ts +1 -1
  105. package/src/utils/data/slice.ts +11 -43
  106. package/src/utils/data/trim.ts +1 -3
  107. package/src/utils/encoding/fromHex.ts +3 -3
  108. package/src/utils/hash/keccak256.ts +1 -3
  109. package/src/utils/promise/createBatchScheduler.ts +7 -24
  110. package/src/utils/rpc.ts +49 -59
  111. package/src/utils/transaction/serializeTransaction.ts +2 -3
@@ -1,10 +1,7 @@
1
1
  import type { Hex } from '../../types/misc.js'
2
2
 
3
- export function isHex(
4
- value: unknown,
5
- { strict = true }: { strict?: boolean } = {},
6
- ): value is Hex {
3
+ export function isHex(value: unknown): value is Hex {
7
4
  if (!value) return false
8
5
  if (typeof value !== 'string') return false
9
- return strict ? /^0x[0-9a-fA-F]*$/.test(value) : value.startsWith('0x')
6
+ return /^0x[0-9a-fA-F]*$/.test(value)
10
7
  }
@@ -9,6 +9,6 @@ import { isHex } from './isHex.js'
9
9
  * @returns The size of the value (in bytes).
10
10
  */
11
11
  export function size(value: Hex | ByteArray) {
12
- if (isHex(value, { strict: false })) return Math.ceil((value.length - 2) / 2)
12
+ if (isHex(value)) return Math.ceil((value.length - 2) / 2)
13
13
  return value.length
14
14
  }
@@ -19,38 +19,15 @@ export function slice<TValue extends ByteArray | Hex>(
19
19
  value: TValue,
20
20
  start?: number,
21
21
  end?: number,
22
- { strict }: { strict?: boolean } = {},
23
22
  ): SliceReturnType<TValue> {
24
- if (isHex(value, { strict: false }))
25
- return sliceHex(value as Hex, start, end, {
26
- strict,
27
- }) as SliceReturnType<TValue>
28
- return sliceBytes(value as ByteArray, start, end, {
29
- strict,
30
- }) as SliceReturnType<TValue>
23
+ if (isHex(value))
24
+ return sliceHex(value as Hex, start, end) as SliceReturnType<TValue>
25
+ return sliceBytes(value as ByteArray, start, end) as SliceReturnType<TValue>
31
26
  }
32
27
 
33
28
  function assertStartOffset(value: Hex | ByteArray, start?: number) {
34
29
  if (typeof start === 'number' && start > 0 && start > size(value) - 1)
35
- throw new SliceOffsetOutOfBoundsError({
36
- offset: start,
37
- position: 'start',
38
- size: size(value),
39
- })
40
- }
41
-
42
- function assertEndOffset(value: Hex | ByteArray, start?: number, end?: number) {
43
- if (
44
- typeof start === 'number' &&
45
- typeof end === 'number' &&
46
- size(value) !== end - start
47
- ) {
48
- throw new SliceOffsetOutOfBoundsError({
49
- offset: end,
50
- position: 'end',
51
- size: size(value),
52
- })
53
- }
30
+ throw new SliceOffsetOutOfBoundsError({ offset: start, size: size(value) })
54
31
  }
55
32
 
56
33
  /**
@@ -61,15 +38,12 @@ function assertEndOffset(value: Hex | ByteArray, start?: number, end?: number) {
61
38
  * @param end The end offset (in bytes).
62
39
  */
63
40
  export function sliceBytes(
64
- value_: ByteArray,
41
+ value: ByteArray,
65
42
  start?: number,
66
43
  end?: number,
67
- { strict }: { strict?: boolean } = {},
68
44
  ): ByteArray {
69
- assertStartOffset(value_, start)
70
- const value = value_.slice(start, end)
71
- if (strict) assertEndOffset(value, start, end)
72
- return value
45
+ assertStartOffset(value, start)
46
+ return value.slice(start, end)
73
47
  }
74
48
 
75
49
  /**
@@ -79,16 +53,10 @@ export function sliceBytes(
79
53
  * @param start The start offset (in bytes).
80
54
  * @param end The end offset (in bytes).
81
55
  */
82
- export function sliceHex(
83
- value_: Hex,
84
- start?: number,
85
- end?: number,
86
- { strict }: { strict?: boolean } = {},
87
- ): Hex {
56
+ export function sliceHex(value_: Hex, start?: number, end?: number): Hex {
88
57
  assertStartOffset(value_, start)
89
- const value = `0x${value_
58
+ const value = value_
90
59
  .replace('0x', '')
91
- .slice((start ?? 0) * 2, (end ?? value_.length) * 2)}` as const
92
- if (strict) assertEndOffset(value, start, end)
93
- return value
60
+ .slice((start ?? 0) * 2, (end ?? value_.length) * 2)
61
+ return `0x${value}`
94
62
  }
@@ -27,9 +27,7 @@ export function trim<TValue extends ByteArray | Hex>(
27
27
 
28
28
  if (typeof hexOrBytes === 'string') {
29
29
  if (data.length === 1 && dir === 'right') data = `${data}0`
30
- return `0x${
31
- data.length % 2 === 1 ? `0${data}` : data
32
- }` as TrimReturnType<TValue>
30
+ return `0x${data}` as TrimReturnType<TValue>
33
31
  }
34
32
  return data as TrimReturnType<TValue>
35
33
  }
@@ -140,7 +140,7 @@ export type HexToBoolOpts = {
140
140
  *
141
141
  * @example
142
142
  * import { hexToBool } from 'viem'
143
- * const data = hexToBool('0x01')
143
+ * const data = hexToBool('0x1')
144
144
  * // true
145
145
  *
146
146
  * @example
@@ -154,8 +154,8 @@ export function hexToBool(hex_: Hex, opts: HexToBoolOpts = {}): boolean {
154
154
  assertSize(hex, { size: opts.size })
155
155
  hex = trim(hex)
156
156
  }
157
- if (trim(hex) === '0x00') return false
158
- if (trim(hex) === '0x01') return true
157
+ if (trim(hex) === '0x0') return false
158
+ if (trim(hex) === '0x1') return true
159
159
  throw new InvalidHexBooleanError(hex)
160
160
  }
161
161
 
@@ -16,9 +16,7 @@ export function keccak256<TTo extends To = 'hex'>(
16
16
  to_?: TTo,
17
17
  ): Keccak256Hash<TTo> {
18
18
  const to = to_ || 'hex'
19
- const bytes = keccak_256(
20
- isHex(value, { strict: false }) ? toBytes(value) : value,
21
- )
19
+ const bytes = keccak_256(isHex(value) ? toBytes(value) : value)
22
20
  if (to === 'bytes') return bytes as Keccak256Hash<TTo>
23
21
  return toHex(bytes) as Keccak256Hash<TTo>
24
22
  }
@@ -10,25 +10,6 @@ type PendingPromise<TReturnType extends readonly unknown[] = any> = {
10
10
 
11
11
  type SchedulerItem = { args: unknown; pendingPromise: PendingPromise }
12
12
 
13
- export type CreateBatchSchedulerArguments<
14
- TParameters = unknown,
15
- TReturnType extends readonly unknown[] = readonly unknown[],
16
- > = {
17
- fn: (args: TParameters[]) => Promise<TReturnType>
18
- id: number | string
19
- shouldSplitBatch?: (args: TParameters[]) => boolean
20
- wait?: number
21
- }
22
- export type CreateBatchSchedulerReturnType<
23
- TParameters = unknown,
24
- TReturnType extends readonly unknown[] = readonly unknown[],
25
- > = {
26
- flush: () => void
27
- schedule: TParameters extends undefined
28
- ? (args?: TParameters) => Promise<Resolved<TReturnType>>
29
- : (args: TParameters) => Promise<Resolved<TReturnType>>
30
- }
31
-
32
13
  const schedulerCache = new Map<number | string, SchedulerItem[]>()
33
14
 
34
15
  export function createBatchScheduler<
@@ -39,10 +20,12 @@ export function createBatchScheduler<
39
20
  id,
40
21
  shouldSplitBatch,
41
22
  wait = 0,
42
- }: CreateBatchSchedulerArguments<
43
- TParameters,
44
- TReturnType
45
- >): CreateBatchSchedulerReturnType<TParameters, TReturnType> {
23
+ }: {
24
+ fn: (args: TParameters[]) => Promise<TReturnType>
25
+ id: number | string
26
+ shouldSplitBatch?: (args: TParameters[]) => boolean
27
+ wait?: number
28
+ }) {
46
29
  const exec = async () => {
47
30
  const scheduler = getScheduler()
48
31
  flush()
@@ -95,5 +78,5 @@ export function createBatchScheduler<
95
78
  setTimeout(exec, wait)
96
79
  return promise
97
80
  },
98
- } as unknown as CreateBatchSchedulerReturnType<TParameters, TReturnType>
81
+ }
99
82
  }
package/src/utils/rpc.ts CHANGED
@@ -7,7 +7,6 @@ import {
7
7
  WebSocketRequestError,
8
8
  } from '../errors/request.js'
9
9
 
10
- import { createBatchScheduler } from './promise/createBatchScheduler.js'
11
10
  import { withTimeout } from './promise/withTimeout.js'
12
11
  import { stringify } from './stringify.js'
13
12
 
@@ -147,70 +146,61 @@ export async function getSocket(url_: string) {
147
146
  // If the socket already exists, return it.
148
147
  if (socket) return socket
149
148
 
150
- const { schedule } = createBatchScheduler<undefined, [Socket]>({
151
- id: urlKey,
152
- fn: async () => {
153
- let WebSocket = await import('isomorphic-ws')
154
- // Workaround for Vite.
155
- // https://github.com/vitejs/vite/issues/9703
156
- // TODO: Remove when issue is resolved.
157
- if (
158
- (WebSocket as unknown as { default?: typeof WebSocket }).default
159
- ?.constructor
160
- )
161
- WebSocket = (WebSocket as unknown as { default: typeof WebSocket })
162
- .default
163
- else WebSocket = WebSocket.WebSocket
164
-
165
- const webSocket = new WebSocket(url)
166
-
167
- // Set up a cache for incoming "synchronous" requests.
168
- const requests = new Map<Id, CallbackFn>()
169
-
170
- // Set up a cache for subscriptions (eth_subscribe).
171
- const subscriptions = new Map<Id, CallbackFn>()
172
-
173
- const onMessage: (event: MessageEvent) => void = ({ data }) => {
174
- const message: RpcResponse = JSON.parse(data as string)
175
- const isSubscription = message.method === 'eth_subscription'
176
- const id = isSubscription ? message.params.subscription : message.id
177
- const cache = isSubscription ? subscriptions : requests
178
- const callback = cache.get(id)
179
- if (callback) callback({ data })
180
- if (!isSubscription) cache.delete(id)
181
- }
182
- const onClose = () => {
183
- sockets.delete(urlKey)
184
- webSocket.removeEventListener('close', onClose)
185
- webSocket.removeEventListener('message', onMessage)
186
- }
149
+ let WebSocket = await import('isomorphic-ws')
150
+ // Workaround for Vite.
151
+ // https://github.com/vitejs/vite/issues/9703
152
+ // TODO: Remove when issue is resolved.
153
+ if (
154
+ (WebSocket as unknown as { default?: typeof WebSocket }).default
155
+ ?.constructor
156
+ )
157
+ WebSocket = (WebSocket as unknown as { default: typeof WebSocket }).default
158
+ else WebSocket = WebSocket.WebSocket
187
159
 
188
- // Setup event listeners for RPC & subscription responses.
189
- webSocket.addEventListener('close', onClose)
190
- webSocket.addEventListener('message', onMessage)
160
+ const webSocket = new WebSocket(url)
191
161
 
192
- // Wait for the socket to open.
193
- if (webSocket.readyState === WebSocket.CONNECTING) {
194
- await new Promise((resolve, reject) => {
195
- if (!webSocket) return
196
- webSocket.onopen = resolve
197
- webSocket.onerror = reject
198
- })
199
- }
162
+ // Set up a cache for incoming "synchronous" requests.
163
+ const requests = new Map<Id, CallbackFn>()
200
164
 
201
- // Create a new socket instance.
202
- socket = Object.assign(webSocket, {
203
- requests,
204
- subscriptions,
205
- })
206
- sockets.set(urlKey, socket)
165
+ // Set up a cache for subscriptions (eth_subscribe).
166
+ const subscriptions = new Map<Id, CallbackFn>()
207
167
 
208
- return [socket]
209
- },
168
+ const onMessage: (event: MessageEvent) => void = ({ data }) => {
169
+ const message: RpcResponse = JSON.parse(data as string)
170
+ const isSubscription = message.method === 'eth_subscription'
171
+ const id = isSubscription ? message.params.subscription : message.id
172
+ const cache = isSubscription ? subscriptions : requests
173
+ const callback = cache.get(id)
174
+ if (callback) callback({ data })
175
+ if (!isSubscription) cache.delete(id)
176
+ }
177
+ const onClose = () => {
178
+ sockets.delete(urlKey)
179
+ webSocket.removeEventListener('close', onClose)
180
+ webSocket.removeEventListener('message', onMessage)
181
+ }
182
+
183
+ // Setup event listeners for RPC & subscription responses.
184
+ webSocket.addEventListener('close', onClose)
185
+ webSocket.addEventListener('message', onMessage)
186
+
187
+ // Wait for the socket to open.
188
+ if (webSocket.readyState === WebSocket.CONNECTING) {
189
+ await new Promise((resolve, reject) => {
190
+ if (!webSocket) return
191
+ webSocket.onopen = resolve
192
+ webSocket.onerror = reject
193
+ })
194
+ }
195
+
196
+ // Create a new socket instance.
197
+ socket = Object.assign(webSocket, {
198
+ requests,
199
+ subscriptions,
210
200
  })
201
+ sockets.set(urlKey, socket)
211
202
 
212
- const [_, [socket_]] = await schedule()
213
- return socket_
203
+ return socket
214
204
  }
215
205
 
216
206
  function webSocket(
@@ -18,7 +18,6 @@ import type {
18
18
  } from '../../types/transaction.js'
19
19
  import { isAddress } from '../address/isAddress.js'
20
20
  import { concatHex } from '../data/concat.js'
21
- import { trim } from '../data/trim.js'
22
21
  import { toHex } from '../encoding/toHex.js'
23
22
  import { type RecursiveArray, toRlp } from '../encoding/toRlp.js'
24
23
 
@@ -98,8 +97,8 @@ function serializeTransactionEIP1559(
98
97
  if (signature)
99
98
  serializedTransaction.push(
100
99
  signature.v === 27n ? '0x' : toHex(1), // yParity
101
- trim(signature.r),
102
- trim(signature.s),
100
+ signature.r,
101
+ signature.s,
103
102
  )
104
103
 
105
104
  return concatHex([