-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathranked.ts
136 lines (122 loc) · 3.63 KB
/
ranked.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
import { IVoteType, OffchainCensus } from '@vocdoni/sdk';
import chalk from 'chalk';
import { createElection, executeElection, getPlainCensus } from './utils/election-process';
/**
* Example for ranked election.
*
* For example: Sort your 5 favorite blockchains: Bitcoin:0, Ethereum:1, Monero:2, Zcash:3, Polkadot:4.
*
* Your first option gets 4 points,... the last 0 points.
*
* https://developer.vocdoni.io/protocol/ballot#linear-weighted-choice
*/
/**
* Defines the total number of random voters participating in the election.
* */
const VOTERS_NUM = 10;
/**
* The value should correspond to the number of choices of the given question.
* */
const MAX_COUNT = 5;
/**
* Specifies the maximum value that can be assigned to any single choice. For instance, if voters can rate
* options on a scale of 0 to 3, `MAX_VALUE` should be set to `4`.
* */
const MAX_VALUE = 4;
/**
* This must be set to true, unlike in the case of approval voting.
*/
const UNIQUE_CHOICES = true;
/**
* An array representing a voter's choices. Each entry in the array corresponds to a choice in the election,
* and the value assigned to each entry represents the voter's rating for that choice. The length of this array
* should match the total number of choices in the election (`MAX_COUNT`), and the value assigned to each entry
* should not exceed `MAX_VALUE`.
*
* With the following vote array `[2, 3, 0, 1, 4]` and with `10` voters, the expected result haves to be:
*
* ```
* [
* [ '0', '0', '10', '0', '0' ],
* [ '0', '0', '0', '10', '0' ],
* [ '10', '0', '0', '0', '0' ],
* [ '0', '10', '0', '0', '0' ],
* [ '0', '0', '0', '0', '10' ]
* ]
* ```
*
* This means:
*
* - For choice `0`, all 10 voters select it as `2` option,
* - For choice `1`, all 10 voters select it as `3` option,
* - For choice `2`, all 10 voters select it as `0` option,
* - For choice `3`, all 10 voters select it as `1` option,
* - For choice `4`, all 10 voters select it as `4` option,
*/
const VOTE_ARRAY = [2, 3, 0, 1, 4];
/**
* An example configuration for setting up a linear weighted system
*/
const VOTE_OPTIONS: IVoteType = {
uniqueChoices: UNIQUE_CHOICES,
costFromWeight: false,
maxCount: MAX_COUNT,
maxValue: MAX_VALUE,
maxTotalCost: 0,
};
const _createElection = (census: OffchainCensus) => {
const election = createElection(
census,
VOTE_OPTIONS,
'Sort your 5 favorite blockchains',
'Sort your 5 favorite blockchains'
);
election.addQuestion('Favourite blockchain', '', [
{
title: 'Bitcoin',
value: 0,
},
{
title: 'Ethereum',
value: 1,
},
{
title: 'Monero',
value: 2,
},
{
title: 'Zcash',
value: 3,
},
{
title: 'Polkadot',
value: 4,
},
]);
return election;
};
async function main() {
console.log('Creating census with some random wallets...');
const { census, participants } = getPlainCensus(VOTERS_NUM);
console.log('Creating election...');
const election = _createElection(census);
await executeElection(election, participants, VOTE_ARRAY);
// Calculate the results array depending on the parameters above
// This example only work if the VOTE_ARRAY is the same for all the voters
const result = VOTE_ARRAY.map((vote) => {
let arr = Array(MAX_COUNT).fill('0');
arr[vote] = VOTERS_NUM.toString();
return arr;
});
console.log('Expected results: ', result);
console.log(chalk.yellow('This results only work if the VOTE_ARRAY is the same for all voters'));
}
main()
.then(() => {
console.log(chalk.green('Done ✅'));
process.exit(0);
})
.catch((err) => {
console.error(err);
process.exit(1);
});