/
ERC20Metadata.sol
635 lines (520 loc) · 19.6 KB
/
ERC20Metadata.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
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
// SPDX-License-Identifier: MIT
pragma solidity >=0.8.21;
/* Autogenerated file. Do not edit manually. */
// Import schema type
import { SchemaType } from "@latticexyz/schema-type/src/solidity/SchemaType.sol";
// Import store internals
import { IStore } from "@latticexyz/store/src/IStore.sol";
import { StoreSwitch } from "@latticexyz/store/src/StoreSwitch.sol";
import { StoreCore } from "@latticexyz/store/src/StoreCore.sol";
import { Bytes } from "@latticexyz/store/src/Bytes.sol";
import { Memory } from "@latticexyz/store/src/Memory.sol";
import { SliceLib } from "@latticexyz/store/src/Slice.sol";
import { EncodeArray } from "@latticexyz/store/src/tightcoder/EncodeArray.sol";
import { FieldLayout, FieldLayoutLib } from "@latticexyz/store/src/FieldLayout.sol";
import { Schema, SchemaLib } from "@latticexyz/store/src/Schema.sol";
import { PackedCounter, PackedCounterLib } from "@latticexyz/store/src/PackedCounter.sol";
import { ResourceId } from "@latticexyz/store/src/ResourceId.sol";
import { RESOURCE_TABLE, RESOURCE_OFFCHAIN_TABLE } from "@latticexyz/store/src/storeResourceTypes.sol";
FieldLayout constant _fieldLayout = FieldLayout.wrap(
0x0001010201000000000000000000000000000000000000000000000000000000
);
struct ERC20MetadataData {
uint8 decimals;
string name;
string symbol;
}
library ERC20Metadata {
/**
* @notice Get the table values' field layout.
* @return _fieldLayout The field layout for the table.
*/
function getFieldLayout() internal pure returns (FieldLayout) {
return _fieldLayout;
}
/**
* @notice Get the table's key schema.
* @return _keySchema The key schema for the table.
*/
function getKeySchema() internal pure returns (Schema) {
SchemaType[] memory _keySchema = new SchemaType[](0);
return SchemaLib.encode(_keySchema);
}
/**
* @notice Get the table's value schema.
* @return _valueSchema The value schema for the table.
*/
function getValueSchema() internal pure returns (Schema) {
SchemaType[] memory _valueSchema = new SchemaType[](3);
_valueSchema[0] = SchemaType.UINT8;
_valueSchema[1] = SchemaType.STRING;
_valueSchema[2] = SchemaType.STRING;
return SchemaLib.encode(_valueSchema);
}
/**
* @notice Get the table's key field names.
* @return keyNames An array of strings with the names of key fields.
*/
function getKeyNames() internal pure returns (string[] memory keyNames) {
keyNames = new string[](0);
}
/**
* @notice Get the table's value field names.
* @return fieldNames An array of strings with the names of value fields.
*/
function getFieldNames() internal pure returns (string[] memory fieldNames) {
fieldNames = new string[](3);
fieldNames[0] = "decimals";
fieldNames[1] = "name";
fieldNames[2] = "symbol";
}
/**
* @notice Register the table with its config.
*/
function register(ResourceId _tableId) internal {
StoreSwitch.registerTable(_tableId, _fieldLayout, getKeySchema(), getValueSchema(), getKeyNames(), getFieldNames());
}
/**
* @notice Register the table with its config.
*/
function _register(ResourceId _tableId) internal {
StoreCore.registerTable(_tableId, _fieldLayout, getKeySchema(), getValueSchema(), getKeyNames(), getFieldNames());
}
/**
* @notice Get decimals.
*/
function getDecimals(ResourceId _tableId) internal view returns (uint8 decimals) {
bytes32[] memory _keyTuple = new bytes32[](0);
bytes32 _blob = StoreSwitch.getStaticField(_tableId, _keyTuple, 0, _fieldLayout);
return (uint8(bytes1(_blob)));
}
/**
* @notice Get decimals.
*/
function _getDecimals(ResourceId _tableId) internal view returns (uint8 decimals) {
bytes32[] memory _keyTuple = new bytes32[](0);
bytes32 _blob = StoreCore.getStaticField(_tableId, _keyTuple, 0, _fieldLayout);
return (uint8(bytes1(_blob)));
}
/**
* @notice Set decimals.
*/
function setDecimals(ResourceId _tableId, uint8 decimals) internal {
bytes32[] memory _keyTuple = new bytes32[](0);
StoreSwitch.setStaticField(_tableId, _keyTuple, 0, abi.encodePacked((decimals)), _fieldLayout);
}
/**
* @notice Set decimals.
*/
function _setDecimals(ResourceId _tableId, uint8 decimals) internal {
bytes32[] memory _keyTuple = new bytes32[](0);
StoreCore.setStaticField(_tableId, _keyTuple, 0, abi.encodePacked((decimals)), _fieldLayout);
}
/**
* @notice Get name.
*/
function getName(ResourceId _tableId) internal view returns (string memory name) {
bytes32[] memory _keyTuple = new bytes32[](0);
bytes memory _blob = StoreSwitch.getDynamicField(_tableId, _keyTuple, 0);
return (string(_blob));
}
/**
* @notice Get name.
*/
function _getName(ResourceId _tableId) internal view returns (string memory name) {
bytes32[] memory _keyTuple = new bytes32[](0);
bytes memory _blob = StoreCore.getDynamicField(_tableId, _keyTuple, 0);
return (string(_blob));
}
/**
* @notice Set name.
*/
function setName(ResourceId _tableId, string memory name) internal {
bytes32[] memory _keyTuple = new bytes32[](0);
StoreSwitch.setDynamicField(_tableId, _keyTuple, 0, bytes((name)));
}
/**
* @notice Set name.
*/
function _setName(ResourceId _tableId, string memory name) internal {
bytes32[] memory _keyTuple = new bytes32[](0);
StoreCore.setDynamicField(_tableId, _keyTuple, 0, bytes((name)));
}
/**
* @notice Get the length of name.
*/
function lengthName(ResourceId _tableId) internal view returns (uint256) {
bytes32[] memory _keyTuple = new bytes32[](0);
uint256 _byteLength = StoreSwitch.getDynamicFieldLength(_tableId, _keyTuple, 0);
unchecked {
return _byteLength / 1;
}
}
/**
* @notice Get the length of name.
*/
function _lengthName(ResourceId _tableId) internal view returns (uint256) {
bytes32[] memory _keyTuple = new bytes32[](0);
uint256 _byteLength = StoreCore.getDynamicFieldLength(_tableId, _keyTuple, 0);
unchecked {
return _byteLength / 1;
}
}
/**
* @notice Get an item of name.
* @dev Reverts with Store_IndexOutOfBounds if `_index` is out of bounds for the array.
*/
function getItemName(ResourceId _tableId, uint256 _index) internal view returns (string memory) {
bytes32[] memory _keyTuple = new bytes32[](0);
unchecked {
bytes memory _blob = StoreSwitch.getDynamicFieldSlice(_tableId, _keyTuple, 0, _index * 1, (_index + 1) * 1);
return (string(_blob));
}
}
/**
* @notice Get an item of name.
* @dev Reverts with Store_IndexOutOfBounds if `_index` is out of bounds for the array.
*/
function _getItemName(ResourceId _tableId, uint256 _index) internal view returns (string memory) {
bytes32[] memory _keyTuple = new bytes32[](0);
unchecked {
bytes memory _blob = StoreCore.getDynamicFieldSlice(_tableId, _keyTuple, 0, _index * 1, (_index + 1) * 1);
return (string(_blob));
}
}
/**
* @notice Push a slice to name.
*/
function pushName(ResourceId _tableId, string memory _slice) internal {
bytes32[] memory _keyTuple = new bytes32[](0);
StoreSwitch.pushToDynamicField(_tableId, _keyTuple, 0, bytes((_slice)));
}
/**
* @notice Push a slice to name.
*/
function _pushName(ResourceId _tableId, string memory _slice) internal {
bytes32[] memory _keyTuple = new bytes32[](0);
StoreCore.pushToDynamicField(_tableId, _keyTuple, 0, bytes((_slice)));
}
/**
* @notice Pop a slice from name.
*/
function popName(ResourceId _tableId) internal {
bytes32[] memory _keyTuple = new bytes32[](0);
StoreSwitch.popFromDynamicField(_tableId, _keyTuple, 0, 1);
}
/**
* @notice Pop a slice from name.
*/
function _popName(ResourceId _tableId) internal {
bytes32[] memory _keyTuple = new bytes32[](0);
StoreCore.popFromDynamicField(_tableId, _keyTuple, 0, 1);
}
/**
* @notice Update a slice of name at `_index`.
*/
function updateName(ResourceId _tableId, uint256 _index, string memory _slice) internal {
bytes32[] memory _keyTuple = new bytes32[](0);
unchecked {
bytes memory _encoded = bytes((_slice));
StoreSwitch.spliceDynamicData(_tableId, _keyTuple, 0, uint40(_index * 1), uint40(_encoded.length), _encoded);
}
}
/**
* @notice Update a slice of name at `_index`.
*/
function _updateName(ResourceId _tableId, uint256 _index, string memory _slice) internal {
bytes32[] memory _keyTuple = new bytes32[](0);
unchecked {
bytes memory _encoded = bytes((_slice));
StoreCore.spliceDynamicData(_tableId, _keyTuple, 0, uint40(_index * 1), uint40(_encoded.length), _encoded);
}
}
/**
* @notice Get symbol.
*/
function getSymbol(ResourceId _tableId) internal view returns (string memory symbol) {
bytes32[] memory _keyTuple = new bytes32[](0);
bytes memory _blob = StoreSwitch.getDynamicField(_tableId, _keyTuple, 1);
return (string(_blob));
}
/**
* @notice Get symbol.
*/
function _getSymbol(ResourceId _tableId) internal view returns (string memory symbol) {
bytes32[] memory _keyTuple = new bytes32[](0);
bytes memory _blob = StoreCore.getDynamicField(_tableId, _keyTuple, 1);
return (string(_blob));
}
/**
* @notice Set symbol.
*/
function setSymbol(ResourceId _tableId, string memory symbol) internal {
bytes32[] memory _keyTuple = new bytes32[](0);
StoreSwitch.setDynamicField(_tableId, _keyTuple, 1, bytes((symbol)));
}
/**
* @notice Set symbol.
*/
function _setSymbol(ResourceId _tableId, string memory symbol) internal {
bytes32[] memory _keyTuple = new bytes32[](0);
StoreCore.setDynamicField(_tableId, _keyTuple, 1, bytes((symbol)));
}
/**
* @notice Get the length of symbol.
*/
function lengthSymbol(ResourceId _tableId) internal view returns (uint256) {
bytes32[] memory _keyTuple = new bytes32[](0);
uint256 _byteLength = StoreSwitch.getDynamicFieldLength(_tableId, _keyTuple, 1);
unchecked {
return _byteLength / 1;
}
}
/**
* @notice Get the length of symbol.
*/
function _lengthSymbol(ResourceId _tableId) internal view returns (uint256) {
bytes32[] memory _keyTuple = new bytes32[](0);
uint256 _byteLength = StoreCore.getDynamicFieldLength(_tableId, _keyTuple, 1);
unchecked {
return _byteLength / 1;
}
}
/**
* @notice Get an item of symbol.
* @dev Reverts with Store_IndexOutOfBounds if `_index` is out of bounds for the array.
*/
function getItemSymbol(ResourceId _tableId, uint256 _index) internal view returns (string memory) {
bytes32[] memory _keyTuple = new bytes32[](0);
unchecked {
bytes memory _blob = StoreSwitch.getDynamicFieldSlice(_tableId, _keyTuple, 1, _index * 1, (_index + 1) * 1);
return (string(_blob));
}
}
/**
* @notice Get an item of symbol.
* @dev Reverts with Store_IndexOutOfBounds if `_index` is out of bounds for the array.
*/
function _getItemSymbol(ResourceId _tableId, uint256 _index) internal view returns (string memory) {
bytes32[] memory _keyTuple = new bytes32[](0);
unchecked {
bytes memory _blob = StoreCore.getDynamicFieldSlice(_tableId, _keyTuple, 1, _index * 1, (_index + 1) * 1);
return (string(_blob));
}
}
/**
* @notice Push a slice to symbol.
*/
function pushSymbol(ResourceId _tableId, string memory _slice) internal {
bytes32[] memory _keyTuple = new bytes32[](0);
StoreSwitch.pushToDynamicField(_tableId, _keyTuple, 1, bytes((_slice)));
}
/**
* @notice Push a slice to symbol.
*/
function _pushSymbol(ResourceId _tableId, string memory _slice) internal {
bytes32[] memory _keyTuple = new bytes32[](0);
StoreCore.pushToDynamicField(_tableId, _keyTuple, 1, bytes((_slice)));
}
/**
* @notice Pop a slice from symbol.
*/
function popSymbol(ResourceId _tableId) internal {
bytes32[] memory _keyTuple = new bytes32[](0);
StoreSwitch.popFromDynamicField(_tableId, _keyTuple, 1, 1);
}
/**
* @notice Pop a slice from symbol.
*/
function _popSymbol(ResourceId _tableId) internal {
bytes32[] memory _keyTuple = new bytes32[](0);
StoreCore.popFromDynamicField(_tableId, _keyTuple, 1, 1);
}
/**
* @notice Update a slice of symbol at `_index`.
*/
function updateSymbol(ResourceId _tableId, uint256 _index, string memory _slice) internal {
bytes32[] memory _keyTuple = new bytes32[](0);
unchecked {
bytes memory _encoded = bytes((_slice));
StoreSwitch.spliceDynamicData(_tableId, _keyTuple, 1, uint40(_index * 1), uint40(_encoded.length), _encoded);
}
}
/**
* @notice Update a slice of symbol at `_index`.
*/
function _updateSymbol(ResourceId _tableId, uint256 _index, string memory _slice) internal {
bytes32[] memory _keyTuple = new bytes32[](0);
unchecked {
bytes memory _encoded = bytes((_slice));
StoreCore.spliceDynamicData(_tableId, _keyTuple, 1, uint40(_index * 1), uint40(_encoded.length), _encoded);
}
}
/**
* @notice Get the full data.
*/
function get(ResourceId _tableId) internal view returns (ERC20MetadataData memory _table) {
bytes32[] memory _keyTuple = new bytes32[](0);
(bytes memory _staticData, PackedCounter _encodedLengths, bytes memory _dynamicData) = StoreSwitch.getRecord(
_tableId,
_keyTuple,
_fieldLayout
);
return decode(_staticData, _encodedLengths, _dynamicData);
}
/**
* @notice Get the full data.
*/
function _get(ResourceId _tableId) internal view returns (ERC20MetadataData memory _table) {
bytes32[] memory _keyTuple = new bytes32[](0);
(bytes memory _staticData, PackedCounter _encodedLengths, bytes memory _dynamicData) = StoreCore.getRecord(
_tableId,
_keyTuple,
_fieldLayout
);
return decode(_staticData, _encodedLengths, _dynamicData);
}
/**
* @notice Set the full data using individual values.
*/
function set(ResourceId _tableId, uint8 decimals, string memory name, string memory symbol) internal {
bytes memory _staticData = encodeStatic(decimals);
PackedCounter _encodedLengths = encodeLengths(name, symbol);
bytes memory _dynamicData = encodeDynamic(name, symbol);
bytes32[] memory _keyTuple = new bytes32[](0);
StoreSwitch.setRecord(_tableId, _keyTuple, _staticData, _encodedLengths, _dynamicData);
}
/**
* @notice Set the full data using individual values.
*/
function _set(ResourceId _tableId, uint8 decimals, string memory name, string memory symbol) internal {
bytes memory _staticData = encodeStatic(decimals);
PackedCounter _encodedLengths = encodeLengths(name, symbol);
bytes memory _dynamicData = encodeDynamic(name, symbol);
bytes32[] memory _keyTuple = new bytes32[](0);
StoreCore.setRecord(_tableId, _keyTuple, _staticData, _encodedLengths, _dynamicData, _fieldLayout);
}
/**
* @notice Set the full data using the data struct.
*/
function set(ResourceId _tableId, ERC20MetadataData memory _table) internal {
bytes memory _staticData = encodeStatic(_table.decimals);
PackedCounter _encodedLengths = encodeLengths(_table.name, _table.symbol);
bytes memory _dynamicData = encodeDynamic(_table.name, _table.symbol);
bytes32[] memory _keyTuple = new bytes32[](0);
StoreSwitch.setRecord(_tableId, _keyTuple, _staticData, _encodedLengths, _dynamicData);
}
/**
* @notice Set the full data using the data struct.
*/
function _set(ResourceId _tableId, ERC20MetadataData memory _table) internal {
bytes memory _staticData = encodeStatic(_table.decimals);
PackedCounter _encodedLengths = encodeLengths(_table.name, _table.symbol);
bytes memory _dynamicData = encodeDynamic(_table.name, _table.symbol);
bytes32[] memory _keyTuple = new bytes32[](0);
StoreCore.setRecord(_tableId, _keyTuple, _staticData, _encodedLengths, _dynamicData, _fieldLayout);
}
/**
* @notice Decode the tightly packed blob of static data using this table's field layout.
*/
function decodeStatic(bytes memory _blob) internal pure returns (uint8 decimals) {
decimals = (uint8(Bytes.slice1(_blob, 0)));
}
/**
* @notice Decode the tightly packed blob of dynamic data using the encoded lengths.
*/
function decodeDynamic(
PackedCounter _encodedLengths,
bytes memory _blob
) internal pure returns (string memory name, string memory symbol) {
uint256 _start;
uint256 _end;
unchecked {
_end = _encodedLengths.atIndex(0);
}
name = (string(SliceLib.getSubslice(_blob, _start, _end).toBytes()));
_start = _end;
unchecked {
_end += _encodedLengths.atIndex(1);
}
symbol = (string(SliceLib.getSubslice(_blob, _start, _end).toBytes()));
}
/**
* @notice Decode the tightly packed blobs using this table's field layout.
* @param _staticData Tightly packed static fields.
* @param _encodedLengths Encoded lengths of dynamic fields.
* @param _dynamicData Tightly packed dynamic fields.
*/
function decode(
bytes memory _staticData,
PackedCounter _encodedLengths,
bytes memory _dynamicData
) internal pure returns (ERC20MetadataData memory _table) {
(_table.decimals) = decodeStatic(_staticData);
(_table.name, _table.symbol) = decodeDynamic(_encodedLengths, _dynamicData);
}
/**
* @notice Delete all data for given keys.
*/
function deleteRecord(ResourceId _tableId) internal {
bytes32[] memory _keyTuple = new bytes32[](0);
StoreSwitch.deleteRecord(_tableId, _keyTuple);
}
/**
* @notice Delete all data for given keys.
*/
function _deleteRecord(ResourceId _tableId) internal {
bytes32[] memory _keyTuple = new bytes32[](0);
StoreCore.deleteRecord(_tableId, _keyTuple, _fieldLayout);
}
/**
* @notice Tightly pack static (fixed length) data using this table's schema.
* @return The static data, encoded into a sequence of bytes.
*/
function encodeStatic(uint8 decimals) internal pure returns (bytes memory) {
return abi.encodePacked(decimals);
}
/**
* @notice Tightly pack dynamic data lengths using this table's schema.
* @return _encodedLengths The lengths of the dynamic fields (packed into a single bytes32 value).
*/
function encodeLengths(
string memory name,
string memory symbol
) internal pure returns (PackedCounter _encodedLengths) {
// Lengths are effectively checked during copy by 2**40 bytes exceeding gas limits
unchecked {
_encodedLengths = PackedCounterLib.pack(bytes(name).length, bytes(symbol).length);
}
}
/**
* @notice Tightly pack dynamic (variable length) data using this table's schema.
* @return The dynamic data, encoded into a sequence of bytes.
*/
function encodeDynamic(string memory name, string memory symbol) internal pure returns (bytes memory) {
return abi.encodePacked(bytes((name)), bytes((symbol)));
}
/**
* @notice Encode all of a record's fields.
* @return The static (fixed length) data, encoded into a sequence of bytes.
* @return The lengths of the dynamic fields (packed into a single bytes32 value).
* @return The dyanmic (variable length) data, encoded into a sequence of bytes.
*/
function encode(
uint8 decimals,
string memory name,
string memory symbol
) internal pure returns (bytes memory, PackedCounter, bytes memory) {
bytes memory _staticData = encodeStatic(decimals);
PackedCounter _encodedLengths = encodeLengths(name, symbol);
bytes memory _dynamicData = encodeDynamic(name, symbol);
return (_staticData, _encodedLengths, _dynamicData);
}
/**
* @notice Encode keys as a bytes32 array using this table's field layout.
*/
function encodeKeyTuple() internal pure returns (bytes32[] memory) {
bytes32[] memory _keyTuple = new bytes32[](0);
return _keyTuple;
}
}