@ardier16/q-js-sdk
Version:
Typescript Library to interact with Q System Contracts
72 lines • 2.77 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.FeeCalculation = void 0;
const IFxPriceFeedInstance_1 = require("../contracts/defi/oracles/IFxPriceFeedInstance");
const web3_utils_1 = require("web3-utils");
const ContractRegistryInstance_1 = require("../contracts/ContractRegistryInstance");
class FeeCalculation {
constructor(web3) {
this.web3 = web3;
}
async lazyInit() {
if (this.qfiParams)
return;
const registry = new ContractRegistryInstance_1.ContractRegistryInstance(this.web3);
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 = (0, web3_utils_1.toBN)(0);
try {
txFee = (0, web3_utils_1.toBN)(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.web3, 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 = (0, web3_utils_1.toBN)(0);
try {
txSize = (0, web3_utils_1.toBN)(await this.qfiParams.getUint('governed.EPQFI.normalizedTransactionSize'));
}
catch (e) {
console.log('Cannot get normalized txSize');
return this.getDefaultGasPrice();
}
const exchangeRate = (0, web3_utils_1.toBN)(exchangeRateRaw);
const gp = txFee.mul((0, web3_utils_1.toBN)(1e+18)).div(exchangeRate).div(txSize).toString();
return Number(gp);
}
async getDefaultGasPrice() {
return Number(await this.web3.eth.getGasPrice());
}
}
exports.FeeCalculation = FeeCalculation;
//# sourceMappingURL=fee-calculation.js.map