@snapshot-labs/snapshot.js 0.3.104 → 0.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -2781,6 +2781,10 @@ var gateways = [
2781
2781
  "ipfs.infura.io"
2782
2782
  ];
2783
2783
 
2784
+ function isValidChoice(voteChoice, proposalChoices) {
2785
+ return (typeof voteChoice === 'number' &&
2786
+ (proposalChoices === null || proposalChoices === void 0 ? void 0 : proposalChoices[voteChoice - 1]) !== undefined);
2787
+ }
2784
2788
  var SingleChoiceVoting = /** @class */ (function () {
2785
2789
  function SingleChoiceVoting(proposal, votes, strategies, selected) {
2786
2790
  this.proposal = proposal;
@@ -2788,38 +2792,49 @@ var SingleChoiceVoting = /** @class */ (function () {
2788
2792
  this.strategies = strategies;
2789
2793
  this.selected = selected;
2790
2794
  }
2791
- // Returns an array with the results for each choice
2792
- SingleChoiceVoting.prototype.resultsByVoteBalance = function () {
2795
+ SingleChoiceVoting.prototype.getValidVotes = function () {
2796
+ var _this = this;
2797
+ return this.votes.filter(function (vote) {
2798
+ return isValidChoice(vote.choice, _this.proposal.choices);
2799
+ });
2800
+ };
2801
+ SingleChoiceVoting.prototype.getScores = function () {
2793
2802
  var _this = this;
2794
2803
  return this.proposal.choices.map(function (choice, i) {
2795
- return _this.votes
2796
- .filter(function (vote) { return vote.choice === i + 1; })
2797
- .reduce(function (a, b) { return a + b.balance; }, 0);
2804
+ var votes = _this.getValidVotes().filter(function (vote) { return vote.choice === i + 1; });
2805
+ var balanceSum = votes.reduce(function (a, b) { return a + b.balance; }, 0);
2806
+ return balanceSum;
2798
2807
  });
2799
2808
  };
2800
- // Returns an array with the results for each choice
2801
- // and for each strategy
2802
- SingleChoiceVoting.prototype.resultsByStrategyScore = function () {
2809
+ SingleChoiceVoting.prototype.getScoresByStrategy = function () {
2803
2810
  var _this = this;
2804
2811
  return this.proposal.choices.map(function (choice, i) {
2805
- return _this.strategies.map(function (strategy, sI) {
2806
- return _this.votes
2807
- .filter(function (vote) { return vote.choice === i + 1; })
2808
- .reduce(function (a, b) { return a + b.scores[sI]; }, 0);
2812
+ var scores = _this.strategies.map(function (strategy, sI) {
2813
+ var votes = _this.getValidVotes().filter(function (vote) { return vote.choice === i + 1; });
2814
+ var scoreSum = votes.reduce(function (a, b) { return a + b.scores[sI]; }, 0);
2815
+ return scoreSum;
2809
2816
  });
2817
+ return scores;
2810
2818
  });
2811
2819
  };
2812
- // Returns the total amount of the results
2813
- SingleChoiceVoting.prototype.sumOfResultsBalance = function () {
2814
- return this.votes.reduce(function (a, b) { return a + b.balance; }, 0);
2820
+ SingleChoiceVoting.prototype.getScoresTotal = function () {
2821
+ return this.getValidVotes().reduce(function (a, b) { return a + b.balance; }, 0);
2815
2822
  };
2816
- // Returns a string of all choices
2817
2823
  SingleChoiceVoting.prototype.getChoiceString = function () {
2818
2824
  return this.proposal.choices[this.selected - 1];
2819
2825
  };
2820
2826
  return SingleChoiceVoting;
2821
2827
  }());
2822
2828
 
2829
+ function isValidChoice$1(voteChoice, proposalChoices) {
2830
+ return (Array.isArray(voteChoice) &&
2831
+ // If voteChoice index is not in proposalChoices, return false
2832
+ voteChoice.every(function (choice) { return (proposalChoices === null || proposalChoices === void 0 ? void 0 : proposalChoices[choice - 1]) !== undefined; }) &&
2833
+ // If any voteChoice is duplicated, return false
2834
+ voteChoice.length === new Set(voteChoice).size &&
2835
+ // If voteChoice is empty, return false
2836
+ voteChoice.length > 0);
2837
+ }
2823
2838
  var ApprovalVoting = /** @class */ (function () {
2824
2839
  function ApprovalVoting(proposal, votes, strategies, selected) {
2825
2840
  this.proposal = proposal;
@@ -2827,26 +2842,32 @@ var ApprovalVoting = /** @class */ (function () {
2827
2842
  this.strategies = strategies;
2828
2843
  this.selected = selected;
2829
2844
  }
2830
- ApprovalVoting.prototype.resultsByVoteBalance = function () {
2845
+ ApprovalVoting.prototype.getValidVotes = function () {
2846
+ var _this = this;
2847
+ return this.votes.filter(function (vote) {
2848
+ return isValidChoice$1(vote.choice, _this.proposal.choices);
2849
+ });
2850
+ };
2851
+ ApprovalVoting.prototype.getScores = function () {
2831
2852
  var _this = this;
2832
2853
  return this.proposal.choices.map(function (choice, i) {
2833
- return _this.votes
2854
+ return _this.getValidVotes()
2834
2855
  .filter(function (vote) { return vote.choice.includes(i + 1); })
2835
2856
  .reduce(function (a, b) { return a + b.balance; }, 0);
2836
2857
  });
2837
2858
  };
2838
- ApprovalVoting.prototype.resultsByStrategyScore = function () {
2859
+ ApprovalVoting.prototype.getScoresByStrategy = function () {
2839
2860
  var _this = this;
2840
2861
  return this.proposal.choices.map(function (choice, i) {
2841
2862
  return _this.strategies.map(function (strategy, sI) {
2842
- return _this.votes
2863
+ return _this.getValidVotes()
2843
2864
  .filter(function (vote) { return vote.choice.includes(i + 1); })
2844
2865
  .reduce(function (a, b) { return a + b.scores[sI]; }, 0);
2845
2866
  });
2846
2867
  });
2847
2868
  };
2848
- ApprovalVoting.prototype.sumOfResultsBalance = function () {
2849
- return this.votes.reduce(function (a, b) { return a + b.balance; }, 0);
2869
+ ApprovalVoting.prototype.getScoresTotal = function () {
2870
+ return this.getValidVotes().reduce(function (a, b) { return a + b.balance; }, 0);
2850
2871
  };
2851
2872
  ApprovalVoting.prototype.getChoiceString = function () {
2852
2873
  var _this = this;
@@ -2859,6 +2880,17 @@ var ApprovalVoting = /** @class */ (function () {
2859
2880
  return ApprovalVoting;
2860
2881
  }());
2861
2882
 
2883
+ function isValidChoice$2(voteChoice, proposalChoices) {
2884
+ return (typeof voteChoice === 'object' &&
2885
+ !Array.isArray(voteChoice) &&
2886
+ voteChoice !== null &&
2887
+ // If voteChoice object keys are not in choices, return false
2888
+ Object.keys(voteChoice).every(function (key) { return (proposalChoices === null || proposalChoices === void 0 ? void 0 : proposalChoices[Number(key) - 1]) !== undefined; }) &&
2889
+ // If voteChoice object is empty, return false
2890
+ Object.keys(voteChoice).length > 0 &&
2891
+ // If voteChoice object values are not a positive integer, return false
2892
+ Object.values(voteChoice).every(function (value) { return typeof value === 'number' && value > 0; }));
2893
+ }
2862
2894
  function percentageOfTotal(i, values, total) {
2863
2895
  var reducedTotal = total.reduce(function (a, b) { return a + b; }, 0);
2864
2896
  var percent = (values[i] / reducedTotal) * 100;
@@ -2867,32 +2899,38 @@ function percentageOfTotal(i, values, total) {
2867
2899
  function quadraticMath(i, choice, balance) {
2868
2900
  return Math.sqrt((percentageOfTotal(i + 1, choice, Object.values(choice)) / 100) * balance);
2869
2901
  }
2870
- var ApprovalVoting$1 = /** @class */ (function () {
2871
- function ApprovalVoting(proposal, votes, strategies, selected) {
2902
+ var QuadraticVoting = /** @class */ (function () {
2903
+ function QuadraticVoting(proposal, votes, strategies, selected) {
2872
2904
  this.proposal = proposal;
2873
2905
  this.votes = votes;
2874
2906
  this.strategies = strategies;
2875
2907
  this.selected = selected;
2876
2908
  }
2877
- ApprovalVoting.prototype.resultsByVoteBalance = function () {
2909
+ QuadraticVoting.prototype.getValidVotes = function () {
2910
+ var _this = this;
2911
+ return this.votes.filter(function (vote) {
2912
+ return isValidChoice$2(vote.choice, _this.proposal.choices);
2913
+ });
2914
+ };
2915
+ QuadraticVoting.prototype.getScores = function () {
2878
2916
  var _this = this;
2879
2917
  var results = this.proposal.choices
2880
2918
  .map(function (choice, i) {
2881
- return _this.votes
2919
+ return _this.getValidVotes()
2882
2920
  .map(function (vote) { return quadraticMath(i, vote.choice, vote.balance); })
2883
2921
  .reduce(function (a, b) { return a + b; }, 0);
2884
2922
  })
2885
2923
  .map(function (sqrt) { return sqrt * sqrt; });
2886
2924
  return results
2887
2925
  .map(function (res, i) { return percentageOfTotal(i, results, results); })
2888
- .map(function (p) { return (_this.sumOfResultsBalance() / 100) * p; });
2926
+ .map(function (p) { return (_this.getScoresTotal() / 100) * p; });
2889
2927
  };
2890
- ApprovalVoting.prototype.resultsByStrategyScore = function () {
2928
+ QuadraticVoting.prototype.getScoresByStrategy = function () {
2891
2929
  var _this = this;
2892
2930
  var results = this.proposal.choices
2893
2931
  .map(function (choice, i) {
2894
2932
  return _this.strategies.map(function (strategy, sI) {
2895
- return _this.votes
2933
+ return _this.getValidVotes()
2896
2934
  .map(function (vote) { return quadraticMath(i, vote.choice, vote.scores[sI]); })
2897
2935
  .reduce(function (a, b) { return a + b; }, 0);
2898
2936
  });
@@ -2900,16 +2938,17 @@ var ApprovalVoting$1 = /** @class */ (function () {
2900
2938
  .map(function (arr) { return arr.map(function (sqrt) { return [sqrt * sqrt]; }); });
2901
2939
  return results.map(function (res, i) {
2902
2940
  return _this.strategies
2903
- .map(function (strategy, sI) { return [
2904
- percentageOfTotal(0, results[i][sI], results.flat(2))
2905
- ]; })
2906
- .map(function (p) { return [(_this.sumOfResultsBalance() / 100) * p]; });
2941
+ .map(function (strategy, sI) {
2942
+ return percentageOfTotal(0, results[i][sI], results.flat(2));
2943
+ })
2944
+ .map(function (p) { return [(_this.getScoresTotal() / 100) * p]; })
2945
+ .flat();
2907
2946
  });
2908
2947
  };
2909
- ApprovalVoting.prototype.sumOfResultsBalance = function () {
2910
- return this.votes.reduce(function (a, b) { return a + b.balance; }, 0);
2948
+ QuadraticVoting.prototype.getScoresTotal = function () {
2949
+ return this.getValidVotes().reduce(function (a, b) { return a + b.balance; }, 0);
2911
2950
  };
2912
- ApprovalVoting.prototype.getChoiceString = function () {
2951
+ QuadraticVoting.prototype.getChoiceString = function () {
2913
2952
  var _this = this;
2914
2953
  return this.proposal.choices
2915
2954
  .map(function (choice, i) {
@@ -2920,19 +2959,34 @@ var ApprovalVoting$1 = /** @class */ (function () {
2920
2959
  .filter(function (el) { return el != null; })
2921
2960
  .join(', ');
2922
2961
  };
2923
- return ApprovalVoting;
2962
+ return QuadraticVoting;
2924
2963
  }());
2925
2964
 
2965
+ function isValidChoice$3(voteChoice, proposalChoices) {
2966
+ return (Array.isArray(voteChoice) &&
2967
+ // If voteChoice index is not in choices, return false
2968
+ voteChoice.every(function (voteChoice) { return (proposalChoices === null || proposalChoices === void 0 ? void 0 : proposalChoices[voteChoice - 1]) !== undefined; }) &&
2969
+ // If any voteChoice is duplicated, return false
2970
+ voteChoice.length === new Set(voteChoice).size &&
2971
+ // If voteChoice is empty, return false
2972
+ voteChoice.length > 0 &&
2973
+ // If not all proposalChoices are selected, return false
2974
+ // TODO: We should add support for pacial bailout in the future
2975
+ voteChoice.length === proposalChoices.length);
2976
+ }
2926
2977
  function irv(ballots, rounds) {
2927
2978
  var candidates = __spread(new Set(ballots.map(function (vote) { return vote[0]; }).flat()));
2928
2979
  var votes = Object.entries(ballots.reduce(function (votes, _a, i, src) {
2929
2980
  var _b = __read(_a, 1), v = _b[0];
2930
- votes[v[0]][0] += src[i][1];
2931
- if (votes[v[0]][1].length > 1)
2932
- votes[v[0]][1] = votes[v[0]][1].map(function (score, sI) { return score + src[i][2][sI]; });
2981
+ var balance = src[i][1];
2982
+ votes[v[0]][0] += balance;
2983
+ var score = src[i][2];
2984
+ if (score.length > 1) {
2985
+ votes[v[0]][1] = score.map(function (s, sI) { return s + votes[v[0]][1][sI] || s; });
2986
+ }
2933
2987
  else
2934
2988
  votes[v[0]][1] = [
2935
- votes[v[0]][1].concat(src[i][2]).reduce(function (a, b) { return a + b; }, 0)
2989
+ votes[v[0]][1].concat(score).reduce(function (a, b) { return a + b; }, 0)
2936
2990
  ];
2937
2991
  return votes;
2938
2992
  }, Object.assign.apply(Object, __spread([{}], candidates.map(function (c) {
@@ -2960,7 +3014,8 @@ function irv(ballots, rounds) {
2960
3014
  round: rounds.length + 1,
2961
3015
  sortedByHighest: sortedByHighest
2962
3016
  });
2963
- return topCount > totalPowerOfVotes / 2 || sortedByHighest.length < 3
3017
+ return topCount > totalPowerOfVotes / 2 ||
3018
+ sortedByHighest.length < 3
2964
3019
  ? rounds
2965
3020
  : irv(ballots
2966
3021
  .map(function (ballot) { return [
@@ -2968,38 +3023,49 @@ function irv(ballots, rounds) {
2968
3023
  ballot[1],
2969
3024
  ballot[2]
2970
3025
  ]; })
2971
- .filter(function (b) { return b[0].length > 0; }), rounds);
3026
+ .filter(function (ballot) { return ballot[0].length > 0; }), rounds);
2972
3027
  }
2973
- function getFinalRound(i, votes) {
2974
- var results = irv(votes.map(function (vote) { return [vote.choice, vote.balance, vote.scores]; }), []);
2975
- var finalRound = results[results.length - 1];
2976
- return finalRound.sortedByHighest.filter(function (res) { return res[0] == i + 1; });
3028
+ function getFinalRound(votes) {
3029
+ var rounds = irv(votes.map(function (vote) { return [vote.choice, vote.balance, vote.scores]; }), []);
3030
+ var finalRound = rounds[rounds.length - 1];
3031
+ return finalRound.sortedByHighest;
2977
3032
  }
2978
- var ApprovalVoting$2 = /** @class */ (function () {
2979
- function ApprovalVoting(proposal, votes, strategies, selected) {
3033
+ var RankedChoiceVoting = /** @class */ (function () {
3034
+ function RankedChoiceVoting(proposal, votes, strategies, selected) {
2980
3035
  this.proposal = proposal;
2981
3036
  this.votes = votes;
2982
3037
  this.strategies = strategies;
2983
3038
  this.selected = selected;
2984
3039
  }
2985
- ApprovalVoting.prototype.resultsByVoteBalance = function () {
3040
+ RankedChoiceVoting.prototype.getValidVotes = function () {
2986
3041
  var _this = this;
3042
+ return this.votes.filter(function (vote) {
3043
+ return isValidChoice$3(vote.choice, _this.proposal.choices);
3044
+ });
3045
+ };
3046
+ RankedChoiceVoting.prototype.getScores = function () {
3047
+ var finalRound = getFinalRound(this.getValidVotes());
2987
3048
  return this.proposal.choices.map(function (choice, i) {
2988
- return getFinalRound(i, _this.votes).reduce(function (a, b) { return a + b[1][0]; }, 0);
3049
+ return finalRound
3050
+ .filter(function (res) { return Number(res[0]) === i + 1; })
3051
+ .reduce(function (a, b) { return a + b[1][0]; }, 0);
2989
3052
  });
2990
3053
  };
2991
- ApprovalVoting.prototype.resultsByStrategyScore = function () {
3054
+ RankedChoiceVoting.prototype.getScoresByStrategy = function () {
2992
3055
  var _this = this;
3056
+ var finalRound = getFinalRound(this.getValidVotes());
2993
3057
  return this.proposal.choices.map(function (choice, i) {
2994
3058
  return _this.strategies.map(function (strategy, sI) {
2995
- return getFinalRound(i, _this.votes).reduce(function (a, b) { return a + b[1][1][sI]; }, 0);
3059
+ return finalRound
3060
+ .filter(function (res) { return Number(res[0]) === i + 1; })
3061
+ .reduce(function (a, b) { return a + b[1][1][sI]; }, 0);
2996
3062
  });
2997
3063
  });
2998
3064
  };
2999
- ApprovalVoting.prototype.sumOfResultsBalance = function () {
3000
- return this.resultsByVoteBalance().reduce(function (a, b) { return a + b; });
3065
+ RankedChoiceVoting.prototype.getScoresTotal = function () {
3066
+ return this.getScores().reduce(function (a, b) { return a + b; });
3001
3067
  };
3002
- ApprovalVoting.prototype.getChoiceString = function () {
3068
+ RankedChoiceVoting.prototype.getChoiceString = function () {
3003
3069
  var _this = this;
3004
3070
  return this.selected
3005
3071
  .map(function (choice) {
@@ -3009,9 +3075,22 @@ var ApprovalVoting$2 = /** @class */ (function () {
3009
3075
  .map(function (el, i) { return "(" + getNumberWithOrdinal(i + 1) + ") " + el; })
3010
3076
  .join(', ');
3011
3077
  };
3012
- return ApprovalVoting;
3078
+ return RankedChoiceVoting;
3013
3079
  }());
3014
3080
 
3081
+ function isValidChoice$4(voteChoice, proposalChoices) {
3082
+ return (typeof voteChoice === 'object' &&
3083
+ !Array.isArray(voteChoice) &&
3084
+ voteChoice !== null &&
3085
+ // If voteChoice object keys are not in choices, return false
3086
+ Object.keys(voteChoice).every(function (key) { return (proposalChoices === null || proposalChoices === void 0 ? void 0 : proposalChoices[Number(key) - 1]) !== undefined; }) &&
3087
+ // If voteChoice object is empty, return false
3088
+ Object.keys(voteChoice).length > 0 &&
3089
+ // If voteChoice object values are not a positive integer, return false
3090
+ Object.values(voteChoice).every(function (value) { return typeof value === 'number' && value > 0; }) &&
3091
+ // If voteChoice is empty, return false
3092
+ Object.keys(voteChoice).length > 0);
3093
+ }
3015
3094
  function percentageOfTotal$1(i, values, total) {
3016
3095
  var reducedTotal = total.reduce(function (a, b) { return a + b; }, 0);
3017
3096
  var percent = (values[i] / reducedTotal) * 100;
@@ -3027,23 +3106,29 @@ var WeightedVoting = /** @class */ (function () {
3027
3106
  this.strategies = strategies;
3028
3107
  this.selected = selected;
3029
3108
  }
3030
- WeightedVoting.prototype.resultsByVoteBalance = function () {
3109
+ WeightedVoting.prototype.getValidVotes = function () {
3110
+ var _this = this;
3111
+ return this.votes.filter(function (vote) {
3112
+ return isValidChoice$4(vote.choice, _this.proposal.choices);
3113
+ });
3114
+ };
3115
+ WeightedVoting.prototype.getScores = function () {
3031
3116
  var _this = this;
3032
3117
  var results = this.proposal.choices.map(function (choice, i) {
3033
- return _this.votes
3118
+ return _this.getValidVotes()
3034
3119
  .map(function (vote) { return weightedPower(i, vote.choice, vote.balance); })
3035
3120
  .reduce(function (a, b) { return a + b; }, 0);
3036
3121
  });
3037
3122
  return results
3038
3123
  .map(function (res, i) { return percentageOfTotal$1(i, results, results); })
3039
- .map(function (p) { return (_this.sumOfResultsBalance() / 100) * p; });
3124
+ .map(function (p) { return (_this.getScoresTotal() / 100) * p; });
3040
3125
  };
3041
- WeightedVoting.prototype.resultsByStrategyScore = function () {
3126
+ WeightedVoting.prototype.getScoresByStrategy = function () {
3042
3127
  var _this = this;
3043
3128
  var results = this.proposal.choices
3044
3129
  .map(function (choice, i) {
3045
3130
  return _this.strategies.map(function (strategy, sI) {
3046
- return _this.votes
3131
+ return _this.getValidVotes()
3047
3132
  .map(function (vote) { return weightedPower(i, vote.choice, vote.scores[sI]); })
3048
3133
  .reduce(function (a, b) { return a + b; }, 0);
3049
3134
  });
@@ -3051,14 +3136,15 @@ var WeightedVoting = /** @class */ (function () {
3051
3136
  .map(function (arr) { return arr.map(function (pwr) { return [pwr]; }); });
3052
3137
  return results.map(function (res, i) {
3053
3138
  return _this.strategies
3054
- .map(function (strategy, sI) { return [
3055
- percentageOfTotal$1(0, results[i][sI], results.flat(2))
3056
- ]; })
3057
- .map(function (p) { return [(_this.sumOfResultsBalance() / 100) * p]; });
3139
+ .map(function (strategy, sI) {
3140
+ return percentageOfTotal$1(0, results[i][sI], results.flat(2));
3141
+ })
3142
+ .map(function (p) { return [(_this.getScoresTotal() / 100) * p]; })
3143
+ .flat();
3058
3144
  });
3059
3145
  };
3060
- WeightedVoting.prototype.sumOfResultsBalance = function () {
3061
- return this.votes.reduce(function (a, b) { return a + b.balance; }, 0);
3146
+ WeightedVoting.prototype.getScoresTotal = function () {
3147
+ return this.getValidVotes().reduce(function (a, b) { return a + b.balance; }, 0);
3062
3148
  };
3063
3149
  WeightedVoting.prototype.getChoiceString = function () {
3064
3150
  var _this = this;
@@ -3077,8 +3163,8 @@ var WeightedVoting = /** @class */ (function () {
3077
3163
  var voting = {
3078
3164
  'single-choice': SingleChoiceVoting,
3079
3165
  approval: ApprovalVoting,
3080
- quadratic: ApprovalVoting$1,
3081
- 'ranked-choice': ApprovalVoting$2,
3166
+ quadratic: QuadraticVoting,
3167
+ 'ranked-choice': RankedChoiceVoting,
3082
3168
  weighted: WeightedVoting,
3083
3169
  basic: SingleChoiceVoting
3084
3170
  };