You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

129 lines
3.2 KiB

7 months ago
  1. // SPDX-License-Identifier: MIT
  2. pragma solidity ^0.8.19;
  3. library Utils {
  4. function isAdmin(address[] memory _admins,address owner) internal pure returns(bool) {
  5. bool _admin;
  6. for(uint256 i=0;i<_admins.length;i++){
  7. if(_admins[i] == owner){
  8. _admin = true;
  9. break;
  10. }
  11. }
  12. return _admin;
  13. }
  14. function isNftBlacks(uint256[] memory _tokens,uint256 _tokenIds) internal pure returns(bool) {
  15. bool _black;
  16. for(uint256 i=0;i<_tokens.length;i++){
  17. if(_tokens[i] == _tokenIds){
  18. _black = true;
  19. break;
  20. }
  21. }
  22. return _black;
  23. }
  24. function addNftBlacks (uint256[] memory _tokens,uint256[] memory _ids) internal pure returns(uint256[] memory) {
  25. uint256 length = _tokens.length + _ids.length;
  26. uint256[] memory tokens = new uint256[](length);
  27. uint256 index = 0;
  28. for(uint256 i=0;i<_tokens.length;i++){
  29. tokens[index] = _tokens[i];
  30. index++;
  31. }
  32. for(uint256 i=0;i<_ids.length;i++){
  33. tokens[index] = _ids[i];
  34. index++;
  35. }
  36. return tokens;
  37. }
  38. function deleteNftBlacks (uint256[] memory _tokens,uint256[] memory _dels) internal pure returns(uint256[] memory) {
  39. bool[] memory toDelete = new bool[](_tokens.length);
  40. // 標記要刪除的元素
  41. for(uint256 i=0;i<_dels.length;i++){
  42. for(uint256 j=0;j<_tokens.length;j++){
  43. if(_tokens[j] == _dels[i]){
  44. toDelete[j] = true;
  45. break;
  46. }
  47. }
  48. }
  49. // 計算數組長度
  50. uint256 size = 0;
  51. for(uint256 i=0;i<_tokens.length;i++){
  52. if(!toDelete[i]){
  53. size++;
  54. }
  55. }
  56. uint256[] memory _newTokens = new uint256[](size);
  57. uint256 index =0 ;
  58. for(uint256 i=0;i<_tokens.length;i++){
  59. if(!toDelete[i]){
  60. _newTokens[index] = _tokens[i];
  61. index++;
  62. }
  63. }
  64. return _newTokens;
  65. }
  66. function addAdmin (address[] memory _admins,address[] memory _adds) internal pure returns(address[] memory) {
  67. uint256 _length = _admins.length + _adds.length;
  68. address[] memory _newTokens = new address[](_length);
  69. uint256 index = 0;
  70. for(uint256 i=0;i<_admins.length;i++){
  71. _newTokens[index] = _admins[i];
  72. index++;
  73. }
  74. for(uint256 i=0;i<_adds.length;i++){
  75. _newTokens[index] = _adds[i];
  76. index++;
  77. }
  78. return _newTokens;
  79. }
  80. function deleteAdmin (address[] memory _admins,address[] memory _dels) internal pure returns(address[] memory) {
  81. bool[] memory toDelete = new bool[](_admins.length);
  82. // 標記要刪除的元素
  83. for(uint256 i=0;i<_dels.length;i++){
  84. for(uint256 j=0;j<_admins.length;j++){
  85. if(_admins[j] == _dels[i]){
  86. toDelete[j] = true;
  87. break;
  88. }
  89. }
  90. }
  91. // 計算數組長度
  92. uint256 size = 0;
  93. for(uint256 i=0;i<_admins.length;i++){
  94. if(!toDelete[i]){
  95. size++;
  96. }
  97. }
  98. address[] memory newAdmins = new address[](size);
  99. uint256 index =0 ;
  100. for(uint256 i=0;i<_admins.length;i++){
  101. if(!toDelete[i]){
  102. newAdmins[index] = _admins[i];
  103. index++;
  104. }
  105. }
  106. return newAdmins;
  107. }
  108. }