evm-blockchain-tools
Version:
This is a collection of resuseable tools to support development for EVM-powered blockchains
82 lines • 3.43 kB
JavaScript
;
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.BscWsGateway = void 0;
const ethers_1 = require("ethers");
const ws_1 = __importDefault(require("ws"));
const hash_1 = require("@ethersproject/hash");
const reconnecting_websocket_1 = __importDefault(require("reconnecting-websocket"));
const constants_1 = require("../common/constants");
const utils_1 = require("../utils");
class BscWsGateway {
constructor(config) {
this.config = config;
this.network = constants_1.APP_NETWORK.BINANCE;
this.connect();
}
get networkName() {
return this.config.network === constants_1.APP_NETWORK.BINANCE
? constants_1.APP_NETWORK.BINANCE
: constants_1.APP_NETWORK.BINANCE_TESTNET;
}
get chainId() {
return this.config.network === constants_1.APP_NETWORK.BINANCE
? constants_1.NETWORK_IDS.BINANCE
: constants_1.NETWORK_IDS.BINANCE_TESTNET;
}
connect() {
const wsClient = new reconnecting_websocket_1.default(this.config.wsUrl, [], Object.assign({ WebSocket: ws_1.default }, (this.config.options || {})));
this.provider = new ethers_1.ethers.providers.WebSocketProvider(wsClient, {
name: this.network,
chainId: this.chainId,
});
}
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);
}
get signer() {
return Promise.resolve(new ethers_1.ethers.Wallet(this.config.privateKey, this.provider));
}
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.BscWsGateway = BscWsGateway;
//# sourceMappingURL=bsc-ws-gateway.js.map