Parcourir la source

Add fees and test

Darun Seethammagari il y a 11 mois
Parent
commit
7f0faa6c56

+ 8 - 1
lazer/contracts/evm/src/PythLazer.sol

@@ -6,6 +6,7 @@ import "@openzeppelin/contracts-upgradeable/access/OwnableUpgradeable.sol";
 
 contract PythLazer is OwnableUpgradeable, UUPSUpgradeable {
     TrustedSignerInfo[2] public trustedSigners;
+    uint256 public verification_fee = 0.01 ether;
 
     struct TrustedSignerInfo {
         address pubkey;
@@ -62,7 +63,13 @@ contract PythLazer is OwnableUpgradeable, UUPSUpgradeable {
 
     function verifyUpdate(
         bytes calldata update
-    ) external view returns (bytes calldata payload, address signer) {
+    ) external payable returns (bytes calldata payload, address signer) {
+        // Require fee and refund excess
+        require(msg.value >= verification_fee, "Insufficient fee provided");
+        if (msg.value > verification_fee) {
+            payable(msg.sender).transfer(msg.value - verification_fee);
+        }
+
         if (update.length < 71) {
             revert("input too short");
         }

+ 36 - 0
lazer/contracts/evm/test/PythLazer.t.sol

@@ -20,4 +20,40 @@ contract PythLazerTest is Test {
         skip(2000);
         assert(!pythLazer.isValidSigner(address(2)));
     }
+
+    function test_verify_with_fee() public {
+        // Prepare dummy update and signer
+        address trustedSigner = 0xEfEf56cD66896f6799A90A4e4d512C330c094e44;
+        vm.prank(address(1));
+        pythLazer.updateTrustedSigner(trustedSigner, 3000000000000000);
+        bytes memory update = hex"2a22999a577d3cc0202197939d736bc0dcf71b9dde7b9470e4d16fa8e2120c0787a1c0d744d0c39cc372af4d1ecf2d09e84160ca905f3f597d20e2eec144a446a0459ad600001c93c7d3750006240af373971c01010000000201000000000005f5e100";
+
+        uint256 fee = pythLazer.verification_fee();
+
+        address alice = makeAddr("alice");
+        vm.deal(alice, 1 ether);
+        address bob = makeAddr("bob");
+        vm.deal(bob, 1 ether);
+
+        // Alice provides appropriate fee
+        vm.prank(alice);
+        pythLazer.verifyUpdate{ value: fee }(update);
+        assertEq(alice.balance, 1 ether - fee);
+
+        // Alice overpays and is refunded
+        vm.prank(alice);
+        pythLazer.verifyUpdate{ value: 0.5 ether }(update);
+        assertEq(alice.balance, 1 ether - fee - fee);
+
+        // Bob does not attach a fee
+        vm.prank(bob);
+        vm.expectRevert("Insufficient fee provided");
+        pythLazer.verifyUpdate(update);
+        assertEq(bob.balance, 1 ether);
+
+        // Bob does not attach enough fees
+        vm.expectRevert("Insufficient fee provided");
+        pythLazer.verifyUpdate{ value: 0.00001 ether }(update);
+        assertEq(bob.balance, 1 ether);
+    }
 }