import { Address , BigInt , ByteArray , Bytes , crypto , DataSourceTemplate , ethereum , log , store , Value , } from '@graphprotocol/graph-ts' ; import { BlacklistedDAO , ContractInfo , ControllerScheme , Debug , TemplateInfo , } from './types/schema' ; export function concat ( a , b ) { let out = new Uint8Array ( a . length + b . length ) ; for ( let i = 0 ; i < a . length ; i ++ ) { out [ i ] = a [ i ] ; } for ( let j = 0 ; j < b . length ; j ++ ) { out [ a . length + j ] = b [ j ] ; } return out as ByteArray ; } export const CLOSING_AT_TIME_DECREASE_GSMC = 32000000 ; export const CLOSING_AT_TIME_INCREASE = 2147483647 ; export function eventId ( event ) { return crypto . keccak256 ( concat ( event . transaction . hash , event . transactionLogIndex as ByteArray ) ) . toHex ( ) ; } export function hexToAddress ( hex ) { return Address . fromString ( hex . substr ( 2 ) ) ; } /**
 * WORKAROUND: there's no `console.log` functionality in mapping.
 * so we use `debug(..)` which writes a `Debug` entity to the store so you can see them in graphiql.
 */ let debugId = 0 ; export function debug ( msg ) { let id = BigInt . fromI32 ( debugId ) . toHex ( ) ; let ent = new Debug ( id ) ; ent . set ( 'message' , Value . fromString ( msg ) ) ; store . set ( 'Debug' , id , ent ) ; debugId ++ ; } export function equalsBytes ( a , b ) { if ( a . length !== b . length ) { return false ; } for ( let i = 0 ; i < a . length ; i ++ ) { if ( a [ i ] !== b [ i ] ) { return false ; } } return true ; } export function equalStrings ( a , b ) { if ( a . length !== b . length ) { return false ; } for ( let i = 0 ; i < a . length ; i ++ ) { if ( a [ i ] . charCodeAt ( 0 ) !== b [ i ] . charCodeAt ( 0 ) ) { return false ; } } return true ; } export function setContractInfo ( address , name , alias , version ) { let contractInfo = ContractInfo . load ( address ) ; if ( contractInfo == null ) { contractInfo = new ContractInfo ( address ) ; contractInfo . address = Address . fromString ( address ) ; contractInfo . name = name ; contractInfo . alias = alias ; contractInfo . version = version ; contractInfo . save ( ) ; } } export function setTemplateInfo ( name , version , templateName ) { let id = name . concat ( version ) ; let templateInfo = TemplateInfo . load ( id ) ; if ( templateInfo == null ) { templateInfo = new TemplateInfo ( id ) ; templateInfo . templateName = templateName ; templateInfo . save ( ) ; } } export function fetchTemplateName ( name , version ) { let id = name . concat ( version ) ; let templateInfo = TemplateInfo . load ( id ) ; if ( templateInfo == null ) { return null ; } else { return templateInfo . templateName ; } } export function createTemplate ( templateName , address ) { DataSourceTemplate . create ( templateName , [ address . toHex ( ) ] ) ; } export function setBlacklistedDAO ( address ) { let blacklistedDAO = BlacklistedDAO . load ( address ) ; if ( blacklistedDAO == null ) { blacklistedDAO = new BlacklistedDAO ( address ) ; blacklistedDAO . save ( ) ; } } export function fixJsonQuotes ( target ) { let targetIndex = 0 ; let result = '' ; for ( targetIndex = 0 ; targetIndex < target . length ; targetIndex ++ ) { if ( target [ targetIndex ] === '"' ) { result += '\\"' ; } else { result += target [ targetIndex ] ; } } return result ; } export function setSchemeError ( schemeId , errorCode , errorMsg ) { let controllerScheme = ControllerScheme . load ( schemeId ) ; if ( controllerScheme != null ) { controllerScheme . error = errorCode ; controllerScheme . save ( ) ; } log . info ( errorMsg , [ ] ) ; }