File

src/api/payment-network/eth/info-retriever.ts

Description

Gets a list of transfer events for an address and payment reference

Implements

Index

Methods

Constructor

constructor(toAddress: string, eventName: Types.EVENTS_NAMES, network: string, paymentReference: string, etherscanApiKey?: string)
Parameters :
Name Type Optional Description
toAddress string No
eventName Types.EVENTS_NAMES No

Indicate if it is an address for payment or refund

network string No

The id of network we want to check

paymentReference string No

The reference to identify the payment

etherscanApiKey string Yes

Methods

Public Async getTransferEvents
getTransferEvents()
import { ethers } from 'ethers';
import * as Types from '../../../types';

/**
 * Gets a list of transfer events for an address and payment reference
 */
export default class ETHInfoRetriever
  implements Types.IPaymentNetworkInfoRetriever<Types.ETHPaymentNetworkEvent> {
  /**
   * @param address Address to check
   * @param eventName Indicate if it is an address for payment or refund
   * @param network The id of network we want to check
   * @param paymentReference The reference to identify the payment
   * @param etherscanApiToken The etherscan API token
   */
  constructor(
    private toAddress: string,
    private eventName: Types.EVENTS_NAMES,
    private network: string,
    private paymentReference: string,
    private etherscanApiKey?: string,
  ) {}

  public async getTransferEvents(): Promise<Types.ETHPaymentNetworkEvent[]> {
    if (this.network === 'private') {
      throw new Error(
        'ETH input data info-retriever works with etherscan and cannot work on a local network',
      );
    }
    const provider = new ethers.providers.EtherscanProvider(this.network, this.etherscanApiKey);
    const history = await provider.getHistory(this.toAddress);

    const events = history
      // keep only when address is the destination
      .filter(
        transaction =>
          transaction.to && transaction.to.toLowerCase() === this.toAddress.toLowerCase(),
      )
      // keep only if data contains the payment reference
      .filter(
        transaction =>
          transaction.data.toLowerCase() === '0x' + this.paymentReference.toLowerCase(),
      )
      .map(transaction => ({
        amount: transaction.value.toString(),
        name: this.eventName,
        parameters: {
          block: transaction.blockNumber,
          confirmations: transaction.confirmations,
          txHash: transaction.hash,
        },
        timestamp: transaction.timestamp,
      }));

    return events;
  }
}

result-matching ""

    No results matching ""