0

So, there's this contract (which I don't own) with a public variable:

contract challenge {
  address[] public winners;
}

I need to know If my address was successfully added to the list, I have tried the following logic:

interface Challenge { 
    function winners() external view returns(bool);
  }
  
contract WinnersHack {
    address[] public array;
    address challengeAddress = 0xcD7........;
    address winner = 0x0C5........;
    Challenge challengeInstance = Challenge(challengeAddress);
 
    function showWinner() public view returns(bool) {   
        array = challengeInstance.winners();
        for (uint i = 0; i < array.length; i++) {
            if (array[i] == winner) {
                return true;  
            }
        }
    }

Yet, I am getting the following error:

TypeError: Type bool is not implicitly convertible to expected type address[] storage ref.

I have been reading on it, but can't quite figure out how to fix it. I understand solidity won't output an array with a lot of inputs (probably dozens) because of associated costs.

How should I confirm that a specific address is indeed stored on the array?

1 Answer 1

1

The interface function is incorrect, I have edited your code to show you what you would need to do! Hope this helps.

interface Challenge {
    // the public array takes in an index and returns an address
    function winners(uint256) external view returns (address);
}

contract WinnersHack {
    address challengeAddress = 0xcD7........;
    Challenge challengeInstance = Challenge(challengeAddress);

    // made this reusable to check any address
    // you should be able to find out the number of winners or just use an arbitrary number
    function showWinner(address _winner, uint256 _numOfWinners)
        public
        view
        returns (bool)
    {
        for (uint256 i = 0; i < _numOfWinners; i++) {
            if (challengeInstance.winners(i) == _winner) {
                return true;
            } else {
                return false;
            }
        }
    }
}
2
  • you beautiful human being, you. What was the issue? I understand I need to pass an index to the array? Is it not possible to bring the whole array and iterate it from my contract? Nov 15, 2022 at 16:14
  • No, it's not possible to copy over the array, you would have to loop through the public array and build it that way. It would not be very gas efficient, and you would have to know the length, or you'd end up with empty slots or not the correct amount of addresses. Nov 15, 2022 at 16:30

Your Answer

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

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