-
Notifications
You must be signed in to change notification settings - Fork 336
/
Test.sol
779 lines (655 loc) · 26.2 KB
/
Test.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
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
// SPDX-License-Identifier: MIT
pragma solidity >=0.6.0 <0.9.0;
import "./Script.sol";
import "ds-test/test.sol";
// Wrappers around Cheatcodes to avoid footguns
abstract contract Test is DSTest, Script {
using stdStorage for StdStorage;
uint256 internal constant UINT256_MAX =
115792089237316195423570985008687907853269984665640564039457584007913129639935;
StdStorage internal stdstore;
/*//////////////////////////////////////////////////////////////////////////
STD-LOGS
//////////////////////////////////////////////////////////////////////////*/
event log_array(uint256[] val);
event log_array(int256[] val);
event log_array(address[] val);
event log_named_array(string key, uint256[] val);
event log_named_array(string key, int256[] val);
event log_named_array(string key, address[] val);
/*//////////////////////////////////////////////////////////////////////////
STD-CHEATS
//////////////////////////////////////////////////////////////////////////*/
// Skip forward or rewind time by the specified number of seconds
function skip(uint256 time) internal {
vm.warp(block.timestamp + time);
}
function rewind(uint256 time) internal {
vm.warp(block.timestamp - time);
}
// Setup a prank from an address that has some ether
function hoax(address who) internal {
vm.deal(who, 1 << 128);
vm.prank(who);
}
function hoax(address who, uint256 give) internal {
vm.deal(who, give);
vm.prank(who);
}
function hoax(address who, address origin) internal {
vm.deal(who, 1 << 128);
vm.prank(who, origin);
}
function hoax(address who, address origin, uint256 give) internal {
vm.deal(who, give);
vm.prank(who, origin);
}
// Start perpetual prank from an address that has some ether
function startHoax(address who) internal {
vm.deal(who, 1 << 128);
vm.startPrank(who);
}
function startHoax(address who, uint256 give) internal {
vm.deal(who, give);
vm.startPrank(who);
}
// Start perpetual prank from an address that has some ether
// tx.origin is set to the origin parameter
function startHoax(address who, address origin) internal {
vm.deal(who, 1 << 128);
vm.startPrank(who, origin);
}
function startHoax(address who, address origin, uint256 give) internal {
vm.deal(who, give);
vm.startPrank(who, origin);
}
function changePrank(address who) internal {
vm.stopPrank();
vm.startPrank(who);
}
// creates a labeled address and the corresponding private key
function makeAddrAndKey(string memory name) internal returns(address addr, uint256 privateKey) {
privateKey = uint256(keccak256(abi.encodePacked(name)));
addr = vm.addr(privateKey);
vm.label(addr, name);
}
// creates a labeled address
function makeAddr(string memory name) internal returns(address addr) {
(addr,) = makeAddrAndKey(name);
}
// DEPRECATED: Use `deal` instead
function tip(address token, address to, uint256 give) internal {
emit log_named_string("WARNING", "Test tip(address,address,uint256): The `tip` stdcheat has been deprecated. Use `deal` instead.");
stdstore
.target(token)
.sig(0x70a08231)
.with_key(to)
.checked_write(give);
}
// The same as Vm's `deal`
// Use the alternative signature for ERC20 tokens
function deal(address to, uint256 give) internal {
vm.deal(to, give);
}
// Set the balance of an account for any ERC20 token
// Use the alternative signature to update `totalSupply`
function deal(address token, address to, uint256 give) internal {
deal(token, to, give, false);
}
function deal(address token, address to, uint256 give, bool adjust) internal {
// get current balance
(, bytes memory balData) = token.call(abi.encodeWithSelector(0x70a08231, to));
uint256 prevBal = abi.decode(balData, (uint256));
// update balance
stdstore
.target(token)
.sig(0x70a08231)
.with_key(to)
.checked_write(give);
// update total supply
if(adjust){
(, bytes memory totSupData) = token.call(abi.encodeWithSelector(0x18160ddd));
uint256 totSup = abi.decode(totSupData, (uint256));
if(give < prevBal) {
totSup -= (prevBal - give);
} else {
totSup += (give - prevBal);
}
stdstore
.target(token)
.sig(0x18160ddd)
.checked_write(totSup);
}
}
function bound(uint256 x, uint256 min, uint256 max) internal virtual returns (uint256 result) {
require(min <= max, "Test bound(uint256,uint256,uint256): Max is less than min.");
uint256 size = max - min;
if (size == 0)
{
result = min;
}
else if (size == UINT256_MAX)
{
result = x;
}
else
{
++size; // make `max` inclusive
uint256 mod = x % size;
result = min + mod;
}
emit log_named_uint("Bound Result", result);
}
// Deploy a contract by fetching the contract bytecode from
// the artifacts directory
// e.g. `deployCode(code, abi.encode(arg1,arg2,arg3))`
function deployCode(string memory what, bytes memory args)
internal
returns (address addr)
{
bytes memory bytecode = abi.encodePacked(vm.getCode(what), args);
/// @solidity memory-safe-assembly
assembly {
addr := create(0, add(bytecode, 0x20), mload(bytecode))
}
require(
addr != address(0),
"Test deployCode(string,bytes): Deployment failed."
);
}
function deployCode(string memory what)
internal
returns (address addr)
{
bytes memory bytecode = vm.getCode(what);
/// @solidity memory-safe-assembly
assembly {
addr := create(0, add(bytecode, 0x20), mload(bytecode))
}
require(
addr != address(0),
"Test deployCode(string): Deployment failed."
);
}
/// deploy contract with value on construction
function deployCode(string memory what, bytes memory args, uint256 val)
internal
returns (address addr)
{
bytes memory bytecode = abi.encodePacked(vm.getCode(what), args);
/// @solidity memory-safe-assembly
assembly {
addr := create(val, add(bytecode, 0x20), mload(bytecode))
}
require(
addr != address(0),
"Test deployCode(string,bytes,uint256): Deployment failed."
);
}
function deployCode(string memory what, uint256 val)
internal
returns (address addr)
{
bytes memory bytecode = vm.getCode(what);
/// @solidity memory-safe-assembly
assembly {
addr := create(val, add(bytecode, 0x20), mload(bytecode))
}
require(
addr != address(0),
"Test deployCode(string,uint256): Deployment failed."
);
}
/*//////////////////////////////////////////////////////////////////////////
STD-ASSERTIONS
//////////////////////////////////////////////////////////////////////////*/
function fail(string memory err) internal virtual {
emit log_named_string("Error", err);
fail();
}
function assertFalse(bool data) internal virtual {
assertTrue(!data);
}
function assertFalse(bool data, string memory err) internal virtual {
assertTrue(!data, err);
}
function assertEq(bool a, bool b) internal {
if (a != b) {
emit log ("Error: a == b not satisfied [bool]");
emit log_named_string (" Expected", b ? "true" : "false");
emit log_named_string (" Actual", a ? "true" : "false");
fail();
}
}
function assertEq(bool a, bool b, string memory err) internal {
if (a != b) {
emit log_named_string("Error", err);
assertEq(a, b);
}
}
function assertEq(bytes memory a, bytes memory b) internal {
assertEq0(a, b);
}
function assertEq(bytes memory a, bytes memory b, string memory err) internal {
assertEq0(a, b, err);
}
function assertEq(uint256[] memory a, uint256[] memory b) internal {
if (keccak256(abi.encode(a)) != keccak256(abi.encode(b))) {
emit log("Error: a == b not satisfied [uint[]]");
emit log_named_array(" Expected", b);
emit log_named_array(" Actual", a);
fail();
}
}
function assertEq(int256[] memory a, int256[] memory b) internal {
if (keccak256(abi.encode(a)) != keccak256(abi.encode(b))) {
emit log("Error: a == b not satisfied [int[]]");
emit log_named_array(" Expected", b);
emit log_named_array(" Actual", a);
fail();
}
}
function assertEq(address[] memory a, address[] memory b) internal {
if (keccak256(abi.encode(a)) != keccak256(abi.encode(b))) {
emit log("Error: a == b not satisfied [address[]]");
emit log_named_array(" Expected", b);
emit log_named_array(" Actual", a);
fail();
}
}
function assertEq(uint256[] memory a, uint256[] memory b, string memory err) internal {
if (keccak256(abi.encode(a)) != keccak256(abi.encode(b))) {
emit log_named_string("Error", err);
assertEq(a, b);
}
}
function assertEq(int256[] memory a, int256[] memory b, string memory err) internal {
if (keccak256(abi.encode(a)) != keccak256(abi.encode(b))) {
emit log_named_string("Error", err);
assertEq(a, b);
}
}
function assertEq(address[] memory a, address[] memory b, string memory err) internal {
if (keccak256(abi.encode(a)) != keccak256(abi.encode(b))) {
emit log_named_string("Error", err);
assertEq(a, b);
}
}
function assertApproxEqAbs(
uint256 a,
uint256 b,
uint256 maxDelta
) internal virtual {
uint256 delta = stdMath.delta(a, b);
if (delta > maxDelta) {
emit log ("Error: a ~= b not satisfied [uint]");
emit log_named_uint (" Expected", b);
emit log_named_uint (" Actual", a);
emit log_named_uint (" Max Delta", maxDelta);
emit log_named_uint (" Delta", delta);
fail();
}
}
function assertApproxEqAbs(
uint256 a,
uint256 b,
uint256 maxDelta,
string memory err
) internal virtual {
uint256 delta = stdMath.delta(a, b);
if (delta > maxDelta) {
emit log_named_string ("Error", err);
assertApproxEqAbs(a, b, maxDelta);
}
}
function assertApproxEqAbs(
int256 a,
int256 b,
uint256 maxDelta
) internal virtual {
uint256 delta = stdMath.delta(a, b);
if (delta > maxDelta) {
emit log ("Error: a ~= b not satisfied [int]");
emit log_named_int (" Expected", b);
emit log_named_int (" Actual", a);
emit log_named_uint (" Max Delta", maxDelta);
emit log_named_uint (" Delta", delta);
fail();
}
}
function assertApproxEqAbs(
int256 a,
int256 b,
uint256 maxDelta,
string memory err
) internal virtual {
uint256 delta = stdMath.delta(a, b);
if (delta > maxDelta) {
emit log_named_string ("Error", err);
assertApproxEqAbs(a, b, maxDelta);
}
}
function assertApproxEqRel(
uint256 a,
uint256 b,
uint256 maxPercentDelta // An 18 decimal fixed point number, where 1e18 == 100%
) internal virtual {
if (b == 0) return assertEq(a, b); // If the expected is 0, actual must be too.
uint256 percentDelta = stdMath.percentDelta(a, b);
if (percentDelta > maxPercentDelta) {
emit log ("Error: a ~= b not satisfied [uint]");
emit log_named_uint (" Expected", b);
emit log_named_uint (" Actual", a);
emit log_named_decimal_uint (" Max % Delta", maxPercentDelta, 18);
emit log_named_decimal_uint (" % Delta", percentDelta, 18);
fail();
}
}
function assertApproxEqRel(
uint256 a,
uint256 b,
uint256 maxPercentDelta, // An 18 decimal fixed point number, where 1e18 == 100%
string memory err
) internal virtual {
if (b == 0) return assertEq(a, b, err); // If the expected is 0, actual must be too.
uint256 percentDelta = stdMath.percentDelta(a, b);
if (percentDelta > maxPercentDelta) {
emit log_named_string ("Error", err);
assertApproxEqRel(a, b, maxPercentDelta);
}
}
function assertApproxEqRel(
int256 a,
int256 b,
uint256 maxPercentDelta
) internal virtual {
if (b == 0) return assertEq(a, b); // If the expected is 0, actual must be too.
uint256 percentDelta = stdMath.percentDelta(a, b);
if (percentDelta > maxPercentDelta) {
emit log ("Error: a ~= b not satisfied [int]");
emit log_named_int (" Expected", b);
emit log_named_int (" Actual", a);
emit log_named_decimal_uint(" Max % Delta", maxPercentDelta, 18);
emit log_named_decimal_uint(" % Delta", percentDelta, 18);
fail();
}
}
function assertApproxEqRel(
int256 a,
int256 b,
uint256 maxPercentDelta,
string memory err
) internal virtual {
if (b == 0) return assertEq(a, b); // If the expected is 0, actual must be too.
uint256 percentDelta = stdMath.percentDelta(a, b);
if (percentDelta > maxPercentDelta) {
emit log_named_string ("Error", err);
assertApproxEqRel(a, b, maxPercentDelta);
}
}
}
/*//////////////////////////////////////////////////////////////////////////
STD-ERRORS
//////////////////////////////////////////////////////////////////////////*/
library stdError {
bytes public constant assertionError = abi.encodeWithSignature("Panic(uint256)", 0x01);
bytes public constant arithmeticError = abi.encodeWithSignature("Panic(uint256)", 0x11);
bytes public constant divisionError = abi.encodeWithSignature("Panic(uint256)", 0x12);
bytes public constant enumConversionError = abi.encodeWithSignature("Panic(uint256)", 0x21);
bytes public constant encodeStorageError = abi.encodeWithSignature("Panic(uint256)", 0x22);
bytes public constant popError = abi.encodeWithSignature("Panic(uint256)", 0x31);
bytes public constant indexOOBError = abi.encodeWithSignature("Panic(uint256)", 0x32);
bytes public constant memOverflowError = abi.encodeWithSignature("Panic(uint256)", 0x41);
bytes public constant zeroVarError = abi.encodeWithSignature("Panic(uint256)", 0x51);
// DEPRECATED: Use Vm's `expectRevert` without any arguments instead
bytes public constant lowLevelError = bytes(""); // `0x`
}
/*//////////////////////////////////////////////////////////////////////////
STD-STORAGE
//////////////////////////////////////////////////////////////////////////*/
struct StdStorage {
mapping (address => mapping(bytes4 => mapping(bytes32 => uint256))) slots;
mapping (address => mapping(bytes4 => mapping(bytes32 => bool))) finds;
bytes32[] _keys;
bytes4 _sig;
uint256 _depth;
address _target;
bytes32 _set;
}
library stdStorage {
event SlotFound(address who, bytes4 fsig, bytes32 keysHash, uint slot);
event WARNING_UninitedSlot(address who, uint slot);
uint256 private constant UINT256_MAX = 115792089237316195423570985008687907853269984665640564039457584007913129639935;
int256 private constant INT256_MAX = 57896044618658097711785492504343953926634992332820282019728792003956564819967;
Vm private constant vm_std_store = Vm(address(uint160(uint256(keccak256('hevm cheat code')))));
function sigs(
string memory sigStr
)
internal
pure
returns (bytes4)
{
return bytes4(keccak256(bytes(sigStr)));
}
/// @notice find an arbitrary storage slot given a function sig, input data, address of the contract and a value to check against
// slot complexity:
// if flat, will be bytes32(uint256(uint));
// if map, will be keccak256(abi.encode(key, uint(slot)));
// if deep map, will be keccak256(abi.encode(key1, keccak256(abi.encode(key0, uint(slot)))));
// if map struct, will be bytes32(uint256(keccak256(abi.encode(key1, keccak256(abi.encode(key0, uint(slot)))))) + structFieldDepth);
function find(
StdStorage storage self
)
internal
returns (uint256)
{
address who = self._target;
bytes4 fsig = self._sig;
uint256 field_depth = self._depth;
bytes32[] memory ins = self._keys;
// calldata to test against
if (self.finds[who][fsig][keccak256(abi.encodePacked(ins, field_depth))]) {
return self.slots[who][fsig][keccak256(abi.encodePacked(ins, field_depth))];
}
bytes memory cald = abi.encodePacked(fsig, flatten(ins));
vm_std_store.record();
bytes32 fdat;
{
(, bytes memory rdat) = who.staticcall(cald);
fdat = bytesToBytes32(rdat, 32*field_depth);
}
(bytes32[] memory reads, ) = vm_std_store.accesses(address(who));
if (reads.length == 1) {
bytes32 curr = vm_std_store.load(who, reads[0]);
if (curr == bytes32(0)) {
emit WARNING_UninitedSlot(who, uint256(reads[0]));
}
if (fdat != curr) {
require(false, "stdStorage find(StdStorage): Packed slot. This would cause dangerous overwriting and currently isn't supported.");
}
emit SlotFound(who, fsig, keccak256(abi.encodePacked(ins, field_depth)), uint256(reads[0]));
self.slots[who][fsig][keccak256(abi.encodePacked(ins, field_depth))] = uint256(reads[0]);
self.finds[who][fsig][keccak256(abi.encodePacked(ins, field_depth))] = true;
} else if (reads.length > 1) {
for (uint256 i = 0; i < reads.length; i++) {
bytes32 prev = vm_std_store.load(who, reads[i]);
if (prev == bytes32(0)) {
emit WARNING_UninitedSlot(who, uint256(reads[i]));
}
// store
vm_std_store.store(who, reads[i], bytes32(hex"1337"));
bool success;
bytes memory rdat;
{
(success, rdat) = who.staticcall(cald);
fdat = bytesToBytes32(rdat, 32*field_depth);
}
if (success && fdat == bytes32(hex"1337")) {
// we found which of the slots is the actual one
emit SlotFound(who, fsig, keccak256(abi.encodePacked(ins, field_depth)), uint256(reads[i]));
self.slots[who][fsig][keccak256(abi.encodePacked(ins, field_depth))] = uint256(reads[i]);
self.finds[who][fsig][keccak256(abi.encodePacked(ins, field_depth))] = true;
vm_std_store.store(who, reads[i], prev);
break;
}
vm_std_store.store(who, reads[i], prev);
}
} else {
require(false, "stdStorage find(StdStorage): No storage use detected for target.");
}
require(self.finds[who][fsig][keccak256(abi.encodePacked(ins, field_depth))], "stdStorage find(StdStorage): Slot(s) not found.");
delete self._target;
delete self._sig;
delete self._keys;
delete self._depth;
return self.slots[who][fsig][keccak256(abi.encodePacked(ins, field_depth))];
}
function target(StdStorage storage self, address _target) internal returns (StdStorage storage) {
self._target = _target;
return self;
}
function sig(StdStorage storage self, bytes4 _sig) internal returns (StdStorage storage) {
self._sig = _sig;
return self;
}
function sig(StdStorage storage self, string memory _sig) internal returns (StdStorage storage) {
self._sig = sigs(_sig);
return self;
}
function with_key(StdStorage storage self, address who) internal returns (StdStorage storage) {
self._keys.push(bytes32(uint256(uint160(who))));
return self;
}
function with_key(StdStorage storage self, uint256 amt) internal returns (StdStorage storage) {
self._keys.push(bytes32(amt));
return self;
}
function with_key(StdStorage storage self, bytes32 key) internal returns (StdStorage storage) {
self._keys.push(key);
return self;
}
function depth(StdStorage storage self, uint256 _depth) internal returns (StdStorage storage) {
self._depth = _depth;
return self;
}
function checked_write(StdStorage storage self, address who) internal {
checked_write(self, bytes32(uint256(uint160(who))));
}
function checked_write(StdStorage storage self, uint256 amt) internal {
checked_write(self, bytes32(amt));
}
function checked_write(StdStorage storage self, bool write) internal {
bytes32 t;
/// @solidity memory-safe-assembly
assembly {
t := write
}
checked_write(self, t);
}
function checked_write(
StdStorage storage self,
bytes32 set
) internal {
address who = self._target;
bytes4 fsig = self._sig;
uint256 field_depth = self._depth;
bytes32[] memory ins = self._keys;
bytes memory cald = abi.encodePacked(fsig, flatten(ins));
if (!self.finds[who][fsig][keccak256(abi.encodePacked(ins, field_depth))]) {
find(self);
}
bytes32 slot = bytes32(self.slots[who][fsig][keccak256(abi.encodePacked(ins, field_depth))]);
bytes32 fdat;
{
(, bytes memory rdat) = who.staticcall(cald);
fdat = bytesToBytes32(rdat, 32*field_depth);
}
bytes32 curr = vm_std_store.load(who, slot);
if (fdat != curr) {
require(false, "stdStorage find(StdStorage): Packed slot. This would cause dangerous overwriting and currently isn't supported.");
}
vm_std_store.store(who, slot, set);
delete self._target;
delete self._sig;
delete self._keys;
delete self._depth;
}
function read(StdStorage storage self) private returns (bytes memory) {
address t = self._target;
uint256 s = find(self);
return abi.encode(vm_std_store.load(t, bytes32(s)));
}
function read_bytes32(StdStorage storage self) internal returns (bytes32) {
return abi.decode(read(self), (bytes32));
}
function read_bool(StdStorage storage self) internal returns (bool) {
int256 v = read_int(self);
if (v == 0) return false;
if (v == 1) return true;
revert("stdStorage read_bool(StdStorage): Cannot decode. Make sure you are reading a bool.");
}
function read_address(StdStorage storage self) internal returns (address) {
return abi.decode(read(self), (address));
}
function read_uint(StdStorage storage self) internal returns (uint256) {
return abi.decode(read(self), (uint256));
}
function read_int(StdStorage storage self) internal returns (int256) {
return abi.decode(read(self), (int256));
}
function bytesToBytes32(bytes memory b, uint offset) public pure returns (bytes32) {
bytes32 out;
uint256 max = b.length > 32 ? 32 : b.length;
for (uint i = 0; i < max; i++) {
out |= bytes32(b[offset + i] & 0xFF) >> (i * 8);
}
return out;
}
function flatten(bytes32[] memory b) private pure returns (bytes memory)
{
bytes memory result = new bytes(b.length * 32);
for (uint256 i = 0; i < b.length; i++) {
bytes32 k = b[i];
/// @solidity memory-safe-assembly
assembly {
mstore(add(result, add(32, mul(32, i))), k)
}
}
return result;
}
}
/*//////////////////////////////////////////////////////////////////////////
STD-MATH
//////////////////////////////////////////////////////////////////////////*/
library stdMath {
int256 private constant INT256_MIN = -57896044618658097711785492504343953926634992332820282019728792003956564819968;
function abs(int256 a) internal pure returns (uint256) {
// Required or it will fail when `a = type(int256).min`
if (a == INT256_MIN)
return 57896044618658097711785492504343953926634992332820282019728792003956564819968;
return uint256(a > 0 ? a : -a);
}
function delta(uint256 a, uint256 b) internal pure returns (uint256) {
return a > b
? a - b
: b - a;
}
function delta(int256 a, int256 b) internal pure returns (uint256) {
// a and b are of the same sign
// this works thanks to two's complement, the left-most bit is the sign bit
if ((a ^ b) > -1) {
return delta(abs(a), abs(b));
}
// a and b are of opposite signs
return abs(a) + abs(b);
}
function percentDelta(uint256 a, uint256 b) internal pure returns (uint256) {
uint256 absDelta = delta(a, b);
return absDelta * 1e18 / b;
}
function percentDelta(int256 a, int256 b) internal pure returns (uint256) {
uint256 absDelta = delta(a, b);
uint256 absB = abs(b);
return absDelta * 1e18 / absB;
}
}