UNPKG

@interlay/interbtc

Version:

JavaScript library to interact with InterBTC

157 lines 11 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } }); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); __setModuleDefault(result, mod); return result; }; 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 }); exports.satoshisToBtcTimeData = exports.indexRedeemToTypedRedeem = exports.indexIssueToTypedIssue = exports.DefaultIndexAPI = void 0; /* eslint @typescript-eslint/no-var-requires: "off" */ const interbtcIndex = __importStar(require("@interlay/interbtc-index-client")); const monetary_js_1 = require("@interlay/monetary-js"); const big_js_1 = __importDefault(require("big.js")); const interbtc_api_1 = require("@interlay/interbtc-api"); const currencyMapper_1 = require("./currencyMapper"); function constructExchangeRate(currencyKey, rawRate) { switch (currencyKey) { case "DOT": return (0, interbtc_api_1.newCollateralBTCExchangeRate)(new big_js_1.default(rawRate), monetary_js_1.Polkadot); case "KSM": return (0, interbtc_api_1.newCollateralBTCExchangeRate)(new big_js_1.default(rawRate), monetary_js_1.Kusama); } } /* Add wrappers here. Use keys matching the raw API call names to override those APIs with the wrappers. */ const explicitWrappers = (index, interBtcApi) => { return { getLatestSubmissionForEachOracle: (currencyKey) => __awaiter(void 0, void 0, void 0, function* () { const oracleStatus = yield index.getLatestSubmissionForEachOracle({ currencyKey }); return oracleStatus.map((rawStatus) => { const exchangeRate = constructExchangeRate(currencyKey, rawStatus.exchangeRate); return Object.assign(Object.assign({}, rawStatus), { exchangeRate }); }); }), getLatestSubmission: (currencyKey) => __awaiter(void 0, void 0, void 0, function* () { const submission = yield index.getLatestSubmission({ currencyKey }); const exchangeRate = constructExchangeRate(currencyKey, submission.exchangeRate); return Object.assign(Object.assign({}, submission), { exchangeRate }); }), currentVaultData: () => __awaiter(void 0, void 0, void 0, function* () { const indexCachedVaults = yield index.currentVaultData(); return indexCachedVaults.map((indexVault) => { const decodedVaultId = (0, interbtc_api_1.decodeVaultId)(interBtcApi.api, indexVault.id); const vaultId = decodedVaultId(interBtcApi.api, indexVault.id); const collateralCurrency = (0, interbtc_api_1.currencyIdToMonetaryCurrency)(vaultId.currencies.collateral); return new interbtc_api_1.VaultExt(interBtcApi.api, interBtcApi.oracle, interBtcApi.system, indexVault.wallet, (0, interbtc_api_1.newMonetaryAmount)(indexVault.backingCollateral, collateralCurrency), decodedVaultId, indexVault.status, indexVault.bannedUntil === null ? undefined : indexVault.bannedUntil, new monetary_js_1.BitcoinAmount(monetary_js_1.Bitcoin, indexVault.toBeIssuedTokens), new monetary_js_1.BitcoinAmount(monetary_js_1.Bitcoin, indexVault.issuedTokens), new monetary_js_1.BitcoinAmount(monetary_js_1.Bitcoin, indexVault.toBeRedeemedTokens), new monetary_js_1.BitcoinAmount(monetary_js_1.Bitcoin, indexVault.toBeReplacedTokens), (0, interbtc_api_1.newMonetaryAmount)(indexVault.replaceCollateral, collateralCurrency), (0, interbtc_api_1.newMonetaryAmount)(indexVault.liquidatedCollateral, collateralCurrency)); }); }), getIssues: (requestParameters) => __awaiter(void 0, void 0, void 0, function* () { const issues = yield index.getIssues(requestParameters); return issues.map(issue => indexIssueToTypedIssue(interBtcApi.api, issue)); }), getRedeems: (requestParameters) => __awaiter(void 0, void 0, void 0, function* () { const redeems = yield index.getRedeems(requestParameters); return redeems.map(redeem => indexRedeemToTypedRedeem(interBtcApi.api, redeem)); }), getFilteredIssues: (requestParameters) => __awaiter(void 0, void 0, void 0, function* () { const issues = yield index.getFilteredIssues(requestParameters); return issues.map(issue => indexIssueToTypedIssue(interBtcApi.api, issue)); }), getFilteredRedeems: (requestParameters) => __awaiter(void 0, void 0, void 0, function* () { const redeems = yield index.getFilteredRedeems(requestParameters); return redeems.map(redeem => indexRedeemToTypedRedeem(interBtcApi.api, redeem)); }), getRecentDailyIssues: (requestParameters) => __awaiter(void 0, void 0, void 0, function* () { const issues = yield index.getRecentDailyIssues(requestParameters); return issues.map(satoshisToBtcTimeData); }), getRecentDailyRedeems: (requestParameters) => __awaiter(void 0, void 0, void 0, function* () { const redeems = yield index.getRecentDailyRedeems(requestParameters); return redeems.map(satoshisToBtcTimeData); }), getDustValue: () => __awaiter(void 0, void 0, void 0, function* () { // the returned string contains double-quotes (e.g. `"100"`), which must be removed const parsedDustValue = (yield index.getDustValue()).split('"').join(""); return monetary_js_1.BitcoinAmount.from.Satoshi(parsedDustValue); }), }; }; /* Rest of the file autogenerates thin wrappers for the rest of the API calls and takes care of the typing. */ // The generated client contains the following autogenerated middleware helpers, which must be filtered out const GeneratedMiddlewareFns = ["withMiddleware", "withPreMiddleware", "withPostMiddleware"]; const DefaultIndexAPI = (configuration, interBtcApi) => { const config = new interbtcIndex.Configuration(Object.assign(Object.assign({}, configuration), { // use custom `fetchAPI`, that works both in browser and in node fetchApi: require("isomorphic-fetch"), // there is a bug in the generator, where the middleware must at least be an empty array, instead of `undefined` middleware: [] })); const index = new interbtcIndex.IndexApi(config); const instantiatedExplicitWrappers = explicitWrappers(index, interBtcApi); const excludeFromThinWrappers = (key) => Object.keys(explicitWrappers).includes(key) || GeneratedMiddlewareFns.includes(key) || key.includes("Raw") || key === "constructor"; const keys = Object.getOwnPropertyNames(Object.getPrototypeOf(index)).filter((apiName) => !excludeFromThinWrappers(apiName)); const thinWrappers = Object.fromEntries(keys.map((apiName) => { return [ apiName, // all functions only have one arg // eslint-disable-next-line @typescript-eslint/no-explicit-any (arg) => { return index[apiName](arg); }, ]; })); return Object.assign(Object.assign({}, thinWrappers), instantiatedExplicitWrappers); }; exports.DefaultIndexAPI = DefaultIndexAPI; function indexIssueToTypedIssue(api, issue) { // TODO determine collateralCurrency based on vault return Object.assign(Object.assign({}, issue), { wrappedAmount: monetary_js_1.BitcoinAmount.from.Satoshi(issue.wrappedAmount), bridgeFee: monetary_js_1.BitcoinAmount.from.Satoshi(issue.bridgeFee), // NOTE: KusamaAmount is hardcoded here, theoretically need a way to pass "ParachainCurrency" // In practice though this will be deprecated before we move off Kusama, hence an extra parameter is not being added for this griefingCollateral: monetary_js_1.KusamaAmount.from.KSM(issue.griefingCollateral), btcAmountSubmittedByUser: issue.btcAmountSubmittedByUser ? monetary_js_1.BitcoinAmount.from.Satoshi(issue.btcAmountSubmittedByUser) : undefined, refundAmountWrapped: issue.refundAmountWrapped ? monetary_js_1.BitcoinAmount.from.Satoshi(issue.refundAmountWrapped) : undefined, executedAmountWrapped: issue.executedAmountWrapped ? monetary_js_1.BitcoinAmount.from.Satoshi(issue.executedAmountWrapped) : undefined, userParachainAddress: issue.userParachainAddress, vaultId: (0, interbtc_api_1.decodeVaultId)(api, issue.vaultId) }); } exports.indexIssueToTypedIssue = indexIssueToTypedIssue; function indexRedeemToTypedRedeem(api, redeem) { const vaultId = (0, interbtc_api_1.decodeVaultId)(api, redeem.vaultId); const vaultCollateralCurrency = (0, interbtc_api_1.currencyIdToMonetaryCurrency)(vaultId.currencies.collateral); const newVaultCollateralAmount = (0, currencyMapper_1.currencyFactory)(vaultCollateralCurrency); return Object.assign(Object.assign({}, redeem), { amountBTC: monetary_js_1.BitcoinAmount.from.Satoshi(redeem.amountBTC), collateralPremium: newVaultCollateralAmount(redeem.collateralPremium, true), bridgeFee: monetary_js_1.BitcoinAmount.from.Satoshi(redeem.bridgeFee), btcTransferFee: monetary_js_1.BitcoinAmount.from.Satoshi(redeem.btcTransferFee), userParachainAddress: redeem.userParachainAddress, vaultId: (0, interbtc_api_1.decodeVaultId)(api, redeem.vaultId) }); } exports.indexRedeemToTypedRedeem = indexRedeemToTypedRedeem; function satoshisToBtcTimeData(data) { return { date: new Date(data.date), btc: monetary_js_1.BitcoinAmount.from.Satoshi(data.sat), }; } exports.satoshisToBtcTimeData = satoshisToBtcTimeData; //# sourceMappingURL=interbtcIndex.js.map