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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
//! `ERC-20` calldata builders for allowance management and token queries.
//!
//! Provides ABI-encoded calldata for common `ERC-20` and `EIP-2612`
//! functions needed when preparing `CoW` Protocol orders. All functions
//! return raw `Vec<u8>` calldata ready to be sent via `eth_call` or
//! included in a transaction.
//!
//! # Key functions
//!
//! | Function | Solidity signature | Size |
//! |---|---|---|
//! | [`build_erc20_approve_calldata`] | `approve(address,uint256)` | 68 B |
//! | [`build_erc20_balance_of_calldata`] | `balanceOf(address)` | 36 B |
//! | [`build_erc20_allowance_calldata`] | `allowance(address,address)` | 68 B |
//! | [`build_erc20_transfer_calldata`] | `transfer(address,uint256)` | 68 B |
//! | [`build_erc20_transfer_from_calldata`] | `transferFrom(address,address,uint256)` | 100 B |
//! | [`build_erc20_decimals_calldata`] | `decimals()` | 4 B |
//! | [`build_erc20_name_calldata`] | `name()` | 4 B |
//! | [`build_eip2612_nonces_calldata`] | `nonces(address)` | 36 B |
//! | [`build_eip2612_version_calldata`] | `version()` | 4 B |
//!
//! The spender for swap orders is typically the
//! [`VAULT_RELAYER`](crate::config::contracts::VAULT_RELAYER).
use ;
/// Compute the 4-byte selector from a Solidity function signature.
/// Left-pad an [`Address`] to a 32-byte ABI word.
/// Encode a [`U256`] as a 32-byte big-endian ABI word.
const
/// Build calldata for `ERC20.approve(address spender, uint256 amount)`.
///
/// Call this on the sell token to grant the
/// [`VAULT_RELAYER`](crate::config::contracts::VAULT_RELAYER) (or any other
/// spender) the allowance it needs to transfer tokens on your behalf.
///
/// # Parameters
///
/// * `spender` — the [`Address`] to approve (typically `VAULT_RELAYER`).
/// * `amount` — the allowance to set (`U256::MAX` for unlimited).
///
/// # Returns
///
/// A 68-byte `Vec<u8>`: 4-byte selector + 32-byte address + 32-byte amount.
///
/// # Example
///
/// ```rust
/// use alloy_primitives::{Address, U256};
/// use cow_rs::{VAULT_RELAYER, erc20::build_erc20_approve_calldata};
///
/// let calldata = build_erc20_approve_calldata(VAULT_RELAYER, U256::MAX);
/// assert_eq!(calldata.len(), 68); // 4 (selector) + 32 (address) + 32 (amount)
/// // First 4 bytes are the `approve(address,uint256)` function selector.
/// assert_eq!(&calldata[..4], &alloy_primitives::keccak256(b"approve(address,uint256)")[..4]);
/// ```
/// Build calldata for `ERC20.balanceOf(address account)`.
///
/// Useful for encoding a `balanceOf` static call to check a token balance
/// before placing an order.
///
/// # Parameters
///
/// * `account` — the [`Address`] whose balance to query.
///
/// # Returns
///
/// A 36-byte `Vec<u8>`: 4-byte selector + 32-byte address.
///
/// # Example
///
/// ```rust
/// use alloy_primitives::Address;
/// use cow_rs::erc20::build_erc20_balance_of_calldata;
///
/// let calldata = build_erc20_balance_of_calldata(Address::ZERO);
/// assert_eq!(calldata.len(), 36); // 4 (selector) + 32 (address)
/// assert_eq!(&calldata[..4], &alloy_primitives::keccak256(b"balanceOf(address)")[..4]);
/// ```
/// Build calldata for `ERC20.allowance(address owner, address spender)`.
///
/// Useful for encoding an `allowance` static call to check existing
/// approval before deciding whether to call `approve`.
///
/// # Parameters
///
/// * `owner` — the token holder's [`Address`].
/// * `spender` — the approved spender's [`Address`].
///
/// # Returns
///
/// A 68-byte `Vec<u8>`: 4-byte selector + 32-byte owner + 32-byte spender.
///
/// # Example
///
/// ```rust
/// use alloy_primitives::Address;
/// use cow_rs::{VAULT_RELAYER, erc20::build_erc20_allowance_calldata};
///
/// let calldata = build_erc20_allowance_calldata(Address::ZERO, VAULT_RELAYER);
/// assert_eq!(calldata.len(), 68); // 4 (selector) + 32 (owner) + 32 (spender)
/// assert_eq!(&calldata[..4], &alloy_primitives::keccak256(b"allowance(address,address)")[..4]);
/// ```
/// Build calldata for `ERC20.transfer(address to, uint256 amount)`.
///
/// Encodes a direct `transfer` call to move tokens from the caller to
/// `to`. Unlike [`build_erc20_transfer_from_calldata`], this does not
/// require a prior `approve` because the caller is always `msg.sender`.
///
/// # Parameters
///
/// * `to` — the recipient [`Address`].
/// * `amount` — the token amount to transfer.
///
/// # Returns
///
/// A 68-byte `Vec<u8>`: 4-byte selector + 32-byte address + 32-byte amount.
///
/// # Example
///
/// ```rust
/// use alloy_primitives::{Address, U256};
/// use cow_rs::erc20::build_erc20_transfer_calldata;
///
/// let cd = build_erc20_transfer_calldata(Address::ZERO, U256::from(100u64));
/// assert_eq!(cd.len(), 68); // 4 (selector) + 32 (address) + 32 (amount)
/// assert_eq!(&cd[..4], &alloy_primitives::keccak256(b"transfer(address,uint256)")[..4]);
/// ```
/// Build calldata for `ERC20.transferFrom(address from, address to, uint256 amount)`.
///
/// Encodes a `transferFrom` call, for use in pre/post-settlement hooks
/// that need to move tokens atomically within the settlement transaction.
///
/// # Parameters
///
/// * `from` — the token holder's [`Address`].
/// * `to` — the recipient [`Address`].
/// * `amount` — the token amount to transfer.
///
/// # Returns
///
/// A 100-byte `Vec<u8>`: 4-byte selector + 3 × 32-byte arguments.
///
/// # Example
///
/// ```rust
/// use alloy_primitives::{Address, U256};
/// use cow_rs::erc20::build_erc20_transfer_from_calldata;
///
/// let cd = build_erc20_transfer_from_calldata(Address::ZERO, Address::ZERO, U256::from(100u64));
/// assert_eq!(cd.len(), 100); // 4 + 32 + 32 + 32
/// ```
/// Build calldata for `ERC20.decimals()` — a 4-byte selector-only call.
///
/// # Returns
///
/// A 4-byte `Vec<u8>` containing the function selector.
///
/// # Example
///
/// ```rust
/// use cow_rs::erc20::build_erc20_decimals_calldata;
///
/// let calldata = build_erc20_decimals_calldata();
/// assert_eq!(calldata.len(), 4);
/// assert_eq!(&calldata, &alloy_primitives::keccak256(b"decimals()")[..4]);
/// ```
/// Build calldata for `ERC20.name()` — a 4-byte selector-only call.
///
/// # Returns
///
/// A 4-byte `Vec<u8>` containing the function selector.
///
/// # Example
///
/// ```rust
/// use cow_rs::erc20::build_erc20_name_calldata;
///
/// let calldata = build_erc20_name_calldata();
/// assert_eq!(calldata.len(), 4);
/// assert_eq!(&calldata, &alloy_primitives::keccak256(b"name()")[..4]);
/// ```
/// Build calldata for `EIP-2612.nonces(address owner)`.
///
/// # Parameters
///
/// * `owner` — the [`Address`] whose permit nonce to query.
///
/// # Returns
///
/// A 36-byte `Vec<u8>`: 4-byte selector + 32-byte address.
///
/// # Example
///
/// ```rust
/// use alloy_primitives::Address;
/// use cow_rs::erc20::build_eip2612_nonces_calldata;
///
/// let calldata = build_eip2612_nonces_calldata(Address::ZERO);
/// assert_eq!(calldata.len(), 36); // 4 (selector) + 32 (address)
/// assert_eq!(&calldata[..4], &alloy_primitives::keccak256(b"nonces(address)")[..4]);
/// ```
/// Build calldata for `EIP-2612.version()` — a 4-byte selector-only call.
///
/// # Returns
///
/// A 4-byte `Vec<u8>` containing the function selector.
///
/// # Example
///
/// ```rust
/// use cow_rs::erc20::build_eip2612_version_calldata;
///
/// let calldata = build_eip2612_version_calldata();
/// assert_eq!(calldata.len(), 4);
/// assert_eq!(&calldata, &alloy_primitives::keccak256(b"version()")[..4]);
/// ```