import { Address , BigDecimal , BigInt , Bytes , Entity , store , Value } from '@graphprotocol/graph-ts' ; import { setContractsInfo , setTemplatesInfo } from '../contractsInfo' ; import { Transfer } from '../types/DAOToken/DAOToken' ; import { ExecuteProposal , GPExecuteProposal , Stake , StateChange , VoteProposal , } from '../types/GenesisProtocol/GenesisProtocol' ; import { Burn , Mint } from '../types/Reputation/Reputation' ; import { GenesisProtocolProposal , Proposal , ReputationContract , ReputationHolder } from '../types/schema' ; import { concat , equalsBytes , eventId , hexToAddress } from '../utils' ; import * as daoModule from './dao' ; import { addNewDAOEvent , addNewReputationHolderEvent , addProposalStateChangeEvent } from './event' ; import * as gpqueueModule from './gpqueue' ; import { getProposal , parseOutcome , saveProposal , updateCRProposal , updateGPProposal , updateGSMCProposal , updateGSProposal , updateProposalAfterVote , updateProposalconfidence , updateProposalExecution , updateProposalExecutionState , updateProposalState , updateSRProposal , } from './proposal' ; import { getReputation , insertReputation , updateReputationTotalSupply , } from './reputation' ; import { daoBountyRedemption , insertGPRewards , insertGPRewardsToHelper , reputationRedemption , tokenRedemption , } from './reward' ; import { insertStake } from './stake' ; import { getToken , insertToken , updateTokenTotalSupply } from './token' ; import { insertVote } from './vote' ; function isProposalValid ( proposalId ) { let p = Proposal . load ( proposalId ) ; return ( ( p != null ) && ( equalsBytes ( p . paramsHash , new Bytes ( 32 ) ) == false ) ) ; } function handleGPProposalPrivate ( proposalId ) { let gpProposal = GenesisProtocolProposal . load ( proposalId ) ; if ( gpProposal != null ) { updateGPProposal ( gpProposal . address as Address , gpProposal . proposalId , gpProposal . proposer as Address , gpProposal . daoAvatarAddress as Address , gpProposal . paramsHash , gpProposal . submittedTime ) ; insertGPRewardsToHelper ( gpProposal . proposalId , gpProposal . proposer as Address ) ; updateProposalState ( gpProposal . proposalId , 3 , gpProposal . address as Address ) ; } } export function handleNewContributionProposal ( proposalId , avatar , timestamp , intVoteInterface , descriptionHash , contract ) { if ( ! daoModule . exists ( avatar ) ) { return ; } updateCRProposal ( proposalId , timestamp , avatar , intVoteInterface , descriptionHash , contract ) ; handleGPProposalPrivate ( proposalId . toHex ( ) ) ; } export function handleNewSchemeRegisterProposal ( proposalId , timestamp , avatar , votingMachine , descriptionHash , schemeAddress ) { if ( ! daoModule . exists ( avatar as Address ) ) { return ; } updateSRProposal ( proposalId , timestamp , avatar as Address , votingMachine as Address , descriptionHash , schemeAddress ) ; handleGPProposalPrivate ( proposalId ) ; } export function handleNewCallProposal ( avatar , proposalId , timestamp , descriptionHash , eventAddress ) { if ( ! daoModule . exists ( avatar ) ) { return ; } updateGSProposal ( proposalId , timestamp , avatar , descriptionHash , eventAddress ) ; handleGPProposalPrivate ( proposalId . toHex ( ) ) ; } export function handleNewMultiCallProposal ( avatar , proposalId , timestamp , descriptionHash , eventAddress ) { if ( ! daoModule . exists ( avatar ) ) { return ; } updateGSMCProposal ( proposalId , timestamp , avatar , descriptionHash , eventAddress ) ; handleGPProposalPrivate ( proposalId . toHex ( ) ) ; } export function handleStake ( event ) { let proposal = getProposal ( event . params . _proposalId . toHex ( ) ) ; if ( equalsBytes ( proposal . paramsHash , new Bytes ( 32 ) ) ) { return ; } if ( event . params . _vote . toI32 ( ) == 1 ) { proposal . stakesFor = proposal . stakesFor . plus ( event . params . _amount ) ; } else { proposal . stakesAgainst = proposal . stakesAgainst . plus ( event . params . _amount ) ; } proposal . confidence = ( new BigDecimal ( proposal . stakesFor ) ) / ( new BigDecimal ( proposal . stakesAgainst ) ) ; saveProposal ( proposal ) ; insertStake ( eventId ( event ) , event . block . timestamp , event . params . _staker , event . params . _amount , event . params . _proposalId . toHex ( ) , event . params . _organization . toHex ( ) , parseOutcome ( event . params . _vote ) ) ; insertGPRewardsToHelper ( event . params . _proposalId , event . params . _staker ) ; } export function handleVoteProposal ( event ) { let proposal = getProposal ( event . params . _proposalId . toHex ( ) ) ; if ( equalsBytes ( proposal . paramsHash , new Bytes ( 32 ) ) ) { return ; } if ( event . params . _vote . toI32 ( ) == 1 ) { proposal . votesFor = proposal . votesFor . plus ( event . params . _reputation ) ; } else { proposal . votesAgainst = proposal . votesAgainst . plus ( event . params . _reputation ) ; } updateProposalAfterVote ( proposal , event . address , event . params . _proposalId , event . params . _voter , event . block . timestamp ) ; saveProposal ( proposal ) ; insertVote ( eventId ( event ) , event . block . timestamp , event . params . _voter , event . params . _proposalId . toHex ( ) , event . params . _organization . toHex ( ) , parseOutcome ( event . params . _vote ) , event . params . _reputation ) ; insertGPRewardsToHelper ( event . params . _proposalId , event . params . _voter ) ; } export function confidenceLevelUpdate ( proposalId , confidenceThreshold ) { if ( isProposalValid ( proposalId . toHex ( ) ) ) { updateProposalconfidence ( proposalId , confidenceThreshold ) ; } } export function handleRegisterScheme ( avatar , nativeTokenAddress , nativeReputationAddress , scheme , paramsHash , timestamp ) { let isFirstRegister = store . get ( 'FirstRegisterSchemeFlag' , avatar . toHex ( ) ) ; if ( isFirstRegister == null ) { setContractsInfo ( ) ; let dao = daoModule . insertNewDAO ( avatar , nativeTokenAddress , nativeReputationAddress ) ; insertToken ( hexToAddress ( dao . nativeToken ) , avatar . toHex ( ) ) ; insertReputation ( hexToAddress ( dao . nativeReputation ) , avatar . toHex ( ) ) ; let repContract = store . get ( 'ReputationContract' , dao . nativeReputation ) as ReputationContract ; let holders = repContract . reputationHolders as string [ ] ; for ( let i = 0 ; i < holders . length ; i ++ ) { let reputationHolder = store . get ( 'ReputationHolder' , holders [ i ] ) as ReputationHolder ; addDaoMember ( reputationHolder ) ; } updateTokenTotalSupply ( hexToAddress ( dao . nativeToken ) ) ; let ent = new Entity ( ) ; ent . set ( 'id' , Value . fromString ( avatar . toHex ( ) ) ) ; store . set ( 'FirstRegisterSchemeFlag' , avatar . toHex ( ) , ent ) ; addNewDAOEvent ( avatar , dao . name , timestamp ) ; } gpqueueModule . create ( avatar , scheme , paramsHash ) ; } export function handleMint ( event ) { let rep = getReputation ( event . address . toHex ( ) ) ; if ( rep . dao == null ) { return ; } updateReputationTotalSupply ( event . address ) ; } export function handleBurn ( event ) { let dao = getReputation ( event . address . toHex ( ) ) . dao ; if ( dao == null ) { return ; } updateReputationTotalSupply ( event . address ) ; } export function handleNativeTokenTransfer ( event ) { let dao = getToken ( event . address . toHex ( ) ) . dao ; if ( dao == null ) { return ; } updateTokenTotalSupply ( event . address ) ; } export function handleExecuteProposal ( event ) { if ( isProposalValid ( event . params . _proposalId . toHex ( ) ) ) { updateProposalExecution ( event . params . _proposalId , event . params . _totalReputation , event . block . timestamp , event . params . _decision ) ; } } export function handleStateChange ( event ) { if ( isProposalValid ( event . params . _proposalId . toHex ( ) ) ) { updateProposalState ( event . params . _proposalId , event . params . _proposalState , event . address ) ; if ( ( event . params . _proposalState == 1 ) || ( event . params . _proposalState == 2 ) ) { insertGPRewards ( event . params . _proposalId , event . block . timestamp , event . address , event . params . _proposalState ) ; } addProposalStateChangeEvent ( event . params . _proposalId , event . transaction . from , event . block . timestamp ) ; } } export function handleExecutionStateChange ( event ) { if ( isProposalValid ( event . params . _proposalId . toHex ( ) ) ) { updateProposalExecutionState ( event . params . _proposalId . toHex ( ) , event . params . _executionState ) ; } } export function handleGPRedemption ( proposalId , beneficiary , timestamp , type ) { if ( isProposalValid ( proposalId . toHex ( ) ) ) { if ( type == 'token' ) { tokenRedemption ( proposalId , beneficiary , timestamp ) ; } else if ( type == 'reputation' ) { reputationRedemption ( proposalId , beneficiary , timestamp ) ; } else { daoBountyRedemption ( proposalId , beneficiary , timestamp ) ; } } } export function daoRegister ( dao , tag ) { daoModule . register ( dao , tag ) ; } export function addDaoMember ( reputationHolder ) { let dao = getReputation ( reputationHolder . contract . toHex ( ) ) . dao ; if ( dao == null ) { return ; } if ( reputationHolder . dao == null ) { reputationHolder . dao = dao ; reputationHolder . save ( ) ; } addNewReputationHolderEvent ( reputationHolder ) ; daoModule . increaseDAOmembersCount ( dao ) ; } export function removeDaoMember ( reputationHolder ) { let dao = getReputation ( reputationHolder . contract . toHex ( ) ) . dao ; if ( dao == null ) { return ; } daoModule . decreaseDAOmembersCount ( dao ) ; }