@holographxyz/cli
Version:
Holograph operator CLI
57 lines (56 loc) • 2.46 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const tslib_1 = require("tslib");
const utils_1 = require("../utils/utils");
const core_chain_service_1 = tslib_1.__importDefault(require("./core-chain-service"));
class FaucetService extends core_chain_service_1.default {
faucet;
constructor(network, networkMonitor, contract) {
super(network, networkMonitor);
this.faucet = contract ? contract : this.getFaucet();
}
estimateGasForRequestTokens = async () => {
const gasPrice = this.getChainGasPrice();
const gasLimit = await this.networkMonitor.getGasLimit({
contract: this.faucet,
methodName: 'requestTokens',
args: [],
network: this.network,
gasPrice,
});
return gasPrice.mul(gasLimit);
};
getFaucetCooldown = async (address) => {
const faucetDefaultCooldown = (await this.faucet.faucetCooldown())?.toNumber();
const lastWithdrawTimestamp = (await this.faucet.getLastAccessTime(address))?.toNumber();
const cooldownTimestamp = lastWithdrawTimestamp + faucetDefaultCooldown;
return lastWithdrawTimestamp === 0 ? 0 : (0, utils_1.getSecondsLeft)(cooldownTimestamp);
};
getFaucetFee = async (address) => {
const cooldown = await this.getFaucetCooldown(address);
if (cooldown > 0)
return { fee: '', hasEnoughBalance: true };
const balance = (await this.getBalance(address))?.toString();
const gas = await this.estimateGasForRequestTokens();
const fee = (0, utils_1.toShort18Str)(gas?.toString());
const hasEnoughBalance = Number(fee) < Number(balance);
return { fee, hasEnoughBalance };
};
getFaucetInfo = async (address) => {
const dripAmount = await this.faucet.faucetDripAmount();
const amount = (0, utils_1.toShort18)(dripAmount)?.toString();
const cooldown = await this.getFaucetCooldown(address);
const isAllowedToWithdraw = await this.faucet.isAllowedToWithdraw(address);
return { amount, cooldown, isAllowedToWithdraw };
};
requestTokens = async () => {
return this.networkMonitor.executeTransaction({
network: this.network,
contract: this.faucet,
methodName: 'requestTokens',
args: [],
waitForReceipt: true,
});
};
}
exports.default = FaucetService;