I'm confused while understanding the local variable _txId
is used in several functions and modifiers, do they all have the same value or are they independent for each call.
I read that local variables are independent but i'm having a hard time understanding that here.
This is a part of my contract for referance.
modifier notExecuted(uint _txId) {
require(!transactions[_txId].executed, "tx already executed");
_;
}
constructor(address[] memory _owners, uint _required) {
require(_owners.length > 0, "owners required");
require(_required > 0 && _required <= _owners.length,
"invalid required number of owners"
);
// Stating _owners to state variable owner
for (uint i; i < _owners.length; i++) {
address owner = _owners[i];
require(owner != address(0), "invalid owner");
require( !isOwner[owner], "owner is not unique");
isOwner[owner] = true;
owners.push(owner);
}
required = _required;
}
receive() external payable {
emit Deposit(msg.sender, msg.value);
}
// Function to submit transactions for approval
function submit( address _to, uint _value, bytes calldata _data)
external
onlyOwner
{
transactions.push(Transaction({
to: _to,
value: _value,
data: _data,
executed: false
}));
emit Submit(transactions.length - 1);
}
// Function for each approval
function approve(uint _txId)
external
onlyOwner
txExists(_txId)
notApproved(_txId)
notExecuted(_txId)
{
approved[_txId][msg.sender] = true;
emit Approve(msg.sender, _txId);
}
// Counting the number of approvals
function _getApprovalCount(uint _txId) private view returns (uint count) {
for (uint i; i < owners.length; i++) {
if (approved[_txId][owners[i]]) {
count += 1;
}
}
}
i
as counter in the for loops so the variable only exists inside them. Likewise_txId
is defined at modifier/function level so it only exists inside that modifier or function. I'll suggest to study the concept en.wikipedia.org/wiki/Scope_(computer_science).