viem 1.4.0 → 1.4.2

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 (57) hide show
  1. package/README.md +6 -0
  2. package/dist/cjs/actions/index.js.map +1 -1
  3. package/dist/cjs/contract.js.map +1 -1
  4. package/dist/cjs/errors/abi.js +8 -1
  5. package/dist/cjs/errors/abi.js.map +1 -1
  6. package/dist/cjs/errors/contract.js +56 -32
  7. package/dist/cjs/errors/contract.js.map +1 -1
  8. package/dist/cjs/errors/version.js +1 -1
  9. package/dist/cjs/index.js.map +1 -1
  10. package/dist/cjs/public.js.map +1 -1
  11. package/dist/cjs/utils/abi/encodePacked.js +2 -0
  12. package/dist/cjs/utils/abi/encodePacked.js.map +1 -1
  13. package/dist/cjs/utils/unit/parseUnits.js +2 -1
  14. package/dist/cjs/utils/unit/parseUnits.js.map +1 -1
  15. package/dist/esm/actions/index.js.map +1 -1
  16. package/dist/esm/contract.js.map +1 -1
  17. package/dist/esm/errors/abi.js +8 -1
  18. package/dist/esm/errors/abi.js.map +1 -1
  19. package/dist/esm/errors/contract.js +56 -32
  20. package/dist/esm/errors/contract.js.map +1 -1
  21. package/dist/esm/errors/version.js +1 -1
  22. package/dist/esm/index.js.map +1 -1
  23. package/dist/esm/public.js.map +1 -1
  24. package/dist/esm/utils/abi/encodePacked.js +2 -0
  25. package/dist/esm/utils/abi/encodePacked.js.map +1 -1
  26. package/dist/esm/utils/unit/parseUnits.js +2 -1
  27. package/dist/esm/utils/unit/parseUnits.js.map +1 -1
  28. package/dist/types/actions/index.d.ts +5 -1
  29. package/dist/types/actions/index.d.ts.map +1 -1
  30. package/dist/types/actions/public/watchContractEvent.d.ts +3 -3
  31. package/dist/types/actions/public/watchContractEvent.d.ts.map +1 -1
  32. package/dist/types/actions/public/watchEvent.d.ts +3 -3
  33. package/dist/types/actions/public/watchEvent.d.ts.map +1 -1
  34. package/dist/types/contract.d.ts +5 -1
  35. package/dist/types/contract.d.ts.map +1 -1
  36. package/dist/types/errors/abi.d.ts +1 -0
  37. package/dist/types/errors/abi.d.ts.map +1 -1
  38. package/dist/types/errors/contract.d.ts +1 -0
  39. package/dist/types/errors/contract.d.ts.map +1 -1
  40. package/dist/types/errors/version.d.ts +1 -1
  41. package/dist/types/index.d.ts +6 -2
  42. package/dist/types/index.d.ts.map +1 -1
  43. package/dist/types/public.d.ts +5 -1
  44. package/dist/types/public.d.ts.map +1 -1
  45. package/dist/types/utils/unit/parseUnits.d.ts.map +1 -1
  46. package/package.json +1 -1
  47. package/src/actions/index.ts +6 -2
  48. package/src/actions/public/watchContractEvent.ts +4 -4
  49. package/src/actions/public/watchEvent.ts +4 -4
  50. package/src/contract.ts +6 -2
  51. package/src/errors/abi.ts +5 -1
  52. package/src/errors/contract.ts +52 -31
  53. package/src/errors/version.ts +1 -1
  54. package/src/index.ts +7 -2
  55. package/src/public.ts +6 -2
  56. package/src/utils/abi/encodePacked.ts +1 -0
  57. package/src/utils/unit/parseUnits.ts +2 -1
@@ -15,6 +15,7 @@ import { getAbiItem } from '../utils/abi/getAbiItem.js'
15
15
  import { formatEther } from '../utils/unit/formatEther.js'
