UNPKG

evm-blockchain-tools

Version:

This is a collection of resuseable tools to support development for EVM-powered blockchains

92 lines 3.62 kB
"use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.EvmGateway = void 0; const ethers_1 = require("ethers"); const hash_1 = require("@ethersproject/hash"); const constants_1 = require("../common/constants"); const utils_1 = require("../utils"); const pino_1 = __importDefault(require("pino")); class EvmGateway { constructor(config) { this.config = config; this.logger = (0, pino_1.default)(); this.network = constants_1.APP_NETWORK.BINANCE; this.connect(); } checkConnection() { return __awaiter(this, void 0, void 0, function* () { try { yield this.provider.getNetwork(); yield this.provider.getBlockNumber(); } catch (error) { this.logger.error(`error checking connection: ${error.message}`); this.connect(); } }); } connect() { if (this.keepAliveInterval) { clearInterval(this.keepAliveInterval); } const provider = new ethers_1.ethers.providers.JsonRpcProvider(this.config.httpsUrl, { name: this.config.name, chainId: this.config.chainId, }); provider.on("error", (err) => { this.logger.error(`http connection error: ${err.message}`); this.connect(); }); this.provider = provider; this.keepAliveInterval = setInterval(this.checkConnection.bind(this), 1000 * 60 * 3); } get signer() { return Promise.resolve(new ethers_1.ethers.Wallet(this.config.privateKey, this.provider)); } getGasPrice() { return __awaiter(this, void 0, void 0, function* () { const gasPrice = yield this.provider.getGasPrice(); return gasPrice.toString(); }); } isValidTxFormat(txHash) { return (0, utils_1.isValidEvmTxFormat)(txHash); } getBlock(blockNumber) { return __awaiter(this, void 0, void 0, function* () { const block = yield this.provider.getBlock(blockNumber); return block; }); } recoverSigner(message, signedMessage) { return __awaiter(this, void 0, void 0, function* () { return ethers_1.ethers.utils.recoverAddress((0, hash_1.hashMessage)(message), signedMessage); }); } getCurrentBlock() { return this.provider.getBlockNumber(); } getSignerAddress() { return __awaiter(this, void 0, void 0, function* () { const signer = yield this.signer; return signer.getAddress(); }); } getTransactionByID(txID) { return this.provider.getTransaction(txID); } } exports.EvmGateway = EvmGateway; //# sourceMappingURL=evm-gateway.js.map