UNPKG

@ardier16/q-js-sdk

Version:

Typescript Library to interact with Q System Contracts

100 lines 3.77 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.Web3Adapter = exports.Web3 = void 0; const hdwallet_provider_1 = __importDefault(require("@truffle/hdwallet-provider")); const web3_1 = __importDefault(require("web3")); exports.Web3 = web3_1.default; const version_json_1 = require("./version.json"); const web3_factory_1 = require("../utils/web3-factory"); class Web3Adapter { constructor(web3) { this.web3 = web3; this.ZERO_ADDRESS = '0x0000000000000000000000000000000000000000'; this.SDK_VERSION = version_json_1.version; this.toWei = this.web3.utils.toWei; this.fromWei = this.web3.utils.fromWei; this.toBN = this.web3.utils.toBN; } startProviderEngine() { if (!(this.web3.currentProvider instanceof hdwallet_provider_1.default)) return false; this.web3.currentProvider.engine.start(); return true; } stopProviderEngine() { if (!(this.web3.currentProvider instanceof hdwallet_provider_1.default)) return false; this.web3.currentProvider.engine.stop(); return true; } getRpcUrl() { const provider = this.web3.currentProvider; if (provider instanceof web3_factory_1.QSdkProvider) { return provider.hdWalletConfig.providerOrUrl; } const guessRpc = provider['host'] || provider['_host']; return guessRpc || 'n.a.'; } async getConnectionInfo() { const networkId = await this.web3.eth.getChainId(); const nodeInfo = await this.web3.eth.getNodeInfo(); const rpcUrl = this.getRpcUrl(); return { networkId, nodeInfo, rpcUrl }; } async getDefaultAccount() { const accounts = await this.getAccounts(); return accounts[0]; } async getAccounts() { return this.web3.eth.getAccounts(); } async getTotalQInExistence() { const initialQ = 1e10; // 10 billion Q const blockSubsidy = 15; // 15 Q per Block const blockNumber = await this.web3.eth.getBlockNumber(); return initialQ + blockNumber * blockSubsidy; } async getBalance(address, convertToQ = true) { const balance = await this.web3.eth.getBalance(address); if (!convertToQ) return balance; return this.web3.utils.fromWei(balance); } async getBalances(addresses, convertToQ = true) { const getBalanceOperation = async (address) => { const balance = await this.getBalance(address, convertToQ); return { address, balance, }; }; return this.forEachAddress(addresses, getBalanceOperation); } async forEachAddress(addresses, operation) { const values = []; const errors = []; const robustOperation = async (address, i) => { var _a; try { const value = await operation(address, i); values[i] = value; } catch (error) { const reason = (_a = error.message) !== null && _a !== void 0 ? _a : error.toString(); errors.push({ address, reason }); } }; const promises = addresses.map(robustOperation); await Promise.all(promises); if (errors.length > 0) { const msg = errors.map(e => `operation rejected for address ${e.address}: ${e.reason}`).join('\n'); throw new Error(msg); } return values; } } exports.Web3Adapter = Web3Adapter; //# sourceMappingURL=web3-adapter.js.map