Created
November 3, 2018 19:16
-
-
Save atvanguard/9cf0d796f5699a8c18b099a9ec865393 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
contract LiquidDemocracy { | |
... | |
function delegateTo(address delegate) public isValidVoter { | |
require( | |
voters[delegate].state != State.Invalid, | |
'Delegating to invalid voter' | |
); | |
require( | |
delegate != msg.sender && !delegationCycleExists(delegate, msg.sender), | |
'Forming a delegation cycle' | |
); | |
Voter storage voter = voters[msg.sender]; | |
voter.delegatedTo = voters[delegate].index; | |
voter.state = State.Delegated; | |
} | |
function delegationCycleExists(address delegate, address sender) internal view returns(bool) { | |
// traverse the delegation cycle until we find a voter that has not delegated | |
while(voters[delegate].state == State.Delegated) { | |
delegate = validVoters[voters[delegate].delegatedTo]; | |
if (delegate == sender) return true; // delegation cycle found | |
} | |
return false; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment