openzeppelin-contracts icon indicating copy to clipboard operation
openzeppelin-contracts copied to clipboard

Add PaymentSplitter.payeesLength()

Open olehmisar opened this issue 3 years ago • 3 comments

🧐 Motivation

I want to list all payees of a PaymentSplitter contract.

📝 Details

PaymentSplitter has a .payee(index) method but it is hard to use it without knowing how many payees are there.

Consider adding this to the PaymentSplitter contract:

function payeesLength() public view returns (uint256) {
  return _payees.length;
}

olehmisar avatar Nov 17 '22 09:11 olehmisar

Hi,

Can you add more details to the motivation? Whoever deploys the contracts provides the payee array, meaning, they already know how many are there, and the payees are only added during deployment. Although there would be other ways to get the total list (like listening to the contract PayeeAdded), I would like to know the exact use case payeesLength might be applied.

JulissaDantes avatar Nov 17 '22 20:11 JulissaDantes

I want to show the list of payees on a website. I can fetch each payee address one by one using .payee(index) but I need to know what is the last index, so I know when to stop. I want something like this:

async function getPayees(paymentSplitter: PaymentSplitter): Promise<string[]> {
  const len = await paymentSplitter.payeesLength(); // missing this
  const payees: string[] = [];
  for (let i = 0; i < len.toNumber(); i++) {
    payees.push(await paymentSplitter.payee(i));
  }
  return payees;
}

olehmisar avatar Nov 17 '22 22:11 olehmisar

Note that you can iterate until payee returns a zero address.

It might still make sense to add a getter for the length.

frangio avatar Jan 05 '23 16:01 frangio