call a public function with another function in the same contract in solidity

Viewed 1202

what is the simplest solution to set up a function in a contract that automatically call a publish function of the same contract?

The basic structure is:

function send(uint256 value) public {
    address sender = _msgSender();
    }

I want to set up a function within the same contract that automatically makes a call trigger of the above public function when a specific wallet caller has a certain amount value.

1 Answers

You can check the sender's balance by using the balance property of the address type.

function send(uint256 value) public {
    address sender = _msgSender();

    // this checks the sender's balance
    if (sender.balance >= 1 ether) {
        publish();
    }
}

function publish() public {
}

If you want to check how much they sent in the transaction executing your function, you can use the global variable msg.value.

Also, in order to accept the non-zero value, you need to make the function payable.

function send(uint256 value) public payable { // mind the `payable` keyword
    address sender = _msgSender();

    // this checks the value of this transaction
    if (msg.value >= 1 ether) {
        publish();
    }
}

function publish() public {
}
Related