EntropyAuthorized.t.sol 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. // SPDX-License-Identifier: Apache 2
  2. pragma solidity ^0.8.0;
  3. import "./utils/EntropyTestUtils.t.sol";
  4. import "../contracts/entropy/EntropyUpgradable.sol";
  5. import "./utils/InvalidMagic.t.sol";
  6. import "@openzeppelin/contracts/proxy/ERC1967/ERC1967Proxy.sol";
  7. import "@pythnetwork/entropy-sdk-solidity/EntropyErrors.sol";
  8. contract EntropyAuthorized is Test, EntropyTestUtils {
  9. ERC1967Proxy public proxy;
  10. EntropyUpgradable public random;
  11. EntropyUpgradable public random2;
  12. InvalidMagic public randomDifferentMagic;
  13. address public owner = address(1);
  14. address public admin = address(2);
  15. address public admin2 = address(3);
  16. // We don't need to register providers for these tests
  17. // We are just checking for the default provider, which
  18. // only required an address.
  19. address public provider1 = address(4);
  20. address public provider2 = address(5);
  21. address public owner2 = address(6);
  22. uint128 pythFeeInWei = 7;
  23. function setUp() public {
  24. EntropyUpgradable _random = new EntropyUpgradable();
  25. // deploy proxy contract and point it to implementation
  26. proxy = new ERC1967Proxy(address(_random), "");
  27. // wrap in ABI to support easier calls
  28. random = EntropyUpgradable(address(proxy));
  29. // to test for upgrade
  30. random2 = new EntropyUpgradable();
  31. randomDifferentMagic = new InvalidMagic();
  32. random.initialize(owner, admin, pythFeeInWei, provider1, false);
  33. }
  34. function testSetPythFeeByAdmin() public {
  35. vm.prank(admin);
  36. random.setPythFee(10);
  37. assertEq(random.getPythFee(), 10);
  38. }
  39. function testSetPythFeeByOwner() public {
  40. vm.prank(owner);
  41. random.setPythFee(10);
  42. assertEq(random.getPythFee(), 10);
  43. }
  44. function testExpectRevertSetPythFeeByUnauthorized() public {
  45. vm.expectRevert(EntropyErrors.Unauthorized.selector);
  46. vm.prank(admin2);
  47. random.setPythFee(10);
  48. }
  49. function testSetDefaultProviderByOwner() public {
  50. vm.prank(owner);
  51. random.setDefaultProvider(provider2);
  52. assertEq(random.getDefaultProvider(), provider2);
  53. }
  54. function testSetDefaultProviderByAdmin() public {
  55. vm.prank(admin);
  56. random.setDefaultProvider(provider2);
  57. assertEq(random.getDefaultProvider(), provider2);
  58. }
  59. function testExpectRevertSetDefaultProviderByUnauthorized() public {
  60. vm.expectRevert(EntropyErrors.Unauthorized.selector);
  61. vm.prank(admin2);
  62. random.setDefaultProvider(provider2);
  63. }
  64. function testUpgradeByOwner() public {
  65. vm.prank(owner);
  66. random.upgradeTo(address(random2));
  67. }
  68. function testExpectRevertUpgradeByAdmin() public {
  69. // The message is returned by openzepplin upgrade contracts
  70. vm.expectRevert("Ownable: caller is not the owner");
  71. vm.prank(admin);
  72. random.upgradeTo(address(random2));
  73. }
  74. function testExpectRevertUpgradeByUnauthorized() public {
  75. // The message is returned by openzepplin upgrade contracts
  76. vm.expectRevert("Ownable: caller is not the owner");
  77. vm.prank(provider1);
  78. random.upgradeTo(address(random2));
  79. }
  80. // There can be another case that the magic function doesn't
  81. // exist but it's fine. (It will revert with no reason)
  82. // The randomDifferentMagic contract do have a magic in this case
  83. function testExpectRevertDifferentMagicContractUpgrade() public {
  84. vm.expectRevert(EntropyErrors.InvalidUpgradeMagic.selector);
  85. vm.prank(owner);
  86. random.upgradeTo(address(randomDifferentMagic));
  87. }
  88. function testExpectRevertRequestOwnershipTransferByUnauthorized() public {
  89. vm.expectRevert("Ownable: caller is not the owner");
  90. vm.prank(provider1);
  91. random.transferOwnership(owner2);
  92. }
  93. function testExpectRevertRequestOwnershipTransferByAdmin() public {
  94. vm.expectRevert("Ownable: caller is not the owner");
  95. vm.prank(admin);
  96. random.transferOwnership(owner2);
  97. }
  98. function testRequestAndAcceptOwnershipTransfer() public {
  99. vm.prank(owner);
  100. random.transferOwnership(owner2);
  101. assertEq(random.pendingOwner(), owner2);
  102. vm.prank(owner2);
  103. random.acceptOwnership();
  104. assertEq(random.owner(), owner2);
  105. }
  106. function testRequestAndAcceptOwnershipTransferUnauthorizedAccept() public {
  107. vm.prank(owner);
  108. random.transferOwnership(owner2);
  109. assertEq(random.pendingOwner(), owner2);
  110. vm.prank(admin);
  111. vm.expectRevert("Ownable2Step: caller is not the new owner");
  112. random.acceptOwnership();
  113. }
  114. function testProposeAdminByOwner() public {
  115. vm.prank(owner);
  116. random.proposeAdmin(admin2);
  117. assertEq(random.proposedAdmin(), admin2);
  118. }
  119. function testProposeAdminByAdmin() public {
  120. vm.prank(admin);
  121. random.proposeAdmin(admin2);
  122. assertEq(random.proposedAdmin(), admin2);
  123. }
  124. function testProposeAdminByUnauthorized() public {
  125. vm.expectRevert(EntropyErrors.Unauthorized.selector);
  126. random.proposeAdmin(admin2);
  127. }
  128. function testAcceptAdminByPropsed() public {
  129. vm.prank(owner);
  130. random.proposeAdmin(admin2);
  131. vm.prank(admin2);
  132. random.acceptAdmin();
  133. assertEq(random.getAdmin(), admin2);
  134. }
  135. function testAcceptAdminByUnauthorized() public {
  136. vm.prank(owner);
  137. random.proposeAdmin(admin2);
  138. vm.prank(provider1);
  139. vm.expectRevert(EntropyErrors.Unauthorized.selector);
  140. random.acceptAdmin();
  141. }
  142. // Helper function to setup contract with fees
  143. function setupContractWithFees(
  144. uint128 feeAmount,
  145. uint numRequests
  146. ) internal returns (uint128 totalFees) {
  147. // Register provider1
  148. bytes32[] memory hashChain = generateHashChain(provider1, 0, 100);
  149. vm.prank(provider1);
  150. random.register(0, hashChain[0], hex"0100", 100, "");
  151. // Set Pyth fee
  152. vm.prank(admin);
  153. random.setPythFee(feeAmount);
  154. // Make requests to accrue fees
  155. bytes32 userCommitment = random.constructUserCommitment(
  156. bytes32(uint256(42))
  157. );
  158. vm.deal(address(this), feeAmount * numRequests);
  159. for (uint i = 0; i < numRequests; i++) {
  160. random.request{value: feeAmount}(provider1, userCommitment, false);
  161. }
  162. totalFees = uint128(feeAmount * numRequests);
  163. assertEq(random.getAccruedPythFees(), totalFees);
  164. return totalFees;
  165. }
  166. function testWithdrawFeeByAdmin() public {
  167. uint128 totalFees = setupContractWithFees(10, 5);
  168. address targetAddress = address(123);
  169. uint128 withdrawAmount = 30;
  170. vm.prank(admin);
  171. random.withdrawFee(targetAddress, withdrawAmount);
  172. assertEq(random.getAccruedPythFees(), totalFees - withdrawAmount);
  173. assertEq(targetAddress.balance, withdrawAmount);
  174. }
  175. function testWithdrawFeeByOwner() public {
  176. uint128 totalFees = setupContractWithFees(10, 5);
  177. address targetAddress = address(123);
  178. uint128 withdrawAmount = 30;
  179. vm.prank(owner);
  180. random.withdrawFee(targetAddress, withdrawAmount);
  181. assertEq(random.getAccruedPythFees(), totalFees - withdrawAmount);
  182. assertEq(targetAddress.balance, withdrawAmount);
  183. }
  184. function testWithdrawFeeByUnauthorized() public {
  185. setupContractWithFees(10, 5);
  186. vm.prank(admin2);
  187. vm.expectRevert(EntropyErrors.Unauthorized.selector);
  188. random.withdrawFee(address(123), 30);
  189. }
  190. function testWithdrawFeeInsufficientBalance() public {
  191. uint128 totalFees = setupContractWithFees(10, 5);
  192. vm.prank(admin);
  193. vm.expectRevert(EntropyErrors.InsufficientFee.selector);
  194. random.withdrawFee(address(123), totalFees + 10);
  195. }
  196. function testWithdrawFeeToZeroAddress() public {
  197. setupContractWithFees(10, 5);
  198. vm.prank(admin);
  199. vm.expectRevert("targetAddress is zero address");
  200. random.withdrawFee(address(0), 30);
  201. }
  202. }