UNPKG

@polkassembly/util

Version:

Set of utility functions for Polkassembly and more.

308 lines (307 loc) 21.8 kB
"use strict"; // Copyright 2019-2020 @paritytech/polkassembly authors & contributors // This software may be modified and distributed under the terms // of the Apache-2.0 license. See the LICENSE file for details. var __assign = (this && this.__assign) || function () { __assign = Object.assign || function(t) { for (var s, i = 1, n = arguments.length; i < n; i++) { s = arguments[i]; for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p]; } return t; }; return __assign.apply(this, arguments); }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); var bn_js_1 = __importDefault(require("bn.js")); var getThresholds_1 = require("../getThresholds"); var types_1 = require("../types"); var ZERO = new bn_js_1.default(0); var ONE = new bn_js_1.default(1); describe('getPassingThreshold', function () { it('returns 1 when there are no nays, for any voteThreshold', function () { var _a, _b, _c; // with 0 nay with conviction of 0 and 10 total issuance // any number of aye would mean it passes, getPassing Threshold answers 1 var common = { nays: ZERO, naysWithoutConviction: ZERO, totalIssuance: new bn_js_1.default(10) }; var res = getThresholds_1.getPassingThreshold(__assign(__assign({}, common), { threshold: types_1.VoteThresholdEnum.Supermajorityapproval })); expect(res.isValid).toBe(true); expect((_a = res.passingThreshold) === null || _a === void 0 ? void 0 : _a.eq(ONE)).toBe(true); res = getThresholds_1.getPassingThreshold(__assign(__assign({}, common), { threshold: types_1.VoteThresholdEnum.Simplemajority })); expect(res.isValid).toBe(true); expect((_b = res.passingThreshold) === null || _b === void 0 ? void 0 : _b.eq(ONE)).toBe(true); res = getThresholds_1.getPassingThreshold(__assign(__assign({}, common), { threshold: types_1.VoteThresholdEnum.Supermajorityrejection })); expect(res.isValid).toBe(true); expect((_c = res.passingThreshold) === null || _c === void 0 ? void 0 : _c.eq(ONE)).toBe(true); }); it('passes with correct values with Supermajorityapproval', function () { var _a, _b; var totalIssuance = new bn_js_1.default(100); var threshold = types_1.VoteThresholdEnum.Supermajorityapproval; // with 3 nays with conviction of 1 // Looking for x (number of aye with conviction of 1): 3/srqt(3 + x) = x/sqrt(100) => 8.75 -> 9 var nays = new bn_js_1.default(3); var naysWithoutConviction = new bn_js_1.default(3); var res = getThresholds_1.getPassingThreshold({ nays: nays, naysWithoutConviction: naysWithoutConviction, threshold: threshold, totalIssuance: totalIssuance }); expect(res.isValid).toBe(true); expect((_a = res.passingThreshold) === null || _a === void 0 ? void 0 : _a.toNumber()).toEqual(9); // with 49 (half of the total issuance) nays with conviction of 1 // Looking for x (number of aye with conviction of 1): 49/srqt(49 + x) = x/sqrt(100) => 49.3-> 50 nays = new bn_js_1.default(49); naysWithoutConviction = new bn_js_1.default(49); res = getThresholds_1.getPassingThreshold({ nays: nays, naysWithoutConviction: naysWithoutConviction, threshold: threshold, totalIssuance: totalIssuance }); expect(res.isValid).toBe(true); expect((_b = res.passingThreshold) === null || _b === void 0 ? void 0 : _b.toNumber()).toEqual(50); }); it('verifies the conviction is working as expected with Supermajorityapproval', function () { var _a, _b, _c; var totalIssuance = new bn_js_1.default(100); var threshold = types_1.VoteThresholdEnum.Supermajorityapproval; // with 30 nays with x1 conviction // Looking for x (number of aye with conviction of 1): 30/srqt(30 + x) = x/sqrt(100) => 36.72 -> 37 var naysWithoutConviction = new bn_js_1.default(30); var nays = new bn_js_1.default(30); var res = getThresholds_1.getPassingThreshold({ nays: nays, naysWithoutConviction: naysWithoutConviction, threshold: threshold, totalIssuance: totalIssuance }); expect(res.isValid).toBe(true); expect((_a = res.passingThreshold) === null || _a === void 0 ? void 0 : _a.toNumber()).toEqual(37); // with 30 nays and x0.1 conviction // Looking for x (number of aye with conviction of 1): 3/srqt(30 + x) = x/sqrt(100) => 5.06-> 6 naysWithoutConviction = new bn_js_1.default(30); nays = new bn_js_1.default(3); res = getThresholds_1.getPassingThreshold({ nays: nays, naysWithoutConviction: naysWithoutConviction, threshold: threshold, totalIssuance: totalIssuance }); expect(res.isValid).toBe(true); expect((_b = res.passingThreshold) === null || _b === void 0 ? void 0 : _b.toNumber()).toEqual(6); // with 30 nays with 2x conviction // Looking for x (number of aye with conviction of 1): 60/srqt(30 + x) = x/sqrt(100) => 62.41-> 63 naysWithoutConviction = new bn_js_1.default(30); nays = new bn_js_1.default(60); res = getThresholds_1.getPassingThreshold({ nays: nays, naysWithoutConviction: naysWithoutConviction, threshold: threshold, totalIssuance: totalIssuance }); expect(res.isValid).toBe(true); expect((_c = res.passingThreshold) === null || _c === void 0 ? void 0 : _c.toNumber()).toEqual(63); }); it('passes with correct values with Simplemajority', function () { var _a, _b; var totalIssuance = new bn_js_1.default(100); var threshold = types_1.VoteThresholdEnum.Simplemajority; var nays = new bn_js_1.default(3); var naysWithoutConviction = new bn_js_1.default(3); // with 3 nays with conviction of 1 var res = getThresholds_1.getPassingThreshold({ nays: nays, naysWithoutConviction: naysWithoutConviction, threshold: threshold, totalIssuance: totalIssuance }); expect(res.isValid).toBe(true); expect((_a = res.passingThreshold) === null || _a === void 0 ? void 0 : _a.toNumber()).toEqual(3); // with 49 (almost half of the total issuance) nays with conviction of 1 nays = new bn_js_1.default(49); naysWithoutConviction = new bn_js_1.default(49); res = getThresholds_1.getPassingThreshold({ nays: nays, naysWithoutConviction: naysWithoutConviction, threshold: threshold, totalIssuance: totalIssuance }); expect(res.isValid).toBe(true); expect((_b = res.passingThreshold) === null || _b === void 0 ? void 0 : _b.toNumber()).toEqual(49); }); it('passes with correct values with Supermajorityrejection', function () { var _a, _b; var totalIssuance = new bn_js_1.default(100); var threshold = types_1.VoteThresholdEnum.Supermajorityrejection; // with 5 nays with conviction of 5 // Looking for x (number of aye with conviction of 1): 5/sqrt(100) = x/srqt(5 + x) => 1.25 -> 1 var nays = new bn_js_1.default(5); var naysWithoutConviction = new bn_js_1.default(5); var res = getThresholds_1.getPassingThreshold({ nays: nays, naysWithoutConviction: naysWithoutConviction, threshold: threshold, totalIssuance: totalIssuance }); expect(res.isValid).toBe(true); expect((_a = res.passingThreshold) === null || _a === void 0 ? void 0 : _a.toNumber()).toEqual(1); // with 49 (half of the total issuance) nays with conviction of 1 // Looking for x (number of aye with conviction of 1): 49/srqt(49 + x) = x/sqrt(100) => 48.3-> 49 nays = new bn_js_1.default(49); naysWithoutConviction = new bn_js_1.default(49); res = getThresholds_1.getPassingThreshold({ nays: nays, naysWithoutConviction: naysWithoutConviction, threshold: threshold, totalIssuance: totalIssuance }); expect(res.isValid).toBe(true); expect((_b = res.passingThreshold) === null || _b === void 0 ? void 0 : _b.toNumber()).toEqual(48); }); it('verifies the conviction is working as expected with Supermajorityrejection', function () { var _a, _b, _c; var totalIssuance = new bn_js_1.default(100); var threshold = types_1.VoteThresholdEnum.Supermajorityrejection; // with 30 nays with x1 conviction // Looking for x (number of aye with conviction of 1): 30/srqt(100) = x/sqrt(30 + x) => 21.5 -> 21 var naysWithoutConviction = new bn_js_1.default(30); var nays = new bn_js_1.default(30); var res = getThresholds_1.getPassingThreshold({ nays: nays, naysWithoutConviction: naysWithoutConviction, threshold: threshold, totalIssuance: totalIssuance }); expect(res.isValid).toBe(true); expect((_a = res.passingThreshold) === null || _a === void 0 ? void 0 : _a.toNumber()).toEqual(21); // with 30 nays and x0.1 conviction // Looking for x (number of aye with conviction of 1): 3/srqt(100) = x/sqrt(30 + x) => 1.6 -> 1 naysWithoutConviction = new bn_js_1.default(30); nays = new bn_js_1.default(3); res = getThresholds_1.getPassingThreshold({ nays: nays, naysWithoutConviction: naysWithoutConviction, threshold: threshold, totalIssuance: totalIssuance }); expect(res.isValid).toBe(true); expect((_b = res.passingThreshold) === null || _b === void 0 ? void 0 : _b.toNumber()).toEqual(1); // with 30 nays with 2x conviction // Looking for x (number of aye with conviction of 1): 60/srqt(100) = x/sqrt(30 + x) => 55.4 -> 55 naysWithoutConviction = new bn_js_1.default(30); nays = new bn_js_1.default(60); res = getThresholds_1.getPassingThreshold({ nays: nays, naysWithoutConviction: naysWithoutConviction, threshold: threshold, totalIssuance: totalIssuance }); expect(res.isValid).toBe(true); expect((_c = res.passingThreshold) === null || _c === void 0 ? void 0 : _c.toNumber()).toEqual(55); }); it('fails with any threshold if there are more nays than half of the total issuance', function () { var nays = new bn_js_1.default(51); var naysWithoutConviction = new bn_js_1.default(51); var threshold = types_1.VoteThresholdEnum.Supermajorityapproval; var totalIssuance = new bn_js_1.default(100); var res = getThresholds_1.getPassingThreshold({ nays: nays, naysWithoutConviction: naysWithoutConviction, threshold: threshold, totalIssuance: totalIssuance }); expect(res.isValid).toBe(false); expect(res.passingThreshold).toBeUndefined(); threshold = types_1.VoteThresholdEnum.Simplemajority; res = getThresholds_1.getPassingThreshold({ nays: nays, naysWithoutConviction: naysWithoutConviction, threshold: threshold, totalIssuance: totalIssuance }); expect(res.isValid).toBe(false); expect(res.passingThreshold).toBeUndefined(); threshold = types_1.VoteThresholdEnum.Supermajorityrejection; res = getThresholds_1.getPassingThreshold({ nays: nays, naysWithoutConviction: naysWithoutConviction, threshold: threshold, totalIssuance: totalIssuance }); expect(res.isValid).toBe(false); expect(res.passingThreshold).toBeUndefined(); }); }); describe('getFailingThreshold', function () { it('returns 1 when there are no ayes, for any voteThreshold', function () { var _a, _b, _c; // with 0 aye with conviction of 0 and 10 total issuance // any number of aye would mean it passes, getPassing Threshold answers 1 var common = { ayes: ZERO, ayesWithoutConviction: ZERO, totalIssuance: new bn_js_1.default(10) }; var res = getThresholds_1.getFailingThreshold(__assign(__assign({}, common), { threshold: types_1.VoteThresholdEnum.Supermajorityapproval })); expect(res.isValid).toBe(true); expect((_a = res.failingThreshold) === null || _a === void 0 ? void 0 : _a.eq(ONE)).toBe(true); res = getThresholds_1.getFailingThreshold(__assign(__assign({}, common), { threshold: types_1.VoteThresholdEnum.Simplemajority })); expect(res.isValid).toBe(true); expect((_b = res.failingThreshold) === null || _b === void 0 ? void 0 : _b.eq(ONE)).toBe(true); res = getThresholds_1.getFailingThreshold(__assign(__assign({}, common), { threshold: types_1.VoteThresholdEnum.Supermajorityrejection })); expect(res.isValid).toBe(true); expect((_c = res.failingThreshold) === null || _c === void 0 ? void 0 : _c.eq(ONE)).toBe(true); }); it('passes with correct values with Supermajorityapproval', function () { var _a, _b; var totalIssuance = new bn_js_1.default(100); var threshold = types_1.VoteThresholdEnum.Supermajorityapproval; // with 3 ayes with conviction of 1 // Looking for x (number of nay with conviction of 1): x/srqt(3 + x) = 3/sqrt(100) => 1.25 -> 1 var ayes = new bn_js_1.default(5); var ayesWithoutConviction = new bn_js_1.default(5); var res = getThresholds_1.getFailingThreshold({ ayes: ayes, ayesWithoutConviction: ayesWithoutConviction, threshold: threshold, totalIssuance: totalIssuance }); expect(res.isValid).toBe(true); expect((_a = res.failingThreshold) === null || _a === void 0 ? void 0 : _a.toNumber()).toEqual(1); // with 49 (half of the total issuance) ayes with conviction of 1 // Looking for x (number of nay with conviction of 1): x/srqt(49 + x) = 49/sqrt(100) => 48.3-> 48 ayes = new bn_js_1.default(49); ayesWithoutConviction = new bn_js_1.default(49); res = getThresholds_1.getFailingThreshold({ ayes: ayes, ayesWithoutConviction: ayesWithoutConviction, threshold: threshold, totalIssuance: totalIssuance }); expect(res.isValid).toBe(true); expect((_b = res.failingThreshold) === null || _b === void 0 ? void 0 : _b.toNumber()).toEqual(48); }); it('verifies the conviction is working as expected with Supermajorityapproval', function () { var _a, _b, _c; var totalIssuance = new bn_js_1.default(100); var threshold = types_1.VoteThresholdEnum.Supermajorityapproval; // with 30 ayes with x1 conviction // Looking for x (number of nay with conviction of 1): x/srqt(30 + x) = 30/sqrt(100) => 21.5 -> 21 var ayesWithoutConviction = new bn_js_1.default(30); var ayes = new bn_js_1.default(30); var res = getThresholds_1.getFailingThreshold({ ayes: ayes, ayesWithoutConviction: ayesWithoutConviction, threshold: threshold, totalIssuance: totalIssuance }); expect(res.isValid).toBe(true); expect((_a = res.failingThreshold) === null || _a === void 0 ? void 0 : _a.toNumber()).toEqual(21); // with 30 ayes and x0.1 conviction // Looking for x (number of nay with conviction of 1): x/srqt(30 + x) = 3/sqrt(100) => 1.6-> 1 ayesWithoutConviction = new bn_js_1.default(30); ayes = new bn_js_1.default(3); res = getThresholds_1.getFailingThreshold({ ayes: ayes, ayesWithoutConviction: ayesWithoutConviction, threshold: threshold, totalIssuance: totalIssuance }); expect(res.isValid).toBe(true); expect((_b = res.failingThreshold) === null || _b === void 0 ? void 0 : _b.toNumber()).toEqual(1); // with 30 ayes with 2x conviction // Looking for x (number of nay with conviction of 1): x/srqt(30 + x) = 60/sqrt(100) => 55.4-> 55 ayesWithoutConviction = new bn_js_1.default(30); ayes = new bn_js_1.default(60); res = getThresholds_1.getFailingThreshold({ ayes: ayes, ayesWithoutConviction: ayesWithoutConviction, threshold: threshold, totalIssuance: totalIssuance }); expect(res.isValid).toBe(true); expect((_c = res.failingThreshold) === null || _c === void 0 ? void 0 : _c.toNumber()).toEqual(55); }); it('passes with correct values with Simplemajority', function () { var _a, _b; var totalIssuance = new bn_js_1.default(100); var threshold = types_1.VoteThresholdEnum.Simplemajority; var ayes = new bn_js_1.default(3); var ayesWithoutConviction = new bn_js_1.default(3); // with 3 ayes with conviction of 1 var res = getThresholds_1.getFailingThreshold({ ayes: ayes, ayesWithoutConviction: ayesWithoutConviction, threshold: threshold, totalIssuance: totalIssuance }); expect(res.isValid).toBe(true); expect((_a = res.failingThreshold) === null || _a === void 0 ? void 0 : _a.toNumber()).toEqual(3); // with 49 (almost half of the total issuance) ayes with conviction of 1 ayes = new bn_js_1.default(49); ayesWithoutConviction = new bn_js_1.default(49); res = getThresholds_1.getFailingThreshold({ ayes: ayes, ayesWithoutConviction: ayesWithoutConviction, threshold: threshold, totalIssuance: totalIssuance }); expect(res.isValid).toBe(true); expect((_b = res.failingThreshold) === null || _b === void 0 ? void 0 : _b.toNumber()).toEqual(49); }); it('passes with correct values with Supermajorityrejection', function () { var _a, _b; var totalIssuance = new bn_js_1.default(100); var threshold = types_1.VoteThresholdEnum.Supermajorityrejection; // with 3 ayes with conviction of 3 // Looking for x (number of nay with conviction of 1): x/sqrt(100) = 3/sqrt(3+x) => 8.75 -> 9 var ayes = new bn_js_1.default(3); var ayesWithoutConviction = new bn_js_1.default(3); var res = getThresholds_1.getFailingThreshold({ ayes: ayes, ayesWithoutConviction: ayesWithoutConviction, threshold: threshold, totalIssuance: totalIssuance }); expect(res.isValid).toBe(true); expect((_a = res.failingThreshold) === null || _a === void 0 ? void 0 : _a.toNumber()).toEqual(9); // with 49 (half of the total issuance) ayes with conviction of 1 // Looking for x (number of nay with conviction of 1): x/sqrt(100) = 49/sqrt(49+x) => 49.3-> 50 ayes = new bn_js_1.default(49); ayesWithoutConviction = new bn_js_1.default(49); res = getThresholds_1.getFailingThreshold({ ayes: ayes, ayesWithoutConviction: ayesWithoutConviction, threshold: threshold, totalIssuance: totalIssuance }); expect(res.isValid).toBe(true); expect((_b = res.failingThreshold) === null || _b === void 0 ? void 0 : _b.toNumber()).toEqual(50); }); it('verifies the conviction is working as expected with Supermajorityrejection', function () { var _a, _b, _c; var totalIssuance = new bn_js_1.default(100); var threshold = types_1.VoteThresholdEnum.Supermajorityrejection; // with 30 ayes with x1 conviction // Looking for x (number of nay with conviction of 1): x/sqrt(100) = 30/sqrt(30+x) => 36.7 -> 37 var ayesWithoutConviction = new bn_js_1.default(30); var ayes = new bn_js_1.default(30); var res = getThresholds_1.getFailingThreshold({ ayes: ayes, ayesWithoutConviction: ayesWithoutConviction, threshold: threshold, totalIssuance: totalIssuance }); expect(res.isValid).toBe(true); expect((_a = res.failingThreshold) === null || _a === void 0 ? void 0 : _a.toNumber()).toEqual(37); // with 30 ayes and x0.1 conviction // Looking for x (number of nay with conviction of 1): x/sqrt(100) = 3/sqrt(30+x) => 5.06 -> 6 ayesWithoutConviction = new bn_js_1.default(30); ayes = new bn_js_1.default(3); res = getThresholds_1.getFailingThreshold({ ayes: ayes, ayesWithoutConviction: ayesWithoutConviction, threshold: threshold, totalIssuance: totalIssuance }); expect(res.isValid).toBe(true); expect((_b = res.failingThreshold) === null || _b === void 0 ? void 0 : _b.toNumber()).toEqual(6); // with 30 ayes with 2x conviction // Looking for x (number of nay with conviction of 1): x/sqrt(100) = 60/sqrt(30+x) => 62.41 -> 63 ayesWithoutConviction = new bn_js_1.default(30); ayes = new bn_js_1.default(60); res = getThresholds_1.getFailingThreshold({ ayes: ayes, ayesWithoutConviction: ayesWithoutConviction, threshold: threshold, totalIssuance: totalIssuance }); expect(res.isValid).toBe(true); expect((_c = res.failingThreshold) === null || _c === void 0 ? void 0 : _c.toNumber()).toEqual(63); }); it('fails with any threshold if there are more ayes than half of the total issuance', function () { // it can't fail in such a case. var ayes = new bn_js_1.default(51); var ayesWithoutConviction = new bn_js_1.default(51); var threshold = types_1.VoteThresholdEnum.Supermajorityapproval; var totalIssuance = new bn_js_1.default(100); var res = getThresholds_1.getFailingThreshold({ ayes: ayes, ayesWithoutConviction: ayesWithoutConviction, threshold: threshold, totalIssuance: totalIssuance }); expect(res.isValid).toBe(false); expect(res.failingThreshold).toBeUndefined(); threshold = types_1.VoteThresholdEnum.Simplemajority; res = getThresholds_1.getFailingThreshold({ ayes: ayes, ayesWithoutConviction: ayesWithoutConviction, threshold: threshold, totalIssuance: totalIssuance }); expect(res.isValid).toBe(false); expect(res.failingThreshold).toBeUndefined(); threshold = types_1.VoteThresholdEnum.Supermajorityrejection; res = getThresholds_1.getFailingThreshold({ ayes: ayes, ayesWithoutConviction: ayesWithoutConviction, threshold: threshold, totalIssuance: totalIssuance }); expect(res.isValid).toBe(false); expect(res.failingThreshold).toBeUndefined(); }); });