import { assert } from "chai" ; import { Address , DefaultSchemePermissions , Hash } from "../lib/commonTypes" ; import { DAO } from "../lib/dao" ; import { TransactionReceiptTruffle } from "../lib/transactionService" ; import { GlobalConstraintRegistrarFactory , GlobalConstraintRegistrarWrapper } from "../lib/wrappers/globalConstraintRegistrar" ; import { TokenCapGCFactory , TokenCapGCWrapper } from "../lib/wrappers/tokenCapGC" ; import { WrapperService } from "../lib/wrapperService" ; import * as helpers from "./helpers" ; describe ( "GlobalConstraintRegistrar" , ( ) => { let dao ; beforeEach ( async ( ) => { dao = await helpers . forgeDao ( ) ; } ) ; const addGlobalConstraint = async ( ) : Promise < { proposalId : Hash ; gcAddress : Address ; globalConstraintRegistrar : GlobalConstraintRegistrarWrapper ; tx : TransactionReceiptTruffle ; } > => { const tokenCapGC = WrapperService . wrappers . TokenCapGC ; const globalConstraintParametersHash = ( await tokenCapGC . setParameters ( { token : dao . token . address , cap : "3141" } ) ) . result ; const globalConstraintRegistrar = await helpers . getDaoScheme ( dao , "GlobalConstraintRegistrar" , GlobalConstraintRegistrarFactory ) as GlobalConstraintRegistrarWrapper ; const votingMachineHash = await helpers . getSchemeVotingMachineParametersHash ( dao , globalConstraintRegistrar ) ; const result = await globalConstraintRegistrar . proposeToAddModifyGlobalConstraint ( { avatar : dao . avatar . address , globalConstraint : tokenCapGC . address , globalConstraintParametersHash , votingMachineHash , } ) ; return { gcAddress : tokenCapGC . address , globalConstraintRegistrar , proposalId : await result . getProposalIdFromMinedTx ( ) , tx : await result . watchForTxMined ( ) , } ; } ; it ( "can get votable proposals" , async ( ) => { dao = await helpers . forgeDao ( { votingMachineParams : { votePerc : 90 , } , } ) ; const result = await addGlobalConstraint ( ) ; const globalConstraintRegistrar = result . globalConstraintRegistrar ; const proposalId = result . proposalId ; const proposals = await ( await globalConstraintRegistrar . getVotableAddGcProposals ( dao . avatar . address ) ) ( { _proposalId : proposalId } , { fromBlock : 0 } ) . get ( ) ; assert . equal ( proposals . length , 1 ) ; const proposal = proposals [ 0 ] ; assert . equal ( proposal . proposalId , proposalId ) ; assert . equal ( proposal . constraintAddress , result . gcAddress ) ; } ) ; it ( "returns the right permission" , async ( ) => { const gcr = await helpers . getDaoScheme ( dao , "GlobalConstraintRegistrar" , GlobalConstraintRegistrarFactory ) as GlobalConstraintRegistrarWrapper ; const perms = await gcr . getSchemePermissions ( dao . avatar . address ) ; assert . equal ( perms , DefaultSchemePermissions . GlobalConstraintRegistrar as any ) ; } ) ; it ( "can get added constraints" , async ( ) => { const result = await addGlobalConstraint ( ) ; const globalConstraintRegistrar = result . globalConstraintRegistrar ; const proposalId = result . proposalId ; const proposals = await globalConstraintRegistrar . NewGlobalConstraintsProposal ( { _gc : result . gcAddress , _proposalId : proposalId } , { fromBlock : 0 } ) . get ( ) ; assert . equal ( proposals . length , 1 ) ; const proposal = proposals [ 0 ] ; assert . equal ( proposal . args . _proposalId , proposalId ) ; } ) ; it ( "can get removed constraints" , async ( ) => { const result = await addGlobalConstraint ( ) ; const globalConstraintRegistrar = result . globalConstraintRegistrar ; const votingMachine = await helpers . getSchemeVotingMachine ( dao , globalConstraintRegistrar ) ; await helpers . vote ( votingMachine , result . proposalId , 1 , accounts [ 1 ] ) ; const removeResult = await globalConstraintRegistrar . proposeToRemoveGlobalConstraint ( { avatar : dao . avatar . address , globalConstraintAddress : result . gcAddress , } ) ; const proposalId = await removeResult . getProposalIdFromMinedTx ( ) ; const proposals = await globalConstraintRegistrar . RemoveGlobalConstraintsProposal ( { _gc : result . gcAddress , _proposalId : proposalId } , { fromBlock : 0 } ) . get ( ) ; assert . equal ( proposals . length , 1 ) ; const proposal = proposals [ 0 ] ; assert . equal ( proposal . args . _proposalId , proposalId ) ; } ) ; it ( "can remove constraints" , async ( ) => { const result = await addGlobalConstraint ( ) ; const globalConstraintRegistrar = result . globalConstraintRegistrar ; const votingMachine = await helpers . getSchemeVotingMachine ( dao , globalConstraintRegistrar ) ; await helpers . vote ( votingMachine , result . proposalId , 1 , accounts [ 1 ] ) ; assert . equal ( ( await dao . controller . globalConstraintsCount ( dao . avatar . address ) ) [ 1 ] . toNumber ( ) , 1 ) ; const removeResult = await globalConstraintRegistrar . proposeToRemoveGlobalConstraint ( { avatar : dao . avatar . address , globalConstraintAddress : result . gcAddress , } ) ; await helpers . vote ( votingMachine , await removeResult . getProposalIdFromMinedTx ( ) , 1 , accounts [ 1 ] ) ; assert . equal ( ( await dao . controller . globalConstraintsCount ( dao . avatar . address ) ) [ 1 ] . toNumber ( ) , 0 ) ; } ) ; it ( "should satisfy a number of basic checks" , async ( ) => { const gcr = await helpers . getDaoScheme ( dao , "GlobalConstraintRegistrar" , GlobalConstraintRegistrarFactory ) as GlobalConstraintRegistrarWrapper ; assert . equal ( await dao . isSchemeRegistered ( gcr . address ) , true ) ; assert . equal ( ( await dao . controller . globalConstraintsCount ( dao . avatar . address ) ) [ 1 ] . toNumber ( ) , 0 ) ; const tokenCapGC = await TokenCapGCFactory . new ( ) ; const tokenCapParams = { token : dao . token . address , cap : "3141" } ; const tokenCapGCParamsHash = ( await tokenCapGC . setParameters ( tokenCapParams ) ) . result ; const votingMachineHash = tokenCapGCParamsHash ; const parametersForGCR = await dao . controller . getSchemeParameters ( gcr . address , dao . avatar . address ) ; const parametersForVotingInGCR = await gcr . contract . parameters ( parametersForGCR ) ; const gcrPermissionsOnOrg = await dao . controller . getSchemePermissions ( gcr . address , dao . avatar . address ) ; assert . equal ( gcrPermissionsOnOrg , DefaultSchemePermissions . GlobalConstraintRegistrar ) ; const votingMachine = await helpers . getSchemeVotingMachine ( dao , gcr ) ; assert . equal ( votingMachine . address , parametersForVotingInGCR [ 1 ] ) ; const voteRegisterParams = await helpers . getVotingMachineParameters ( votingMachine , parametersForVotingInGCR [ 0 ] ) ; const msg = "These parameters are not known the voting machine..." ; assert . notEqual ( voteRegisterParams [ 0 ] , "0x0000000000000000000000000000000000000000" , msg ) ; const result = await gcr . proposeToAddModifyGlobalConstraint ( { avatar : dao . avatar . address , globalConstraint : tokenCapGC . address , globalConstraintParametersHash : tokenCapGCParamsHash , votingMachineHash , } ) ; const proposalId = await result . getProposalIdFromMinedTx ( ) ; await helpers . vote ( votingMachine , proposalId , 1 , accounts [ 1 ] ) ; assert . equal ( ( await dao . controller . globalConstraintsCount ( dao . avatar . address ) ) [ 1 ] . toNumber ( ) , 1 ) ; const tokenWrapper = await WrapperService . factories . TokenCapGC . at ( tokenCapGC . address ) as TokenCapGCWrapper ; const paramsHash = await tokenWrapper . getRegisteredParametersHash ( dao . avatar . address ) ; assert . equal ( paramsHash , tokenCapGCParamsHash ) ; const params = await tokenWrapper . getRegisteredParameters ( dao . avatar . address ) ; assert . equal ( params . cap . toString ( ) , tokenCapParams . cap ) ; assert . equal ( params . token , tokenCapParams . token ) ; } ) ; } ) ;