viem 0.3.13 → 0.3.14

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 (108) hide show
  1. package/dist/cjs/actions/public/call.js +12 -1
  2. package/dist/cjs/actions/public/call.js.map +1 -1
  3. package/dist/cjs/actions/public/simulateContract.js +2 -2
  4. package/dist/cjs/actions/public/simulateContract.js.map +1 -1
  5. package/dist/cjs/actions/wallet/writeContract.js +2 -2
  6. package/dist/cjs/actions/wallet/writeContract.js.map +1 -1
  7. package/dist/cjs/chains.js +2 -4
  8. package/dist/cjs/chains.js.map +1 -1
  9. package/dist/cjs/constants/address.js +5 -0
  10. package/dist/cjs/constants/address.js.map +1 -0
  11. package/dist/cjs/constants/index.js +3 -1
  12. package/dist/cjs/constants/index.js.map +1 -1
  13. package/dist/cjs/errors/base.js +17 -0
  14. package/dist/cjs/errors/base.js.map +1 -1
  15. package/dist/cjs/errors/ccip.js +68 -0
  16. package/dist/cjs/errors/ccip.js.map +1 -0
  17. package/dist/cjs/errors/index.js +7 -3
  18. package/dist/cjs/errors/index.js.map +1 -1
  19. package/dist/cjs/errors/request.js +1 -1
  20. package/dist/cjs/errors/request.js.map +1 -1
  21. package/dist/cjs/errors/version.js +1 -1
  22. package/dist/cjs/index.js +10 -4
  23. package/dist/cjs/index.js.map +1 -1
  24. package/dist/cjs/utils/ccip.js +97 -0
  25. package/dist/cjs/utils/ccip.js.map +1 -0
  26. package/dist/cjs/utils/errors/getContractError.js +1 -5
  27. package/dist/cjs/utils/errors/getContractError.js.map +1 -1
  28. package/dist/cjs/utils/errors/getNodeError.js +1 -1
  29. package/dist/cjs/utils/errors/getNodeError.js.map +1 -1
  30. package/dist/cjs/utils/index.js +8 -3
  31. package/dist/cjs/utils/index.js.map +1 -1
  32. package/dist/esm/actions/public/call.js +12 -2
  33. package/dist/esm/actions/public/call.js.map +1 -1
  34. package/dist/esm/actions/public/simulateContract.js +2 -2
  35. package/dist/esm/actions/public/simulateContract.js.map +1 -1
  36. package/dist/esm/actions/wallet/writeContract.js +2 -2
  37. package/dist/esm/actions/wallet/writeContract.js.map +1 -1
  38. package/dist/esm/chains.js +0 -1
  39. package/dist/esm/chains.js.map +1 -1
  40. package/dist/esm/constants/address.js +2 -0
  41. package/dist/esm/constants/address.js.map +1 -0
  42. package/dist/esm/constants/index.js +1 -0
  43. package/dist/esm/constants/index.js.map +1 -1
  44. package/dist/esm/errors/base.js +17 -0
  45. package/dist/esm/errors/base.js.map +1 -1
  46. package/dist/esm/errors/ccip.js +62 -0
  47. package/dist/esm/errors/ccip.js.map +1 -0
  48. package/dist/esm/errors/index.js +1 -0
  49. package/dist/esm/errors/index.js.map +1 -1
  50. package/dist/esm/errors/request.js +1 -1
  51. package/dist/esm/errors/request.js.map +1 -1
  52. package/dist/esm/errors/version.js +1 -1
  53. package/dist/esm/index.js +2 -2
  54. package/dist/esm/index.js.map +1 -1
  55. package/dist/esm/utils/ccip.js +92 -0
  56. package/dist/esm/utils/ccip.js.map +1 -0
  57. package/dist/esm/utils/errors/getContractError.js +3 -7
  58. package/dist/esm/utils/errors/getContractError.js.map +1 -1
  59. package/dist/esm/utils/errors/getNodeError.js +1 -1
  60. package/dist/esm/utils/errors/getNodeError.js.map +1 -1
  61. package/dist/esm/utils/index.js +1 -0
  62. package/dist/esm/utils/index.js.map +1 -1
  63. package/dist/types/actions/public/call.d.ts +1 -0
  64. package/dist/types/actions/public/call.d.ts.map +1 -1
  65. package/dist/types/actions/public/simulateContract.d.ts +4 -2
  66. package/dist/types/actions/public/simulateContract.d.ts.map +1 -1
  67. package/dist/types/actions/wallet/writeContract.d.ts +6 -3
  68. package/dist/types/actions/wallet/writeContract.d.ts.map +1 -1
  69. package/dist/types/chains.d.ts +0 -1
  70. package/dist/types/chains.d.ts.map +1 -1
  71. package/dist/types/constants/address.d.ts +2 -0
  72. package/dist/types/constants/address.d.ts.map +1 -0
  73. package/dist/types/constants/index.d.ts +1 -0
  74. package/dist/types/constants/index.d.ts.map +1 -1
  75. package/dist/types/errors/base.d.ts +2 -0
  76. package/dist/types/errors/base.d.ts.map +1 -1
  77. package/dist/types/errors/ccip.d.ts +29 -0
  78. package/dist/types/errors/ccip.d.ts.map +1 -0
  79. package/dist/types/errors/index.d.ts +1 -0
  80. package/dist/types/errors/index.d.ts.map +1 -1
  81. package/dist/types/errors/request.d.ts +2 -2
  82. package/dist/types/errors/request.d.ts.map +1 -1
  83. package/dist/types/errors/version.d.ts +1 -1
  84. package/dist/types/index.d.ts +2 -2
  85. package/dist/types/index.d.ts.map +1 -1
  86. package/dist/types/utils/ccip.d.ts +35 -0
  87. package/dist/types/utils/ccip.d.ts.map +1 -0
  88. package/dist/types/utils/errors/getContractError.d.ts.map +1 -1
  89. package/dist/types/utils/formatters/transaction.d.ts +1 -1
  90. package/dist/types/utils/index.d.ts +1 -0
  91. package/dist/types/utils/index.d.ts.map +1 -1
  92. package/package.json +1 -1
  93. package/src/actions/public/call.ts +12 -1
  94. package/src/actions/public/simulateContract.ts +5 -1
  95. package/src/actions/wallet/writeContract.ts +7 -2
  96. package/src/chains.ts +0 -1
  97. package/src/constants/address.ts +1 -0
  98. package/src/constants/index.ts +2 -0
  99. package/src/errors/base.ts +10 -0
  100. package/src/errors/ccip.ts +74 -0
  101. package/src/errors/index.ts +6 -0
  102. package/src/errors/request.ts +3 -3
  103. package/src/errors/version.ts +1 -1
  104. package/src/index.ts +5 -0
  105. package/src/utils/ccip.ts +136 -0
  106. package/src/utils/errors/getContractError.ts +1 -7
  107. package/src/utils/errors/getNodeError.ts +1 -1
  108. package/src/utils/index.ts +7 -0
