2

I am trying to capture the address of the use who has been given the "right to vote" in the giveRightToVote function. If I use msg.sender that only captures the user invoking the function. Any ideas?

contract FantasyLeague {
// Define address of commissioner
address public commissioner;
// Hold data of proposed number
uint[] public proposals;
address[] public members;
address[] public rightsGiven;

// Deploy contract
constructor() public payable {
    commissioner = msg.sender;
}

function giveRightToVote(address) public payable {
    require(msg.sender == commissioner);
    // Ensures that the right to vote given matches the actual voted addresses
    rightsGiven.push();
}
0

I think you just want this?

function giveRightToVote(address voter) public payable {
    require(msg.sender == commissioner);
    rightsGiven.push(voter);
}
  • Thanks smarx. I tried that already and unfortunately it only stores the the first address that is added to an array. How can I store an array of multiple addresses? I am able to do that through msg.sender in a different function but can't call that here since the sender must be the commissioner. – kefa Aug 21 '18 at 21:03
  • 1
    You'll have to explain your testing methodology. (Certainly the above function will store an additional voter on each invocation.) – user19510 Aug 21 '18 at 21:10
  • Thanks smarx, I understand I wasn't very helpful not providing more about my use case. Your implementation works well. I was able to use another function that returns the dynamic array. I appreciate all of your help on this contract! – kefa Aug 22 '18 at 13:12
2

Name the input argument so you can work with it.

contract FantasyLeague {
  // Define address of commissioner
  address public commissioner;
  // Hold data of proposed number
  uint[] public proposals;
  address[] public members;
  address[] public rightsGiven;

// Deploy contract
  constructor() public payable {
    commissioner = msg.sender;
  }

  function giveRightToVote(address newVoter) public payable {
    require(msg.sender == commissioner);
    // Ensures that the right to vote given matches the actual voted addresses
    rightsGiven.push(newVoter); // <== here
  }
}

Hope it helps.

  • Thanks Rob. I tried that already and unfortunately it only stores the the first address that is added to an array. How can I store an array of multiple addresses? I am able to do that through msg.sender in a different function but can't call that here since the sender must be the commissioner. – kefa Aug 21 '18 at 21:03
  • It's obviously a simple example and there are lots of little things we would tweak to make it audit-ready, but setting those details aside, I'm certain this code and Smarx's code does exactly what we say it does. Something is misleading you to think it doesn't work. As smarx says, the focus should be on the testing process. – Rob Hitchens - B9lab Aug 21 '18 at 21:40
  • I didn't say that the function doesn't work it surely does, I understand that there is more to my use case that wasn't explained which wasn't helpful. I will dig deeper into my use case and it helps to know that the function will work so thanks for your help! – kefa Aug 22 '18 at 13:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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