import BN from 'bn.js' ; import { getFailingThreshold , getPassingThreshold } from '../getThresholds' ; import { VoteThresholdEnum } from '../types' ; const ZERO = new BN ( 0 ) ; const ONE = new BN ( 1 ) ; describe ( 'getPassingThreshold' , ( ) => { it ( 'returns 1 when there are no nays, for any voteThreshold' , ( ) => { const common = { nays : ZERO , naysWithoutConviction : ZERO , totalIssuance : new BN ( 10 ) } ; let res = getPassingThreshold ( { ... common , threshold : VoteThresholdEnum . Supermajorityapproval } ) ; expect ( res . isValid ) . toBe ( true ) ; expect ( res . passingThreshold ?. eq ( ONE ) ) . toBe ( true ) ; res = getPassingThreshold ( { ... common , threshold : VoteThresholdEnum . Simplemajority } ) ; expect ( res . isValid ) . toBe ( true ) ; expect ( res . passingThreshold ?. eq ( ONE ) ) . toBe ( true ) ; res = getPassingThreshold ( { ... common , threshold : VoteThresholdEnum . Supermajorityrejection } ) ; expect ( res . isValid ) . toBe ( true ) ; expect ( res . passingThreshold ?. eq ( ONE ) ) . toBe ( true ) ; } ) ; it ( 'passes with correct values with Supermajorityapproval' , ( ) => { const totalIssuance = new BN ( 100 ) ; const threshold = VoteThresholdEnum . Supermajorityapproval ; let nays = new BN ( 3 ) ; let naysWithoutConviction = new BN ( 3 ) ; let res = getPassingThreshold ( { nays , naysWithoutConviction , threshold , totalIssuance } ) ; expect ( res . isValid ) . toBe ( true ) ; expect ( res . passingThreshold ?. toNumber ( ) ) . toEqual ( 9 ) ; nays = new BN ( 49 ) ; naysWithoutConviction = new BN ( 49 ) ; res = getPassingThreshold ( { nays , naysWithoutConviction , threshold , totalIssuance } ) ; expect ( res . isValid ) . toBe ( true ) ; expect ( res . passingThreshold ?. toNumber ( ) ) . toEqual ( 50 ) ; } ) ; it ( 'verifies the conviction is working as expected with Supermajorityapproval' , ( ) => { const totalIssuance = new BN ( 100 ) ; const threshold = VoteThresholdEnum . Supermajorityapproval ; let naysWithoutConviction = new BN ( 30 ) ; let nays = new BN ( 30 ) ; let res = getPassingThreshold ( { nays , naysWithoutConviction , threshold , totalIssuance } ) ; expect ( res . isValid ) . toBe ( true ) ; expect ( res . passingThreshold ?. toNumber ( ) ) . toEqual ( 37 ) ; naysWithoutConviction = new BN ( 30 ) ; nays = new BN ( 3 ) ; res = getPassingThreshold ( { nays , naysWithoutConviction , threshold , totalIssuance } ) ; expect ( res . isValid ) . toBe ( true ) ; expect ( res . passingThreshold ?. toNumber ( ) ) . toEqual ( 6 ) ; naysWithoutConviction = new BN ( 30 ) ; nays = new BN ( 60 ) ; res = getPassingThreshold ( { nays , naysWithoutConviction , threshold , totalIssuance } ) ; expect ( res . isValid ) . toBe ( true ) ; expect ( res . passingThreshold ?. toNumber ( ) ) . toEqual ( 63 ) ; } ) ; it ( 'passes with correct values with Simplemajority' , ( ) => { const totalIssuance = new BN ( 100 ) ; const threshold = VoteThresholdEnum . Simplemajority ; let nays = new BN ( 3 ) ; let naysWithoutConviction = new BN ( 3 ) ; let res = getPassingThreshold ( { nays , naysWithoutConviction , threshold , totalIssuance } ) ; expect ( res . isValid ) . toBe ( true ) ; expect ( res . passingThreshold ?. toNumber ( ) ) . toEqual ( 3 ) ; nays = new BN ( 49 ) ; naysWithoutConviction = new BN ( 49 ) ; res = getPassingThreshold ( { nays , naysWithoutConviction , threshold , totalIssuance } ) ; expect ( res . isValid ) . toBe ( true ) ; expect ( res . passingThreshold ?. toNumber ( ) ) . toEqual ( 49 ) ; } ) ; it ( 'passes with correct values with Supermajorityrejection' , ( ) => { const totalIssuance = new BN ( 100 ) ; const threshold = VoteThresholdEnum . Supermajorityrejection ; let nays = new BN ( 5 ) ; let naysWithoutConviction = new BN ( 5 ) ; let res = getPassingThreshold ( { nays , naysWithoutConviction , threshold , totalIssuance } ) ; expect ( res . isValid ) . toBe ( true ) ; expect ( res . passingThreshold ?. toNumber ( ) ) . toEqual ( 1 ) ; nays = new BN ( 49 ) ; naysWithoutConviction = new BN ( 49 ) ; res = getPassingThreshold ( { nays , naysWithoutConviction , threshold , totalIssuance } ) ; expect ( res . isValid ) . toBe ( true ) ; expect ( res . passingThreshold ?. toNumber ( ) ) . toEqual ( 48 ) ; } ) ; it ( 'verifies the conviction is working as expected with Supermajorityrejection' , ( ) => { const totalIssuance = new BN ( 100 ) ; const threshold = VoteThresholdEnum . Supermajorityrejection ; let naysWithoutConviction = new BN ( 30 ) ; let nays = new BN ( 30 ) ; let res = getPassingThreshold ( { nays , naysWithoutConviction , threshold , totalIssuance } ) ; expect ( res . isValid ) . toBe ( true ) ; expect ( res . passingThreshold ?. toNumber ( ) ) . toEqual ( 21 ) ; naysWithoutConviction = new BN ( 30 ) ; nays = new BN ( 3 ) ; res = getPassingThreshold ( { nays , naysWithoutConviction , threshold , totalIssuance } ) ; expect ( res . isValid ) . toBe ( true ) ; expect ( res . passingThreshold ?. toNumber ( ) ) . toEqual ( 1 ) ; naysWithoutConviction = new BN ( 30 ) ; nays = new BN ( 60 ) ; res = getPassingThreshold ( { nays , naysWithoutConviction , threshold , totalIssuance } ) ; expect ( res . isValid ) . toBe ( true ) ; expect ( res . passingThreshold ?. toNumber ( ) ) . toEqual ( 55 ) ; } ) ; it ( 'fails with any threshold if there are more nays than half of the total issuance' , ( ) => { const nays = new BN ( 51 ) ; const naysWithoutConviction = new BN ( 51 ) ; let threshold = VoteThresholdEnum . Supermajorityapproval ; const totalIssuance = new BN ( 100 ) ; let res = getPassingThreshold ( { nays , naysWithoutConviction , threshold , totalIssuance } ) ; expect ( res . isValid ) . toBe ( false ) ; expect ( res . passingThreshold ) . toBeUndefined ( ) ; threshold = VoteThresholdEnum . Simplemajority ; res = getPassingThreshold ( { nays , naysWithoutConviction , threshold , totalIssuance } ) ; expect ( res . isValid ) . toBe ( false ) ; expect ( res . passingThreshold ) . toBeUndefined ( ) ; threshold = VoteThresholdEnum . Supermajorityrejection ; res = getPassingThreshold ( { nays , naysWithoutConviction , threshold , totalIssuance } ) ; expect ( res . isValid ) . toBe ( false ) ; expect ( res . passingThreshold ) . toBeUndefined ( ) ; } ) ; } ) ; describe ( 'getFailingThreshold' , ( ) => { it ( 'returns 1 when there are no ayes, for any voteThreshold' , ( ) => { const common = { ayes : ZERO , ayesWithoutConviction : ZERO , totalIssuance : new BN ( 10 ) } ; let res = getFailingThreshold ( { ... common , threshold : VoteThresholdEnum . Supermajorityapproval } ) ; expect ( res . isValid ) . toBe ( true ) ; expect ( res . failingThreshold ?. eq ( ONE ) ) . toBe ( true ) ; res = getFailingThreshold ( { ... common , threshold : VoteThresholdEnum . Simplemajority } ) ; expect ( res . isValid ) . toBe ( true ) ; expect ( res . failingThreshold ?. eq ( ONE ) ) . toBe ( true ) ; res = getFailingThreshold ( { ... common , threshold : VoteThresholdEnum . Supermajorityrejection } ) ; expect ( res . isValid ) . toBe ( true ) ; expect ( res . failingThreshold ?. eq ( ONE ) ) . toBe ( true ) ; } ) ; it ( 'passes with correct values with Supermajorityapproval' , ( ) => { const totalIssuance = new BN ( 100 ) ; const threshold = VoteThresholdEnum . Supermajorityapproval ; let ayes = new BN ( 5 ) ; let ayesWithoutConviction = new BN ( 5 ) ; let res = getFailingThreshold ( { ayes , ayesWithoutConviction , threshold , totalIssuance } ) ; expect ( res . isValid ) . toBe ( true ) ; expect ( res . failingThreshold ?. toNumber ( ) ) . toEqual ( 1 ) ; ayes = new BN ( 49 ) ; ayesWithoutConviction = new BN ( 49 ) ; res = getFailingThreshold ( { ayes , ayesWithoutConviction , threshold , totalIssuance } ) ; expect ( res . isValid ) . toBe ( true ) ; expect ( res . failingThreshold ?. toNumber ( ) ) . toEqual ( 48 ) ; } ) ; it ( 'verifies the conviction is working as expected with Supermajorityapproval' , ( ) => { const totalIssuance = new BN ( 100 ) ; const threshold = VoteThresholdEnum . Supermajorityapproval ; let ayesWithoutConviction = new BN ( 30 ) ; let ayes = new BN ( 30 ) ; let res = getFailingThreshold ( { ayes , ayesWithoutConviction , threshold , totalIssuance } ) ; expect ( res . isValid ) . toBe ( true ) ; expect ( res . failingThreshold ?. toNumber ( ) ) . toEqual ( 21 ) ; ayesWithoutConviction = new BN ( 30 ) ; ayes = new BN ( 3 ) ; res = getFailingThreshold ( { ayes , ayesWithoutConviction , threshold , totalIssuance } ) ; expect ( res . isValid ) . toBe ( true ) ; expect ( res . failingThreshold ?. toNumber ( ) ) . toEqual ( 1 ) ; ayesWithoutConviction = new BN ( 30 ) ; ayes = new BN ( 60 ) ; res = getFailingThreshold ( { ayes , ayesWithoutConviction , threshold , totalIssuance } ) ; expect ( res . isValid ) . toBe ( true ) ; expect ( res . failingThreshold ?. toNumber ( ) ) . toEqual ( 55 ) ; } ) ; it ( 'passes with correct values with Simplemajority' , ( ) => { const totalIssuance = new BN ( 100 ) ; const threshold = VoteThresholdEnum . Simplemajority ; let ayes = new BN ( 3 ) ; let ayesWithoutConviction = new BN ( 3 ) ; let res = getFailingThreshold ( { ayes , ayesWithoutConviction , threshold , totalIssuance } ) ; expect ( res . isValid ) . toBe ( true ) ; expect ( res . failingThreshold ?. toNumber ( ) ) . toEqual ( 3 ) ; ayes = new BN ( 49 ) ; ayesWithoutConviction = new BN ( 49 ) ; res = getFailingThreshold ( { ayes , ayesWithoutConviction , threshold , totalIssuance } ) ; expect ( res . isValid ) . toBe ( true ) ; expect ( res . failingThreshold ?. toNumber ( ) ) . toEqual ( 49 ) ; } ) ; it ( 'passes with correct values with Supermajorityrejection' , ( ) => { const totalIssuance = new BN ( 100 ) ; const threshold = VoteThresholdEnum . Supermajorityrejection ; let ayes = new BN ( 3 ) ; let ayesWithoutConviction = new BN ( 3 ) ; let res = getFailingThreshold ( { ayes , ayesWithoutConviction , threshold , totalIssuance } ) ; expect ( res . isValid ) . toBe ( true ) ; expect ( res . failingThreshold ?. toNumber ( ) ) . toEqual ( 9 ) ; ayes = new BN ( 49 ) ; ayesWithoutConviction = new BN ( 49 ) ; res = getFailingThreshold ( { ayes , ayesWithoutConviction , threshold , totalIssuance } ) ; expect ( res . isValid ) . toBe ( true ) ; expect ( res . failingThreshold ?. toNumber ( ) ) . toEqual ( 50 ) ; } ) ; it ( 'verifies the conviction is working as expected with Supermajorityrejection' , ( ) => { const totalIssuance = new BN ( 100 ) ; const threshold = VoteThresholdEnum . Supermajorityrejection ; let ayesWithoutConviction = new BN ( 30 ) ; let ayes = new BN ( 30 ) ; let res = getFailingThreshold ( { ayes , ayesWithoutConviction , threshold , totalIssuance } ) ; expect ( res . isValid ) . toBe ( true ) ; expect ( res . failingThreshold ?. toNumber ( ) ) . toEqual ( 37 ) ; ayesWithoutConviction = new BN ( 30 ) ; ayes = new BN ( 3 ) ; res = getFailingThreshold ( { ayes , ayesWithoutConviction , threshold , totalIssuance } ) ; expect ( res . isValid ) . toBe ( true ) ; expect ( res . failingThreshold ?. toNumber ( ) ) . toEqual ( 6 ) ; ayesWithoutConviction = new BN ( 30 ) ; ayes = new BN ( 60 ) ; res = getFailingThreshold ( { ayes , ayesWithoutConviction , threshold , totalIssuance } ) ; expect ( res . isValid ) . toBe ( true ) ; expect ( res . failingThreshold ?. toNumber ( ) ) . toEqual ( 63 ) ; } ) ; it ( 'fails with any threshold if there are more ayes than half of the total issuance' , ( ) => { const ayes = new BN ( 51 ) ; const ayesWithoutConviction = new BN ( 51 ) ; let threshold = VoteThresholdEnum . Supermajorityapproval ; const totalIssuance = new BN ( 100 ) ; let res = getFailingThreshold ( { ayes , ayesWithoutConviction , threshold , totalIssuance } ) ; expect ( res . isValid ) . toBe ( false ) ; expect ( res . failingThreshold ) . toBeUndefined ( ) ; threshold = VoteThresholdEnum . Simplemajority ; res = getFailingThreshold ( { ayes , ayesWithoutConviction , threshold , totalIssuance } ) ; expect ( res . isValid ) . toBe ( false ) ; expect ( res . failingThreshold ) . toBeUndefined ( ) ; threshold = VoteThresholdEnum . Supermajorityrejection ; res = getFailingThreshold ( { ayes , ayesWithoutConviction , threshold , totalIssuance } ) ; expect ( res . isValid ) . toBe ( false ) ; expect ( res . failingThreshold ) . toBeUndefined ( ) ; } ) ; } ) ;