UNPKG

evm-blockchain-tools

Version:

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

72 lines 3.03 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.AlchemyWsGateway = void 0; const alchemy_sdk_1 = require("alchemy-sdk"); const ws_1 = __importDefault(require("ws")); const reconnecting_websocket_1 = __importDefault(require("reconnecting-websocket")); const hash_1 = require("@ethersproject/hash"); const ethers_1 = require("ethers"); const constants_1 = require("../common/constants"); const utils_1 = require("../utils"); class AlchemyWsGateway { constructor(config) { this.config = config; this.network = constants_1.APP_NETWORK.ETH; this._alchemy = new alchemy_sdk_1.Alchemy({ apiKey: config.apiKey, }); this.connect(); } connect() { const wsClient = new reconnecting_websocket_1.default(this.config.wsUrl, [], Object.assign({ WebSocket: ws_1.default }, (this.config.options || {}))); const alchemyProvider = new ethers_1.ethers.providers.WebSocketProvider(wsClient, this.config.apiKey); this.wallet = new ethers_1.Wallet(this.config.privateKey, alchemyProvider); this._provider = alchemyProvider; } get provider() { return this._provider; } get signer() { return Promise.resolve(this.wallet); } getCurrentBlock() { return this._provider.getBlockNumber(); } isValidTxFormat(txHash) { return (0, utils_1.isValidEvmTxFormat)(txHash); } recoverSigner(message, signedMessage) { return __awaiter(this, void 0, void 0, function* () { return ethers_1.ethers.utils.recoverAddress((0, hash_1.hashMessage)(message), signedMessage); }); } getBlock(blockNumber) { return this._provider.getBlock(blockNumber); } getGasPrice() { return __awaiter(this, void 0, void 0, function* () { const gasPrice = yield this._provider.getGasPrice(); return gasPrice.toString(); }); } getSignerAddress() { return this.wallet.getAddress(); } getTransactionByID(txID) { return this._provider.getTransaction(txID); } } exports.AlchemyWsGateway = AlchemyWsGateway; //# sourceMappingURL=alchemy-ws-gateway.js.map