@@ -59,4 +59,14 @@ export class BaseError extends Error {
59
59
  this.metaMessages = args.metaMessages
60
60
  this.shortMessage = shortMessage
61
61
  }
62
+
63
+ walk(fn?: (err: unknown) => boolean) {
64
+ return this.#walk(this, fn)
65
+ }
66
+
67
+ #walk(err: unknown, fn?: (err: unknown) => boolean): unknown {
68
+ if (fn?.(err)) return err
69
+ if ((err as Error).cause) return this.#walk((err as Error).cause, fn)
70
+ return err
71
+ }
62
72
  }
@@ -0,0 +1,74 @@
1
+ import type { Address } from 'abitype'
2
+ import { BaseError } from './base.js'
3
+ import type { Hex } from '../types/index.js'
4
+ import { getUrl } from './utils.js'
5
+
6
+ export class OffchainLookupError extends BaseError {
7
+ override name = 'OffchainLookupError'
8
+ constructor({
9
+ callbackSelector,
10
+ cause,
11
+ data,
12
+ extraData,
13
+ sender,
14
+ urls,
15
+ }: {
16
+ callbackSelector: Hex
17
+ cause: BaseError
18
+ data: Hex
19
+ extraData: Hex
20
+ sender: Address
21
+ urls: readonly string[]
22
+ }) {
23
+ super(
24
+ cause.shortMessage ||
25
+ 'An error occurred while fetching for an offchain result.',
26
+ {
27
+ cause,
28
+ metaMessages: [
29
+ ...(cause.metaMessages || []),
30
+ cause.metaMessages?.length ? '' : [],
31
+ 'Offchain Gateway Call:',
32
+ urls && [
33
+ ' Gateway URL(s):',
34
+ ...urls.map((url) => ` ${getUrl(url)}`),
35
+ ],
36
+ ` Sender: ${sender}`,
37
+ ` Data: ${data}`,
38
+ ` Callback selector: ${callbackSelector}`,
39
+ ` Extra data: ${extraData}`,
40
+ ].flat(),
41
+ },
42
+ )
43
+ }
44
+ }
45
+
46
+ export class OffchainLookupResponseMalformedError extends BaseError {
47
+ override name = 'OffchainLookupResponseMalformedError'
48
+ constructor({ result, url }: { result: any; url: string }) {
49
+ super(
50
+ 'Offchain gateway response is malformed. Response data must be a hex value.',
51
+ {
52
+ metaMessages: [
53
+ `Gateway URL: ${getUrl(url)}`,
54
+ `Response: ${JSON.stringify(result)}`,
55
+ ],
56
+ },
57
+ )
58
+ }
59
+ }
60
+
61
+ export class OffchainLookupSenderMismatchError extends BaseError {
62
+ override name = 'OffchainLookupSenderMismatchError'
63
+ constructor({ sender, to }: { sender: Address; to: Address }) {
64
+ super(
65
+ 'Reverted sender address does not match target contract address (`to`).',
66
+ {
67
+ metaMessages: [
68
+ `Contract address: ${to}`,
69
+ `OffchainLookup sender address: ${sender}`,
70
+ ],
71
+ },
72
+ )
73
+ }
74
+ }
@@ -41,6 +41,12 @@ export {
41
41
  InvalidChainIdError,
42
42
  } from './chain.js'
