argosjs
Version:
Ethereum smart-contract events visualiser
127 lines (126 loc) • 4.26 kB
TypeScript
import { ethers } from "ethers";
import { Strategies } from "../utils/strategy";
import { DatabaseConstructorType } from "../utils/types";
import { EventInfoDataStruct, ProviderConfig, Watcher } from "./Watcher";
export default class EthereumWatcher extends Watcher {
private _contractAddr;
private _dbService;
private _clearDB;
private _config;
private _provider;
private _contract;
private _exportDir;
private _event;
private _timeout;
private _dbType;
private _strategies;
private _blockTime;
private _latestTime;
private _latestBlockNo;
private _startTime;
private _logSizePerOp;
private _nbContractCall;
/**
* Create a watcher for Ethereum blockchain
* @param {string} contractAddr the address of the verified contract
* @param {string} abi the ABI of the verified contract
* @param {number} providerType the Etherscan API Token
* @param {DatabaseConstructorType} dbType the database servcice constructor
* @param {ProviderConfig} providerConfig the loaded config file
* @param {boolean} clearDB retrieve from genesis block instead of the latest in DB (db cleared)
* @param {string} exportDir export dir
* @returns {EthereumWatcher} Ethereum instance
*/
constructor(contractAddr: string, abi: string, providerType: number, dbType: DatabaseConstructorType, providerConfig: ProviderConfig, clearDB: boolean, exportDir: string);
/**
* Watch event with particular model
* @param {string} eventName name of the event
* @param {Neode.SchemaObject} dbModel the model loaded via require()
* @param {number} fromDate timestamp
* @param {number} toDate timestamp
*/
watchEvents(eventName: string, fromDate?: Date, toDate?: Date): Promise<void>;
/**
* Convert timestamp to blocknumber
* @param {Date} date
*/
timeToBlock(date: Date): Promise<number>;
/**
* Tell the Watcher to clear the database before the next operation
* @param clearFlag
*/
setClearDBFlag(clearFlag: boolean): void;
/**
* Assemble a selection of data out of a log part
* @param {ethers.providers.Log[]} logPart the extracted log part
*/
getLogData(logPart: ethers.providers.Log[]): Promise<EventInfoDataStruct[]>;
/**
* Check for timeout
*/
checkTimeout(): void;
/**
* Get events from provider and store data to database
* @param {string} eventName
* @param {number} fromBlock
* @param {number} toBlock
*/
getEvents(eventName: string, fromBlock: number, toBlock: number): Promise<void>;
/**
* Load the strategies to the watcher
* @param strategies the user-defined strategy to extract and persists data
*/
setStrategies(strategies: Strategies): void;
/**
* Convert blockNo to timestamp
* @param blockNo
*/
private blockToTime;
/**
* Export to CSV
*/
private exportCSV;
/**
* Import from CSV
*/
private importCSV;
/**
* Call the right provider for the contract
* @param {ProviderEnum} providerType the provider type
* @param {ProviderConfig} config the loaded config file
* @returns {ethers.providers.BaseProvider} a provider
*/
private getProvider;
/**
* Persist processed data to the database
* @param eidss the processed data
*/
private sendToDB;
/**
* Extract data from log entry
* @param {ethers.providers.Log} logEntry a log entry
* @returns {EventInfoDataStruct} the required information to build a db node
*/
private extractData;
/**
* Call contract method
* @param funcName method name
* @param args method args
*/
private contractCall;
/**
* Apply process function after extracting data. By default, the data will be processed as String
* @param strategy DES strategy
* @param init unformatted/raw data
*/
private processData;
/**
* Get the smaller batch of events
* @param {string} eventName
* @param {number} fromBlock
* @param {number} toBlock
* @returns {Promise<ethers.providers.Log[]>}
*/
private getEventPatch;
}
export { EthereumWatcher };