@snapshot-labs/snapshot.js 0.3.102 → 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.
- package/dist/snapshot.cjs.js +171 -71
- package/dist/snapshot.esm.js +171 -71
- package/dist/snapshot.min.js +7 -7
- package/dist/voting/approval.d.ts +20 -9
- package/dist/voting/quadratic.d.ts +23 -10
- package/dist/voting/rankedChoice.d.ts +17 -10
- package/dist/voting/singleChoice.d.ts +16 -9
- package/dist/voting/types.d.ts +34 -0
- package/dist/voting/weighted.d.ts +22 -9
- package/package.json +10 -12
- package/src/networks.json +15 -1
- package/src/voting/approval.spec.js +208 -0
- package/src/voting/approval.ts +48 -16
- package/src/voting/examples/approval.json +2042 -0
- package/src/voting/examples/quadratic.json +3280 -0
- package/src/voting/examples/rankedChoice.json +2595 -0
- package/src/voting/examples/singleChoice.json +3805 -0
- package/src/voting/examples/weighted.json +4339 -0
- package/src/voting/quadratic.spec.js +208 -0
- package/src/voting/quadratic.ts +54 -19
- package/src/voting/rankedChoice.spec.js +202 -0
- package/src/voting/rankedChoice.ts +85 -38
- package/src/voting/singleChoice.spec.js +182 -0
- package/src/voting/singleChoice.ts +49 -26
- package/src/voting/types.ts +35 -0
- package/src/voting/weighted.spec.js +213 -0
- package/src/voting/weighted.ts +55 -18
package/dist/snapshot.cjs.js
CHANGED
|
@@ -1397,8 +1397,8 @@ var networks = {
|
|
|
1397
1397
|
network: "mainnet",
|
|
1398
1398
|
multicall: "0x1ee38d535d541c55c9dae27b12edf090c608e6fb",
|
|
1399
1399
|
rpc: [
|
|
1400
|
-
"https://rpc.ankr.com/bsc",
|
|
1401
1400
|
"https://speedy-nodes-nyc.moralis.io/b9aed21e7bb7bdeb35972c9a/bsc/mainnet/archive",
|
|
1401
|
+
"https://rpc.ankr.com/bsc",
|
|
1402
1402
|
"https://bsc.getblock.io/mainnet/?api_key=91f8195f-bf46-488f-846a-73d6853790e7",
|
|
1403
1403
|
"https://bsc-private-dataseed1.nariox.org",
|
|
1404
1404
|
"https://bsc-dataseed1.ninicoin.io",
|
|
@@ -1897,6 +1897,20 @@ var networks = {
|
|
|
1897
1897
|
explorer: "https://blockscout.com/astar",
|
|
1898
1898
|
start: 366482,
|
|
1899
1899
|
logo: "ipfs://QmZLQVsUqHBDXutu6ywTvcYXDZG2xBstMfHkfJSzUNpZsc"
|
|
1900
|
+
},
|
|
1901
|
+
"595": {
|
|
1902
|
+
key: "595",
|
|
1903
|
+
name: "Acala Mandala Testnet",
|
|
1904
|
+
chainId: 595,
|
|
1905
|
+
network: "testnet",
|
|
1906
|
+
testnet: true,
|
|
1907
|
+
multicall: "0x8ce86f733024c1ccae2224f05c11fd50713d6f81",
|
|
1908
|
+
rpc: [
|
|
1909
|
+
"https://tc7-eth.aca-dev.network"
|
|
1910
|
+
],
|
|
1911
|
+
explorer: "https://blockscout.mandala.acala.network",
|
|
1912
|
+
start: 1335512,
|
|
1913
|
+
logo: "ipfs://QmY1ZYBUzb46Mto7G1GitQWfaqq6n8Q4WArxFBzhNdLqvg"
|
|
1900
1914
|
},
|
|
1901
1915
|
"888": {
|
|
1902
1916
|
key: "888",
|
|
@@ -2767,6 +2781,10 @@ var gateways = [
|
|
|
2767
2781
|
"ipfs.infura.io"
|
|
2768
2782
|
];
|
|
2769
2783
|
|
|
2784
|
+
function isValidChoice(voteChoice, proposalChoices) {
|
|
2785
|
+
return (typeof voteChoice === 'number' &&
|
|
2786
|
+
(proposalChoices === null || proposalChoices === void 0 ? void 0 : proposalChoices[voteChoice - 1]) !== undefined);
|
|
2787
|
+
}
|
|
2770
2788
|
var SingleChoiceVoting = /** @class */ (function () {
|
|
2771
2789
|
function SingleChoiceVoting(proposal, votes, strategies, selected) {
|
|
2772
2790
|
this.proposal = proposal;
|
|
@@ -2774,38 +2792,49 @@ var SingleChoiceVoting = /** @class */ (function () {
|
|
|
2774
2792
|
this.strategies = strategies;
|
|
2775
2793
|
this.selected = selected;
|
|
2776
2794
|
}
|
|
2777
|
-
|
|
2778
|
-
|
|
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 () {
|
|
2779
2802
|
var _this = this;
|
|
2780
2803
|
return this.proposal.choices.map(function (choice, i) {
|
|
2781
|
-
|
|
2782
|
-
|
|
2783
|
-
|
|
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;
|
|
2784
2807
|
});
|
|
2785
2808
|
};
|
|
2786
|
-
|
|
2787
|
-
// and for each strategy
|
|
2788
|
-
SingleChoiceVoting.prototype.resultsByStrategyScore = function () {
|
|
2809
|
+
SingleChoiceVoting.prototype.getScoresByStrategy = function () {
|
|
2789
2810
|
var _this = this;
|
|
2790
2811
|
return this.proposal.choices.map(function (choice, i) {
|
|
2791
|
-
|
|
2792
|
-
|
|
2793
|
-
|
|
2794
|
-
|
|
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;
|
|
2795
2816
|
});
|
|
2817
|
+
return scores;
|
|
2796
2818
|
});
|
|
2797
2819
|
};
|
|
2798
|
-
|
|
2799
|
-
|
|
2800
|
-
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);
|
|
2801
2822
|
};
|
|
2802
|
-
// Returns a string of all choices
|
|
2803
2823
|
SingleChoiceVoting.prototype.getChoiceString = function () {
|
|
2804
2824
|
return this.proposal.choices[this.selected - 1];
|
|
2805
2825
|
};
|
|
2806
2826
|
return SingleChoiceVoting;
|
|
2807
2827
|
}());
|
|
2808
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
|
+
}
|
|
2809
2838
|
var ApprovalVoting = /** @class */ (function () {
|
|
2810
2839
|
function ApprovalVoting(proposal, votes, strategies, selected) {
|
|
2811
2840
|
this.proposal = proposal;
|
|
@@ -2813,26 +2842,32 @@ var ApprovalVoting = /** @class */ (function () {
|
|
|
2813
2842
|
this.strategies = strategies;
|
|
2814
2843
|
this.selected = selected;
|
|
2815
2844
|
}
|
|
2816
|
-
ApprovalVoting.prototype.
|
|
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 () {
|
|
2817
2852
|
var _this = this;
|
|
2818
2853
|
return this.proposal.choices.map(function (choice, i) {
|
|
2819
|
-
return _this.
|
|
2854
|
+
return _this.getValidVotes()
|
|
2820
2855
|
.filter(function (vote) { return vote.choice.includes(i + 1); })
|
|
2821
2856
|
.reduce(function (a, b) { return a + b.balance; }, 0);
|
|
2822
2857
|
});
|
|
2823
2858
|
};
|
|
2824
|
-
ApprovalVoting.prototype.
|
|
2859
|
+
ApprovalVoting.prototype.getScoresByStrategy = function () {
|
|
2825
2860
|
var _this = this;
|
|
2826
2861
|
return this.proposal.choices.map(function (choice, i) {
|
|
2827
2862
|
return _this.strategies.map(function (strategy, sI) {
|
|
2828
|
-
return _this.
|
|
2863
|
+
return _this.getValidVotes()
|
|
2829
2864
|
.filter(function (vote) { return vote.choice.includes(i + 1); })
|
|
2830
2865
|
.reduce(function (a, b) { return a + b.scores[sI]; }, 0);
|
|
2831
2866
|
});
|
|
2832
2867
|
});
|
|
2833
2868
|
};
|
|
2834
|
-
ApprovalVoting.prototype.
|
|
2835
|
-
return this.
|
|
2869
|
+
ApprovalVoting.prototype.getScoresTotal = function () {
|
|
2870
|
+
return this.getValidVotes().reduce(function (a, b) { return a + b.balance; }, 0);
|
|
2836
2871
|
};
|
|
2837
2872
|
ApprovalVoting.prototype.getChoiceString = function () {
|
|
2838
2873
|
var _this = this;
|
|
@@ -2845,6 +2880,17 @@ var ApprovalVoting = /** @class */ (function () {
|
|
|
2845
2880
|
return ApprovalVoting;
|
|
2846
2881
|
}());
|
|
2847
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
|
+
}
|
|
2848
2894
|
function percentageOfTotal(i, values, total) {
|
|
2849
2895
|
var reducedTotal = total.reduce(function (a, b) { return a + b; }, 0);
|
|
2850
2896
|
var percent = (values[i] / reducedTotal) * 100;
|
|
@@ -2853,32 +2899,38 @@ function percentageOfTotal(i, values, total) {
|
|
|
2853
2899
|
function quadraticMath(i, choice, balance) {
|
|
2854
2900
|
return Math.sqrt((percentageOfTotal(i + 1, choice, Object.values(choice)) / 100) * balance);
|
|
2855
2901
|
}
|
|
2856
|
-
var
|
|
2857
|
-
function
|
|
2902
|
+
var QuadraticVoting = /** @class */ (function () {
|
|
2903
|
+
function QuadraticVoting(proposal, votes, strategies, selected) {
|
|
2858
2904
|
this.proposal = proposal;
|
|
2859
2905
|
this.votes = votes;
|
|
2860
2906
|
this.strategies = strategies;
|
|
2861
2907
|
this.selected = selected;
|
|
2862
2908
|
}
|
|
2863
|
-
|
|
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 () {
|
|
2864
2916
|
var _this = this;
|
|
2865
2917
|
var results = this.proposal.choices
|
|
2866
2918
|
.map(function (choice, i) {
|
|
2867
|
-
return _this.
|
|
2919
|
+
return _this.getValidVotes()
|
|
2868
2920
|
.map(function (vote) { return quadraticMath(i, vote.choice, vote.balance); })
|
|
2869
2921
|
.reduce(function (a, b) { return a + b; }, 0);
|
|
2870
2922
|
})
|
|
2871
2923
|
.map(function (sqrt) { return sqrt * sqrt; });
|
|
2872
2924
|
return results
|
|
2873
2925
|
.map(function (res, i) { return percentageOfTotal(i, results, results); })
|
|
2874
|
-
.map(function (p) { return (_this.
|
|
2926
|
+
.map(function (p) { return (_this.getScoresTotal() / 100) * p; });
|
|
2875
2927
|
};
|
|
2876
|
-
|
|
2928
|
+
QuadraticVoting.prototype.getScoresByStrategy = function () {
|
|
2877
2929
|
var _this = this;
|
|
2878
2930
|
var results = this.proposal.choices
|
|
2879
2931
|
.map(function (choice, i) {
|
|
2880
2932
|
return _this.strategies.map(function (strategy, sI) {
|
|
2881
|
-
return _this.
|
|
2933
|
+
return _this.getValidVotes()
|
|
2882
2934
|
.map(function (vote) { return quadraticMath(i, vote.choice, vote.scores[sI]); })
|
|
2883
2935
|
.reduce(function (a, b) { return a + b; }, 0);
|
|
2884
2936
|
});
|
|
@@ -2886,16 +2938,17 @@ var ApprovalVoting$1 = /** @class */ (function () {
|
|
|
2886
2938
|
.map(function (arr) { return arr.map(function (sqrt) { return [sqrt * sqrt]; }); });
|
|
2887
2939
|
return results.map(function (res, i) {
|
|
2888
2940
|
return _this.strategies
|
|
2889
|
-
.map(function (strategy, sI) {
|
|
2890
|
-
percentageOfTotal(0, results[i][sI], results.flat(2))
|
|
2891
|
-
|
|
2892
|
-
.map(function (p) { return [(_this.
|
|
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();
|
|
2893
2946
|
});
|
|
2894
2947
|
};
|
|
2895
|
-
|
|
2896
|
-
return this.
|
|
2948
|
+
QuadraticVoting.prototype.getScoresTotal = function () {
|
|
2949
|
+
return this.getValidVotes().reduce(function (a, b) { return a + b.balance; }, 0);
|
|
2897
2950
|
};
|
|
2898
|
-
|
|
2951
|
+
QuadraticVoting.prototype.getChoiceString = function () {
|
|
2899
2952
|
var _this = this;
|
|
2900
2953
|
return this.proposal.choices
|
|
2901
2954
|
.map(function (choice, i) {
|
|
@@ -2906,19 +2959,34 @@ var ApprovalVoting$1 = /** @class */ (function () {
|
|
|
2906
2959
|
.filter(function (el) { return el != null; })
|
|
2907
2960
|
.join(', ');
|
|
2908
2961
|
};
|
|
2909
|
-
return
|
|
2962
|
+
return QuadraticVoting;
|
|
2910
2963
|
}());
|
|
2911
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
|
+
}
|
|
2912
2977
|
function irv(ballots, rounds) {
|
|
2913
2978
|
var candidates = __spread(new Set(ballots.map(function (vote) { return vote[0]; }).flat()));
|
|
2914
2979
|
var votes = Object.entries(ballots.reduce(function (votes, _a, i, src) {
|
|
2915
2980
|
var _b = __read(_a, 1), v = _b[0];
|
|
2916
|
-
|
|
2917
|
-
|
|
2918
|
-
|
|
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
|
+
}
|
|
2919
2987
|
else
|
|
2920
2988
|
votes[v[0]][1] = [
|
|
2921
|
-
votes[v[0]][1].concat(
|
|
2989
|
+
votes[v[0]][1].concat(score).reduce(function (a, b) { return a + b; }, 0)
|
|
2922
2990
|
];
|
|
2923
2991
|
return votes;
|
|
2924
2992
|
}, Object.assign.apply(Object, __spread([{}], candidates.map(function (c) {
|
|
@@ -2946,7 +3014,8 @@ function irv(ballots, rounds) {
|
|
|
2946
3014
|
round: rounds.length + 1,
|
|
2947
3015
|
sortedByHighest: sortedByHighest
|
|
2948
3016
|
});
|
|
2949
|
-
return topCount > totalPowerOfVotes / 2 ||
|
|
3017
|
+
return topCount > totalPowerOfVotes / 2 ||
|
|
3018
|
+
sortedByHighest.length < 3
|
|
2950
3019
|
? rounds
|
|
2951
3020
|
: irv(ballots
|
|
2952
3021
|
.map(function (ballot) { return [
|
|
@@ -2954,38 +3023,49 @@ function irv(ballots, rounds) {
|
|
|
2954
3023
|
ballot[1],
|
|
2955
3024
|
ballot[2]
|
|
2956
3025
|
]; })
|
|
2957
|
-
.filter(function (
|
|
3026
|
+
.filter(function (ballot) { return ballot[0].length > 0; }), rounds);
|
|
2958
3027
|
}
|
|
2959
|
-
function getFinalRound(
|
|
2960
|
-
var
|
|
2961
|
-
var finalRound =
|
|
2962
|
-
return finalRound.sortedByHighest
|
|
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;
|
|
2963
3032
|
}
|
|
2964
|
-
var
|
|
2965
|
-
function
|
|
3033
|
+
var RankedChoiceVoting = /** @class */ (function () {
|
|
3034
|
+
function RankedChoiceVoting(proposal, votes, strategies, selected) {
|
|
2966
3035
|
this.proposal = proposal;
|
|
2967
3036
|
this.votes = votes;
|
|
2968
3037
|
this.strategies = strategies;
|
|
2969
3038
|
this.selected = selected;
|
|
2970
3039
|
}
|
|
2971
|
-
|
|
3040
|
+
RankedChoiceVoting.prototype.getValidVotes = function () {
|
|
2972
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());
|
|
2973
3048
|
return this.proposal.choices.map(function (choice, i) {
|
|
2974
|
-
return
|
|
3049
|
+
return finalRound
|
|
3050
|
+
.filter(function (res) { return Number(res[0]) === i + 1; })
|
|
3051
|
+
.reduce(function (a, b) { return a + b[1][0]; }, 0);
|
|
2975
3052
|
});
|
|
2976
3053
|
};
|
|
2977
|
-
|
|
3054
|
+
RankedChoiceVoting.prototype.getScoresByStrategy = function () {
|
|
2978
3055
|
var _this = this;
|
|
3056
|
+
var finalRound = getFinalRound(this.getValidVotes());
|
|
2979
3057
|
return this.proposal.choices.map(function (choice, i) {
|
|
2980
3058
|
return _this.strategies.map(function (strategy, sI) {
|
|
2981
|
-
return
|
|
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);
|
|
2982
3062
|
});
|
|
2983
3063
|
});
|
|
2984
3064
|
};
|
|
2985
|
-
|
|
2986
|
-
return this.
|
|
3065
|
+
RankedChoiceVoting.prototype.getScoresTotal = function () {
|
|
3066
|
+
return this.getScores().reduce(function (a, b) { return a + b; });
|
|
2987
3067
|
};
|
|
2988
|
-
|
|
3068
|
+
RankedChoiceVoting.prototype.getChoiceString = function () {
|
|
2989
3069
|
var _this = this;
|
|
2990
3070
|
return this.selected
|
|
2991
3071
|
.map(function (choice) {
|
|
@@ -2995,9 +3075,22 @@ var ApprovalVoting$2 = /** @class */ (function () {
|
|
|
2995
3075
|
.map(function (el, i) { return "(" + getNumberWithOrdinal(i + 1) + ") " + el; })
|
|
2996
3076
|
.join(', ');
|
|
2997
3077
|
};
|
|
2998
|
-
return
|
|
3078
|
+
return RankedChoiceVoting;
|
|
2999
3079
|
}());
|
|
3000
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
|
+
}
|
|
3001
3094
|
function percentageOfTotal$1(i, values, total) {
|
|
3002
3095
|
var reducedTotal = total.reduce(function (a, b) { return a + b; }, 0);
|
|
3003
3096
|
var percent = (values[i] / reducedTotal) * 100;
|
|
@@ -3013,23 +3106,29 @@ var WeightedVoting = /** @class */ (function () {
|
|
|
3013
3106
|
this.strategies = strategies;
|
|
3014
3107
|
this.selected = selected;
|
|
3015
3108
|
}
|
|
3016
|
-
WeightedVoting.prototype.
|
|
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 () {
|
|
3017
3116
|
var _this = this;
|
|
3018
3117
|
var results = this.proposal.choices.map(function (choice, i) {
|
|
3019
|
-
return _this.
|
|
3118
|
+
return _this.getValidVotes()
|
|
3020
3119
|
.map(function (vote) { return weightedPower(i, vote.choice, vote.balance); })
|
|
3021
3120
|
.reduce(function (a, b) { return a + b; }, 0);
|
|
3022
3121
|
});
|
|
3023
3122
|
return results
|
|
3024
3123
|
.map(function (res, i) { return percentageOfTotal$1(i, results, results); })
|
|
3025
|
-
.map(function (p) { return (_this.
|
|
3124
|
+
.map(function (p) { return (_this.getScoresTotal() / 100) * p; });
|
|
3026
3125
|
};
|
|
3027
|
-
WeightedVoting.prototype.
|
|
3126
|
+
WeightedVoting.prototype.getScoresByStrategy = function () {
|
|
3028
3127
|
var _this = this;
|
|
3029
3128
|
var results = this.proposal.choices
|
|
3030
3129
|
.map(function (choice, i) {
|
|
3031
3130
|
return _this.strategies.map(function (strategy, sI) {
|
|
3032
|
-
return _this.
|
|
3131
|
+
return _this.getValidVotes()
|
|
3033
3132
|
.map(function (vote) { return weightedPower(i, vote.choice, vote.scores[sI]); })
|
|
3034
3133
|
.reduce(function (a, b) { return a + b; }, 0);
|
|
3035
3134
|
});
|
|
@@ -3037,14 +3136,15 @@ var WeightedVoting = /** @class */ (function () {
|
|
|
3037
3136
|
.map(function (arr) { return arr.map(function (pwr) { return [pwr]; }); });
|
|
3038
3137
|
return results.map(function (res, i) {
|
|
3039
3138
|
return _this.strategies
|
|
3040
|
-
.map(function (strategy, sI) {
|
|
3041
|
-
percentageOfTotal$1(0, results[i][sI], results.flat(2))
|
|
3042
|
-
|
|
3043
|
-
.map(function (p) { return [(_this.
|
|
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();
|
|
3044
3144
|
});
|
|
3045
3145
|
};
|
|
3046
|
-
WeightedVoting.prototype.
|
|
3047
|
-
return this.
|
|
3146
|
+
WeightedVoting.prototype.getScoresTotal = function () {
|
|
3147
|
+
return this.getValidVotes().reduce(function (a, b) { return a + b.balance; }, 0);
|
|
3048
3148
|
};
|
|
3049
3149
|
WeightedVoting.prototype.getChoiceString = function () {
|
|
3050
3150
|
var _this = this;
|
|
@@ -3063,8 +3163,8 @@ var WeightedVoting = /** @class */ (function () {
|
|
|
3063
3163
|
var voting = {
|
|
3064
3164
|
'single-choice': SingleChoiceVoting,
|
|
3065
3165
|
approval: ApprovalVoting,
|
|
3066
|
-
quadratic:
|
|
3067
|
-
'ranked-choice':
|
|
3166
|
+
quadratic: QuadraticVoting,
|
|
3167
|
+
'ranked-choice': RankedChoiceVoting,
|
|
3068
3168
|
weighted: WeightedVoting,
|
|
3069
3169
|
basic: SingleChoiceVoting
|
|
3070
3170
|
};
|