import { Address , BigInt , crypto , store } from '@graphprotocol/graph-ts' ; import { Burn , Mint , OwnershipTransferred , Reputation , } from '../../types/Reputation/Reputation' ; import { concat , eventId } from '../../utils' ; import * as domain from '../../domain' ; import { ReputationBurn , ReputationContract , ReputationHolder , ReputationMint , } from '../../types/schema' ; function update ( contract , owner , timestamp ) { let rep = Reputation . bind ( contract ) ; let reputationContract = ReputationContract . load ( contract . toHex ( ) ) ; if ( reputationContract == null ) { reputationContract = new ReputationContract ( contract . toHex ( ) ) ; reputationContract . reputationHolders = new Array < string > ( ) ; } let reputationHolders = reputationContract . reputationHolders ; let repHolderId = crypto . keccak256 ( concat ( contract , owner ) ) . toHex ( ) ; let repHolder = ReputationHolder . load ( repHolderId ) ; let balance = rep . balanceOf ( owner ) ; if ( repHolder == null ) { repHolder = new ReputationHolder ( repHolderId ) ; repHolder . contract = contract ; repHolder . address = owner ; repHolder . balance = balance ; repHolder . createdAt = timestamp ; if ( ! balance . isZero ( ) ) { repHolder . save ( ) ; reputationHolders . push ( repHolder . id ) ; domain . addDaoMember ( repHolder as ReputationHolder ) ; } } else { repHolder . balance = balance ; if ( ! balance . isZero ( ) ) { repHolder . save ( ) ; reputationHolders . push ( repHolder . id ) ; } else { store . remove ( 'ReputationHolder' , repHolder . id ) ; domain . removeDaoMember ( repHolder as ReputationHolder ) ; } } reputationContract . reputationHolders = reputationHolders ; reputationContract . address = contract ; reputationContract . totalSupply = rep . totalSupply ( ) ; reputationContract . save ( ) ; } export function handleMint ( event ) { domain . handleMint ( event ) ; update ( event . address , event . params . _to as Address , event . block . timestamp ) ; let ent = new ReputationMint ( eventId ( event ) ) ; ent . txHash = event . transaction . hash ; ent . contract = event . address ; ent . address = event . params . _to ; ent . amount = event . params . _amount ; store . set ( 'ReputationMint' , ent . id , ent ) ; } export function handleBurn ( event ) { domain . handleBurn ( event ) ; update ( event . address , event . params . _from as Address , event . block . timestamp ) ; let ent = new ReputationBurn ( eventId ( event ) ) ; ent . txHash = event . transaction . hash ; ent . contract = event . address ; ent . address = event . params . _from ; ent . amount = event . params . _amount ; store . set ( 'ReputationBurn' , ent . id , ent ) ; } export function handleOwnershipTransferred ( event ) { update ( event . address , event . params . newOwner as Address , event . block . timestamp ) ; }