import { BigNumber } from "bignumber.js" ; import { promisify } from "es6-promisify" ; import { Address , Hash } from "../lib/commonTypes" ; import { DAO , NewDaoConfig } from "../lib/dao" ; import { ArcTransactionResult , DecodedLogEntryEvent , IContractWrapper , IContractWrapperFactory , IUniversalSchemeWrapper } from "../lib/iContractWrapperBase" ; import { ContributionRewardWrapper , } from "../lib/wrappers/contributionReward" ; import { ConfigService } from "../lib/configService" ; import { InitializeArcJs } from "../lib/index" ; import { ExecuteProposalEventResult , IIntVoteInterface } from "../lib/wrappers/iIntVoteInterface" ; import { IntVoteInterfaceWrapper , } from "../lib/wrappers/intVoteInterface" ; import { ProposalGeneratorBase , } from "../lib/proposalGeneratorBase" ; import { LoggingService , LogLevel } from "../lib/loggingService" ; import { Utils } from "../lib/utils" ; import { UtilsInternal } from "../lib/utilsInternal" ; import { DaoTokenWrapper } from "../lib/wrappers/daoToken" ; import { SchemeRegistrarFactory , SchemeRegistrarWrapper } from "../lib/wrappers/schemeRegistrar" ; import { WrapperService } from "../lib/wrapperService" ; const env = require ( "env-variable" ) ( ) ; export const NULL_HASH = Utils . NULL_HASH ; export const NULL_ADDRESS = Utils . NULL_ADDRESS ; export const SOME_HASH = "0x1000000000000000000000000000000000000000000000000000000000000000" ; export const SOME_ADDRESS = "0x1000000000000000000000000000000000000000" ; export const DefaultLogLevel = LogLevel . error | LogLevel . info ; ConfigService . set ( "logLevel" , DefaultLogLevel ) ; ConfigService . set ( "estimateGas" , true ) ; ConfigService . set ( "cacheContractWrappers" , true ) ; let testWeb3 ; let network ; const etherForEveryone = async ( ) : Promise < void > => { const count = accounts . length - 1 ; for ( let i = 0 ; i < count ; i ++ ) { await promisify ( ( callback ) : void => web3 . eth . sendTransaction ( { from : accounts [ accounts . length - 1 ] , to : accounts [ i ] , value : web3 . toWei ( 0.1 , "ether" ) , } , callback ) ) ( ) ; } } ; const genTokensForEveryone = async ( ) : Promise < void > => { const genToken = await DaoTokenWrapper . getGenToken ( ) ; accounts . forEach ( ( account ) => { genToken . contract . mint ( account , web3 . toWei ( 1000 ) ) ; } ) ; } ; let provider ; const setupForNonGanacheNet = ( ) : void => { const webConstructor = require ( "web3" ) ; let providerConfig ; console . log ( `providerConfig at: ${ env . arcjs_providerConfig }` ) ; providerConfig = require ( env . arcjs_providerConfig ) ; const HDWalletProvider = require ( "truffle-hdwallet-provider" ) ; console . log ( `Provider: '${ providerConfig . providerUrl }'` ) ; console . log ( `Account: '${ providerConfig . mnemonic }'` ) ; provider = new HDWalletProvider ( providerConfig . mnemonic , providerConfig . providerUrl ) ; ( global as any ) . web3 = new webConstructor ( provider ) ; } ; beforeEach ( async ( ) => { network = ( env . arcjs_network || "ganache" ) . toLowerCase ( ) ; if ( ! testWeb3 ) { if ( env . arcjs_providerConfig ) { setupForNonGanacheNet ( ) ; } ( global as any ) . web3 = testWeb3 = await InitializeArcJs ( ) ; } ( global as any ) . accounts = await promisify ( web3 . eth . getAccounts ) ( ) ; if ( network === "ganache" ) { await etherForEveryone ( ) ; await genTokensForEveryone ( ) ; } } ) ; after ( ( ) => { if ( provider ) { console . log ( "stopping provider engine..." ) ; provider . engine . stop ( ) ; } } ) ; export async function forgeDao ( opts = { } ) { const defaultFounders = [ { address : accounts [ 0 ] , reputation : web3 . toWei ( 1000 ) , tokens : web3 . toWei ( 100 ) , } , { address : accounts [ 1 ] , reputation : web3 . toWei ( 1000 ) , tokens : web3 . toWei ( 100 ) , } , { address : accounts [ 2 ] , reputation : web3 . toWei ( 1000 ) , tokens : web3 . toWei ( 100 ) , } , ] ; const defaultSchemes = [ { name : "SchemeRegistrar" } , { name : "UpgradeScheme" } , { name : "GlobalConstraintRegistrar" } , ] ; try { return await DAO . new ( Object . assign ( { founders : defaultFounders , name : opts . name || "ArcJsTestDao" , schemes : defaultSchemes , tokenName : opts . tokenName || "Tokens of ArcJsTestDao" , tokenSymbol : opts . tokenSymbol || "ATD" , } , opts ) ) ; } catch ( ex ) { throw new Error ( `error creating DAO: ${ ex . message ? ex . message : ex }` ) ; } } returns export async function addProposeContributionReward ( dao ) { const schemeRegistrar = await getDaoScheme ( dao , "SchemeRegistrar" , SchemeRegistrarFactory ) as SchemeRegistrarWrapper ; const contributionReward = await WrapperService . wrappers . ContributionReward ; const votingMachineHash = await getSchemeVotingMachineParametersHash ( dao , schemeRegistrar ) ; const votingMachine = await getSchemeVotingMachine ( dao , schemeRegistrar ) ; const schemeParametersHash = ( await contributionReward . setParameters ( { orgNativeTokenFee : "0" , voteParametersHash : votingMachineHash , votingMachineAddress : votingMachine . address , } ) ) . result ; const result = await schemeRegistrar . proposeToAddModifyScheme ( { avatar : dao . avatar . address , schemeAddress : contributionReward . address , schemeName : "ContributionReward" , schemeParametersHash , } ) ; const proposalId = await result . getProposalIdFromMinedTx ( ) ; await vote ( votingMachine , proposalId , 1 , accounts [ 1 ] ) ; return contributionReward ; } export function wrapperForContract ( contractName , address ? ) { return WrapperService . getContractWrapper ( contractName , address ) ; } export async function getSchemeVotingMachineParametersHash ( dao , scheme ) { return ( await scheme . getSchemeParameters ( dao . avatar . address ) ) . voteParametersHash ; } export async function getSchemeVotingMachine ( dao , scheme ) { return await scheme . getVotingMachine ( dao . avatar . address ) ; } export async function getVotingMachineParameters ( votingMachine , votingMachineParamsHash ) { /**
     * only works for originally-deployed voting machines
     */ const wrapper = wrapperForVotingMachine ( votingMachine ) ; return wrapper . getParameters ( votingMachineParamsHash ) ; } /**
 * vote for the proposal given by proposalId.
 */ export function vote ( votingMachine , proposalId , theVote , voter ) { voter = ( voter ? voter : accounts [ 0 ] ) ; return ( votingMachine as any ) . contract . vote ( proposalId , theVote , NULL_ADDRESS , { from : voter } ) ; } export function wrapperForVotingMachine ( votingMachine ) { return WrapperService . wrappersByAddress . get ( votingMachine . address ) as IUniversalSchemeWrapper ; } export async function voteWasExecuted ( votingMachine , proposalId ) { return new Promise ( ( resolve , reject ) : void => { const vmWrapper = wrapperForVotingMachine ( votingMachine ) as any ; const event = vmWrapper . ExecuteProposal ( { _proposalId : proposalId } , { fromBlock : 0 } ) ; event . get ( ( err , events ) : void => { if ( err ) { return reject ( err ) ; } resolve ( ( events . length === 1 ) && ( events [ 0 ] . args . _proposalId === proposalId ) ) ; } ) ; } ) ; } export async function increaseTime ( duration ) { if ( network === "ganache" ) { const id = new Date ( ) . getTime ( ) ; return new Promise ( ( resolve , reject ) : void => { web3 . currentProvider . sendAsync ( { id , jsonrpc : "2.0" , method : "evm_increaseTime" , params : [ duration ] , } , ( err1 ) => { if ( err1 ) { return reject ( err1 ) ; } web3 . currentProvider . sendAsync ( { id : id + 1 , jsonrpc : "2.0" , method : "evm_mine" , } , ( err2 , res ) : void => { return err2 ? reject ( err2 ) : resolve ( res ) ; } ) ; } ) ; } ) ; } } export async function waitForBlocks ( blocks ) { const currentBlock = await UtilsInternal . lastBlockNumber ( ) ; while ( ( await UtilsInternal . lastBlockNumber ( ) ) - currentBlock < blocks ) { } } export async function getDaoScheme ( dao , schemeName , factory ) { return factory . at ( ( await dao . getSchemes ( schemeName ) ) [ 0 ] . address ) ; } param { DAO } dao param { number } amount param { string } fromAddress param { string } token export function transferTokensToDao ( dao , amount , fromAddress , token ) { token = token ? token : dao . token ; token = token . contract ? token . contract : token ; return token . transfer ( dao . avatar . address , web3 . toWei ( amount ) , { from : fromAddress } ) ; } param { DAO } dao param { number } amount param { string } fromAddress export function transferEthToDao ( dao , amount , fromAddress ? ) { fromAddress = fromAddress || accounts [ 0 ] ; return web3 . eth . sendTransaction ( { from : fromAddress , to : dao . avatar . address , value : web3 . toWei ( amount ) } ) ; } export function sleep ( milliseconds ) { return UtilsInternal . sleep ( milliseconds ) ; } export function fromWei ( amount ) { const result = web3 . fromWei ( amount as any ) ; return web3 . toBigNumber ( result ) ; }