UNPKG

@q-dev/q-js-sdk

Version:

Typescript Library to interact with Q System Contracts

75 lines 2.97 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.FeeCalculation = void 0; const IFxPriceFeedInstance_1 = require("../contracts/defi/oracles/IFxPriceFeedInstance"); const bn_js_1 = __importDefault(require("bn.js")); const ContractRegistryInstance_1 = require("../contracts/ContractRegistryInstance"); class FeeCalculation { constructor(signerOrProvider) { this.signerOrProvider = signerOrProvider; } async lazyInit() { if (this.qfiParams) return; const registry = new ContractRegistryInstance_1.ContractRegistryInstance(this.signerOrProvider); this.qfiParams = await registry.epqfiParameters(); } async calculateGasPrice() { try { await this.lazyInit(); } catch (e) { console.log(`Cannot initialize fee calculater: ${e}`); return this.getDefaultGasPrice(); } let txFee = new bn_js_1.default(0); try { txFee = new bn_js_1.default(await this.qfiParams.getUint('governed.EPQFI.txFee')); } catch (e) { console.log(`Cannot determine transaction fee: ${e}`); return this.getDefaultGasPrice(); } let oracleAddress; try { oracleAddress = await this.qfiParams.getAddr('governed.EPQFI.Q_QUSD_source'); } catch (e) { console.log(`Cannot determine oracle address: ${e}`); return this.getDefaultGasPrice(); } const priceFeed = await new IFxPriceFeedInstance_1.IFxPriceFeedInstance(this.signerOrProvider, oracleAddress); let exchangeRateRaw = '0'; try { exchangeRateRaw = await priceFeed.exchangeRate(); } catch (e) { console.log(`Cannot determine exchangeRate: ${e}`); } if (exchangeRateRaw == '0') { console.log('Exchange rate is zero'); return this.getDefaultGasPrice(); // console.log('Exchange rate is zero. Continue with 1:1, instead') // exchangeRateRaw = '1e+18' } let txSize = new bn_js_1.default(0); try { txSize = new bn_js_1.default(await this.qfiParams.getUint('governed.EPQFI.normalizedTransactionSize')); } catch (e) { console.log('Cannot get normalized txSize'); return this.getDefaultGasPrice(); } const exchangeRate = new bn_js_1.default(exchangeRateRaw); const gp = txFee.mul(new bn_js_1.default(1e+18)).div(exchangeRate).div(txSize).toString(); return Number(gp); } async getDefaultGasPrice() { return (await this.signerOrProvider.getGasPrice()).toNumber(); } } exports.FeeCalculation = FeeCalculation; //# sourceMappingURL=fee-calculation.js.map