43
43
 
44
+ export {
45
+ OffchainLookupError,
46
+ OffchainLookupResponseMalformedError,
47
+ OffchainLookupSenderMismatchError,
48
+ } from './ccip.js'
49
+
44
50
  export {
45
51
  CallExecutionError,
46
52
  ContractFunctionExecutionError,
@@ -5,7 +5,7 @@ import { getUrl } from './utils.js'
5
5
  export class HttpRequestError extends BaseError {
6
6
  override name = 'HttpRequestError'
7
7
 
8
- body: { [key: string]: unknown }
8
+ body?: { [key: string]: unknown }
9
9
  headers?: Headers
10
10
  status?: number
11
11
  url: string
@@ -17,7 +17,7 @@ export class HttpRequestError extends BaseError {
17
17
  status,
18
18
  url,
19
19
  }: {
20
- body: { [key: string]: unknown }
20
+ body?: { [key: string]: unknown }
21
21
  details?: string
22
22
  headers?: Headers
23
23
  status?: number
@@ -28,7 +28,7 @@ export class HttpRequestError extends BaseError {
28
28
  metaMessages: [
29
29
  status && `Status: ${status}`,
30
30
  `URL: ${getUrl(url)}`,
31
- `Request body: ${stringify(body)}`,
31
+ body && `Request body: ${stringify(body)}`,
32
32
  ].filter(Boolean) as string[],
33
33
  })
34
34
  this.body = body
@@ -1 +1 @@
1
- export const version = '0.3.13'
1
+ export const version = '0.3.14'
package/src/index.ts CHANGED
@@ -159,6 +159,7 @@ export {
159
159
  etherUnits,
160
160
  gweiUnits,
161
161
  weiUnits,
162
+ zeroAddress,
162
163
  } from './constants/index.js'
163
164
 
164
165
  export {
@@ -398,6 +399,7 @@ export {
398
399
  bytesToHex,
399
400
  bytesToNumber,
400
401
  bytesToString,
402
+ ccipFetch,
401
403
  concat,
402
404
  concatBytes,
403
405
  concatHex,
@@ -452,6 +454,9 @@ export {
452
454
  keccak256,
453
455
  numberToBytes,
454
456
  numberToHex,
457
+ offchainLookup,
458
+ offchainLookupAbiItem,
459
+ offchainLookupSignature,
455
460
  pad,
456
461
  padBytes,
457
462
  padHex,
@@ -0,0 +1,136 @@
1
+ import { parseAbiItem, parseAbiParameters, type Address } from 'abitype'
2
+ import type { PublicClient, Transport } from '../clients/index.js'
3
+ import {
4
+ BaseError,
5
+ HttpRequestError,
6
+ OffchainLookupError,
7
+ OffchainLookupResponseMalformedError,
8
+ OffchainLookupSenderMismatchError,
9
+ } from '../errors/index.js'
10
+ import { call, type CallParameters } from '../public.js'
11
+ import type { Chain, GetErrorArgs, Hex } from '../types/index.js'
12
+ import { decodeErrorResult, encodeAbiParameters } from './abi/index.js'
13
+ import { isAddressEqual } from './address/index.js'
14
+ import { concat, isHex } from './data/index.js'
15
+ import { stringify } from './stringify.js'
16
+
17
+ export const offchainLookupSignature = '0x556f1830'
18
+ export const offchainLookupAbiItem = parseAbiItem(
19
+ 'error OffchainLookup(address sender, string[] urls, bytes callData, bytes4 callbackFunction, bytes extraData)',
20
+ )
21
+
22
+ export async function offchainLookup<TChain extends Chain | undefined>(
23
+ client: PublicClient<Transport, TChain>,
24
+ {
25
+ blockNumber,
26
+ blockTag,
27
+ data,
28
+ to,
29
+ }: Pick<CallParameters, 'blockNumber' | 'blockTag'> & {
30
+ data: Hex
31
+ to: Address
32
+ },
33
+ ): Promise<Hex> {
34
+ const { args } = decodeErrorResult({
35
+ data,
36
+ abi: [offchainLookupAbiItem],
37
+ }) as unknown as GetErrorArgs<
38
+ [typeof offchainLookupAbiItem],
39
+ 'OffchainLookup'
40
+ >
41
+ const [sender, urls, callData, callbackSelector, extraData] = args
42
+
43
+ try {
44
+ if (!isAddressEqual(to, sender))
45
+ throw new OffchainLookupSenderMismatchError({ sender, to })
46
+
47
+ const result = await ccipFetch({ data: callData, sender, urls })
48
+
49
+ const { data: data_ } = await call(client, {
50
+ blockNumber,
51
+ blockTag,
52
+ data: concat([
53
+ callbackSelector,
54
+ encodeAbiParameters(parseAbiParameters('bytes,bytes'), [
55
+ result,
56
+ extraData,
57
+ ]),
58
+ ]),
59
+ to,
60
+ } as CallParameters)
61
+
62
+ return data_!
63
+ } catch (err) {
64
+ throw new OffchainLookupError({
65
+ callbackSelector,
66
+ cause: err as BaseError,
67
+ data,
68
+ extraData,
69
+ sender,
70
+ urls,
71
+ })
72
+ }
73
+ }
74
+
75
+ export async function ccipFetch({
76
+ data,
77
+ sender,
78
+ urls,
79
+ }: { data: Hex; sender: Address; urls: readonly string[] }) {
80
+ let error = new Error('An unknown error occurred.')
81
+
82
+ for (let i = 0; i < urls.length; i++) {
83
+ const url = urls[i]
84
+ const method =
85
+ url.includes('{sender}') || url.includes('{data}') ? 'GET' : 'POST'
86
+ const body = method === 'POST' ? { data, sender } : undefined
87
+
88
+ try {
89
+ const response = await fetch(
90
+ url.replace('{sender}', sender).replace('{data}', data),
91
+ {
92
+ body: JSON.stringify(body),
93
+ method,
94
+ },
95
+ )
96
+
97
+ let result
98
+ if (
99
+ response.headers.get('Content-Type')?.startsWith('application/json')
100
+ ) {
101
+ result = (await response.json()).data
102
+ } else {
103
+ result = (await response.text()) as any
104
+ }
105
+
106
+ if (!response.ok) {
107
+ error = new HttpRequestError({
108
+ body,
109
+ details: stringify(result.error) || response.statusText,
110
+ headers: response.headers,
111
+ status: response.status,
112
+ url,
113
+ })
114
+ continue
115
+ }
116
+
117
+ if (!isHex(result)) {
118
+ error = new OffchainLookupResponseMalformedError({
119
+ result,
120
+ url,
121
+ })
122
+ continue
123
+ }
124
+
125
+ return result
126
+ } catch (err) {
127
+ error = new HttpRequestError({
128
+ body,
129
+ details: (err as Error).message,
130
+ url,
131
+ })
132
+ }
133
+ }
134
+
135
+ throw error
136
+ }
@@ -3,11 +3,9 @@ import type { BaseError } from '../../errors/index.js'
3
3
  import {
4
4
  AbiDecodingZeroDataError,
5
5
  ContractFunctionExecutionError,
6
- EstimateGasExecutionError,
7
6
  RawContractError,
8
7
  } from '../../errors/index.js'
9
8
  import {
10
- CallExecutionError,
11
9
  ContractFunctionRevertedError,
12
10
  ContractFunctionZeroDataError,
13
11
  } from '../../errors/contract.js'
@@ -36,11 +34,7 @@ export function getContractError(
36
34
  const { code, data, message, shortMessage } = (
37
35
  err instanceof RawContractError
38
36
  ? err
39
- : !(err.cause && 'data' in (err.cause as BaseError)) &&
40
- (err instanceof CallExecutionError ||
41
- err instanceof EstimateGasExecutionError)
42
- ? ((err.cause as BaseError)?.cause as BaseError)?.cause || {}
43
- : err.cause || {}
37
+ : err.walk((err) => 'data' in (err as Error))
44
38
  ) as RawContractError
45
39
 
46
40
  let cause = err
@@ -67,7 +67,7 @@ export function getNodeError(
67
67
  )
68
68
  return new ExecutionRevertedError({
69
69
  cause: err,
70
- message: (err.cause as BaseError).details,
70
+ message: (err.cause as BaseError).details || err.details,
71
71
  })
72
72
  return new UnknownNodeError({
73
73
  cause: (err.cause as BaseError).cause as BaseError,
@@ -60,6 +60,13 @@ export {
60
60
 
61
61
  export { buildRequest } from './buildRequest.js'
62
62
 
63
+ export {
64
+ ccipFetch,
65
+ offchainLookup,
66
+ offchainLookupAbiItem,
67
+ offchainLookupSignature,
68
+ } from './ccip.js'
69
+
63
70
  export { defineChain, getChainContractAddress } from './chain.js'
64
71
 
65
72
  export {