@snapshot-labs/snapshot.js 0.3.104 → 0.4.2
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/sign/types.d.ts +1 -0
- package/dist/sign/utils.d.ts +1 -1
- package/dist/snapshot.cjs.js +164 -75
- package/dist/snapshot.esm.js +164 -75
- package/dist/snapshot.min.js +5 -5
- 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 +5 -7
- package/src/networks.json +2 -2
- package/src/sign/index.ts +3 -1
- package/src/sign/types.ts +1 -0
- package/src/sign/utils.ts +2 -2
- 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/sign/types.d.ts
CHANGED
package/dist/sign/utils.d.ts
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
1
|
export declare function getHash(data: any): string;
|
|
2
|
-
export declare function verify(address: any, sig: any, data: any): Promise<boolean>;
|
|
2
|
+
export declare function verify(address: any, sig: any, data: any, network?: string): Promise<boolean>;
|
package/dist/snapshot.cjs.js
CHANGED
|
@@ -353,7 +353,9 @@ var Client = /** @class */ (function () {
|
|
|
353
353
|
type = type2 ? voteString2Types : voteStringTypes;
|
|
354
354
|
message.choice = JSON.stringify(message.choice);
|
|
355
355
|
}
|
|
356
|
-
|
|
356
|
+
if ((message === null || message === void 0 ? void 0 : message.privacy) === 'shutter')
|
|
357
|
+
type = type2 ? voteString2Types : voteStringTypes;
|
|
358
|
+
delete message.privacy;
|
|
357
359
|
delete message.type;
|
|
358
360
|
return [4 /*yield*/, this.sign(web3, address, message, type)];
|
|
359
361
|
case 1: return [2 /*return*/, _a.sent()];
|
|
@@ -2393,8 +2395,8 @@ var networks = {
|
|
|
2393
2395
|
testnet: true,
|
|
2394
2396
|
multicall: "0x08411ADd0b5AA8ee47563b146743C13b3556c9Cc",
|
|
2395
2397
|
rpc: [
|
|
2396
|
-
"https://
|
|
2397
|
-
"https://
|
|
2398
|
+
"https://speedy-nodes-nyc.moralis.io/9e03baabdc27be2a35bdec4a/polygon/mumbai/archive",
|
|
2399
|
+
"https://rpc-mumbai.matic.today"
|
|
2398
2400
|
],
|
|
2399
2401
|
ws: [
|
|
2400
2402
|
"wss://ws-mumbai.matic.today"
|
|
@@ -2744,7 +2746,8 @@ function getHash(data) {
|
|
|
2744
2746
|
var domain = data.domain, types = data.types, message = data.message;
|
|
2745
2747
|
return hash._TypedDataEncoder.hash(domain, types, message);
|
|
2746
2748
|
}
|
|
2747
|
-
function verify$1(address, sig, data) {
|
|
2749
|
+
function verify$1(address, sig, data, network) {
|
|
2750
|
+
if (network === void 0) { network = '1'; }
|
|
2748
2751
|
return __awaiter(this, void 0, void 0, function () {
|
|
2749
2752
|
var domain, types, message, hash, recoverAddress;
|
|
2750
2753
|
return __generator(this, function (_a) {
|
|
@@ -2764,7 +2767,7 @@ function verify$1(address, sig, data) {
|
|
|
2764
2767
|
console.log('Could not recoverAddress:' + e.message);
|
|
2765
2768
|
}
|
|
2766
2769
|
console.log('Check EIP1271 signature');
|
|
2767
|
-
return [4 /*yield*/, verify(address, sig, hash)];
|
|
2770
|
+
return [4 /*yield*/, verify(address, sig, hash, network)];
|
|
2768
2771
|
case 1: return [2 /*return*/, _a.sent()];
|
|
2769
2772
|
}
|
|
2770
2773
|
});
|
|
@@ -2781,6 +2784,10 @@ var gateways = [
|
|
|
2781
2784
|
"ipfs.infura.io"
|
|
2782
2785
|
];
|
|
2783
2786
|
|
|
2787
|
+
function isValidChoice(voteChoice, proposalChoices) {
|
|
2788
|
+
return (typeof voteChoice === 'number' &&
|
|
2789
|
+
(proposalChoices === null || proposalChoices === void 0 ? void 0 : proposalChoices[voteChoice - 1]) !== undefined);
|
|
2790
|
+
}
|
|
2784
2791
|
var SingleChoiceVoting = /** @class */ (function () {
|
|
2785
2792
|
function SingleChoiceVoting(proposal, votes, strategies, selected) {
|
|
2786
2793
|
this.proposal = proposal;
|
|
@@ -2788,38 +2795,49 @@ var SingleChoiceVoting = /** @class */ (function () {
|
|
|
2788
2795
|
this.strategies = strategies;
|
|
2789
2796
|
this.selected = selected;
|
|
2790
2797
|
}
|
|
2791
|
-
|
|
2792
|
-
|
|
2798
|
+
SingleChoiceVoting.prototype.getValidVotes = function () {
|
|
2799
|
+
var _this = this;
|
|
2800
|
+
return this.votes.filter(function (vote) {
|
|
2801
|
+
return isValidChoice(vote.choice, _this.proposal.choices);
|
|
2802
|
+
});
|
|
2803
|
+
};
|
|
2804
|
+
SingleChoiceVoting.prototype.getScores = function () {
|
|
2793
2805
|
var _this = this;
|
|
2794
2806
|
return this.proposal.choices.map(function (choice, i) {
|
|
2795
|
-
|
|
2796
|
-
|
|
2797
|
-
|
|
2807
|
+
var votes = _this.getValidVotes().filter(function (vote) { return vote.choice === i + 1; });
|
|
2808
|
+
var balanceSum = votes.reduce(function (a, b) { return a + b.balance; }, 0);
|
|
2809
|
+
return balanceSum;
|
|
2798
2810
|
});
|
|
2799
2811
|
};
|
|
2800
|
-
|
|
2801
|
-
// and for each strategy
|
|
2802
|
-
SingleChoiceVoting.prototype.resultsByStrategyScore = function () {
|
|
2812
|
+
SingleChoiceVoting.prototype.getScoresByStrategy = function () {
|
|
2803
2813
|
var _this = this;
|
|
2804
2814
|
return this.proposal.choices.map(function (choice, i) {
|
|
2805
|
-
|
|
2806
|
-
|
|
2807
|
-
|
|
2808
|
-
|
|
2815
|
+
var scores = _this.strategies.map(function (strategy, sI) {
|
|
2816
|
+
var votes = _this.getValidVotes().filter(function (vote) { return vote.choice === i + 1; });
|
|
2817
|
+
var scoreSum = votes.reduce(function (a, b) { return a + b.scores[sI]; }, 0);
|
|
2818
|
+
return scoreSum;
|
|
2809
2819
|
});
|
|
2820
|
+
return scores;
|
|
2810
2821
|
});
|
|
2811
2822
|
};
|
|
2812
|
-
|
|
2813
|
-
|
|
2814
|
-
return this.votes.reduce(function (a, b) { return a + b.balance; }, 0);
|
|
2823
|
+
SingleChoiceVoting.prototype.getScoresTotal = function () {
|
|
2824
|
+
return this.getValidVotes().reduce(function (a, b) { return a + b.balance; }, 0);
|
|
2815
2825
|
};
|
|
2816
|
-
// Returns a string of all choices
|
|
2817
2826
|
SingleChoiceVoting.prototype.getChoiceString = function () {
|
|
2818
2827
|
return this.proposal.choices[this.selected - 1];
|
|
2819
2828
|
};
|
|
2820
2829
|
return SingleChoiceVoting;
|
|
2821
2830
|
}());
|
|
2822
2831
|
|
|
2832
|
+
function isValidChoice$1(voteChoice, proposalChoices) {
|
|
2833
|
+
return (Array.isArray(voteChoice) &&
|
|
2834
|
+
// If voteChoice index is not in proposalChoices, return false
|
|
2835
|
+
voteChoice.every(function (choice) { return (proposalChoices === null || proposalChoices === void 0 ? void 0 : proposalChoices[choice - 1]) !== undefined; }) &&
|
|
2836
|
+
// If any voteChoice is duplicated, return false
|
|
2837
|
+
voteChoice.length === new Set(voteChoice).size &&
|
|
2838
|
+
// If voteChoice is empty, return false
|
|
2839
|
+
voteChoice.length > 0);
|
|
2840
|
+
}
|
|
2823
2841
|
var ApprovalVoting = /** @class */ (function () {
|
|
2824
2842
|
function ApprovalVoting(proposal, votes, strategies, selected) {
|
|
2825
2843
|
this.proposal = proposal;
|
|
@@ -2827,26 +2845,32 @@ var ApprovalVoting = /** @class */ (function () {
|
|
|
2827
2845
|
this.strategies = strategies;
|
|
2828
2846
|
this.selected = selected;
|
|
2829
2847
|
}
|
|
2830
|
-
ApprovalVoting.prototype.
|
|
2848
|
+
ApprovalVoting.prototype.getValidVotes = function () {
|
|
2849
|
+
var _this = this;
|
|
2850
|
+
return this.votes.filter(function (vote) {
|
|
2851
|
+
return isValidChoice$1(vote.choice, _this.proposal.choices);
|
|
2852
|
+
});
|
|
2853
|
+
};
|
|
2854
|
+
ApprovalVoting.prototype.getScores = function () {
|
|
2831
2855
|
var _this = this;
|
|
2832
2856
|
return this.proposal.choices.map(function (choice, i) {
|
|
2833
|
-
return _this.
|
|
2857
|
+
return _this.getValidVotes()
|
|
2834
2858
|
.filter(function (vote) { return vote.choice.includes(i + 1); })
|
|
2835
2859
|
.reduce(function (a, b) { return a + b.balance; }, 0);
|
|
2836
2860
|
});
|
|
2837
2861
|
};
|
|
2838
|
-
ApprovalVoting.prototype.
|
|
2862
|
+
ApprovalVoting.prototype.getScoresByStrategy = function () {
|
|
2839
2863
|
var _this = this;
|
|
2840
2864
|
return this.proposal.choices.map(function (choice, i) {
|
|
2841
2865
|
return _this.strategies.map(function (strategy, sI) {
|
|
2842
|
-
return _this.
|
|
2866
|
+
return _this.getValidVotes()
|
|
2843
2867
|
.filter(function (vote) { return vote.choice.includes(i + 1); })
|
|
2844
2868
|
.reduce(function (a, b) { return a + b.scores[sI]; }, 0);
|
|
2845
2869
|
});
|
|
2846
2870
|
});
|
|
2847
2871
|
};
|
|
2848
|
-
ApprovalVoting.prototype.
|
|
2849
|
-
return this.
|
|
2872
|
+
ApprovalVoting.prototype.getScoresTotal = function () {
|
|
2873
|
+
return this.getValidVotes().reduce(function (a, b) { return a + b.balance; }, 0);
|
|
2850
2874
|
};
|
|
2851
2875
|
ApprovalVoting.prototype.getChoiceString = function () {
|
|
2852
2876
|
var _this = this;
|
|
@@ -2859,6 +2883,17 @@ var ApprovalVoting = /** @class */ (function () {
|
|
|
2859
2883
|
return ApprovalVoting;
|
|
2860
2884
|
}());
|
|
2861
2885
|
|
|
2886
|
+
function isValidChoice$2(voteChoice, proposalChoices) {
|
|
2887
|
+
return (typeof voteChoice === 'object' &&
|
|
2888
|
+
!Array.isArray(voteChoice) &&
|
|
2889
|
+
voteChoice !== null &&
|
|
2890
|
+
// If voteChoice object keys are not in choices, return false
|
|
2891
|
+
Object.keys(voteChoice).every(function (key) { return (proposalChoices === null || proposalChoices === void 0 ? void 0 : proposalChoices[Number(key) - 1]) !== undefined; }) &&
|
|
2892
|
+
// If voteChoice object is empty, return false
|
|
2893
|
+
Object.keys(voteChoice).length > 0 &&
|
|
2894
|
+
// If voteChoice object values are not a positive integer, return false
|
|
2895
|
+
Object.values(voteChoice).every(function (value) { return typeof value === 'number' && value > 0; }));
|
|
2896
|
+
}
|
|
2862
2897
|
function percentageOfTotal(i, values, total) {
|
|
2863
2898
|
var reducedTotal = total.reduce(function (a, b) { return a + b; }, 0);
|
|
2864
2899
|
var percent = (values[i] / reducedTotal) * 100;
|
|
@@ -2867,32 +2902,38 @@ function percentageOfTotal(i, values, total) {
|
|
|
2867
2902
|
function quadraticMath(i, choice, balance) {
|
|
2868
2903
|
return Math.sqrt((percentageOfTotal(i + 1, choice, Object.values(choice)) / 100) * balance);
|
|
2869
2904
|
}
|
|
2870
|
-
var
|
|
2871
|
-
function
|
|
2905
|
+
var QuadraticVoting = /** @class */ (function () {
|
|
2906
|
+
function QuadraticVoting(proposal, votes, strategies, selected) {
|
|
2872
2907
|
this.proposal = proposal;
|
|
2873
2908
|
this.votes = votes;
|
|
2874
2909
|
this.strategies = strategies;
|
|
2875
2910
|
this.selected = selected;
|
|
2876
2911
|
}
|
|
2877
|
-
|
|
2912
|
+
QuadraticVoting.prototype.getValidVotes = function () {
|
|
2913
|
+
var _this = this;
|
|
2914
|
+
return this.votes.filter(function (vote) {
|
|
2915
|
+
return isValidChoice$2(vote.choice, _this.proposal.choices);
|
|
2916
|
+
});
|
|
2917
|
+
};
|
|
2918
|
+
QuadraticVoting.prototype.getScores = function () {
|
|
2878
2919
|
var _this = this;
|
|
2879
2920
|
var results = this.proposal.choices
|
|
2880
2921
|
.map(function (choice, i) {
|
|
2881
|
-
return _this.
|
|
2922
|
+
return _this.getValidVotes()
|
|
2882
2923
|
.map(function (vote) { return quadraticMath(i, vote.choice, vote.balance); })
|
|
2883
2924
|
.reduce(function (a, b) { return a + b; }, 0);
|
|
2884
2925
|
})
|
|
2885
2926
|
.map(function (sqrt) { return sqrt * sqrt; });
|
|
2886
2927
|
return results
|
|
2887
2928
|
.map(function (res, i) { return percentageOfTotal(i, results, results); })
|
|
2888
|
-
.map(function (p) { return (_this.
|
|
2929
|
+
.map(function (p) { return (_this.getScoresTotal() / 100) * p; });
|
|
2889
2930
|
};
|
|
2890
|
-
|
|
2931
|
+
QuadraticVoting.prototype.getScoresByStrategy = function () {
|
|
2891
2932
|
var _this = this;
|
|
2892
2933
|
var results = this.proposal.choices
|
|
2893
2934
|
.map(function (choice, i) {
|
|
2894
2935
|
return _this.strategies.map(function (strategy, sI) {
|
|
2895
|
-
return _this.
|
|
2936
|
+
return _this.getValidVotes()
|
|
2896
2937
|
.map(function (vote) { return quadraticMath(i, vote.choice, vote.scores[sI]); })
|
|
2897
2938
|
.reduce(function (a, b) { return a + b; }, 0);
|
|
2898
2939
|
});
|
|
@@ -2900,16 +2941,17 @@ var ApprovalVoting$1 = /** @class */ (function () {
|
|
|
2900
2941
|
.map(function (arr) { return arr.map(function (sqrt) { return [sqrt * sqrt]; }); });
|
|
2901
2942
|
return results.map(function (res, i) {
|
|
2902
2943
|
return _this.strategies
|
|
2903
|
-
.map(function (strategy, sI) {
|
|
2904
|
-
percentageOfTotal(0, results[i][sI], results.flat(2))
|
|
2905
|
-
|
|
2906
|
-
.map(function (p) { return [(_this.
|
|
2944
|
+
.map(function (strategy, sI) {
|
|
2945
|
+
return percentageOfTotal(0, results[i][sI], results.flat(2));
|
|
2946
|
+
})
|
|
2947
|
+
.map(function (p) { return [(_this.getScoresTotal() / 100) * p]; })
|
|
2948
|
+
.flat();
|
|
2907
2949
|
});
|
|
2908
2950
|
};
|
|
2909
|
-
|
|
2910
|
-
return this.
|
|
2951
|
+
QuadraticVoting.prototype.getScoresTotal = function () {
|
|
2952
|
+
return this.getValidVotes().reduce(function (a, b) { return a + b.balance; }, 0);
|
|
2911
2953
|
};
|
|
2912
|
-
|
|
2954
|
+
QuadraticVoting.prototype.getChoiceString = function () {
|
|
2913
2955
|
var _this = this;
|
|
2914
2956
|
return this.proposal.choices
|
|
2915
2957
|
.map(function (choice, i) {
|
|
@@ -2920,19 +2962,34 @@ var ApprovalVoting$1 = /** @class */ (function () {
|
|
|
2920
2962
|
.filter(function (el) { return el != null; })
|
|
2921
2963
|
.join(', ');
|
|
2922
2964
|
};
|
|
2923
|
-
return
|
|
2965
|
+
return QuadraticVoting;
|
|
2924
2966
|
}());
|
|
2925
2967
|
|
|
2968
|
+
function isValidChoice$3(voteChoice, proposalChoices) {
|
|
2969
|
+
return (Array.isArray(voteChoice) &&
|
|
2970
|
+
// If voteChoice index is not in choices, return false
|
|
2971
|
+
voteChoice.every(function (voteChoice) { return (proposalChoices === null || proposalChoices === void 0 ? void 0 : proposalChoices[voteChoice - 1]) !== undefined; }) &&
|
|
2972
|
+
// If any voteChoice is duplicated, return false
|
|
2973
|
+
voteChoice.length === new Set(voteChoice).size &&
|
|
2974
|
+
// If voteChoice is empty, return false
|
|
2975
|
+
voteChoice.length > 0 &&
|
|
2976
|
+
// If not all proposalChoices are selected, return false
|
|
2977
|
+
// TODO: We should add support for pacial bailout in the future
|
|
2978
|
+
voteChoice.length === proposalChoices.length);
|
|
2979
|
+
}
|
|
2926
2980
|
function irv(ballots, rounds) {
|
|
2927
2981
|
var candidates = __spread(new Set(ballots.map(function (vote) { return vote[0]; }).flat()));
|
|
2928
2982
|
var votes = Object.entries(ballots.reduce(function (votes, _a, i, src) {
|
|
2929
2983
|
var _b = __read(_a, 1), v = _b[0];
|
|
2930
|
-
|
|
2931
|
-
|
|
2932
|
-
|
|
2984
|
+
var balance = src[i][1];
|
|
2985
|
+
votes[v[0]][0] += balance;
|
|
2986
|
+
var score = src[i][2];
|
|
2987
|
+
if (score.length > 1) {
|
|
2988
|
+
votes[v[0]][1] = score.map(function (s, sI) { return s + votes[v[0]][1][sI] || s; });
|
|
2989
|
+
}
|
|
2933
2990
|
else
|
|
2934
2991
|
votes[v[0]][1] = [
|
|
2935
|
-
votes[v[0]][1].concat(
|
|
2992
|
+
votes[v[0]][1].concat(score).reduce(function (a, b) { return a + b; }, 0)
|
|
2936
2993
|
];
|
|
2937
2994
|
return votes;
|
|
2938
2995
|
}, Object.assign.apply(Object, __spread([{}], candidates.map(function (c) {
|
|
@@ -2960,7 +3017,8 @@ function irv(ballots, rounds) {
|
|
|
2960
3017
|
round: rounds.length + 1,
|
|
2961
3018
|
sortedByHighest: sortedByHighest
|
|
2962
3019
|
});
|
|
2963
|
-
return topCount > totalPowerOfVotes / 2 ||
|
|
3020
|
+
return topCount > totalPowerOfVotes / 2 ||
|
|
3021
|
+
sortedByHighest.length < 3
|
|
2964
3022
|
? rounds
|
|
2965
3023
|
: irv(ballots
|
|
2966
3024
|
.map(function (ballot) { return [
|
|
@@ -2968,38 +3026,49 @@ function irv(ballots, rounds) {
|
|
|
2968
3026
|
ballot[1],
|
|
2969
3027
|
ballot[2]
|
|
2970
3028
|
]; })
|
|
2971
|
-
.filter(function (
|
|
3029
|
+
.filter(function (ballot) { return ballot[0].length > 0; }), rounds);
|
|
2972
3030
|
}
|
|
2973
|
-
function getFinalRound(
|
|
2974
|
-
var
|
|
2975
|
-
var finalRound =
|
|
2976
|
-
return finalRound.sortedByHighest
|
|
3031
|
+
function getFinalRound(votes) {
|
|
3032
|
+
var rounds = irv(votes.map(function (vote) { return [vote.choice, vote.balance, vote.scores]; }), []);
|
|
3033
|
+
var finalRound = rounds[rounds.length - 1];
|
|
3034
|
+
return finalRound.sortedByHighest;
|
|
2977
3035
|
}
|
|
2978
|
-
var
|
|
2979
|
-
function
|
|
3036
|
+
var RankedChoiceVoting = /** @class */ (function () {
|
|
3037
|
+
function RankedChoiceVoting(proposal, votes, strategies, selected) {
|
|
2980
3038
|
this.proposal = proposal;
|
|
2981
3039
|
this.votes = votes;
|
|
2982
3040
|
this.strategies = strategies;
|
|
2983
3041
|
this.selected = selected;
|
|
2984
3042
|
}
|
|
2985
|
-
|
|
3043
|
+
RankedChoiceVoting.prototype.getValidVotes = function () {
|
|
2986
3044
|
var _this = this;
|
|
3045
|
+
return this.votes.filter(function (vote) {
|
|
3046
|
+
return isValidChoice$3(vote.choice, _this.proposal.choices);
|
|
3047
|
+
});
|
|
3048
|
+
};
|
|
3049
|
+
RankedChoiceVoting.prototype.getScores = function () {
|
|
3050
|
+
var finalRound = getFinalRound(this.getValidVotes());
|
|
2987
3051
|
return this.proposal.choices.map(function (choice, i) {
|
|
2988
|
-
return
|
|
3052
|
+
return finalRound
|
|
3053
|
+
.filter(function (res) { return Number(res[0]) === i + 1; })
|
|
3054
|
+
.reduce(function (a, b) { return a + b[1][0]; }, 0);
|
|
2989
3055
|
});
|
|
2990
3056
|
};
|
|
2991
|
-
|
|
3057
|
+
RankedChoiceVoting.prototype.getScoresByStrategy = function () {
|
|
2992
3058
|
var _this = this;
|
|
3059
|
+
var finalRound = getFinalRound(this.getValidVotes());
|
|
2993
3060
|
return this.proposal.choices.map(function (choice, i) {
|
|
2994
3061
|
return _this.strategies.map(function (strategy, sI) {
|
|
2995
|
-
return
|
|
3062
|
+
return finalRound
|
|
3063
|
+
.filter(function (res) { return Number(res[0]) === i + 1; })
|
|
3064
|
+
.reduce(function (a, b) { return a + b[1][1][sI]; }, 0);
|
|
2996
3065
|
});
|
|
2997
3066
|
});
|
|
2998
3067
|
};
|
|
2999
|
-
|
|
3000
|
-
return this.
|
|
3068
|
+
RankedChoiceVoting.prototype.getScoresTotal = function () {
|
|
3069
|
+
return this.getScores().reduce(function (a, b) { return a + b; });
|
|
3001
3070
|
};
|
|
3002
|
-
|
|
3071
|
+
RankedChoiceVoting.prototype.getChoiceString = function () {
|
|
3003
3072
|
var _this = this;
|
|
3004
3073
|
return this.selected
|
|
3005
3074
|
.map(function (choice) {
|
|
@@ -3009,9 +3078,22 @@ var ApprovalVoting$2 = /** @class */ (function () {
|
|
|
3009
3078
|
.map(function (el, i) { return "(" + getNumberWithOrdinal(i + 1) + ") " + el; })
|
|
3010
3079
|
.join(', ');
|
|
3011
3080
|
};
|
|
3012
|
-
return
|
|
3081
|
+
return RankedChoiceVoting;
|
|
3013
3082
|
}());
|
|
3014
3083
|
|
|
3084
|
+
function isValidChoice$4(voteChoice, proposalChoices) {
|
|
3085
|
+
return (typeof voteChoice === 'object' &&
|
|
3086
|
+
!Array.isArray(voteChoice) &&
|
|
3087
|
+
voteChoice !== null &&
|
|
3088
|
+
// If voteChoice object keys are not in choices, return false
|
|
3089
|
+
Object.keys(voteChoice).every(function (key) { return (proposalChoices === null || proposalChoices === void 0 ? void 0 : proposalChoices[Number(key) - 1]) !== undefined; }) &&
|
|
3090
|
+
// If voteChoice object is empty, return false
|
|
3091
|
+
Object.keys(voteChoice).length > 0 &&
|
|
3092
|
+
// If voteChoice object values are not a positive integer, return false
|
|
3093
|
+
Object.values(voteChoice).every(function (value) { return typeof value === 'number' && value > 0; }) &&
|
|
3094
|
+
// If voteChoice is empty, return false
|
|
3095
|
+
Object.keys(voteChoice).length > 0);
|
|
3096
|
+
}
|
|
3015
3097
|
function percentageOfTotal$1(i, values, total) {
|
|
3016
3098
|
var reducedTotal = total.reduce(function (a, b) { return a + b; }, 0);
|
|
3017
3099
|
var percent = (values[i] / reducedTotal) * 100;
|
|
@@ -3027,23 +3109,29 @@ var WeightedVoting = /** @class */ (function () {
|
|
|
3027
3109
|
this.strategies = strategies;
|
|
3028
3110
|
this.selected = selected;
|
|
3029
3111
|
}
|
|
3030
|
-
WeightedVoting.prototype.
|
|
3112
|
+
WeightedVoting.prototype.getValidVotes = function () {
|
|
3113
|
+
var _this = this;
|
|
3114
|
+
return this.votes.filter(function (vote) {
|
|
3115
|
+
return isValidChoice$4(vote.choice, _this.proposal.choices);
|
|
3116
|
+
});
|
|
3117
|
+
};
|
|
3118
|
+
WeightedVoting.prototype.getScores = function () {
|
|
3031
3119
|
var _this = this;
|
|
3032
3120
|
var results = this.proposal.choices.map(function (choice, i) {
|
|
3033
|
-
return _this.
|
|
3121
|
+
return _this.getValidVotes()
|
|
3034
3122
|
.map(function (vote) { return weightedPower(i, vote.choice, vote.balance); })
|
|
3035
3123
|
.reduce(function (a, b) { return a + b; }, 0);
|
|
3036
3124
|
});
|
|
3037
3125
|
return results
|
|
3038
3126
|
.map(function (res, i) { return percentageOfTotal$1(i, results, results); })
|
|
3039
|
-
.map(function (p) { return (_this.
|
|
3127
|
+
.map(function (p) { return (_this.getScoresTotal() / 100) * p; });
|
|
3040
3128
|
};
|
|
3041
|
-
WeightedVoting.prototype.
|
|
3129
|
+
WeightedVoting.prototype.getScoresByStrategy = function () {
|
|
3042
3130
|
var _this = this;
|
|
3043
3131
|
var results = this.proposal.choices
|
|
3044
3132
|
.map(function (choice, i) {
|
|
3045
3133
|
return _this.strategies.map(function (strategy, sI) {
|
|
3046
|
-
return _this.
|
|
3134
|
+
return _this.getValidVotes()
|
|
3047
3135
|
.map(function (vote) { return weightedPower(i, vote.choice, vote.scores[sI]); })
|
|
3048
3136
|
.reduce(function (a, b) { return a + b; }, 0);
|
|
3049
3137
|
});
|
|
@@ -3051,14 +3139,15 @@ var WeightedVoting = /** @class */ (function () {
|
|
|
3051
3139
|
.map(function (arr) { return arr.map(function (pwr) { return [pwr]; }); });
|
|
3052
3140
|
return results.map(function (res, i) {
|
|
3053
3141
|
return _this.strategies
|
|
3054
|
-
.map(function (strategy, sI) {
|
|
3055
|
-
percentageOfTotal$1(0, results[i][sI], results.flat(2))
|
|
3056
|
-
|
|
3057
|
-
.map(function (p) { return [(_this.
|
|
3142
|
+
.map(function (strategy, sI) {
|
|
3143
|
+
return percentageOfTotal$1(0, results[i][sI], results.flat(2));
|
|
3144
|
+
})
|
|
3145
|
+
.map(function (p) { return [(_this.getScoresTotal() / 100) * p]; })
|
|
3146
|
+
.flat();
|
|
3058
3147
|
});
|
|
3059
3148
|
};
|
|
3060
|
-
WeightedVoting.prototype.
|
|
3061
|
-
return this.
|
|
3149
|
+
WeightedVoting.prototype.getScoresTotal = function () {
|
|
3150
|
+
return this.getValidVotes().reduce(function (a, b) { return a + b.balance; }, 0);
|
|
3062
3151
|
};
|
|
3063
3152
|
WeightedVoting.prototype.getChoiceString = function () {
|
|
3064
3153
|
var _this = this;
|
|
@@ -3077,8 +3166,8 @@ var WeightedVoting = /** @class */ (function () {
|
|
|
3077
3166
|
var voting = {
|
|
3078
3167
|
'single-choice': SingleChoiceVoting,
|
|
3079
3168
|
approval: ApprovalVoting,
|
|
3080
|
-
quadratic:
|
|
3081
|
-
'ranked-choice':
|
|
3169
|
+
quadratic: QuadraticVoting,
|
|
3170
|
+
'ranked-choice': RankedChoiceVoting,
|
|
3082
3171
|
weighted: WeightedVoting,
|
|
3083
3172
|
basic: SingleChoiceVoting
|
|
3084
3173
|
};
|