Last active
November 25, 2023 01:14
-
-
Save Galadrin/952dc687bc8d14f65c94e6e81f1360e1 to your computer and use it in GitHub Desktop.
This python script parse delegators votes for a proposal
This file contains hidden or 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
| #!/bin/env python3 | |
| """ | |
| MIT License | |
| Copyright (c) 2023 Crosnest B.V. | |
| Permission is hereby granted, free of charge, to any person obtaining a copy | |
| of this software and associated documentation files (the "Software"), to deal | |
| in the Software without restriction, including without limitation the rights | |
| to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
| copies of the Software, and to permit persons to whom the Software is | |
| furnished to do so, subject to the following conditions: | |
| The above copyright notice and this permission notice shall be included in all | |
| copies or substantial portions of the Software. | |
| THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
| IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
| FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
| AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
| LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
| OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | |
| SOFTWARE. | |
| """ | |
| import argparse | |
| import json | |
| from concurrent.futures import ThreadPoolExecutor | |
| from types import SimpleNamespace | |
| from urllib.parse import quote | |
| from cosmpy.common.rest_client import RestClient | |
| from cosmpy.gov.rest_client import GovRestClient | |
| def get_votes_weight(node: str, validator_addr: str, proposal_id: int): | |
| delegator_list = list() | |
| rpc = RestClient(node) | |
| gov = GovRestClient(rest_api=rpc) | |
| print('gathering list of delegators') | |
| json_response = rpc.get(f"/cosmos/staking/v1beta1/validators/{validator_addr}/delegations?pagination.count_total=true") | |
| chunk = json.loads(json_response, object_hook=lambda d: SimpleNamespace(**d)) | |
| next_key = chunk.pagination.next_key | |
| total_deleg = int(chunk.pagination.total) | |
| delegator_list.extend(chunk.delegation_responses) | |
| while len(delegator_list) < total_deleg - 1: | |
| print(f"received {len(delegator_list)}, expected {total_deleg - 1}") | |
| json_response = rpc.get(f"/cosmos/staking/v1beta1/validators/{validator_addr}/delegations?pagination.key={quote(next_key)}") | |
| chunk = json.loads(json_response, object_hook=lambda d: SimpleNamespace(**d)) | |
| next_key = chunk.pagination.next_key | |
| delegator_list.extend(chunk.delegation_responses) | |
| print("done, {} delegators".format(len(delegator_list))) | |
| vote_yes = 0 | |
| vote_no = 0 | |
| vote_abstain = 0 | |
| vote_veto = 0 | |
| dont_vote = 0 | |
| count_yes = 0 | |
| count_no = 0 | |
| count_abstain = 0 | |
| count_veto = 0 | |
| count_dont = 0 | |
| def check_vote(delegator): | |
| try: | |
| json_response = rpc.get(f"/cosmos/gov/v1beta1/proposals/{proposal_id}/votes/{delegator.delegation.delegator_address}") | |
| vote = json.loads(json_response, object_hook=lambda d: SimpleNamespace(**d)) | |
| except RuntimeError: | |
| # print("[{}] delegator {} didn't vote".format(i, delegator.delegation.delegator_address)) | |
| return -1, delegator | |
| else: | |
| return vote.vote.option, delegator | |
| with ThreadPoolExecutor(max_workers=16) as pool: | |
| results = pool.map(check_vote, delegator_list) | |
| i = 0 | |
| for vote_option, delegator in results: | |
| i += 1 | |
| if vote_option == 1 or vote_option == 'VOTE_OPTION_YES': | |
| print("[{}] delegator {} vote YES".format(i, delegator.delegation.delegator_address)) | |
| vote_yes += float(delegator.delegation.shares) | |
| count_yes += 1 | |
| elif vote_option == 2 or vote_option == 'VOTE_OPTION_ABSTAIN': | |
| print("[{}] delegator {} vote ABSTAIN".format(i, delegator.delegation.delegator_address)) | |
| vote_abstain += float(delegator.delegation.shares) | |
| count_abstain += 1 | |
| elif vote_option == 3 or vote_option == 'VOTE_OPTION_NO': | |
| print("[{}] delegator {} vote NO".format(i, delegator.delegation.delegator_address)) | |
| vote_no += float(delegator.delegation.shares) | |
| count_no += 1 | |
| elif vote_option == 4 or vote_option == 'VOTE_OPTION_NO_WITH_VETO': | |
| print("[{}] delegator {} vote NO_with_VETO".format(i, delegator.delegation.delegator_address)) | |
| vote_veto += float(delegator.delegation.shares) | |
| count_veto += 1 | |
| elif vote_option == -1: # 'VOTE_OPTION_DONT': | |
| print("[{}] delegator {} did not vote".format(i, delegator.delegation.delegator_address)) | |
| dont_vote += float(delegator.delegation.shares) | |
| count_dont += 1 | |
| else: | |
| print("[{}] delegator {} vote UNKNOWN option".format(i, delegator.delegation.delegator_address)) | |
| total_vote = vote_no + vote_yes + vote_veto + vote_abstain | |
| total_count = (count_yes + count_no + count_veto + count_abstain) | |
| weight_yes = (vote_yes / total_vote) * 100 | |
| weight_no = (vote_no / total_vote) * 100 | |
| weight_veto = (vote_veto / total_vote) * 100 | |
| weight_abstain = (vote_abstain / total_vote) * 100 | |
| ratio = (total_vote / (total_vote + dont_vote)) *100 | |
| ratio_count = (total_count / len(delegator_list)) * 100 | |
| print("#####################################\n" | |
| f" Vote results for proposal #{proposal_id} in Voting Power:\n" | |
| f" YES : {int(vote_yes):d} points ({count_yes} addresses)\n" | |
| f" NO : {int(vote_no):d} points ({count_no} addresses)\n" | |
| f" VETO : {int(vote_veto):d} points ({count_veto} addresses)\n" | |
| f" ABSTAIN : {int(vote_abstain):d} points ({count_abstain} addresses)\n" | |
| f" DON\'T : {int(dont_vote):d} points ({count_dont} addresses)\n" | |
| f" RATIO : {ratio:.3f}%" | |
| ) | |
| print("#####################################\n" | |
| f" Vote results for proposal #{proposal_id} in percents:\n" | |
| f" YES : {weight_yes:.3f}%\n" | |
| f" NO : {weight_no:.3f}%\n" | |
| f" VETO : {weight_veto:.3f}%\n" | |
| f" ABSTAIN : {weight_abstain:.3f}%\n" | |
| f" RATIO VOTERS : {total_count} vote / {len(delegator_list)} delegators => {ratio_count:.3f}%\n" | |
| ) | |
| if __name__ == "__main__": | |
| parser = argparse.ArgumentParser(description='Get vote result by validator') | |
| parser.add_argument('-p', '--proposal', type=int, required=True, | |
| help='set the proposal number to check') | |
| parser.add_argument('-v', '--validator', type=str, required=True, | |
| help='set the valoper address') | |
| parser.add_argument('-c', '--chain', type=str, required=False, | |
| help='set the chain to query') | |
| parser.add_argument('-n', '--node', type=str, required=False, | |
| help='set the RPC URL to query') | |
| args = parser.parse_args() | |
| get_votes_weight(node=args.node, validator_addr=args.validator, proposal_id=args.proposal) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment