UNPKG

@nitin-spheron/desub-js

Version:

Typescript library for working with the decentralised subscription contracts in Spheron

259 lines (258 loc) 13.1 kB
"use strict"; 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 }); const constants_1 = require("./constants"); const deployed_1 = __importDefault(require("./abstracts/deployed")); const errors_1 = require("./errors"); const price_feed_1 = require("./constants/price-feed"); class Payment extends deployed_1.default { constructor(vendor, coinMarketCapKey) { super(vendor, constants_1.PAYMENT_ABI, constants_1.ERC20_ABI, constants_1.SUBSCRIPTION_PAYMENT_ABI, constants_1.SUBSCRIPTION_DATA_ABI, constants_1.SUBSCRIPTION_NATIVE_PAYMENT_ABI, constants_1.SUBSCRIPTION_MANTLE_PAYMENT_ABI); this.coinMarketCapKey = coinMarketCapKey; } paymentWithFee(userAddress, buildTimeInSeconds, deploymentCost, providerQuote, providerCharged, providerName) { var _a, _b, _c, _d; return __awaiter(this, void 0, void 0, function* () { const wei = this.vendor.convertToWei(deploymentCost, (_a = this.tokenPrecision) !== null && _a !== void 0 ? _a : 18); const buildTime = this.vendor.convertToBN(buildTimeInSeconds); const quote = this.vendor.convertToWei(providerQuote, (_b = this.tokenPrecision) !== null && _b !== void 0 ? _b : 18); const charge = this.vendor.convertToWei(providerCharged, (_c = this.tokenPrecision) !== null && _c !== void 0 ? _c : 18); return yield ((_d = this.paymentsContract) === null || _d === void 0 ? void 0 : _d.functions.chargeWithProvider(userAddress, buildTime, wei, quote, charge, providerName)); }); } paymentWithoutFee(userAddress, buildTimeInSeconds) { var _a; return __awaiter(this, void 0, void 0, function* () { const buildTime = this.vendor.convertToBN(buildTimeInSeconds); return yield ((_a = this.paymentsContract) === null || _a === void 0 ? void 0 : _a.functions.charge(userAddress, buildTime)); }); } updateUnderlyingToken(address) { var _a; return __awaiter(this, void 0, void 0, function* () { return yield ((_a = this.paymentsContract) === null || _a === void 0 ? void 0 : _a.functions.updateUnderlyingToken(address)); }); } updateEscrow(a) { var _a; return __awaiter(this, void 0, void 0, function* () { return yield ((_a = this.paymentsContract) === null || _a === void 0 ? void 0 : _a.functions.updateEscrow(a)); }); } updateFeederAddress(a) { var _a; return __awaiter(this, void 0, void 0, function* () { return yield ((_a = this.paymentsContract) === null || _a === void 0 ? void 0 : _a.functions.updateFeederAddress(a)); }); } updateStakedToken(a) { var _a; return __awaiter(this, void 0, void 0, function* () { return yield ((_a = this.paymentsContract) === null || _a === void 0 ? void 0 : _a.functions.updateStakedToken(a)); }); } updateToken(a) { var _a; return __awaiter(this, void 0, void 0, function* () { return yield ((_a = this.paymentsContract) === null || _a === void 0 ? void 0 : _a.functions.updateToken(a)); }); } updateDiscountSlabs(d, p) { var _a; return __awaiter(this, void 0, void 0, function* () { const discountSlabs = this.vendor.convertStringArrayToBigNumberArray(d); const percents = this.vendor.convertStringArrayToBigNumberArray(p); return yield ((_a = this.paymentsContract) === null || _a === void 0 ? void 0 : _a.functions.updateDiscountSlabs(discountSlabs, percents)); }); } changeBuildTimeRate(p) { var _a; return __awaiter(this, void 0, void 0, function* () { const wei = this.vendor.convertToWei(p, this.tokenPrecision || 18); return yield ((_a = this.paymentsContract) === null || _a === void 0 ? void 0 : _a.functions.changeBuildTimeRate(wei)); }); } enableDiscounts(h) { var _a; return __awaiter(this, void 0, void 0, function* () { return yield ((_a = this.paymentsContract) === null || _a === void 0 ? void 0 : _a.functions.enableDiscounts(h)); }); } disableDiscounts() { var _a; return __awaiter(this, void 0, void 0, function* () { return yield ((_a = this.paymentsContract) === null || _a === void 0 ? void 0 : _a.functions.disableDiscounts()); }); } setGovernanceAddress(h) { var _a; return __awaiter(this, void 0, void 0, function* () { return yield ((_a = this.paymentsContract) === null || _a === void 0 ? void 0 : _a.functions.setGovernanceAddress(h)); }); } setManagers(h) { var _a; return __awaiter(this, void 0, void 0, function* () { return yield ((_a = this.paymentsContract) === null || _a === void 0 ? void 0 : _a.functions.setManagers(h)); }); } setNewApprovals(a) { var _a, _b; return __awaiter(this, void 0, void 0, function* () { const wei = this.vendor.convertToWei(a, this.tokenPrecision || 18); return yield ((_a = this.erc20Contract) === null || _a === void 0 ? void 0 : _a.functions.approve((_b = this.paymentsContract) === null || _b === void 0 ? void 0 : _b.address, wei)); }); } gasslessApproval(approvalAmount, chainId) { var _a, _b; return __awaiter(this, void 0, void 0, function* () { if (!this.vendor.biconomy) throw new Error(errors_1.INVALID_BICONOMY_KEY); const wei = this.vendor.convertToWei(approvalAmount, this.tokenPrecision || 18); const abiEncodedApprove = this.vendor.abiEncodeErc20Functions('approve', [(_a = this.paymentsContract) === null || _a === void 0 ? void 0 : _a.address, wei]); const userAddress = yield this.vendor.signer.getAddress(); const nonce = yield this.getNonceForGaslessERC20(userAddress); const signedMessage = yield this.vendor.signedMessageForTx(userAddress, nonce, abiEncodedApprove, ((_b = this.erc20Contract) === null || _b === void 0 ? void 0 : _b.address) || '', chainId); const rsv = this.vendor.getSignatureParameters(signedMessage); return yield this.sendRawBiconomyERC20Transaction(userAddress, abiEncodedApprove, rsv); }); } sendRawBiconomyERC20Transaction(u, f, rsv) { var _a; return __awaiter(this, void 0, void 0, function* () { if (this.vendor.biconomy.status === this.vendor.biconomy.READY) { const tx = yield ((_a = this.biconomyERC20Contract) === null || _a === void 0 ? void 0 : _a.functions.executeMetaTransaction(u, f, rsv.r, rsv.s, rsv.v)); return tx; } else { return new Promise((resolve, reject) => { this.vendor.biconomy .onEvent(this.vendor.biconomy.READY, () => __awaiter(this, void 0, void 0, function* () { var _a; const tx = yield ((_a = this.biconomyERC20Contract) === null || _a === void 0 ? void 0 : _a.functions.executeMetaTransaction(u, f, rsv.r, rsv.s, rsv.v)); resolve(tx); })) .onEvent(this.vendor.biconomy.ERROR, (error) => { console.log(error); reject(error); }); }); } }); } getApprovalAmount(userAddress) { var _a, _b, _c; return __awaiter(this, void 0, void 0, function* () { const allowanceInWei = yield ((_a = this.erc20Contract) === null || _a === void 0 ? void 0 : _a.functions.allowance(userAddress, (_b = this.paymentsContract) === null || _b === void 0 ? void 0 : _b.address)); const allowanceInEth = this.vendor.convertWeiToEth(allowanceInWei, (_c = this.tokenPrecision) !== null && _c !== void 0 ? _c : 18); return allowanceInEth; }); } getNonceForGaslessERC20(userAddress) { var _a, _b, _c; return __awaiter(this, void 0, void 0, function* () { const nonce = yield ((_a = this.erc20Contract) === null || _a === void 0 ? void 0 : _a.functions.getNonce(userAddress)); return (_c = (_b = nonce === null || nonce === void 0 ? void 0 : nonce[0]) === null || _b === void 0 ? void 0 : _b.toNumber()) !== null && _c !== void 0 ? _c : 0; }); } getUserBalance(a) { var _a; return __awaiter(this, void 0, void 0, function* () { const wei = yield ((_a = this.erc20Contract) === null || _a === void 0 ? void 0 : _a.functions.balanceOf(a)); return this.vendor.convertWeiToEth(wei, this.tokenPrecision || 18); }); } getManagers() { var _a; return __awaiter(this, void 0, void 0, function* () { return yield ((_a = this.paymentsContract) === null || _a === void 0 ? void 0 : _a.functions.getManagers()); }); } getGovernanceAddress() { var _a; return __awaiter(this, void 0, void 0, function* () { return yield ((_a = this.paymentsContract) === null || _a === void 0 ? void 0 : _a.functions.governanceAddress()); }); } getToken() { var _a; return __awaiter(this, void 0, void 0, function* () { return yield ((_a = this.paymentsContract) === null || _a === void 0 ? void 0 : _a.functions.underlying()); }); } getEscrow() { var _a; return __awaiter(this, void 0, void 0, function* () { return yield ((_a = this.paymentsContract) === null || _a === void 0 ? void 0 : _a.functions.escrow()); }); } checkIfDiscountsEnabled() { var _a; return __awaiter(this, void 0, void 0, function* () { return yield ((_a = this.paymentsContract) === null || _a === void 0 ? void 0 : _a.functions.discountsEnabled()); }); } getStakingManagerAddress() { var _a; return __awaiter(this, void 0, void 0, function* () { return yield ((_a = this.paymentsContract) === null || _a === void 0 ? void 0 : _a.functions.stakingManager()); }); } getStakedTokenAddress() { var _a; return __awaiter(this, void 0, void 0, function* () { return yield ((_a = this.paymentsContract) === null || _a === void 0 ? void 0 : _a.functions.stakedToken()); }); } getDiscountSlabs() { var _a; return __awaiter(this, void 0, void 0, function* () { const slabs = yield ((_a = this.paymentsContract) === null || _a === void 0 ? void 0 : _a.functions.discountSlabs()); return this.vendor.parseDiscountSlabs(slabs); }); } getArweaveConvertedUsd(a) { return __awaiter(this, void 0, void 0, function* () { if (!this.coinMarketCapKey) throw new Error(errors_1.API_KEY_REQUIRED); const qoute = yield this.services.tokenToUSD(a, price_feed_1.arweaveTokenId, this.coinMarketCapKey); return qoute; }); } getArweaveQuote() { return __awaiter(this, void 0, void 0, function* () { if (!this.coinMarketCapKey) throw new Error(errors_1.API_KEY_REQUIRED); const qoute = yield this.services.tokenQuote(price_feed_1.arweaveTokenId, this.coinMarketCapKey); return qoute; }); } getAkashConvertedUsd(a) { return __awaiter(this, void 0, void 0, function* () { if (!this.coinMarketCapKey) throw new Error(errors_1.API_KEY_REQUIRED); const qoute = yield this.services.tokenToUSD(a, price_feed_1.akashTokenId, this.coinMarketCapKey); return qoute; }); } getAkashQuote() { return __awaiter(this, void 0, void 0, function* () { if (!this.coinMarketCapKey) throw new Error(errors_1.API_KEY_REQUIRED); const qoute = yield this.services.tokenQuote(price_feed_1.akashTokenId, this.coinMarketCapKey); return qoute; }); } } exports.default = Payment;