1

In sol 0.8.10 I used this approach given by this

contract Campaign {
struct Request {
    string description;
    uint value;
    address recipient;
    bool complete;
    uint approvalCount;
    mapping(address => bool) approvals;
}

uint numRequests;
mapping (uint => Request) requests;

function createRequest(string memory description, uint value, address recipient) 
public restricted {            
    Request storage r = requests[numRequests++];
    r.description = description;
    r.value = value;
    r.recipient = recipient;
    r.complete = false;
    r.approvalCount = 0;
 }
}

function getSummary() public view returns (uint, uint, uint, unit, address) {
return (
  minimumContribution,
  address(this).balance,
  requests.length, // TypeError: Member "length" not found or not visible after argument-dependent lookup in mapping
  approversCount,
  manager
 );
}

How to find the length of this requests mapping?

1 Answer 1

0

You cannot iterate over the keys or get a mapping's length, but you do track the number of elements in your mapping in numRequests

See this to complete your approach: How to loop through mapping in solidity?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.