PatrickAlphaC / hardhat-fund-me-fcc

82 stars 182 forks source link

conditional code #150

Closed nzarambaelisa closed 1 year ago

nzarambaelisa commented 1 year ago

hello i'm trying to include a condition in my code. i added a function in my contract that is only called by the owner of the contract that lock a certain address of choice from withdrawing funds. QUESTION? does the code make sense? first i created a variable

mapping(address => bool) public locked;

then i made a function that sets a particular address to true

function lockAccount(address _staker) external onlyOwner {
        require(_staker != address(0), "invalid address");
        require(_staker != owner, "cannot lock owner account");
        require(locked[_account] != true, "account is already in requested lock state");

        locked[_staker] = true;
    }

then i created a modifier that checks for true otherwise false so that other addresses when they call withdraw, they mark as false because in the unstake function it is said to require require(locked[msg.sender] = false);

modifier situation() {
        locked[msg.sender] = lockAccount(msg.sender) ? true : false;
        _;
    } 

function Unstake(uint _amount) external situation nonReentrant {
        require(_amount > 0, "amount is <= 0");
        require(staked[msg.sender] >= _amount, "amount is > staked");
        require(locked[msg.sender] = false);
        claim();
        staked[msg.sender] -= _amount;
        emit Unstake(msg.sender, _amount);
        bool success = token.transfer(msg.sender, _amount);
         if (!success) {
            revert Fixedstaking__TransferFailed();
        }

    }
GunjanSurti commented 1 year ago

In modifier try using require statement

modifier situation() {
       // locked[msg.sender] == lockAccount(msg.sender) ? true : false;
       require(locked[msg.sender] == false)
        _;
    }

and in your modifier you should do "==" instead of "=" this should be the mistake

PatrickAlphaC commented 1 year ago

Can you:

  1. Make this a discusson on the full repo? https://github.com/smartcontractkit/full-blockchain-solidity-course-js/
  2. Follow this section for formatting questions? https://www.youtube.com/watch?t=19846&v=gyMwXuJrbJQ&feature=youtu.be