16
16
  import { formatGwei } from '../utils/unit/formatGwei.js'
17
17
 
18
+ import { AbiErrorSignatureNotFoundError } from './abi.js'
18
19
  import { BaseError } from './base.js'
19
20
  import { prettyPrint } from './transaction.js'
20
21
  import { getContractAddress } from './utils.js'
@@ -154,6 +155,7 @@ export class ContractFunctionRevertedError extends BaseError {
154
155
 
155
156
  data?: DecodeErrorResultReturnType
156
157
  reason?: string
158
+ signature?: Hex
157
159
 
158
160
  constructor({
159
161
  abi,
@@ -161,56 +163,75 @@ export class ContractFunctionRevertedError extends BaseError {
161
163
  functionName,
162
164
  message,
163
165
  }: { abi: Abi; data?: Hex; functionName: string; message?: string }) {
166
+ let cause: Error | undefined
164
167
  let decodedData: DecodeErrorResultReturnType | undefined = undefined
165
168
  let metaMessages
166
169
  let reason
167
170
  if (data && data !== '0x') {
168
- decodedData = decodeErrorResult({ abi, data })
169
- const { abiItem, errorName, args: errorArgs } = decodedData
170
- if (errorName === 'Error') {
171
- reason = (errorArgs as [string])[0]
172
- } else if (errorName === 'Panic') {
173
- const [firstArg] = errorArgs as [number]
174
- reason = panicReasons[firstArg as keyof typeof panicReasons]
175
- } else {
176
- const errorWithParams = abiItem
177
- ? formatAbiItem(abiItem, { includeName: true })
178
- : undefined
179
- const formattedArgs =
180
- abiItem && errorArgs
181
- ? formatAbiItemWithArgs({
182
- abiItem,
183
- args: errorArgs,
184
- includeFunctionName: false,
185
- includeName: false,
186
- })
171
+ try {
172
+ decodedData = decodeErrorResult({ abi, data })
173
+ const { abiItem, errorName, args: errorArgs } = decodedData
174
+ if (errorName === 'Error') {
175
+ reason = (errorArgs as [string])[0]
176
+ } else if (errorName === 'Panic') {
177
+ const [firstArg] = errorArgs as [number]
178
+ reason = panicReasons[firstArg as keyof typeof panicReasons]
179
+ } else {
180
+ const errorWithParams = abiItem
181
+ ? formatAbiItem(abiItem, { includeName: true })
187
182
  : undefined
183
+ const formattedArgs =
184
+ abiItem && errorArgs
185
+ ? formatAbiItemWithArgs({
186
+ abiItem,
187
+ args: errorArgs,
188
+ includeFunctionName: false,
189
+ includeName: false,
190
+ })
191
+ : undefined
188
192
 
189
- metaMessages = [
190
- errorWithParams ? `Error: ${errorWithParams}` : '',
191
- formattedArgs && formattedArgs !== '()'
192
- ? ` ${[...Array(errorName?.length ?? 0).keys()]
193
- .map(() => ' ')
194
- .join('')}${formattedArgs}`
195
- : '',
196
- ]
193
+ metaMessages = [
194
+ errorWithParams ? `Error: ${errorWithParams}` : '',
195
+ formattedArgs && formattedArgs !== '()'
196
+ ? ` ${[...Array(errorName?.length ?? 0).keys()]
197
+ .map(() => ' ')
198
+ .join('')}${formattedArgs}`
199
+ : '',
200
+ ]
201
+ }
202
+ } catch (err) {
203
+ cause = err as Error
197
204
  }
198
205
  } else if (message) reason = message
199
206
 
207
+ let signature: Hex | undefined
208
+ if (cause instanceof AbiErrorSignatureNotFoundError) {
209
+ signature = cause.signature
210
+ metaMessages = [
211
+ `Unable to decode signature "${signature}" as it was not found on the provided ABI.`,
212
+ 'Make sure you are using the correct ABI and that the error exists on it.',
213
+ `You can look up the decoded signature here: https://openchain.xyz/signatures?query=${signature}.`,
214
+ ]
215
+ }
216
+
200
217
  super(
201
- reason && reason !== 'execution reverted'
218
+ (reason && reason !== 'execution reverted') || signature
202
219
  ? [
203
- `The contract function "${functionName}" reverted with the following reason:`,
204
- reason,
220
+ `The contract function "${functionName}" reverted with the following ${
221
+ signature ? 'signature' : 'reason'
222
+ }:`,
223
+ reason || signature,
205
224
  ].join('\n')
206
225
  : `The contract function "${functionName}" reverted.`,
207
226
  {
227
+ cause,
208
228
  metaMessages,
209
229
  },
210
230
  )
211
231
 
212
- this.reason = reason
213
232
  this.data = decodedData
233
+ this.reason = reason
234
+ this.signature = signature
214
235
  }
215
236
  }
216
237
 
@@ -1 +1 @@
1
- export const version = '1.3.1'
1
+ export const version = '1.4.2'
package/src/index.ts CHANGED
@@ -156,8 +156,12 @@ export type {
156
156
  WatchBlockNumberReturnType,
157
157
  } from './actions/public/watchBlockNumber.js'
158
158
  export type {
159
- OnLogsFn,
160
- OnLogsParameter,
159
+ WatchEventOnLogsFn,
160
+ /** @deprecated - use `WatchEventOnLogsFn` instead. */
161
+ WatchEventOnLogsFn as OnLogFn,
162
+ WatchEventOnLogsParameter,
163
+ /** @deprecated - use `WatchEventOnLogsParameter` instead. */
164
+ WatchEventOnLogsParameter as OnLogParameter,
161
165
  WatchEventParameters,
162
166
  WatchEventReturnType,
163
167
  } from './actions/public/watchEvent.js'
@@ -233,6 +237,7 @@ export type {
233
237
  VerifyHashReturnType,
234
238
  } from './actions/public/verifyHash.js'
235
239
  export type {
240
+ WatchContractEventOnLogsParameter,
236
241
  WatchContractEventParameters,
237
242
  WatchContractEventReturnType,
238
243
  } from './actions/public/watchContractEvent.js'
package/src/public.ts CHANGED
@@ -114,8 +114,12 @@ export {
114
114
  } from './actions/public/watchBlocks.js'
115
115
  export {
116
116
  watchEvent,
117
- type OnLogsFn,
118
- type OnLogsParameter,
117
+ type WatchEventOnLogsFn,
118
+ /** @deprecated - use `WatchEventOnLogsFn` instead. */
119
+ type WatchEventOnLogsFn as OnLogsFn,
120
+ type WatchEventOnLogsParameter,
121
+ /** @deprecated - use `WatchEventOnLogsParameter` instead. */
122
+ type WatchEventOnLogsParameter as OnLogsParameter,
119
123
  } from './actions/public/watchEvent.js'
120
124
  export {
121
125
  watchPendingTransactions,
@@ -104,6 +104,7 @@ function encode<TPackedAbiType extends PackedAbiType | unknown>(
104
104
  for (let i = 0; i < value.length; i++) {
105
105
  data.push(encode(childType, value[i], true))
106
106
  }
107
+ if (data.length === 0) return '0x'
107
108
  return concat(data)
108
109
  }
109
110
 
@@ -9,7 +9,8 @@ export function parseUnits(value: string, decimals: number) {
9
9
 
10
10
  // round off if the fraction is larger than the number of decimals.
11
11
  if (decimals === 0) {
12
- integer = `${Math.round(Number(`${integer}.${fraction}`))}`
12
+ if (Math.round(Number(`.${fraction}`)) === 1)
13
+ integer = `${BigInt(integer) + 1n}`
13
14
  fraction = ''
14
15
  } else if (fraction.length > decimals) {
15
16
  const [left, unit, right] = [