All files / src/contract index.ts

88.57% Statements 31/35
57.14% Branches 4/7
66.66% Functions 6/9
93.1% Lines 27/29

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 921x 1x 1x             1x   1x             1x 1x 1x   1x             1x   1x                                   1x       1x         1x 2x   1x 1x   1x   1x   1x             1x 1x 1x   1x 1x           1x       1x  
import {Contract as EthersContract, InterfaceAbi, LogDescription} from 'ethers'
import {stripShardRealm} from '../utils'
import {ContractEventLogs} from '../client/graphql'
import HgraphClient, {
  Contract,
  ContractOptions,
  ContractQueryEventsParams,
} from '../types'
 
export {LogDescription, InterfaceAbi, Contract as EthersContract} from 'ethers'
 
export class HgraphContract implements Contract {
  public contractId: string
  public abi: InterfaceAbi
  public client: HgraphClient
  public contract: EthersContract
 
  constructor(options: ContractOptions) {
    this.contractId = options.contractId
    this.abi = options.abi
    this.client = options.client
    // We don't need a valid contract evm address for parsing;
    this.contract = new EthersContract(
      '0x0000000000000000000000000000000000000000',
      options.abi
    )
  }
 
  async queryEvents(params: ContractQueryEventsParams = {}) {
    const {limit = 10, offset = 0, order = 'desc'} = params
 
    const {data: eventLogs, errors} = await this.client.query<{
      logs: {
        data: string | null
        topic0: string | null
        topic1: string | null
        topic2: string | null
        topic3: string | null
      }[]
    }>({
      query: ContractEventLogs,
      variables: {
        contract_id: stripShardRealm(this.contractId),
        limit,
        offset,
        order,
      },
    })
 
    Iif (errors?.length) {
      throw errors[0]
    }
 
    const parsedEvents: LogDescription[] = []
    // format logs to fit ethers logs structure
    const formattedLogs: {
      topics: string[]
      data: string
    }[] = []
    const formatHex = (value: string) => '0x' + value.slice(2).padStart(64, '0')
 
    eventLogs.logs?.forEach((log) => {
      Iif (!log.data) return
 
      const topics = [log.topic0, log.topic1, log.topic2, log.topic3]
        .filter(Boolean)
        .map((topic) => formatHex(topic!))
 
      formattedLogs.push({
        data: formatHex(log.data),
        topics,
      })
    })
 
    // decode logs
    formattedLogs.forEach((log) => {
      try {
        const eventFragment = this.contract.interface.parseLog(log)
 
        Iif (!eventFragment) return
        parsedEvents.push(eventFragment)
      } catch (error) {
        console.error('Error decoding log:', error)
      }
    })
 
    return parsedEvents
  }
}
 
export {detectContractType, type ContractType} from './detectContractType'