-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathERC6909.sol
459 lines (365 loc) · 14.8 KB
/
ERC6909.sol
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
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
// SPDX-License-Identifier: AGPL-3.0-only
pragma solidity ^0.8.25;
// ## ERC6909 Type Wrapper
//
// This type wraps the address primitive type and contains functions to call the core ERC6909
// interface without allocating new memory. Functions perform returndata validation.
//
// All external calls that fail will revert internally. This is to simplify the API.
type ERC6909 is address;
using {
supportsInterface,
balanceOf,
allowance,
isOperator,
transfer,
transferFrom,
approve,
setOperator,
// -- operators
eq as ==,
neq as !=,
gt as >,
gte as >=,
lt as <,
lte as <=,
add as +,
sub as -,
mul as *,
div as /,
mod as %,
and as &,
or as |,
xor as ^,
not as ~
} for ERC6909 global;
// -------------------------------------------------------------------------------------------------
// Query ERC6909.supportsInterface without allocating new memory.
//
// Procedures:
// 01. right shifts interface id by 32 bits to pack with the selector
// 02. store the packed supportsInterface selector and interface id in memory
// 03. staticcall supportsInterface; cache as ok
// 04. check that the return value is 32 bytes; compose with ok
// 05. revert if ok is false
// 06. assign the return value to output
function supportsInterface(ERC6909 erc6909, bytes4 interfaceId) view returns (bool output) {
assembly {
interfaceId := shr(0x20, interfaceId)
mstore(0x00, or(interfaceId, 0x01ffc9a700000000000000000000000000000000000000000000000000000000))
let ok := staticcall(gas(), erc6909, 0x00, 0x24, 0x00, 0x20)
ok := and(ok, eq(returndatasize(), 0x20))
if iszero(ok) { revert(0x00, 0x00) }
output := mload(0x00)
}
}
// -------------------------------------------------------------------------------------------------
// Query ERC6909.balanceOf without allocating new memory.
//
// Procedures:
// 01. store balanceOf selector in memory
// 02. store owner in memory
// 03. store id in memory
// 04. staticcall balanceOf; cache as ok
// 05. check that the return value is 32 bytes; compose with ok
// 06. revert if ok is false
// 07. assign the return value to output
// 08. restore the upper bits of the free memory pointer to zero
function balanceOf(ERC6909 erc6909, address owner, uint256 id) view returns (uint256 output) {
assembly {
mstore(0x00, 0x00fdd58e00000000000000000000000000000000000000000000000000000000)
mstore(0x04, owner)
mstore(0x24, id)
let ok := staticcall(gas(), erc6909, 0x00, 0x44, 0x00, 0x20)
ok := and(ok, eq(returndatasize(), 0x20))
if iszero(ok) { revert(0x00, 0x00) }
output := mload(0x00)
mstore(0x24, 0x00)
}
}
// -------------------------------------------------------------------------------------------------
// Query ERC6909.allowance without allocating new memory.
//
// Procedures:
// 01. load free memory pointer from memory; cache as fmp
// 02. store allowance selector in memory
// 03. store owner in memory
// 04. store spender in memory
// 05. store id in memory
// 06. staticcall allowance; cache as ok
// 07. check that the return value is 32 bytes; compose with ok
// 08. revert if ok is false
// 09. assign the return value to output
// 10. restore the free memory pointer to fmp
// 11. restore the zero slot to zero
function allowance(ERC6909 erc6909, address owner, address spender, uint256 id) view returns (uint256 output) {
assembly {
let fmp := mload(0x40)
mstore(0x00, 0x598af9e700000000000000000000000000000000000000000000000000000000)
mstore(0x04, owner)
mstore(0x24, spender)
mstore(0x44, id)
let ok := staticcall(gas(), erc6909, 0x00, 0x64, 0x00, 0x20)
ok := and(ok, eq(returndatasize(), 0x20))
if iszero(ok) { revert(0x00, 0x00) }
output := mload(0x00)
mstore(0x40, fmp)
mstore(0x60, 0x00)
}
}
// -------------------------------------------------------------------------------------------------
// Query ERC6909.isOperator without allocating new memory.
//
// Procedures:
// 01. store isOperator selector in memory
// 02. store owner in memory
// 03. store spender in memory
// 04. staticcall isOperator; cache as ok
// 05. check that the return value is 32 bytes; compose with ok
// 06. revert if ok is false
// 07. assign the return value to output
// 08. restore the upper bits of the free memory pointer to zero
function isOperator(ERC6909 erc6909, address owner, address spender) view returns (bool output) {
assembly {
mstore(0x00, 0xb6363cf200000000000000000000000000000000000000000000000000000000)
mstore(0x04, owner)
mstore(0x24, spender)
let ok := staticcall(gas(), erc6909, 0x00, 0x44, 0x00, 0x20)
ok := and(ok, eq(returndatasize(), 0x20))
if iszero(ok) { revert(0x00, 0x00) }
output := mload(0x00)
mstore(0x24, 0x00)
}
}
// -------------------------------------------------------------------------------------------------
// Call ERC6909.transfer without allocating new memory.
//
// Procedures:
// 01. load free memory pointer from memory; cache as fmp
// 02. store transfer selector in memory
// 03. store receiver in memory
// 04. store id in memory
// 05. store amount in memory
// 06. call transfer; cache result as ok
// 07. check that the return value is 32 bytes; compose with ok
// 08. check that the return value is true; compose with ok
// 09. revert if ok is false
// 10. restore the free memory pointer to fmp
// 11. restore the zero slot to zero
function transfer(ERC6909 erc6909, address receiver, uint256 id, uint256 amount) {
assembly {
let fmp := mload(0x40)
mstore(0x00, 0x095bcdb600000000000000000000000000000000000000000000000000000000)
mstore(0x04, receiver)
mstore(0x24, id)
mstore(0x44, amount)
let ok := call(gas(), erc6909, 0x00, 0x00, 0x64, 0x00, 0x20)
ok := and(ok, eq(returndatasize(), 0x20))
ok := and(ok, mload(0x00))
if iszero(ok) { revert(0x00, 0x00) }
mstore(0x40, fmp)
mstore(0x60, 0x00)
}
}
// -------------------------------------------------------------------------------------------------
// Call ERC6909.transferFrom without allocating new memory.
//
// Procedures:
// 01. load free memory pointer from memory; cache as fmp
// 02. load first word of allocated memory; cache as allocatedWord
// 03. store transferFrom selector in memory
// 04. store sender in memory
// 05. store receiver in memory
// 06. store id in memory
// 07. store amount in memory
// 08. call transferFrom; cache result as ok
// 09. check that the return value is 32 bytes; compose with ok
// 10. check that the return value is true; compose with ok
// 11. revert if ok is false
// 12. restore the free memory pointer to fmp
// 13. restore the zero slot to zero
// 14. restore the first word of allocated memory to allocatedWord
function transferFrom(ERC6909 erc6909, address sender, address receiver, uint256 id, uint256 amount) {
assembly {
let fmp := mload(0x40)
let allocatedWord := mload(0x80)
mstore(0x00, 0xfe99049a00000000000000000000000000000000000000000000000000000000)
mstore(0x04, sender)
mstore(0x24, receiver)
mstore(0x44, id)
mstore(0x64, amount)
let ok := call(gas(), erc6909, 0x00, 0x00, 0x84, allocatedWord, 0x20)
ok := and(ok, eq(returndatasize(), 0x20))
ok := and(ok, mload(allocatedWord))
if iszero(ok) { revert(0x00, 0x00) }
mstore(0x40, fmp)
mstore(0x60, 0x00)
mstore(0x80, allocatedWord)
}
}
// -------------------------------------------------------------------------------------------------
// Call ERC6909.approve without allocating new memory.
//
// Procedures:
// 01. load free memory pointer from memory; cache as fmp
// 02. store approve selector in memory
// 03. store spender in memory
// 04. store id in memory
// 05. store amount in memory
// 06. call approve; cache result as ok
// 07. check that the return value is 32 bytes; compose with ok
// 08. check that the return value is true; compose with ok
// 09. revert if ok is false
// 10. restore the free memory pointer to fmp
// 11. restore the zero slot to zero
function approve(ERC6909 erc6909, address spender, uint256 id, uint256 amount) {
assembly {
let fmp := mload(0x40)
mstore(0x00, 0x426a849300000000000000000000000000000000000000000000000000000000)
mstore(0x04, spender)
mstore(0x24, id)
mstore(0x44, amount)
let ok := call(gas(), erc6909, 0x00, 0x00, 0x64, 0x00, 0x20)
ok := and(ok, eq(returndatasize(), 0x20))
ok := and(ok, mload(0x00))
if iszero(ok) { revert(0x00, 0x00) }
mstore(0x40, fmp)
mstore(0x60, 0x00)
}
}
// -------------------------------------------------------------------------------------------------
// Call ERC6909.setOperator without allocating new memory.
//
// Procedures:
// 01. store setOperator selector in memory
// 02. store spender in memory
// 03. store approved in memory
// 04. call setOperator; cache result as ok
// 05. check that the return value is 32 bytes; compose with ok
// 06. check that the return value is true; compose with ok
// 07. revert if ok is false
// 08. restore the upper bits of the free memory pointer to zero
function setOperator(ERC6909 erc6909, address spender, bool approved) {
assembly {
mstore(0x00, 0x558a729700000000000000000000000000000000000000000000000000000000)
mstore(0x04, spender)
mstore(0x24, approved)
let ok := call(gas(), erc6909, 0x00, 0x00, 0x44, 0x00, 0x20)
ok := and(ok, eq(returndatasize(), 0x20))
ok := and(ok, mload(0x00))
if iszero(ok) { revert(0x00, 0x00) }
mstore(0x24, 0x00)
}
}
// -------------------------------------------------------------------------------------------------
// Returns `true` if the two ERC6909 instances are equal, `false` otherwise.
function eq(ERC6909 lhs, ERC6909 rhs) pure returns (bool output) {
assembly {
output := eq(lhs, rhs)
}
}
// -------------------------------------------------------------------------------------------------
// Returns `true` if the two ERC6909 instances are not equal, `false` otherwise.
function neq(ERC6909 lhs, ERC6909 rhs) pure returns (bool output) {
assembly {
output := iszero(eq(lhs, rhs))
}
}
// -------------------------------------------------------------------------------------------------
// Returns `true` if `lhs` is greater than `rhs`, `false` otherwise.
function gt(ERC6909 lhs, ERC6909 rhs) pure returns (bool output) {
assembly {
output := gt(lhs, rhs)
}
}
// -------------------------------------------------------------------------------------------------
// Returns `true` if `lhs` is greater than or equal to `rhs`, `false` otherwise.
function gte(ERC6909 lhs, ERC6909 rhs) pure returns (bool output) {
assembly {
output := iszero(lt(lhs, rhs))
}
}
// -------------------------------------------------------------------------------------------------
// Returns `true` if `lhs` is less than `rhs`, `false` otherwise.
function lt(ERC6909 lhs, ERC6909 rhs) pure returns (bool output) {
assembly {
output := lt(lhs, rhs)
}
}
// -------------------------------------------------------------------------------------------------
// Returns `true` if `lhs` is less than or equal to `rhs`, `false` otherwise.
function lte(ERC6909 lhs, ERC6909 rhs) pure returns (bool output) {
assembly {
output := iszero(gt(lhs, rhs))
}
}
// -------------------------------------------------------------------------------------------------
// Returns the sum of two ERC6909 instances.
function add(ERC6909 lhs, ERC6909 rhs) pure returns (ERC6909 output) {
assembly {
output := add(lhs, rhs)
if gt(output, 0xffffffffffffffffffffffffffffffffffffffff) { revert(0x00, 0x00) }
}
}
// -------------------------------------------------------------------------------------------------
// Returns the difference of two ERC6909 instances.
function sub(ERC6909 lhs, ERC6909 rhs) pure returns (ERC6909 output) {
assembly {
output := sub(lhs, rhs)
if gt(output, 0xffffffffffffffffffffffffffffffffffffffff) { revert(0x00, 0x00) }
}
}
// -------------------------------------------------------------------------------------------------
// Returns the product of two ERC6909 instances.
function mul(ERC6909 lhs, ERC6909 rhs) pure returns (ERC6909 output) {
assembly {
if lhs {
output := and(mul(lhs, rhs), 0xffffffffffffffffffffffffffffffffffffffff)
if iszero(eq(div(output, lhs), rhs)) { revert(0x00, 0x00) }
}
}
}
// -------------------------------------------------------------------------------------------------
// Returns the division of two ERC6909 instances.
function div(ERC6909 lhs, ERC6909 rhs) pure returns (ERC6909 output) {
assembly {
if iszero(rhs) { revert(0x00, 0x00) }
output := div(lhs, rhs)
}
}
// -------------------------------------------------------------------------------------------------
// Returns the modulus of two ERC6909 instances.
function mod(ERC6909 lhs, ERC6909 rhs) pure returns (ERC6909 output) {
assembly {
if iszero(rhs) { revert(0x00, 0x00) }
output := mod(lhs, rhs)
}
}
// -------------------------------------------------------------------------------------------------
// Returns the bitwise AND of two ERC6909 instances.
function and(ERC6909 lhs, ERC6909 rhs) pure returns (ERC6909 output) {
assembly {
output := and(lhs, rhs)
}
}
// -------------------------------------------------------------------------------------------------
// Returns the bitwise OR of two ERC6909 instances.
function or(ERC6909 lhs, ERC6909 rhs) pure returns (ERC6909 output) {
assembly {
output := or(lhs, rhs)
}
}
// -------------------------------------------------------------------------------------------------
// Returns the bitwise XOR of two ERC6909 instances.
function xor(ERC6909 lhs, ERC6909 rhs) pure returns (ERC6909 output) {
assembly {
output := xor(lhs, rhs)
}
}
// -------------------------------------------------------------------------------------------------
// Returns the bitwise NOT of an ERC6909 instance.
function not(ERC6909 lhs) pure returns (ERC6909 output) {
assembly {
output := and(not(lhs), 0xffffffffffffffffffffffffffffffffffffffff)
}
}