import { BigNumber } from "bignumber.js" ; import { assert } from "chai" ; import { BinaryVoteResult , Hash } from "../lib/commonTypes" ; import { DAO } from "../lib/dao" ; import { LoggingService } from "../lib/loggingService" ; import { TransactionReceiptTruffle , TransactionService } from "../lib/transactionService" ; import { Utils , Web3 } from "../lib/utils" ; import { ContributionRewardFactory , ContributionRewardWrapper } from "../lib/wrappers/contributionReward" ; import { GenesisProtocolWrapper } from "../lib/wrappers/genesisProtocol" ; import { WrapperService } from "../lib/wrapperService" ; import * as helpers from "./helpers" ; describe ( "estimate gas" , ( ) => { let dao ; let proposalId ; let scheme ; let network ; let web3 ; let stakingAmount ; let votingMachine ; const setup = async ( ) : Promise < void > => { if ( ! network ) { network = await Utils . getNetworkName ( ) ; LoggingService . info ( `running against network: ${ network }` ) ; } web3 = await Utils . getWeb3 ( ) ; stakingAmount = web3 . toWei ( "10" ) ; if ( ! dao ) { if ( network !== "Ganache" ) { const daoAddress = network === "Kovan" ? "0xae6ecbf473e550cca70d348c334ff6191d5bfab3" : "???" ; dao = await DAO . at ( daoAddress ) ; } else { dao = await helpers . forgeDao ( { schemes : [ { name : "ContributionReward" , votingMachineParams : { votingMachineName : "GenesisProtocol" , } , } , ] , } ) ; } assert . isOk ( dao ) ; LoggingService . info ( `DAO at: ${ dao . avatar . address }` ) ; } if ( ! scheme ) { scheme = await helpers . getDaoScheme ( dao , "ContributionReward" , ContributionRewardFactory ) as ContributionRewardWrapper ; assert . isOk ( scheme ) ; votingMachine = await WrapperService . factories . GenesisProtocol . at ( ( await scheme . getSchemeParameters ( dao . avatar . address ) ) . votingMachineAddress ) ; assert . isOk ( votingMachine ) ; } } ; beforeEach ( async ( ) => { await setup ( ) ; } ) ; it ( "can create a proposal" , async ( ) => { const gas = await scheme . estimateGas ( scheme . contract . proposeContributionReward , [ dao . avatar . address , helpers . SOME_HASH , "0" , [ "1" , "0" , "0" , 1 , 1 ] , helpers . SOME_ADDRESS , accounts [ 0 ] ] , { from : accounts [ 0 ] } ) ; LoggingService . info ( `estimated gas for creating a proposal: ${ gas }` ) ; ( scheme . constructor as any ) . synchronization_timeout = 0 ; const result = await scheme . contract . proposeContributionReward ( dao . avatar . address , helpers . SOME_HASH , "0" , [ "1" , "0" , "0" , 1 , 1 ] , helpers . SOME_ADDRESS , accounts [ 0 ] , { gas , from : accounts [ 0 ] } ) as TransactionReceiptTruffle ; proposalId = TransactionService . getValueFromLogs ( result , "_proposalId" ) ; LoggingService . info ( `Created proposal Id: ${ proposalId }` ) ; } ) ; it ( "can preapprove a stake transaction" , async ( ) => { LoggingService . info ( `accounts[0]: ${ accounts [ 0 ] }` ) ; const stakingToken = await votingMachine . getStakingToken ( ) ; const gas = await scheme . estimateGas ( stakingToken . contract . approve , [ votingMachine . address , stakingAmount ] , { from : accounts [ 0 ] } ) ; LoggingService . info ( `estimated gas for preapproving token transfer: ${ gas }` ) ; ( scheme . constructor as any ) . synchronization_timeout = 0 ; await stakingToken . contract . approve ( votingMachine . address , stakingAmount , { gas , from : accounts [ 0 ] } ) ; } ) ; it ( "can stake on a proposal" , async ( ) => { const gas = await scheme . estimateGas ( votingMachine . contract . stake , [ proposalId , 1 , stakingAmount ] , { from : accounts [ 0 ] } ) ; LoggingService . info ( `estimated gas for staking: ${ gas }` ) ; ( scheme . constructor as any ) . synchronization_timeout = 0 ; await votingMachine . contract . stake ( proposalId , 1 , stakingAmount , { gas , from : accounts [ 0 ] } ) ; } ) ; it ( "can vote on a proposal" , async ( ) => { const gas = await scheme . estimateGas ( votingMachine . contract . vote , [ proposalId , 1 , helpers . NULL_ADDRESS ] , { from : accounts [ 0 ] } ) ; LoggingService . info ( `estimated gas for voting: ${ gas }` ) ; ( scheme . constructor as any ) . synchronization_timeout = 0 ; await votingMachine . contract . vote ( proposalId , 1 , helpers . NULL_ADDRESS , { gas , from : accounts [ 0 ] } ) ; } ) ; it ( "can execute proposal" , async ( ) => { const gas = await scheme . estimateGas ( votingMachine . contract . execute , [ proposalId ] , { from : accounts [ 0 ] } ) ; LoggingService . info ( `estimated gas for executing proposal: ${ gas }` ) ; ( scheme . constructor as any ) . synchronization_timeout = 0 ; await votingMachine . contract . execute ( proposalId , { gas , from : accounts [ 0 ] } ) ; } ) ; it ( "can redeem proposal" , async ( ) => { if ( network === "Ganache" ) { await helpers . vote ( votingMachine , proposalId , BinaryVoteResult . Yes , accounts [ 1 ] ) ; } if ( network === "Ganache" ) { await helpers . increaseTime ( 1 ) ; } else { await helpers . waitForBlocks ( 2 ) ; } const gas = await scheme . estimateGas ( scheme . contract . redeemNativeToken , [ proposalId , dao . avatar . address , helpers . NULL_ADDRESS ] , { from : accounts [ 0 ] } ) ; LoggingService . info ( `estimated gas for redeeming native token: ${ gas }` ) ; ( scheme . constructor as any ) . synchronization_timeout = 0 ; await scheme . contract . redeemNativeToken ( proposalId , dao . avatar . address , { gas , from : accounts [ 0 ] } ) ; } ) ; } ) ;