viem 0.3.2 → 0.3.4
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.
- package/README.md +6 -0
- package/dist/cjs/actions/public/call.js +97 -10
- package/dist/cjs/actions/public/call.js.map +1 -1
- package/dist/cjs/actions/public/simulateContract.js +1 -0
- package/dist/cjs/actions/public/simulateContract.js.map +1 -1
- package/dist/cjs/chains.js +1 -24
- package/dist/cjs/chains.js.map +1 -1
- package/dist/cjs/clients/createPublicClient.js +2 -1
- package/dist/cjs/clients/createPublicClient.js.map +1 -1
- package/dist/cjs/clients/transports/fallback.js +1 -1
- package/dist/cjs/clients/transports/fallback.js.map +1 -1
- package/dist/cjs/constants/contract.js +5 -0
- package/dist/cjs/constants/contract.js.map +1 -0
- package/dist/cjs/constants/index.js +3 -1
- package/dist/cjs/constants/index.js.map +1 -1
- package/dist/cjs/errors/chain.js +13 -1
- package/dist/cjs/errors/chain.js.map +1 -1
- package/dist/cjs/errors/contract.js +1 -1
- package/dist/cjs/errors/contract.js.map +1 -1
- package/dist/cjs/errors/encoding.js +13 -1
- package/dist/cjs/errors/encoding.js.map +1 -1
- package/dist/cjs/errors/index.js +5 -2
- package/dist/cjs/errors/index.js.map +1 -1
- package/dist/cjs/errors/version.js +1 -1
- package/dist/cjs/index.js +5 -4
- package/dist/cjs/index.js.map +1 -1
- package/dist/cjs/utils/encoding/fromBytes.js +29 -12
- package/dist/cjs/utils/encoding/fromBytes.js.map +1 -1
- package/dist/cjs/utils/encoding/fromHex.js +32 -10
- package/dist/cjs/utils/encoding/fromHex.js.map +1 -1
- package/dist/cjs/utils/encoding/toBytes.js +31 -15
- package/dist/cjs/utils/encoding/toBytes.js.map +1 -1
- package/dist/cjs/utils/encoding/toHex.js +31 -20
- package/dist/cjs/utils/encoding/toHex.js.map +1 -1
- package/dist/cjs/utils/errors/getContractError.js +7 -5
- package/dist/cjs/utils/errors/getContractError.js.map +1 -1
- package/dist/cjs/utils/promise/createBatchScheduler.js +47 -0
- package/dist/cjs/utils/promise/createBatchScheduler.js.map +1 -0
- package/dist/cjs/utils/promise/index.js +3 -1
- package/dist/cjs/utils/promise/index.js.map +1 -1
- package/dist/cjs/utils/transaction/serializeTransaction.js +13 -15
- package/dist/cjs/utils/transaction/serializeTransaction.js.map +1 -1
- package/dist/esm/actions/public/call.js +91 -4
- package/dist/esm/actions/public/call.js.map +1 -1
- package/dist/esm/actions/public/simulateContract.js +1 -0
- package/dist/esm/actions/public/simulateContract.js.map +1 -1
- package/dist/esm/clients/createPublicClient.js +2 -1
- package/dist/esm/clients/createPublicClient.js.map +1 -1
- package/dist/esm/clients/transports/fallback.js +1 -1
- package/dist/esm/clients/transports/fallback.js.map +1 -1
- package/dist/esm/constants/contract.js +2 -0
- package/dist/esm/constants/contract.js.map +1 -0
- package/dist/esm/constants/index.js +1 -0
- package/dist/esm/constants/index.js.map +1 -1
- package/dist/esm/errors/chain.js +11 -0
- package/dist/esm/errors/chain.js.map +1 -1
- package/dist/esm/errors/contract.js +1 -1
- package/dist/esm/errors/contract.js.map +1 -1
- package/dist/esm/errors/encoding.js +11 -0
- package/dist/esm/errors/encoding.js.map +1 -1
- package/dist/esm/errors/index.js +2 -2
- package/dist/esm/errors/index.js.map +1 -1
- package/dist/esm/errors/version.js +1 -1
- package/dist/esm/index.js +1 -1
- package/dist/esm/index.js.map +1 -1
- package/dist/esm/utils/encoding/fromBytes.js +30 -13
- package/dist/esm/utils/encoding/fromBytes.js.map +1 -1
- package/dist/esm/utils/encoding/fromHex.js +31 -10
- package/dist/esm/utils/encoding/fromHex.js.map +1 -1
- package/dist/esm/utils/encoding/toBytes.js +31 -15
- package/dist/esm/utils/encoding/toBytes.js.map +1 -1
- package/dist/esm/utils/encoding/toHex.js +28 -17
- package/dist/esm/utils/encoding/toHex.js.map +1 -1
- package/dist/esm/utils/errors/getContractError.js +7 -5
- package/dist/esm/utils/errors/getContractError.js.map +1 -1
- package/dist/esm/utils/promise/createBatchScheduler.js +43 -0
- package/dist/esm/utils/promise/createBatchScheduler.js.map +1 -0
- package/dist/esm/utils/promise/index.js +1 -0
- package/dist/esm/utils/promise/index.js.map +1 -1
- package/dist/esm/utils/transaction/serializeTransaction.js +13 -15
- package/dist/esm/utils/transaction/serializeTransaction.js.map +1 -1
- package/dist/types/actions/public/call.d.ts +1 -0
- package/dist/types/actions/public/call.d.ts.map +1 -1
- package/dist/types/actions/public/simulateContract.d.ts +1 -1
- package/dist/types/actions/public/simulateContract.d.ts.map +1 -1
- package/dist/types/clients/createPublicClient.d.ts +15 -3
- package/dist/types/clients/createPublicClient.d.ts.map +1 -1
- package/dist/types/constants/contract.d.ts +2 -0
- package/dist/types/constants/contract.d.ts.map +1 -0
- package/dist/types/constants/index.d.ts +1 -0
- package/dist/types/constants/index.d.ts.map +1 -1
- package/dist/types/errors/chain.d.ts +4 -0
- package/dist/types/errors/chain.d.ts.map +1 -1
- package/dist/types/errors/encoding.d.ts +7 -0
- package/dist/types/errors/encoding.d.ts.map +1 -1
- package/dist/types/errors/index.d.ts +2 -2
- package/dist/types/errors/index.d.ts.map +1 -1
- package/dist/types/errors/version.d.ts +1 -1
- package/dist/types/index.d.ts +1 -1
- package/dist/types/index.d.ts.map +1 -1
- package/dist/types/types/eip1193.d.ts +1 -1
- package/dist/types/types/eip1193.d.ts.map +1 -1
- package/dist/types/utils/encoding/fromBytes.d.ts +95 -10
- package/dist/types/utils/encoding/fromBytes.d.ts.map +1 -1
- package/dist/types/utils/encoding/fromHex.d.ts +122 -12
- package/dist/types/utils/encoding/fromHex.d.ts.map +1 -1
- package/dist/types/utils/encoding/toBytes.d.ts +113 -9
- package/dist/types/utils/encoding/toBytes.d.ts.map +1 -1
- package/dist/types/utils/encoding/toHex.d.ts +120 -10
- package/dist/types/utils/encoding/toHex.d.ts.map +1 -1
- package/dist/types/utils/errors/getContractError.d.ts.map +1 -1
- package/dist/types/utils/promise/createBatchScheduler.d.ts +15 -0
- package/dist/types/utils/promise/createBatchScheduler.d.ts.map +1 -0
- package/dist/types/utils/promise/index.d.ts +1 -0
- package/dist/types/utils/promise/index.d.ts.map +1 -1
- package/package.json +1 -1
- package/src/actions/public/call.ts +141 -7
- package/src/actions/public/simulateContract.ts +2 -1
- package/src/clients/createPublicClient.ts +17 -1
- package/src/clients/transports/fallback.ts +1 -1
- package/src/constants/contract.ts +1 -0
- package/src/constants/index.ts +2 -0
- package/src/errors/chain.ts +8 -0
- package/src/errors/contract.ts +1 -1
- package/src/errors/encoding.ts +9 -0
- package/src/errors/index.ts +2 -0
- package/src/errors/version.ts +1 -1
- package/src/index.ts +1 -0
- package/src/types/eip1193.ts +1 -1
- package/src/utils/encoding/fromBytes.ts +147 -18
- package/src/utils/encoding/fromHex.ts +162 -19
- package/src/utils/encoding/toBytes.ts +148 -18
- package/src/utils/encoding/toHex.ts +151 -26
- package/src/utils/errors/getContractError.ts +9 -5
- package/src/utils/promise/createBatchScheduler.ts +82 -0
- package/src/utils/promise/index.ts +1 -0
- package/src/utils/transaction/serializeTransaction.ts +13 -15
@@ -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;
|
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;AAQ1D,MAAM,MAAM,eAAe,GAAG;IAC5B,mDAAmD;IACnD,IAAI,CAAC,EAAE,MAAM,CAAA;CACd,CAAA;AAED;;;;;;;;;;;;;;;;;;;;;;;;GAwBG;AACH,wBAAgB,KAAK,CACnB,KAAK,EAAE,MAAM,GAAG,MAAM,GAAG,MAAM,GAAG,OAAO,GAAG,SAAS,EACrD,IAAI,GAAE,eAAoB,GACzB,GAAG,CAQL;AAED,MAAM,MAAM,aAAa,GAAG;IAC1B,mDAAmD;IACnD,IAAI,CAAC,EAAE,MAAM,CAAA;CACd,CAAA;AAED;;;;;;;;;;;;;;;;;;;;;;;GAuBG;AACH,wBAAgB,SAAS,CAAC,KAAK,EAAE,OAAO,EAAE,IAAI,GAAE,aAAkB,GAAG,GAAG,CAOvE;AAED,MAAM,MAAM,cAAc,GAAG;IAC3B,mDAAmD;IACnD,IAAI,CAAC,EAAE,MAAM,CAAA;CACd,CAAA;AAED;;;;;;;;;;;;;;;;;;GAkBG;AACH,wBAAgB,UAAU,CAAC,KAAK,EAAE,SAAS,EAAE,IAAI,GAAE,cAAmB,GAAG,GAAG,CAY3E;AAED,MAAM,MAAM,eAAe,GACvB;IACE,4DAA4D;IAC5D,MAAM,CAAC,EAAE,OAAO,CAAA;IAChB,mDAAmD;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,
|
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 +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
@@ -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
|
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
|
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: [
|
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'
|
package/src/constants/index.ts
CHANGED
package/src/errors/chain.ts
CHANGED
@@ -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
|
|
package/src/errors/contract.ts
CHANGED
@@ -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,
|
package/src/errors/encoding.ts
CHANGED
@@ -81,3 +81,12 @@ export class OffsetOutOfBoundsError extends BaseError {
|
|
81
81
|
)
|
82
82
|
}
|
83
83
|
}
|
84
|
+
|
85
|
+
export class SizeOverflowError extends BaseError {
|
86
|
+
override name = 'SizeOverflowError'
|
87
|
+
constructor({ givenSize, maxSize }: { givenSize: number; maxSize: number }) {
|
88
|
+
super(
|
89
|
+
`Size cannot exceed ${maxSize} bytes. Given size: ${givenSize} bytes.`,
|
90
|
+
)
|
91
|
+
}
|
92
|
+
}
|
package/src/errors/index.ts
CHANGED
@@ -37,6 +37,7 @@ export {
|
|
37
37
|
ChainDoesNotSupportContract,
|
38
38
|
ChainMismatchError,
|
39
39
|
ChainNotFoundError,
|
40
|
+
ClientChainNotConfiguredError,
|
40
41
|
InvalidChainIdError,
|
41
42
|
} from './chain.js'
|
42
43
|
|
@@ -58,6 +59,7 @@ export {
|
|
58
59
|
InvalidHexBooleanError,
|
59
60
|
InvalidHexValueError,
|
60
61
|
OffsetOutOfBoundsError,
|
62
|
+
SizeOverflowError,
|
61
63
|
} from './encoding.js'
|
62
64
|
|
63
65
|
export {
|
package/src/errors/version.ts
CHANGED
@@ -1 +1 @@
|
|
1
|
-
export const version = '0.3.
|
1
|
+
export const version = '0.3.4'
|
package/src/index.ts
CHANGED
package/src/types/eip1193.ts
CHANGED
@@ -1,8 +1,20 @@
|
|
1
1
|
import { InvalidBytesBooleanError } from '../../errors/index.js'
|
2
2
|
import type { ByteArray, Hex } from '../../types/index.js'
|
3
|
-
import {
|
3
|
+
import { trim } from '../data/trim.js'
|
4
|
+
import { assertSize, hexToBigInt, hexToNumber } from './fromHex.js'
|
4
5
|
import { bytesToHex } from './toHex.js'
|
5
6
|
|
7
|
+
export type FromBytesParameters<
|
8
|
+
TTo extends 'string' | 'hex' | 'bigint' | 'number' | 'boolean',
|
9
|
+
> =
|
10
|
+
| TTo
|
11
|
+
| {
|
12
|
+
/** Size of the bytes. */
|
13
|
+
size?: number
|
14
|
+
/** Type to convert to. */
|
15
|
+
to: TTo
|
16
|
+
}
|
17
|
+
|
6
18
|
type FromBytesReturnType<TTo> = TTo extends 'string'
|
7
19
|
? string
|
8
20
|
: TTo extends 'hex'
|
@@ -16,30 +28,106 @@ type FromBytesReturnType<TTo> = TTo extends 'string'
|
|
16
28
|
: never
|
17
29
|
|
18
30
|
/**
|
19
|
-
*
|
31
|
+
* Decodes a byte array into a UTF-8 string, hex value, number, bigint or boolean.
|
32
|
+
*
|
33
|
+
* - Docs: https://viem.sh/docs/utilities/fromBytes.html
|
34
|
+
* - Example: https://viem.sh/docs/utilities/fromBytes.html#usage
|
35
|
+
*
|
36
|
+
* @param bytes Byte array to decode.
|
37
|
+
* @param toOrOpts Type to convert to or options.
|
38
|
+
* @returns Decoded value.
|
39
|
+
*
|
40
|
+
* @example
|
41
|
+
* import { fromBytes } from 'viem'
|
42
|
+
* const data = fromBytes(new Uint8Array([1, 164]), 'number')
|
43
|
+
* // 420
|
44
|
+
*
|
45
|
+
* @example
|
46
|
+
* import { fromBytes } from 'viem'
|
47
|
+
* const data = fromBytes(
|
48
|
+
* new Uint8Array([72, 101, 108, 108, 111, 32, 87, 111, 114, 108, 100, 33]),
|
49
|
+
* 'string'
|
50
|
+
* )
|
51
|
+
* // 'Hello world'
|
20
52
|
*/
|
21
53
|
export function fromBytes<
|
22
54
|
TTo extends 'string' | 'hex' | 'bigint' | 'number' | 'boolean',
|
23
|
-
>(
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
55
|
+
>(
|
56
|
+
bytes: ByteArray,
|
57
|
+
toOrOpts: FromBytesParameters<TTo>,
|
58
|
+
): FromBytesReturnType<TTo> {
|
59
|
+
const opts = typeof toOrOpts === 'string' ? { to: toOrOpts } : toOrOpts
|
60
|
+
const to = opts.to
|
61
|
+
|
62
|
+
if (to === 'number')
|
63
|
+
return bytesToNumber(bytes, opts) as FromBytesReturnType<TTo>
|
64
|
+
if (to === 'bigint')
|
65
|
+
return bytesToBigint(bytes, opts) as FromBytesReturnType<TTo>
|
66
|
+
if (to === 'boolean')
|
67
|
+
return bytesToBool(bytes, opts) as FromBytesReturnType<TTo>
|
68
|
+
if (to === 'string')
|
69
|
+
return bytesToString(bytes, opts) as FromBytesReturnType<TTo>
|
70
|
+
return bytesToHex(bytes, opts) as FromBytesReturnType<TTo>
|
71
|
+
}
|
72
|
+
|
73
|
+
export type BytesToBigIntOpts = {
|
74
|
+
/** Whether or not the number of a signed representation. */
|
75
|
+
signed?: boolean
|
76
|
+
/** Size of the bytes. */
|
77
|
+
size?: number
|
29
78
|
}
|
30
79
|
|
31
80
|
/**
|
32
|
-
*
|
81
|
+
* Decodes a byte array into a bigint.
|
82
|
+
*
|
83
|
+
* - Docs: https://viem.sh/docs/utilities/fromBytes.html#bytestobigint
|
84
|
+
*
|
85
|
+
* @param bytes Byte array to decode.
|
86
|
+
* @param opts Options.
|
87
|
+
* @returns BigInt value.
|
88
|
+
*
|
89
|
+
* @example
|
90
|
+
* import { bytesToBigint } from 'viem'
|
91
|
+
* const data = bytesToBigint(new Uint8Array([1, 164]))
|
92
|
+
* // 420n
|
33
93
|
*/
|
34
|
-
export function bytesToBigint(
|
35
|
-
|
94
|
+
export function bytesToBigint(
|
95
|
+
bytes: ByteArray,
|
96
|
+
opts: BytesToBigIntOpts = {},
|
97
|
+
): bigint {
|
98
|
+
if (typeof opts.size !== 'undefined') assertSize(bytes, { size: opts.size })
|
99
|
+
const hex = bytesToHex(bytes, opts)
|
36
100
|
return hexToBigInt(hex)
|
37
101
|
}
|
38
102
|
|
103
|
+
export type BytesToBoolOpts = {
|
104
|
+
/** Size of the bytes. */
|
105
|
+
size?: number
|
106
|
+
}
|
107
|
+
|
39
108
|
/**
|
40
|
-
*
|
109
|
+
* Decodes a byte array into a boolean.
|
110
|
+
*
|
111
|
+
* - Docs: https://viem.sh/docs/utilities/fromBytes.html#bytestobool
|
112
|
+
*
|
113
|
+
* @param bytes Byte array to decode.
|
114
|
+
* @param opts Options.
|
115
|
+
* @returns Boolean value.
|
116
|
+
*
|
117
|
+
* @example
|
118
|
+
* import { bytesToBool } from 'viem'
|
119
|
+
* const data = bytesToBool(new Uint8Array([1]))
|
120
|
+
* // true
|
41
121
|
*/
|
42
|
-
export function bytesToBool(
|
122
|
+
export function bytesToBool(
|
123
|
+
bytes_: ByteArray,
|
124
|
+
opts: BytesToBoolOpts = {},
|
125
|
+
): boolean {
|
126
|
+
let bytes = bytes_
|
127
|
+
if (typeof opts.size !== 'undefined') {
|
128
|
+
assertSize(bytes, { size: opts.size })
|
129
|
+
bytes = trim(bytes)
|
130
|
+
}
|
43
131
|
if (bytes.length > 1 || bytes[0] > 1)
|
44
132
|
throw new InvalidBytesBooleanError(bytes)
|
45
133
|
return Boolean(bytes[0])
|
@@ -47,17 +135,58 @@ export function bytesToBool(bytes: ByteArray): boolean {
|
|
47
135
|
|
48
136
|
export { bytesToHex }
|
49
137
|
|
138
|
+
export type BytesToNumberOpts = BytesToBigIntOpts
|
139
|
+
|
50
140
|
/**
|
51
|
-
*
|
141
|
+
* Decodes a byte array into a number.
|
142
|
+
*
|
143
|
+
* - Docs: https://viem.sh/docs/utilities/fromBytes.html#bytestonumber
|
144
|
+
*
|
145
|
+
* @param bytes Byte array to decode.
|
146
|
+
* @param opts Options.
|
147
|
+
* @returns Number value.
|
148
|
+
*
|
149
|
+
* @example
|
150
|
+
* import { bytesToNumber } from 'viem'
|
151
|
+
* const data = bytesToNumber(new Uint8Array([1, 164]))
|
152
|
+
* // 420
|
52
153
|
*/
|
53
|
-
export function bytesToNumber(
|
54
|
-
|
154
|
+
export function bytesToNumber(
|
155
|
+
bytes: ByteArray,
|
156
|
+
opts: BytesToNumberOpts = {},
|
157
|
+
): number {
|
158
|
+
if (typeof opts.size !== 'undefined') assertSize(bytes, { size: opts.size })
|
159
|
+
const hex = bytesToHex(bytes, opts)
|
55
160
|
return hexToNumber(hex)
|
56
161
|
}
|
57
162
|
|
163
|
+
export type BytesToStringOpts = {
|
164
|
+
/** Size of the bytes. */
|
165
|
+
size?: number
|
166
|
+
}
|
167
|
+
|
58
168
|
/**
|
59
|
-
*
|
169
|
+
* Decodes a byte array into a UTF-8 string.
|
170
|
+
*
|
171
|
+
* - Docs: https://viem.sh/docs/utilities/fromBytes.html#bytestostring
|
172
|
+
*
|
173
|
+
* @param bytes Byte array to decode.
|
174
|
+
* @param opts Options.
|
175
|
+
* @returns String value.
|
176
|
+
*
|
177
|
+
* @example
|
178
|
+
* import { bytesToString } from 'viem'
|
179
|
+
* const data = bytesToString(new Uint8Array([72, 101, 108, 108, 111, 32, 87, 111, 114, 108, 100, 33]))
|
180
|
+
* // 'Hello world'
|
60
181
|
*/
|
61
|
-
export function bytesToString(
|
182
|
+
export function bytesToString(
|
183
|
+
bytes_: ByteArray,
|
184
|
+
opts: BytesToStringOpts = {},
|
185
|
+
): string {
|
186
|
+
let bytes = bytes_
|
187
|
+
if (typeof opts.size !== 'undefined') {
|
188
|
+
assertSize(bytes, { size: opts.size })
|
189
|
+
bytes = trim(bytes, { dir: 'right' })
|
190
|
+
}
|
62
191
|
return new TextDecoder().decode(bytes)
|
63
192
|
}
|