UNPKG

raiden-ts

Version:

Raiden Light Client Typescript/Javascript SDK

207 lines 10.4 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (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; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.intervalFromConfig = exports.makeDefaultConfig = exports.PartialRaidenConfig = exports.RaidenConfig = void 0; const constants_1 = require("@ethersproject/constants"); const units_1 = require("@ethersproject/units"); const t = __importStar(require("io-ts")); const operators_1 = require("rxjs/operators"); const constants_2 = require("./constants"); const types_1 = require("./services/types"); const utils_1 = require("./transfers/epics/utils"); const types_2 = require("./transport/types"); const types_3 = require("./utils/types"); const RTCIceServer = t.type({ urls: t.union([t.string, t.array(t.string)]) }); /** * A Raiden configuration object with required and optional params from [[PartialRaidenConfig]]. * * Notice partial/undefined values are special: when a raidenConfigUpdate is called with an * undefined value, it won't be set, as they can't be [re]stored in the JSON state, but instead * means it'll be *reset* to the default value; therefore, if a partial value has a defined * default, it can't be unset; if you want to support "empty" values, use null, empty string or * other falsy serializable types, and/or ensure it never gets a default * * - matrixServerLookup - Matrix server URL to fetch existing matrix servers from. * After intializing a [[Raiden]] instance, the matrix server can't be changed later on. * - revealTimeout - Timeout for secrets to be revealed (in seconds) * - expiryFactor - Multiply revealTimeout to get how far in the future transfer expiration should be * - httpTimeout - Used in http fetch requests * - additionalServices - Array of extra services URLs (or addresses, if URL set on SecretRegistry) * - pfsMode - One of 'disabled' (disables PFS usage and notifications), 'auto' (notifies all of * registered and additionalServices, picks cheapest for transfers without explicit pfs), * or 'onlyAdditional' (notifies all, but pick first responding from additionalServices only). * - pfsSafetyMargin - Safety margin to be added to fees received from PFS. Either a fee * multiplier, or a [fee, amount] pair ofmultipliers. Use `1.1` to add a 10% over estimated fee * margin, or `[0.03, 0.0005]` to add a 3% over fee plus 0.05% over amount. * - pfsMaxPaths - Limit number of paths requested from PFS for a route. * - pfsMaxFee - Maximum fee we're willing to pay a PFS for a route (in SVT/RDN wei) * - pfsIouTimeout - Number of seconds to timeout an IOU to a PFS. * - confirmationBlocks - How many blocks to wait before considering a transaction as confirmed * - monitoringReward - Reward to be paid to MS, in SVT/RDN; use Zero or null to disable * - logger - String specifying the console log level of redux-logger. Use '' to silence. * - caps - Own transport capabilities overrides. Set to null to disable all, including defaults * - fallbackIceServers - STUN servers to be used as a fallback for WebRTC * - rateToSvt - Exchange rate between tokens and SVT, in wei: e.g. rate[TKN]=2e18 => 1TKN = 2SVT * - pollingInterval - Interval at which to poll ETH provider for new blocks/events (milliseconds) * Honored only at start time * - minimumAllowance - Minimum value to call `approve` on tokens; default to MaxUint256, so * approving tokens should be needed only once, trusting TokenNetwork's & UDC contracts; * Set to Zero to fallback to approving the strictly needed deposit amounts * - autoSettle - Whether to channelSettle.request settleable channels automatically * - autoUDCWithdraw - Whether to udcWithdraw.request planned withdraws automatically * - mediationFees - deps.mediationFeeCalculator config. It's typed as unknown because it'll be * validated and decoded by [[FeeModel.decodeConfig]]. * - encryptSecret - Whether to send secret encrypted to target by default on transfers * - matrixServer? - Specify a matrix server to use. * - subkey? - When using subkey, this sets the behavior when { subkey } option isn't explicitly * set in on-chain method calls. false (default) = use main key; true = use subkey * - gasPriceFactor - Multiplier to be applied over base estimated/average gasPrice; * post-EPI1559 (London), this gets applied only to `maxPriorityFeePerGas`, or eth-node's * `eth_gasPrice` returned value; default does nothing and let provider guess (e.g. Metamask); * default ethers `maxPriorityFeePerGas` is 2.5 Gwei */ exports.RaidenConfig = t.readonly(t.intersection([ t.type({ matrixServerLookup: t.string, revealTimeout: t.number, expiryFactor: t.number, httpTimeout: t.number, additionalServices: t.readonlyArray(t.union([types_3.Address, t.string])), pfsMode: types_1.PfsModeC, pfsSafetyMargin: t.union([t.number, t.tuple([t.number, t.number])]), pfsMaxPaths: t.number, pfsMaxFee: (0, types_3.UInt)(32), pfsIouTimeout: t.number, confirmationBlocks: t.number, monitoringReward: t.union([t.null, (0, types_3.UInt)(32)]), logger: t.keyof({ ['']: null, trace: null, debug: null, info: null, warn: null, error: null, }), caps: t.union([t.null, types_2.Caps]), fallbackIceServers: t.array(RTCIceServer), rateToSvt: t.record(t.string, (0, types_3.UInt)(32)), pollingInterval: t.number, minimumAllowance: (0, types_3.UInt)(32), autoSettle: t.boolean, autoUDCWithdraw: t.boolean, mediationFees: t.unknown, encryptSecret: t.boolean, }), t.partial({ matrixServer: t.string, subkey: t.boolean, gasPriceFactor: t.union([t.number, t.null]), }), ])); exports.PartialRaidenConfig = t.readonly(t.exact(t.partial({ ...exports.RaidenConfig.type.types['0'].props, ...exports.RaidenConfig.type.types['1'].props }))); const rollupChainIds = new Set([ 421611 /* arbitrum rinkeby */, 42161 /* arbitrum one/mainnet beta */, ]); /** * Create a RaidenConfig from some common options * * @param obj - Object containing common parameters for config * @param obj.network - ether's Network object for the current blockchain * @param overwrites - Overwrites values from default config * @returns A full config object */ function makeDefaultConfig({ network }, overwrites) { const matrixServerInfos = network.chainId === 1 ? 'https://raw.githubusercontent.com/raiden-network/raiden-service-bundle/master/known_servers/known_servers-production-v1.2.0.json' : 'https://raw.githubusercontent.com/raiden-network/raiden-service-bundle/master/known_servers/known_servers-development-v1.2.0.json'; // merge caps independently const caps = overwrites?.caps === null ? null : { [constants_2.Capabilities.DELIVERY]: 0, [constants_2.Capabilities.MEDIATE]: 0, [constants_2.Capabilities.TO_DEVICE]: 1, [constants_2.Capabilities.IMMUTABLE_METADATA]: 1, ...overwrites?.caps, }; return { matrixServerLookup: matrixServerInfos, revealTimeout: constants_2.DEFAULT_REVEAL_TIMEOUT, expiryFactor: 1.1, httpTimeout: 30e3, additionalServices: [], pfsMode: types_1.PfsMode.auto, pfsSafetyMargin: 1.0, pfsMaxPaths: constants_2.DEFAULT_PFS_MAX_PATHS, pfsMaxFee: (0, units_1.parseEther)('0.05'), pfsIouTimeout: constants_2.DEFAULT_PFS_IOU_TIMEOUT, confirmationBlocks: rollupChainIds.has(network.chainId) ? 0 : constants_2.DEFAULT_CONFIRMATIONS, monitoringReward: constants_2.DEFAULT_MS_REWARD, logger: 'info', fallbackIceServers: [{ urls: 'stun:stun.l.google.com:19302' }], rateToSvt: {}, pollingInterval: constants_2.DEFAULT_POLLING_INTERVAL * 1e3, minimumAllowance: constants_1.MaxUint256, autoSettle: false, autoUDCWithdraw: true, mediationFees: {}, encryptSecret: true, ...overwrites, caps, // merged caps overwrites 'overwrites.caps' }; } exports.makeDefaultConfig = makeDefaultConfig; /** * A function which returns an Iterable of intervals based on parameters in config$ * By default, it returns an iterable which, on every iterator call, will return a new * exponentialBackoff iterator ranging from config.pollingInterval to 2 * config.httpTimeout * * @param config$ - Config-like observable, emiting objects with pollingInterval & httpTimeout * @returns Iterable (resettable) of intervals */ function intervalFromConfig(config$) { const self = { _iter: undefined, [Symbol.iterator]() { this._iter = undefined; return this; }, next() { if (!this._iter) { let lowerInterval = 5e3; let upperInterval = 60e3; config$.pipe((0, operators_1.first)()).subscribe(({ pollingInterval, httpTimeout }) => { lowerInterval = pollingInterval; upperInterval = 2 * httpTimeout; }); this._iter = (0, utils_1.exponentialBackoff)(lowerInterval, upperInterval); } return this._iter.next(); }, }; return self; } exports.intervalFromConfig = intervalFromConfig; //# sourceMappingURL=config.js.map