Search code examples
ethereumsmartcontractsremix

Rinkeby Smart Contract


looking to create a challenge for a CTF, where I give the user a smart contract for a piece of art that requires a minimum cost of 20 ether. Once the payment has been accepted, he gets by return the flag. The code below works and checks balances against the account in remix but how can I get it to return the flag if the payment is correct? Any help and pointers would be appreciated.

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.4;

contract payment_for_art {
    function invest() external payable {
        if(msg.value < 20 ether) {
            revert();
        }
    }
    function balance_of() external view returns(uint) {
        return address(this).balance;
    }
}

Regards

K


Solution

  • You can create a bool property that flags whether the payment has been done.

    contract payment_for_art {
    
        // default value is `false`, don't need to explicitly state it
        bool public isPaid;
    
        function invest() external payable {
            // to prevent multiple payments
            // reverts if the condition is not met
            require(isPaid == false);
    
            if(msg.value < 20 ether) {
                revert();
            }
            
            isPaid = true; // flag that the payment has been done
        }
    
        // ... rest of your code
    }
    

    Since it has the public modifier, any other contract or off-chain app can read its value (but cannot rewrite it). You can remove the modifier if you don't want the value to be accessible from outside of the contract.