-
Notifications
You must be signed in to change notification settings - Fork 210
Expand file tree
/
Copy pathencodeCalls.ts
More file actions
35 lines (32 loc) · 998 Bytes
/
encodeCalls.ts
File metadata and controls
35 lines (32 loc) · 998 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
import { encodeFunctionData } from "viem";
import type { Abi, Address } from "abitype";
import type { Call } from "viem";
import type { Hex } from "viem";
type EncodedCall = {
to: Address;
data?: Hex;
value?: bigint;
};
/**
* Encodes an array of calls, converting any abi-style calls to encoded data.
* Calls that already have encoded `data` are passed through unchanged.
*
* @param {ReadonlyArray<Call>} calls - Array of calls, either encoded or abi-style
* @returns {Array<EncodedCall>} Array of calls with encoded data
*/
export function encodeCalls(calls: readonly Call[]): EncodedCall[] {
return calls.map((call) => {
const data = call.abi
? encodeFunctionData({
abi: call.abi as Abi,
functionName: call.functionName!,
args: call.args as readonly unknown[],
})
: call.data;
return {
to: call.to,
...(data != null ? { data } : {}),
...(call.value != null ? { value: call.value } : {}),
};
});
}