github.com/DxChainNetwork/dxc@v0.8.1-0.20220824085222-1162e304b6e7/consensus/dpos/systemcontract/contracts/Migrate.sol (about) 1 // SPDX-License-Identifier: MIT 2 pragma solidity ^0.8.0; 3 4 // OpenZeppelin Contracts (last updated v4.7.0) (utils/structs/EnumerableSet.sol) 5 /** 6 7 * @dev Library for managing 8 9 * https://en.wikipedia.org/wiki/Set_(abstract_data_type)[sets] of primitive 10 11 * types. 12 * 13 14 * Sets have the following properties: 15 * 16 17 * - Elements are added, removed, and checked for existence in constant time 18 19 * (O(1)). 20 21 * - Elements are enumerated in O(n). No guarantees are made on the ordering. 22 * 23 24 * ``` 25 26 ``` 27 28 * contract Example { 29 30 * // Add the library methods 31 32 * using EnumerableSet for EnumerableSet.AddressSet; 33 * 34 35 * // Declare a set state variable 36 37 * EnumerableSet.AddressSet private mySet; 38 39 * } 40 41 * ``` 42 * 43 ``` 44 45 * As of v3.3.0, sets of type `bytes32` (`Bytes32Set`), `address` (`AddressSet`) 46 47 * and `uint256` (`UintSet`) are supported. 48 * 49 50 * [WARNING] 51 52 * ==== 53 54 * Trying to delete such a structure from storage will likely result in data corruption, rendering the structure unusable. 55 56 * See https://github.com/ethereum/solidity/pull/11843[ethereum/solidity#11843] for more info. 57 * 58 59 * In order to clean an EnumerableSet, you can either remove all elements one by one or create a fresh instance using an array of EnumerableSet. 60 61 * ==== 62 */ 63 library EnumerableSet { 64 // To implement this library for multiple types with as little code 65 // repetition as possible, we write it in terms of a generic Set type with 66 // bytes32 values. 67 // The Set implementation uses private functions, and user-facing 68 // implementations (such as AddressSet) are just wrappers around the 69 // underlying Set. 70 // This means that we can only create new EnumerableSets for types that fit 71 // in bytes32. 72 73 struct Set { 74 // Storage of set values 75 bytes32[] _values; 76 // Position of the value in the `values` array, plus 1 because index 0 77 // means a value is not in the set. 78 mapping(bytes32 => uint256) _indexes; 79 } 80 81 /** 82 83 * @dev Add a value to a set. O(1). 84 * 85 * Returns true if the value was added to the set, that is if it was not 86 * already present. 87 */ 88 function _add(Set storage set, bytes32 value) private returns (bool) { 89 if (!_contains(set, value)) { 90 set._values.push(value); 91 // The value is stored at length-1, but we add 1 to all indexes 92 // and use 0 as a sentinel value 93 set._indexes[value] = set._values.length; 94 return true; 95 } else { 96 return false; 97 } 98 } 99 100 /** 101 102 * @dev Removes a value from a set. O(1). 103 * 104 105 * Returns true if the value was removed from the set, that is if it was 106 107 * present. 108 */ 109 function _remove(Set storage set, bytes32 value) private returns (bool) { 110 // We read and store the value's index to prevent multiple reads from the same storage slot 111 uint256 valueIndex = set._indexes[value]; 112 113 if (valueIndex != 0) { 114 // Equivalent to contains(set, value) 115 // To delete an element from the _values array in O(1), we swap the element to delete with the last one in 116 // the array, and then remove the last element (sometimes called as 'swap and pop'). 117 // This modifies the order of the array, as noted in {at}. 118 119 uint256 toDeleteIndex = valueIndex - 1; 120 uint256 lastIndex = set._values.length - 1; 121 122 if (lastIndex != toDeleteIndex) { 123 bytes32 lastValue = set._values[lastIndex]; 124 125 // Move the last value to the index where the value to delete is 126 set._values[toDeleteIndex] = lastValue; 127 // Update the index for the moved value 128 set._indexes[lastValue] = valueIndex; // Replace lastValue's index to valueIndex 129 } 130 131 // Delete the slot where the moved value was stored 132 set._values.pop(); 133 134 // Delete the index for the deleted slot 135 delete set._indexes[value]; 136 137 return true; 138 139 } else { 140 return false; 141 } 142 } 143 144 /** 145 146 * @dev Returns true if the value is in the set. O(1). 147 */ 148 function _contains(Set storage set, bytes32 value) 149 private 150 view 151 returns (bool) 152 { 153 return set._indexes[value] != 0; 154 } 155 156 /** 157 158 * @dev Returns the number of values on the set. O(1). 159 */ 160 function _length(Set storage set) private view returns (uint256) { 161 return set._values.length; 162 } 163 164 /** 165 166 * @dev Returns the value stored at position `index` in the set. O(1). 167 * 168 * Note that there are no guarantees on the ordering of values inside the 169 * array, and it may change when more values are added or removed. 170 * 171 * Requirements: 172 * 173 * - `index` must be strictly less than {length}. 174 */ 175 function _at(Set storage set, uint256 index) 176 private 177 view 178 returns (bytes32) 179 { 180 return set._values[index]; 181 } 182 183 /** 184 185 * @dev Return the entire set in an array 186 * 187 * WARNING: This operation will copy the entire storage to memory, which can be quite expensive. This is designed 188 * to mostly be used by view accessors that are queried without any gas fees. Developers should keep in mind that 189 * this function has an unbounded cost, and using it as part of a state-changing function may render the function 190 * uncallable if the set grows to a point where copying to memory consumes too much gas to fit in a block. 191 */ 192 function _values(Set storage set) private view returns (bytes32[] memory) { 193 return set._values; 194 } 195 196 // Bytes32Set 197 198 struct Bytes32Set { 199 Set _inner; 200 } 201 202 /** 203 204 * @dev Add a value to a set. O(1). 205 * 206 * Returns true if the value was added to the set, that is if it was not 207 * already present. 208 */ 209 function add(Bytes32Set storage set, bytes32 value) 210 internal 211 returns (bool) 212 { 213 return _add(set._inner, value); 214 } 215 216 /** 217 218 * @dev Removes a value from a set. O(1). 219 * 220 * Returns true if the value was removed from the set, that is if it was 221 * present. 222 */ 223 function remove(Bytes32Set storage set, bytes32 value) 224 internal 225 returns (bool) 226 { 227 return _remove(set._inner, value); 228 } 229 230 /** 231 232 * @dev Returns true if the value is in the set. O(1). 233 */ 234 function contains(Bytes32Set storage set, bytes32 value) 235 internal 236 view 237 returns (bool) 238 { 239 return _contains(set._inner, value); 240 } 241 242 /** 243 244 * @dev Returns the number of values in the set. O(1). 245 */ 246 function length(Bytes32Set storage set) internal view returns (uint256) { 247 return _length(set._inner); 248 } 249 250 /** 251 252 * @dev Returns the value stored at position `index` in the set. O(1). 253 * 254 * Note that there are no guarantees on the ordering of values inside the 255 * array, and it may change when more values are added or removed. 256 * 257 * Requirements: 258 * 259 * - `index` must be strictly less than {length}. 260 */ 261 function at(Bytes32Set storage set, uint256 index) 262 internal 263 view 264 returns (bytes32) 265 { 266 return _at(set._inner, index); 267 } 268 269 /** 270 271 * @dev Return the entire set in an array 272 * 273 * WARNING: This operation will copy the entire storage to memory, which can be quite expensive. This is designed 274 * to mostly be used by view accessors that are queried without any gas fees. Developers should keep in mind that 275 * this function has an unbounded cost, and using it as part of a state-changing function may render the function 276 * uncallable if the set grows to a point where copying to memory consumes too much gas to fit in a block. 277 */ 278 function values(Bytes32Set storage set) 279 internal 280 view 281 returns (bytes32[] memory) 282 { 283 return _values(set._inner); 284 } 285 286 // AddressSet 287 288 struct AddressSet { 289 Set _inner; 290 } 291 292 /** 293 294 * @dev Add a value to a set. O(1). 295 * 296 * Returns true if the value was added to the set, that is if it was not 297 * already present. 298 */ 299 function add(AddressSet storage set, address value) 300 internal 301 returns (bool) 302 { 303 return _add(set._inner, bytes32(uint256(uint160(value)))); 304 } 305 306 /** 307 308 * @dev Removes a value from a set. O(1). 309 * 310 * Returns true if the value was removed from the set, that is if it was 311 * present. 312 */ 313 function remove(AddressSet storage set, address value) 314 internal 315 returns (bool) 316 { 317 return _remove(set._inner, bytes32(uint256(uint160(value)))); 318 } 319 320 /** 321 322 * @dev Returns true if the value is in the set. O(1). 323 */ 324 function contains(AddressSet storage set, address value) 325 internal 326 view 327 returns (bool) 328 { 329 return _contains(set._inner, bytes32(uint256(uint160(value)))); 330 } 331 332 /** 333 334 * @dev Returns the number of values in the set. O(1). 335 */ 336 function length(AddressSet storage set) internal view returns (uint256) { 337 return _length(set._inner); 338 } 339 340 /** 341 342 * @dev Returns the value stored at position `index` in the set. O(1). 343 * 344 * Note that there are no guarantees on the ordering of values inside the 345 * array, and it may change when more values are added or removed. 346 * 347 * Requirements: 348 * 349 * - `index` must be strictly less than {length}. 350 */ 351 function at(AddressSet storage set, uint256 index) 352 internal 353 view 354 returns (address) 355 { 356 return address(uint160(uint256(_at(set._inner, index)))); 357 } 358 359 /** 360 361 * @dev Return the entire set in an array 362 * 363 364 * WARNING: This operation will copy the entire storage to memory, which can be quite expensive. This is designed 365 366 * to mostly be used by view accessors that are queried without any gas fees. Developers should keep in mind that 367 368 * this function has an unbounded cost, and using it as part of a state-changing function may render the function 369 370 * uncallable if the set grows to a point where copying to memory consumes too much gas to fit in a block. 371 */ 372 function values(AddressSet storage set) 373 internal 374 view 375 returns (address[] memory) 376 { 377 bytes32[] memory store = _values(set._inner); 378 address[] memory result; 379 380 /// @solidity memory-safe-assembly 381 assembly { 382 result := store 383 } 384 385 return result; 386 } 387 388 // UintSet 389 390 struct UintSet { 391 Set _inner; 392 } 393 394 /** 395 396 * @dev Add a value to a set. O(1). 397 * 398 * Returns true if the value was added to the set, that is if it was not 399 * already present. 400 */ 401 function add(UintSet storage set, uint256 value) internal returns (bool) { 402 return _add(set._inner, bytes32(value)); 403 } 404 405 /** 406 407 * @dev Removes a value from a set. O(1). 408 * 409 * Returns true if the value was removed from the set, that is if it was 410 * present. 411 */ 412 function remove(UintSet storage set, uint256 value) 413 internal 414 returns (bool) 415 { 416 return _remove(set._inner, bytes32(value)); 417 } 418 419 /** 420 421 * @dev Returns true if the value is in the set. O(1). 422 */ 423 function contains(UintSet storage set, uint256 value) 424 internal 425 view 426 returns (bool) 427 { 428 return _contains(set._inner, bytes32(value)); 429 } 430 431 /** 432 433 * @dev Returns the number of values on the set. O(1). 434 */ 435 function length(UintSet storage set) internal view returns (uint256) { 436 return _length(set._inner); 437 } 438 439 /** 440 441 * @dev Returns the value stored at position `index` in the set. O(1). 442 * 443 * Note that there are no guarantees on the ordering of values inside the 444 * array, and it may change when more values are added or removed. 445 * 446 * Requirements: 447 * 448 * - `index` must be strictly less than {length}. 449 */ 450 function at(UintSet storage set, uint256 index) 451 internal 452 view 453 returns (uint256) 454 { 455 return uint256(_at(set._inner, index)); 456 } 457 458 /** 459 460 * @dev Return the entire set in an array 461 * 462 463 * WARNING: This operation will copy the entire storage to memory, which can be quite expensive. This is designed 464 465 * to mostly be used by view accessors that are queried without any gas fees. Developers should keep in mind that 466 467 * this function has an unbounded cost, and using it as part of a state-changing function may render the function 468 469 * uncallable if the set grows to a point where copying to memory consumes too much gas to fit in a block. 470 */ 471 function values(UintSet storage set) 472 internal 473 view 474 returns (uint256[] memory) 475 { 476 bytes32[] memory store = _values(set._inner); 477 uint256[] memory result; 478 479 /// @solidity memory-safe-assembly 480 assembly { 481 result := store 482 } 483 484 return result; 485 } 486 } 487 488 // OpenZeppelin Contracts v4.4.1 (utils/Context.sol) 489 /** 490 491 * @dev Provides information about the current execution context, including the 492 493 * sender of the transaction and its data. While these are generally available 494 495 * via msg.sender and msg.data, they should not be accessed in such a direct 496 497 * manner, since when dealing with meta-transactions the account sending and 498 499 * paying for execution may not be the actual sender (as far as an application 500 501 * is concerned). 502 * 503 504 * This contract is only required for intermediate, library-like contracts. 505 */ 506 abstract contract Context { 507 function _msgSender() internal view virtual returns (address) { 508 return msg.sender; 509 } 510 511 function _msgData() internal view virtual returns (bytes calldata) { 512 return msg.data; 513 } 514 } 515 516 // OpenZeppelin Contracts (last updated v4.7.0) (security/Pausable.sol) 517 /** 518 519 * @dev Contract module which allows children to implement an emergency stop 520 521 * mechanism that can be triggered by an authorized account. 522 * 523 524 * This module is used through inheritance. It will make available the 525 526 * modifiers `whenNotPaused` and `whenPaused`, which can be applied to 527 528 * the functions of your contract. Note that they will not be pausable by 529 530 * simply including this module, only once the modifiers are put in place. 531 */ 532 abstract contract Pausable is Context { 533 /** 534 535 * @dev Emitted when the pause is triggered by `account`. 536 */ 537 event Paused(address account); 538 539 /** 540 541 * @dev Emitted when the pause is lifted by `account`. 542 */ 543 event Unpaused(address account); 544 545 bool private _paused; 546 547 /** 548 549 * @dev Initializes the contract in unpaused state. 550 */ 551 constructor() { 552 _paused = false; 553 } 554 555 /** 556 557 * @dev Modifier to make a function callable only when the contract is not paused. 558 * 559 * Requirements: 560 * 561 * - The contract must not be paused. 562 */ 563 modifier whenNotPaused() { 564 _requireNotPaused(); 565 _; 566 } 567 568 /** 569 570 * @dev Modifier to make a function callable only when the contract is paused. 571 * 572 * Requirements: 573 * 574 * - The contract must be paused. 575 */ 576 modifier whenPaused() { 577 _requirePaused(); 578 _; 579 } 580 581 /** 582 583 * @dev Returns true if the contract is paused, and false otherwise. 584 */ 585 function paused() public view virtual returns (bool) { 586 return _paused; 587 } 588 589 /** 590 591 * @dev Throws if the contract is paused. 592 */ 593 function _requireNotPaused() internal view virtual { 594 require(!paused(), "Pausable: paused"); 595 } 596 597 /** 598 599 * @dev Throws if the contract is not paused. 600 */ 601 function _requirePaused() internal view virtual { 602 require(paused(), "Pausable: not paused"); 603 } 604 605 /** 606 607 * @dev Triggers stopped state. 608 * 609 * Requirements: 610 * 611 * - The contract must not be paused. 612 */ 613 function _pause() internal virtual whenNotPaused { 614 _paused = true; 615 emit Paused(_msgSender()); 616 } 617 618 /** 619 620 * @dev Returns to normal state. 621 * 622 * Requirements: 623 * 624 * - The contract must be paused. 625 */ 626 function _unpause() internal virtual whenPaused { 627 _paused = false; 628 emit Unpaused(_msgSender()); 629 } 630 } 631 632 // OpenZeppelin Contracts v4.4.1 (security/ReentrancyGuard.sol) 633 /** 634 635 * @dev Contract module that helps prevent reentrant calls to a function. 636 * 637 638 * Inheriting from `ReentrancyGuard` will make the {nonReentrant} modifier 639 640 * available, which can be applied to functions to make sure there are no nested 641 642 * (reentrant) calls to them. 643 * 644 645 * Note that because there is a single `nonReentrant` guard, functions marked as 646 647 * `nonReentrant` may not call one another. This can be worked around by making 648 649 * those functions `private`, and then adding `external` `nonReentrant` entry 650 651 * points to them. 652 * 653 654 * TIP: If you would like to learn more about reentrancy and alternative ways 655 656 * to protect against it, check out our blog post 657 658 * https://blog.openzeppelin.com/reentrancy-after-istanbul/[Reentrancy After Istanbul]. 659 */ 660 abstract contract ReentrancyGuard { 661 // Booleans are more expensive than uint256 or any type that takes up a full 662 // word because each write operation emits an extra SLOAD to first read the 663 // slot's contents, replace the bits taken up by the boolean, and then write 664 // back. This is the compiler's defense against contract upgrades and 665 // pointer aliasing, and it cannot be disabled. 666 667 // The values being non-zero value makes deployment a bit more expensive, 668 // but in exchange the refund on every call to nonReentrant will be lower in 669 // amount. Since refunds are capped to a percentage of the total 670 // transaction's gas, it is best to keep them low in cases like this one, to 671 // increase the likelihood of the full refund coming into effect. 672 uint256 private constant _NOT_ENTERED = 1; 673 uint256 private constant _ENTERED = 2; 674 675 uint256 private _status; 676 677 constructor() { 678 _status = _NOT_ENTERED; 679 } 680 681 /** 682 683 * @dev Prevents a contract from calling itself, directly or indirectly. 684 685 * Calling a `nonReentrant` function from another `nonReentrant` 686 687 * function is not supported. It is possible to prevent this from happening 688 689 * by making the `nonReentrant` function external, and making it call a 690 691 * `private` function that does the actual work. 692 */ 693 modifier nonReentrant() { 694 // On the first call to nonReentrant, _notEntered will be true 695 require(_status != _ENTERED, "ReentrancyGuard: reentrant call"); 696 697 // Any calls to nonReentrant after this point will fail 698 _status = _ENTERED; 699 700 _; 701 702 // By storing the original value once again, a refund is triggered (see 703 // https://eips.ethereum.org/EIPS/eip-2200) 704 _status = _NOT_ENTERED; 705 } 706 } 707 708 // OpenZeppelin Contracts (last updated v4.7.0) (utils/Address.sol) 709 /** 710 711 * @dev Collection of functions related to the address type 712 */ 713 library Address { 714 /** 715 716 * @dev Returns true if `account` is a contract. 717 * 718 719 * [IMPORTANT] 720 721 * ==== 722 723 * It is unsafe to assume that an address for which this function returns 724 725 * false is an externally-owned account (EOA) and not a contract. 726 * 727 728 * Among others, `isContract` will return false for the following 729 730 * types of addresses: 731 * 732 733 * - an externally-owned account 734 735 * - a contract in construction 736 737 * - an address where a contract will be created 738 739 * - an address where a contract lived, but was destroyed 740 741 * ==== 742 * 743 744 * [IMPORTANT] 745 746 * ==== 747 748 * You shouldn't rely on `isContract` to protect against flash loan attacks! 749 * 750 751 * Preventing calls from contracts is highly discouraged. It breaks composability, breaks support for smart wallets 752 753 * like Gnosis Safe, and does not provide security since it can be circumvented by calling from a contract 754 755 * constructor. 756 757 * ==== 758 */ 759 function isContract(address account) internal view returns (bool) { 760 // This method relies on extcodesize/address.code.length, which returns 0 761 // for contracts in construction, since the code is only stored at the end 762 // of the constructor execution. 763 764 return account.code.length > 0; 765 } 766 767 /** 768 769 * @dev Replacement for Solidity's `transfer`: sends `amount` wei to 770 771 * `recipient`, forwarding all available gas and reverting on errors. 772 * 773 774 * https://eips.ethereum.org/EIPS/eip-1884[EIP1884] increases the gas cost 775 776 * of certain opcodes, possibly making contracts go over the 2300 gas limit 777 778 * imposed by `transfer`, making them unable to receive funds via 779 780 * `transfer`. {sendValue} removes this limitation. 781 * 782 783 * https://diligence.consensys.net/posts/2019/09/stop-using-soliditys-transfer-now/[Learn more]. 784 * 785 786 * IMPORTANT: because control is transferred to `recipient`, care must be 787 788 * taken to not create reentrancy vulnerabilities. Consider using 789 790 * {ReentrancyGuard} or the 791 792 * https://solidity.readthedocs.io/en/v0.5.11/security-considerations.html#use-the-checks-effects-interactions-pattern[checks-effects-interactions pattern]. 793 */ 794 function sendValue(address payable recipient, uint256 amount) internal { 795 require( 796 address(this).balance >= amount, 797 "Address: insufficient balance" 798 ); 799 800 (bool success, ) = recipient.call{value: amount}(""); 801 require( 802 success, 803 "Address: unable to send value, recipient may have reverted" 804 ); 805 } 806 807 /** 808 809 * @dev Performs a Solidity function call using a low level `call`. A 810 * plain `call` is an unsafe replacement for a function call: use this 811 * function instead. 812 * 813 * If `target` reverts with a revert reason, it is bubbled up by this 814 * function (like regular Solidity function calls). 815 * 816 * Returns the raw returned data. To convert to the expected return value, 817 * use https://solidity.readthedocs.io/en/latest/units-and-global-variables.html?highlight=abi.decode#abi-encoding-and-decoding-functions[`abi.decode`]. 818 * 819 * Requirements: 820 * 821 * - `target` must be a contract. 822 * - calling `target` with `data` must not revert. 823 * 824 * _Available since v3.1._ 825 */ 826 function functionCall(address target, bytes memory data) 827 internal 828 returns (bytes memory) 829 { 830 return functionCall(target, data, "Address: low-level call failed"); 831 } 832 833 /** 834 835 * @dev Same as {xref-Address-functionCall-address-bytes-}[`functionCall`], but with 836 * `errorMessage` as a fallback revert reason when `target` reverts. 837 * 838 * _Available since v3.1._ 839 */ 840 function functionCall( 841 address target, 842 bytes memory data, 843 string memory errorMessage 844 ) internal returns (bytes memory) { 845 return functionCallWithValue(target, data, 0, errorMessage); 846 } 847 848 /** 849 850 * @dev Same as {xref-Address-functionCall-address-bytes-}[`functionCall`], 851 * but also transferring `value` wei to `target`. 852 * 853 * Requirements: 854 * 855 * - the calling contract must have an ETH balance of at least `value`. 856 * - the called Solidity function must be `payable`. 857 * 858 * _Available since v3.1._ 859 */ 860 function functionCallWithValue( 861 address target, 862 bytes memory data, 863 uint256 value 864 ) internal returns (bytes memory) { 865 return 866 functionCallWithValue( 867 target, 868 data, 869 value, 870 "Address: low-level call with value failed" 871 ); 872 } 873 874 /** 875 876 * @dev Same as {xref-Address-functionCallWithValue-address-bytes-uint256-}[`functionCallWithValue`], but 877 878 * with `errorMessage` as a fallback revert reason when `target` reverts. 879 * 880 881 * _Available since v3.1._ 882 */ 883 function functionCallWithValue( 884 address target, 885 bytes memory data, 886 uint256 value, 887 string memory errorMessage 888 ) internal returns (bytes memory) { 889 require( 890 address(this).balance >= value, 891 "Address: insufficient balance for call" 892 ); 893 require(isContract(target), "Address: call to non-contract"); 894 895 (bool success, bytes memory returndata) = target.call{value: value}( 896 data 897 ); 898 return verifyCallResult(success, returndata, errorMessage); 899 } 900 901 /** 902 903 * @dev Same as {xref-Address-functionCall-address-bytes-}[`functionCall`], 904 * but performing a static call. 905 * 906 * _Available since v3.3._ 907 */ 908 function functionStaticCall(address target, bytes memory data) 909 internal 910 view 911 returns (bytes memory) 912 { 913 return 914 functionStaticCall( 915 target, 916 data, 917 "Address: low-level static call failed" 918 ); 919 } 920 921 /** 922 923 * @dev Same as {xref-Address-functionCall-address-bytes-string-}[`functionCall`], 924 925 * but performing a static call. 926 * 927 928 * _Available since v3.3._ 929 */ 930 function functionStaticCall( 931 address target, 932 bytes memory data, 933 string memory errorMessage 934 ) internal view returns (bytes memory) { 935 require(isContract(target), "Address: static call to non-contract"); 936 937 (bool success, bytes memory returndata) = target.staticcall(data); 938 return verifyCallResult(success, returndata, errorMessage); 939 } 940 941 /** 942 943 * @dev Same as {xref-Address-functionCall-address-bytes-}[`functionCall`], 944 * but performing a delegate call. 945 * 946 * _Available since v3.4._ 947 */ 948 function functionDelegateCall(address target, bytes memory data) 949 internal 950 returns (bytes memory) 951 { 952 return 953 functionDelegateCall( 954 target, 955 data, 956 "Address: low-level delegate call failed" 957 ); 958 } 959 960 /** 961 962 * @dev Same as {xref-Address-functionCall-address-bytes-string-}[`functionCall`], 963 964 * but performing a delegate call. 965 * 966 967 * _Available since v3.4._ 968 */ 969 function functionDelegateCall( 970 address target, 971 bytes memory data, 972 string memory errorMessage 973 ) internal returns (bytes memory) { 974 require(isContract(target), "Address: delegate call to non-contract"); 975 976 (bool success, bytes memory returndata) = target.delegatecall(data); 977 return verifyCallResult(success, returndata, errorMessage); 978 } 979 980 /** 981 982 * @dev Tool to verifies that a low level call was successful, and revert if it wasn't, either by bubbling the 983 * revert reason using the provided one. 984 * 985 * _Available since v4.3._ 986 */ 987 function verifyCallResult( 988 bool success, 989 bytes memory returndata, 990 string memory errorMessage 991 ) internal pure returns (bytes memory) { 992 if (success) { 993 return returndata; 994 } else { 995 // Look for revert reason and bubble it up if present 996 if (returndata.length > 0) { 997 // The easiest way to bubble the revert reason is using memory via assembly 998 /// @solidity memory-safe-assembly 999 assembly { 1000 let returndata_size := mload(returndata) 1001 revert(add(32, returndata), returndata_size) 1002 } 1003 } else { 1004 revert(errorMessage); 1005 } 1006 } 1007 } 1008 } 1009 1010 // OpenZeppelin Contracts (last updated v4.7.0) (proxy/utils/Initializable.sol) 1011 /** 1012 1013 * @dev This is a base contract to aid in writing upgradeable contracts, or any kind of contract that will be deployed 1014 1015 * behind a proxy. Since proxied contracts do not make use of a constructor, it's common to move constructor logic to an 1016 1017 * external initializer function, usually called `initialize`. It then becomes necessary to protect this initializer 1018 1019 * function so it can only be called once. The {initializer} modifier provided by this contract will have this effect. 1020 * 1021 1022 * The initialization functions use a version number. Once a version number is used, it is consumed and cannot be 1023 1024 * reused. This mechanism prevents re-execution of each "step" but allows the creation of new initialization steps in 1025 1026 * case an upgrade adds a module that needs to be initialized. 1027 * 1028 1029 * For example: 1030 * 1031 1032 * [.hljs-theme-light.nopadding] 1033 1034 * ``` 1035 1036 ``` 1037 1038 * contract MyToken is ERC20Upgradeable { 1039 1040 * function initialize() initializer public { 1041 1042 * __ERC20_init("MyToken", "MTK"); 1043 1044 * } 1045 1046 * } 1047 1048 * contract MyTokenV2 is MyToken, ERC20PermitUpgradeable { 1049 1050 * function initializeV2() reinitializer(2) public { 1051 1052 * __ERC20Permit_init("MyToken"); 1053 1054 * } 1055 1056 * } 1057 1058 * ``` 1059 * 1060 ``` 1061 1062 * TIP: To avoid leaving the proxy in an uninitialized state, the initializer function should be called as early as 1063 1064 * possible by providing the encoded function call as the `_data` argument to {ERC1967Proxy-constructor}. 1065 * 1066 1067 * CAUTION: When used with inheritance, manual care must be taken to not invoke a parent initializer twice, or to ensure 1068 1069 * that all initializers are idempotent. This is not verified automatically as constructors are by Solidity. 1070 * 1071 1072 * [CAUTION] 1073 1074 * ==== 1075 1076 * Avoid leaving a contract uninitialized. 1077 * 1078 1079 * An uninitialized contract can be taken over by an attacker. This applies to both a proxy and its implementation 1080 1081 * contract, which may impact the proxy. To prevent the implementation contract from being used, you should invoke 1082 1083 * the {_disableInitializers} function in the constructor to automatically lock it when it is deployed: 1084 * 1085 1086 * [.hljs-theme-light.nopadding] 1087 1088 * ``` 1089 1090 ``` 1091 1092 * /// @custom:oz-upgrades-unsafe-allow constructor 1093 1094 * constructor() { 1095 1096 * _disableInitializers(); 1097 1098 * } 1099 1100 * ``` 1101 1102 ``` 1103 1104 * ==== 1105 */ 1106 abstract contract Initializable { 1107 /** 1108 1109 * @dev Indicates that the contract has been initialized. 1110 * @custom:oz-retyped-from bool 1111 */ 1112 uint8 private _initialized; 1113 1114 /** 1115 1116 * @dev Indicates that the contract is in the process of being initialized. 1117 */ 1118 bool private _initializing; 1119 1120 /** 1121 1122 * @dev Triggered when the contract has been initialized or reinitialized. 1123 */ 1124 event Initialized(uint8 version); 1125 1126 /** 1127 1128 * @dev A modifier that defines a protected initializer function that can be invoked at most once. In its scope, 1129 * `onlyInitializing` functions can be used to initialize parent contracts. Equivalent to `reinitializer(1)`. 1130 */ 1131 modifier initializer() { 1132 bool isTopLevelCall = !_initializing; 1133 require( 1134 (isTopLevelCall && _initialized < 1) || 1135 (!Address.isContract(address(this)) && _initialized == 1), 1136 "Initializable: contract is already initialized" 1137 ); 1138 _initialized = 1; 1139 if (isTopLevelCall) { 1140 _initializing = true; 1141 } 1142 _; 1143 if (isTopLevelCall) { 1144 _initializing = false; 1145 emit Initialized(1); 1146 } 1147 } 1148 1149 /** 1150 1151 * @dev A modifier that defines a protected reinitializer function that can be invoked at most once, and only if the 1152 * contract hasn't been initialized to a greater version before. In its scope, `onlyInitializing` functions can be 1153 * used to initialize parent contracts. 1154 * 1155 * `initializer` is equivalent to `reinitializer(1)`, so a reinitializer may be used after the original 1156 * initialization step. This is essential to configure modules that are added through upgrades and that require 1157 * initialization. 1158 * 1159 * Note that versions can jump in increments greater than 1; this implies that if multiple reinitializers coexist in 1160 * a contract, executing them in the right order is up to the developer or operator. 1161 */ 1162 modifier reinitializer(uint8 version) { 1163 require( 1164 !_initializing && _initialized < version, 1165 "Initializable: contract is already initialized" 1166 ); 1167 _initialized = version; 1168 _initializing = true; 1169 _; 1170 _initializing = false; 1171 emit Initialized(version); 1172 } 1173 1174 /** 1175 1176 * @dev Modifier to protect an initialization function so that it can only be invoked by functions with the 1177 * {initializer} and {reinitializer} modifiers, directly or indirectly. 1178 */ 1179 modifier onlyInitializing() { 1180 require(_initializing, "Initializable: contract is not initializing"); 1181 _; 1182 } 1183 1184 /** 1185 1186 * @dev Locks the contract, preventing any future reinitialization. This cannot be part of an initializer call. 1187 * Calling this in the constructor of a contract will prevent that contract from being initialized or reinitialized 1188 * to any version. It is recommended to use this to lock implementation contracts that are designed to be called 1189 * through proxies. 1190 */ 1191 function _disableInitializers() internal virtual { 1192 require(!_initializing, "Initializable: contract is initializing"); 1193 if (_initialized < type(uint8).max) { 1194 _initialized = type(uint8).max; 1195 emit Initialized(type(uint8).max); 1196 } 1197 } 1198 } 1199 1200 // OpenZeppelin Contracts (last updated v4.7.0) (access/Ownable.sol) 1201 /** 1202 1203 * @dev Contract module which provides a basic access control mechanism, where 1204 1205 * there is an account (an owner) that can be granted exclusive access to 1206 1207 * specific functions. 1208 * 1209 1210 * By default, the owner account will be the one that deploys the contract. This 1211 1212 * can later be changed with {transferOwnership}. 1213 * 1214 1215 * This module is used through inheritance. It will make available the modifier 1216 1217 * `onlyOwner`, which can be applied to your functions to restrict their use to 1218 1219 * the owner. 1220 */ 1221 abstract contract Ownable is Context { 1222 address private _owner; 1223 1224 event OwnershipTransferred( 1225 address indexed previousOwner, 1226 address indexed newOwner 1227 ); 1228 1229 /** 1230 1231 * @dev Initializes the contract setting the deployer as the initial owner. 1232 */ 1233 constructor() { 1234 _transferOwnership(_msgSender()); 1235 } 1236 1237 /** 1238 1239 * @dev Throws if called by any account other than the owner. 1240 */ 1241 modifier onlyOwner() { 1242 _checkOwner(); 1243 _; 1244 } 1245 1246 /** 1247 1248 * @dev Returns the address of the current owner. 1249 */ 1250 function owner() public view virtual returns (address) { 1251 return _owner; 1252 } 1253 1254 /** 1255 1256 * @dev Throws if the sender is not the owner. 1257 */ 1258 function _checkOwner() internal view virtual { 1259 require(owner() == _msgSender(), "Ownable: caller is not the owner"); 1260 } 1261 1262 /** 1263 1264 * @dev Leaves the contract without owner. It will not be possible to call 1265 * `onlyOwner` functions anymore. Can only be called by the current owner. 1266 * 1267 * NOTE: Renouncing ownership will leave the contract without an owner, 1268 * thereby removing any functionality that is only available to the owner. 1269 */ 1270 function renounceOwnership() public virtual onlyOwner { 1271 _transferOwnership(address(0)); 1272 } 1273 1274 /** 1275 1276 * @dev Transfers ownership of the contract to a new account (`newOwner`). 1277 * Can only be called by the current owner. 1278 */ 1279 function transferOwnership(address newOwner) public virtual onlyOwner { 1280 require( 1281 newOwner != address(0), 1282 "Ownable: new owner is the zero address" 1283 ); 1284 _transferOwnership(newOwner); 1285 } 1286 1287 /** 1288 1289 * @dev Transfers ownership of the contract to a new account (`newOwner`). 1290 * Internal function without access restriction. 1291 */ 1292 function _transferOwnership(address newOwner) internal virtual { 1293 address oldOwner = _owner; 1294 _owner = newOwner; 1295 emit OwnershipTransferred(oldOwner, newOwner); 1296 } 1297 } 1298 1299 contract Migrate is Pausable, Initializable, ReentrancyGuard, Ownable { 1300 using EnumerableSet for EnumerableSet.AddressSet; 1301 bool public burnAllFirstConfirmed; 1302 bool public burnAllSecondConfirmed; 1303 mapping(address => uint256) public balance; 1304 mapping(address => bool) public isClaimed; 1305 EnumerableSet.AddressSet _accounts; 1306 1307 event LogClaim(address indexed account, uint256 balance); 1308 event LogBurnAll(uint256 balance); 1309 event LogAddAccount(address indexed account, uint256 balance); 1310 1311 receive() external payable {} 1312 1313 function initialize( 1314 address _owner, 1315 address[] memory _addrs, 1316 uint256[] memory _bals 1317 ) public initializer { 1318 _transferOwnership(_owner); 1319 burnAllFirstConfirmed = false; 1320 burnAllSecondConfirmed = false; 1321 _addAccountsBatch(_addrs, _bals); 1322 } 1323 1324 function accountsLength() public view returns (uint256) { 1325 return _accounts.length(); 1326 } 1327 1328 function isAccountExist(address _addr) public view returns (bool) { 1329 return _accounts.contains(_addr); 1330 } 1331 1332 function accounts(uint256 page, uint256 size) 1333 public 1334 view 1335 returns (address[] memory) 1336 { 1337 require(page > 0 && size > 0, "Migrate: Requests param error"); 1338 uint256 start = (page - 1) * size; 1339 if (_accounts.length() < start) { 1340 size = 0; 1341 } else { 1342 uint256 length = _accounts.length() - start; 1343 if (length < size) { 1344 size = length; 1345 } 1346 } 1347 address[] memory addrs = new address[](size); 1348 for (uint256 i = 0; i < size; i++) { 1349 addrs[i] = _accounts.at(i + start); 1350 } 1351 return addrs; 1352 } 1353 1354 function canClaim(address _addr) public view returns (uint256) { 1355 uint256 _balance = balance[_addr]; 1356 if (isClaimed[_addr] || _balance == 0 || !_accounts.contains(_addr)) { 1357 return 0; 1358 } else { 1359 return _balance; 1360 } 1361 } 1362 1363 function addAccount(address _addr, uint256 _bal) 1364 public 1365 onlyOwner 1366 nonReentrant 1367 { 1368 require(!isClaimed[_addr], "Migrate: already claim"); 1369 require(!_accounts.contains(_addr), "Migrate: error account"); 1370 _accounts.add(_addr); 1371 balance[_addr] = _bal; 1372 1373 emit LogAddAccount(_addr, _bal); 1374 } 1375 1376 function _addAccountsBatch(address[] memory _addrs, uint256[] memory _bals) 1377 internal 1378 { 1379 require( 1380 _addrs.length == _bals.length, 1381 "Migrate: length of addrs is not equal bals" 1382 ); 1383 for (uint256 i = 0; i < _addrs.length; i++) { 1384 (address _addr, uint256 _bal) = (_addrs[i], _bals[i]); 1385 if (isClaimed[_addr] || _accounts.contains(_addr)) { 1386 continue; 1387 } 1388 _accounts.add(_addr); 1389 balance[_addr] = _bal; 1390 emit LogAddAccount(_addr, _bal); 1391 } 1392 } 1393 1394 function addAccountsBatch(address[] memory _addrs, uint256[] memory _bals) 1395 public 1396 onlyOwner 1397 nonReentrant 1398 { 1399 _addAccountsBatch(_addrs, _bals); 1400 } 1401 1402 function claim() public whenNotPaused nonReentrant { 1403 address _addr = msg.sender; 1404 uint256 _balance = balance[_addr]; 1405 if (isClaimed[_addr] || _balance == 0 || !_accounts.contains(_addr)) 1406 return; 1407 1408 isClaimed[_addr] = true; 1409 _accounts.remove(_addr); 1410 payable(_addr).transfer(_balance); 1411 1412 emit LogClaim(_addr, _balance); 1413 } 1414 1415 function burnAll() public onlyOwner { 1416 if (!burnAllFirstConfirmed) { 1417 burnAllFirstConfirmed = true; 1418 } else { 1419 uint256 bal = address(this).balance; 1420 payable(address(0)).transfer(bal); 1421 burnAllSecondConfirmed = true; 1422 1423 emit LogBurnAll(bal); 1424 } 1425 } 1426 1427 function pause() public onlyOwner { 1428 _pause(); 1429 } 1430 1431 function unpause() public onlyOwner { 1432 _unpause(); 1433 } 1434 1435 }