UNPKG

equation-sdk

Version:

🛠 An SDK for building applications on top of Equation.

2,561 lines (2,551 loc) • 133 kB
import { ethers, BigNumber } from 'ethers'; import Decimal from 'decimal.js'; import _, { cloneDeep } from 'lodash-es'; import dayjs from 'dayjs'; import { parseUnits as parseUnits$1 } from 'ethers/lib/utils'; import axios from 'axios'; import 'qs'; import { MaxUint256 } from '@ethersproject/constants'; import invariant from 'tiny-invariant'; function _regeneratorRuntime() { _regeneratorRuntime = function () { return e; }; var t, e = {}, r = Object.prototype, n = r.hasOwnProperty, o = Object.defineProperty || function (t, e, r) { t[e] = r.value; }, i = "function" == typeof Symbol ? Symbol : {}, a = i.iterator || "@@iterator", c = i.asyncIterator || "@@asyncIterator", u = i.toStringTag || "@@toStringTag"; function define(t, e, r) { return Object.defineProperty(t, e, { value: r, enumerable: !0, configurable: !0, writable: !0 }), t[e]; } try { define({}, ""); } catch (t) { define = function (t, e, r) { return t[e] = r; }; } function wrap(t, e, r, n) { var i = e && e.prototype instanceof Generator ? e : Generator, a = Object.create(i.prototype), c = new Context(n || []); return o(a, "_invoke", { value: makeInvokeMethod(t, r, c) }), a; } function tryCatch(t, e, r) { try { return { type: "normal", arg: t.call(e, r) }; } catch (t) { return { type: "throw", arg: t }; } } e.wrap = wrap; var h = "suspendedStart", l = "suspendedYield", f = "executing", s = "completed", y = {}; function Generator() {} function GeneratorFunction() {} function GeneratorFunctionPrototype() {} var p = {}; define(p, a, function () { return this; }); var d = Object.getPrototypeOf, v = d && d(d(values([]))); v && v !== r && n.call(v, a) && (p = v); var g = GeneratorFunctionPrototype.prototype = Generator.prototype = Object.create(p); function defineIteratorMethods(t) { ["next", "throw", "return"].forEach(function (e) { define(t, e, function (t) { return this._invoke(e, t); }); }); } function AsyncIterator(t, e) { function invoke(r, o, i, a) { var c = tryCatch(t[r], t, o); if ("throw" !== c.type) { var u = c.arg, h = u.value; return h && "object" == typeof h && n.call(h, "__await") ? e.resolve(h.__await).then(function (t) { invoke("next", t, i, a); }, function (t) { invoke("throw", t, i, a); }) : e.resolve(h).then(function (t) { u.value = t, i(u); }, function (t) { return invoke("throw", t, i, a); }); } a(c.arg); } var r; o(this, "_invoke", { value: function (t, n) { function callInvokeWithMethodAndArg() { return new e(function (e, r) { invoke(t, n, e, r); }); } return r = r ? r.then(callInvokeWithMethodAndArg, callInvokeWithMethodAndArg) : callInvokeWithMethodAndArg(); } }); } function makeInvokeMethod(e, r, n) { var o = h; return function (i, a) { if (o === f) throw new Error("Generator is already running"); if (o === s) { if ("throw" === i) throw a; return { value: t, done: !0 }; } for (n.method = i, n.arg = a;;) { var c = n.delegate; if (c) { var u = maybeInvokeDelegate(c, n); if (u) { if (u === y) continue; return u; } } if ("next" === n.method) n.sent = n._sent = n.arg;else if ("throw" === n.method) { if (o === h) throw o = s, n.arg; n.dispatchException(n.arg); } else "return" === n.method && n.abrupt("return", n.arg); o = f; var p = tryCatch(e, r, n); if ("normal" === p.type) { if (o = n.done ? s : l, p.arg === y) continue; return { value: p.arg, done: n.done }; } "throw" === p.type && (o = s, n.method = "throw", n.arg = p.arg); } }; } function maybeInvokeDelegate(e, r) { var n = r.method, o = e.iterator[n]; if (o === t) return r.delegate = null, "throw" === n && e.iterator.return && (r.method = "return", r.arg = t, maybeInvokeDelegate(e, r), "throw" === r.method) || "return" !== n && (r.method = "throw", r.arg = new TypeError("The iterator does not provide a '" + n + "' method")), y; var i = tryCatch(o, e.iterator, r.arg); if ("throw" === i.type) return r.method = "throw", r.arg = i.arg, r.delegate = null, y; var a = i.arg; return a ? a.done ? (r[e.resultName] = a.value, r.next = e.nextLoc, "return" !== r.method && (r.method = "next", r.arg = t), r.delegate = null, y) : a : (r.method = "throw", r.arg = new TypeError("iterator result is not an object"), r.delegate = null, y); } function pushTryEntry(t) { var e = { tryLoc: t[0] }; 1 in t && (e.catchLoc = t[1]), 2 in t && (e.finallyLoc = t[2], e.afterLoc = t[3]), this.tryEntries.push(e); } function resetTryEntry(t) { var e = t.completion || {}; e.type = "normal", delete e.arg, t.completion = e; } function Context(t) { this.tryEntries = [{ tryLoc: "root" }], t.forEach(pushTryEntry, this), this.reset(!0); } function values(e) { if (e || "" === e) { var r = e[a]; if (r) return r.call(e); if ("function" == typeof e.next) return e; if (!isNaN(e.length)) { var o = -1, i = function next() { for (; ++o < e.length;) if (n.call(e, o)) return next.value = e[o], next.done = !1, next; return next.value = t, next.done = !0, next; }; return i.next = i; } } throw new TypeError(typeof e + " is not iterable"); } return GeneratorFunction.prototype = GeneratorFunctionPrototype, o(g, "constructor", { value: GeneratorFunctionPrototype, configurable: !0 }), o(GeneratorFunctionPrototype, "constructor", { value: GeneratorFunction, configurable: !0 }), GeneratorFunction.displayName = define(GeneratorFunctionPrototype, u, "GeneratorFunction"), e.isGeneratorFunction = function (t) { var e = "function" == typeof t && t.constructor; return !!e && (e === GeneratorFunction || "GeneratorFunction" === (e.displayName || e.name)); }, e.mark = function (t) { return Object.setPrototypeOf ? Object.setPrototypeOf(t, GeneratorFunctionPrototype) : (t.__proto__ = GeneratorFunctionPrototype, define(t, u, "GeneratorFunction")), t.prototype = Object.create(g), t; }, e.awrap = function (t) { return { __await: t }; }, defineIteratorMethods(AsyncIterator.prototype), define(AsyncIterator.prototype, c, function () { return this; }), e.AsyncIterator = AsyncIterator, e.async = function (t, r, n, o, i) { void 0 === i && (i = Promise); var a = new AsyncIterator(wrap(t, r, n, o), i); return e.isGeneratorFunction(r) ? a : a.next().then(function (t) { return t.done ? t.value : a.next(); }); }, defineIteratorMethods(g), define(g, u, "Generator"), define(g, a, function () { return this; }), define(g, "toString", function () { return "[object Generator]"; }), e.keys = function (t) { var e = Object(t), r = []; for (var n in e) r.push(n); return r.reverse(), function next() { for (; r.length;) { var t = r.pop(); if (t in e) return next.value = t, next.done = !1, next; } return next.done = !0, next; }; }, e.values = values, Context.prototype = { constructor: Context, reset: function (e) { if (this.prev = 0, this.next = 0, this.sent = this._sent = t, this.done = !1, this.delegate = null, this.method = "next", this.arg = t, this.tryEntries.forEach(resetTryEntry), !e) for (var r in this) "t" === r.charAt(0) && n.call(this, r) && !isNaN(+r.slice(1)) && (this[r] = t); }, stop: function () { this.done = !0; var t = this.tryEntries[0].completion; if ("throw" === t.type) throw t.arg; return this.rval; }, dispatchException: function (e) { if (this.done) throw e; var r = this; function handle(n, o) { return a.type = "throw", a.arg = e, r.next = n, o && (r.method = "next", r.arg = t), !!o; } for (var o = this.tryEntries.length - 1; o >= 0; --o) { var i = this.tryEntries[o], a = i.completion; if ("root" === i.tryLoc) return handle("end"); if (i.tryLoc <= this.prev) { var c = n.call(i, "catchLoc"), u = n.call(i, "finallyLoc"); if (c && u) { if (this.prev < i.catchLoc) return handle(i.catchLoc, !0); if (this.prev < i.finallyLoc) return handle(i.finallyLoc); } else if (c) { if (this.prev < i.catchLoc) return handle(i.catchLoc, !0); } else { if (!u) throw new Error("try statement without catch or finally"); if (this.prev < i.finallyLoc) return handle(i.finallyLoc); } } } }, abrupt: function (t, e) { for (var r = this.tryEntries.length - 1; r >= 0; --r) { var o = this.tryEntries[r]; if (o.tryLoc <= this.prev && n.call(o, "finallyLoc") && this.prev < o.finallyLoc) { var i = o; break; } } i && ("break" === t || "continue" === t) && i.tryLoc <= e && e <= i.finallyLoc && (i = null); var a = i ? i.completion : {}; return a.type = t, a.arg = e, i ? (this.method = "next", this.next = i.finallyLoc, y) : this.complete(a); }, complete: function (t, e) { if ("throw" === t.type) throw t.arg; return "break" === t.type || "continue" === t.type ? this.next = t.arg : "return" === t.type ? (this.rval = this.arg = t.arg, this.method = "return", this.next = "end") : "normal" === t.type && e && (this.next = e), y; }, finish: function (t) { for (var e = this.tryEntries.length - 1; e >= 0; --e) { var r = this.tryEntries[e]; if (r.finallyLoc === t) return this.complete(r.completion, r.afterLoc), resetTryEntry(r), y; } }, catch: function (t) { for (var e = this.tryEntries.length - 1; e >= 0; --e) { var r = this.tryEntries[e]; if (r.tryLoc === t) { var n = r.completion; if ("throw" === n.type) { var o = n.arg; resetTryEntry(r); } return o; } } throw new Error("illegal catch attempt"); }, delegateYield: function (e, r, n) { return this.delegate = { iterator: values(e), resultName: r, nextLoc: n }, "next" === this.method && (this.arg = t), y; } }, e; } function _toPrimitive(t, r) { if ("object" != typeof t || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != typeof i) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); } function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == typeof i ? i : String(i); } function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { Promise.resolve(value).then(_next, _throw); } } function _asyncToGenerator(fn) { return function () { var self = this, args = arguments; return new Promise(function (resolve, reject) { var gen = fn.apply(self, args); function _next(value) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value); } function _throw(err) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err); } _next(undefined); }); }; } function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, _toPropertyKey(descriptor.key), descriptor); } } function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); Object.defineProperty(Constructor, "prototype", { writable: false }); return Constructor; } function _extends() { _extends = Object.assign ? Object.assign.bind() : function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); } /** * Represents the side of an equation. */ var Side; (function (Side) { Side[Side["LONG"] = 1] = "LONG"; Side[Side["SHORT"] = 2] = "SHORT"; })(Side || (Side = {})); /** * Flips the side of an equation. * @param side The original side of the equation. * @returns The flipped side of the equation. */ var SideFlip = function SideFlip(side) { return side === Side.LONG ? Side.SHORT : Side.LONG; }; var Order_Status; (function (Order_Status) { Order_Status["Created"] = "CREATED"; Order_Status["Executed"] = "EXECUTED"; Order_Status["Cancelled"] = "CANCELLED"; })(Order_Status || (Order_Status = {})); var Q96 = '79228162514264337593543950336'; var BASIS_POINTS_DIVISOR = 100000000; var CONFIG = { ChainId: 421614, ApiURL: "https://api-goldfish-sepolia.metacontract.pro/v1", RpcURL: "https://arbitrum-sepolia.blockpi.network/v1/rpc/public", BackendHost: "https://api-goldfish-sepolia.metacontract.pro/v1", PositionRouter: '0x46bE3DfFDcfbBe3A393d2d66Ee33F28375fc29F5', OrderBook: '0xA0A8051160a2C6CcC881e50cF37C8d137b79E5DC', Router: '0x6889a9bE8bf089B961811122Ed2813041eC06355', USDT: '0x130a10D76E53eC70C2d1c05e9C2EcfB5C3350fe0' }; var DEFAULT_PRECISION = 18; var DEFAULT_USER_PRECISION = 6; var DEFAULT_QUOTE_PRECISION = 6; var QUOTE_USD_PRECISION = 6; var DEFAULT_SETTING_SLIPPAGE = '0.30'; var DEFAULT_QUOTE_AMOUNT = '1000000'; var Web3Provider = /*#__PURE__*/function () { function Web3Provider() { var _globalThis$bitkeep; globalThis.ethereum = (globalThis == null ? void 0 : globalThis.ethereum) || {}; this.supportedProviderList = { okx: globalThis == null ? void 0 : globalThis.okxwallet, bybit: globalThis == null ? void 0 : globalThis.bybitWallet, bitkeep: globalThis == null || (_globalThis$bitkeep = globalThis.bitkeep) == null ? void 0 : _globalThis$bitkeep.ethereum, injected: globalThis.ethereum, privateKey: 'privateKey' }; } /** * Retrieves the provider from the window object. * @returns {Object} The provider object. */ var _proto = Web3Provider.prototype; _proto.getProvider = function getProvider() { return globalThis.ethereum; } /** * Connects to the Web3Provider based on the provided provider name. * @param {string} providerName - The name of the provider. * @param {string} [privateKey] - Private key. * @returns {<string>} - Create a new Web3Provider. * @throws {Error} - Throws an error if the provider is not supported. */; _proto.connect = /*#__PURE__*/ function () { var _connect = /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee(providerName, privateKey) { var supportProviderNames, providerArb; return _regeneratorRuntime().wrap(function _callee$(_context) { while (1) switch (_context.prev = _context.next) { case 0: if (privateKey === void 0) { privateKey = null; } supportProviderNames = Object.keys(this.supportedProviderList).join(','); if (supportProviderNames.includes(providerName)) { _context.next = 4; break; } throw new Error("Provider not supported,only support " + supportProviderNames + " now"); case 4: if (this.supportedProviderList[providerName]) { _context.next = 6; break; } throw new Error("No " + providerName + " provider found"); case 6: if (!(providerName === 'privateKey' && privateKey)) { _context.next = 11; break; } providerArb = new ethers.providers.JsonRpcProvider(CONFIG.RpcURL); return _context.abrupt("return", new ethers.Wallet(privateKey, providerArb)); case 11: return _context.abrupt("return", new ethers.providers.Web3Provider(this.supportedProviderList[providerName]).getSigner()); case 12: case "end": return _context.stop(); } }, _callee, this); })); function connect(_x, _x2) { return _connect.apply(this, arguments); } return connect; }(); return Web3Provider; }(); function catchFn(func, defaultValue) { try { var result = func(); return result; } catch (error) { return defaultValue; } } /** * For Contract: value * 2^96 * @param value * @param tokenDecimals * @param usdDecimals * @returns */ function computePriceX96(value, tokenDecimals, usdDecimals) { try { return new Decimal(value).mul(new Decimal(10).pow(usdDecimals)).div(new Decimal(10).pow(tokenDecimals)).mul(new Decimal(2).pow(96)).toFixed(0); } catch (_unused) { return '0'; } } /** * For interface: priceX96 / 2^96 * @param priceX96 * @param tokenDecimals * @param usdDecimals * @returns */ function calculatePrice(priceX96, tokenDecimals, usdDecimals) { try { return new Decimal(priceX96.toString()).div(new Decimal(2).pow(96)).mul(new Decimal(10).pow(tokenDecimals)).div(new Decimal(10).pow(usdDecimals)).toFixed(); } catch (_unused2) { return '0'; } } var PositionRouterABI = [ { inputs: [ { internalType: "contract IERC20", name: "_usd", type: "address" }, { internalType: "contract Router", name: "_router", type: "address" }, { internalType: "contract IMarketManager", name: "_marketManager", type: "address" }, { internalType: "uint256", name: "_minExecutionFee", type: "uint256" } ], stateMutability: "nonpayable", type: "constructor" }, { inputs: [ { internalType: "address", name: "target", type: "address" } ], name: "AddressEmptyCode", type: "error" }, { inputs: [ { internalType: "address", name: "account", type: "address" } ], name: "AddressInsufficientBalance", type: "error" }, { inputs: [ { internalType: "uint256", name: "expiredAt", type: "uint256" } ], name: "Expired", type: "error" }, { inputs: [ ], name: "FailedInnerCall", type: "error" }, { inputs: [ ], name: "Forbidden", type: "error" }, { inputs: [ { internalType: "uint256", name: "available", type: "uint256" }, { internalType: "uint256", name: "required", type: "uint256" } ], name: "InsufficientExecutionFee", type: "error" }, { inputs: [ { internalType: "uint256", name: "available", type: "uint256" }, { internalType: "uint256", name: "required", type: "uint256" } ], name: "InvalidExecutionFee", type: "error" }, { inputs: [ { internalType: "uint128", name: "margin", type: "uint128" }, { internalType: "uint128", name: "acceptableMinMargin", type: "uint128" } ], name: "InvalidMargin", type: "error" }, { inputs: [ { internalType: "Side", name: "side", type: "uint8" } ], name: "InvalidSide", type: "error" }, { inputs: [ { internalType: "uint160", name: "tradePriceX96", type: "uint160" }, { internalType: "uint160", name: "acceptableTradePriceX96", type: "uint160" } ], name: "InvalidTradePrice", type: "error" }, { inputs: [ { internalType: "contract IMarketDescriptor", name: "market", type: "address" }, { internalType: "address", name: "account", type: "address" } ], name: "LiquidityNotFound", type: "error" }, { inputs: [ { internalType: "contract IMarketDescriptor", name: "market", type: "address" }, { internalType: "address", name: "account", type: "address" }, { internalType: "Side", name: "side", type: "uint8" } ], name: "PositionNotFound", type: "error" }, { inputs: [ ], name: "ReentrancyGuardReentrantCall", type: "error" }, { inputs: [ { internalType: "uint8", name: "bits", type: "uint8" }, { internalType: "uint256", name: "value", type: "uint256" } ], name: "SafeCastOverflowedUintDowncast", type: "error" }, { inputs: [ { internalType: "address", name: "token", type: "address" } ], name: "SafeERC20FailedOperation", type: "error" }, { inputs: [ { internalType: "uint256", name: "earliest", type: "uint256" } ], name: "TooEarly", type: "error" }, { anonymous: false, inputs: [ { indexed: true, internalType: "address", name: "previousGov", type: "address" }, { indexed: true, internalType: "address", name: "newGov", type: "address" } ], name: "ChangeGovStarted", type: "event" }, { anonymous: false, inputs: [ { indexed: true, internalType: "uint128", name: "index", type: "uint128" }, { indexed: false, internalType: "address payable", name: "executionFeeReceiver", type: "address" } ], name: "DecreaseLiquidityPositionCancelled", type: "event" }, { anonymous: false, inputs: [ { indexed: true, internalType: "address", name: "account", type: "address" }, { indexed: true, internalType: "contract IMarketDescriptor", name: "market", type: "address" }, { indexed: false, internalType: "uint128", name: "marginDelta", type: "uint128" }, { indexed: false, internalType: "uint256", name: "liquidityDelta", type: "uint256" }, { indexed: false, internalType: "uint256", name: "acceptableMinMargin", type: "uint256" }, { indexed: false, internalType: "address", name: "receiver", type: "address" }, { indexed: false, internalType: "uint256", name: "executionFee", type: "uint256" }, { indexed: true, internalType: "uint128", name: "index", type: "uint128" } ], name: "DecreaseLiquidityPositionCreated", type: "event" }, { anonymous: false, inputs: [ { indexed: true, internalType: "uint128", name: "index", type: "uint128" }, { indexed: false, internalType: "address payable", name: "executionFeeReceiver", type: "address" } ], name: "DecreaseLiquidityPositionExecuted", type: "event" }, { anonymous: false, inputs: [ { indexed: true, internalType: "uint128", name: "index", type: "uint128" }, { indexed: false, internalType: "address payable", name: "executionFeeReceiver", type: "address" } ], name: "DecreasePositionCancelled", type: "event" }, { anonymous: false, inputs: [ { indexed: true, internalType: "address", name: "account", type: "address" }, { indexed: true, internalType: "contract IMarketDescriptor", name: "market", type: "address" }, { indexed: false, internalType: "Side", name: "side", type: "uint8" }, { indexed: false, internalType: "uint128", name: "marginDelta", type: "uint128" }, { indexed: false, internalType: "uint128", name: "sizeDelta", type: "uint128" }, { indexed: false, internalType: "uint160", name: "acceptableTradePriceX96", type: "uint160" }, { indexed: false, internalType: "address", name: "receiver", type: "address" }, { indexed: false, internalType: "uint256", name: "executionFee", type: "uint256" }, { indexed: true, internalType: "uint128", name: "index", type: "uint128" } ], name: "DecreasePositionCreated", type: "event" }, { anonymous: false, inputs: [ { indexed: true, internalType: "uint128", name: "index", type: "uint128" }, { indexed: false, internalType: "address payable", name: "executionFeeReceiver", type: "address" } ], name: "DecreasePositionExecuted", type: "event" }, { anonymous: false, inputs: [ { indexed: false, internalType: "uint32", name: "minBlockDelayExecutor", type: "uint32" }, { indexed: false, internalType: "uint32", name: "minTimeDelayPublic", type: "uint32" }, { indexed: false, internalType: "uint32", name: "maxTimeDelay", type: "uint32" } ], name: "DelayValuesUpdated", type: "event" }, { anonymous: false, inputs: [ { indexed: true, internalType: "address", name: "previousGov", type: "address" }, { indexed: true, internalType: "address", name: "newGov", type: "address" } ], name: "GovChanged", type: "event" }, { anonymous: false, inputs: [ { indexed: true, internalType: "uint128", name: "index", type: "uint128" }, { indexed: false, internalType: "address payable", name: "executionFeeReceiver", type: "address" } ], name: "IncreaseLiquidityPositionCancelled", type: "event" }, { anonymous: false, inputs: [ { indexed: true, internalType: "address", name: "account", type: "address" }, { indexed: true, internalType: "contract IMarketDescriptor", name: "market", type: "address" }, { indexed: false, internalType: "uint128", name: "marginDelta", type: "uint128" }, { indexed: false, internalType: "uint256", name: "liquidityDelta", type: "uint256" }, { indexed: false, internalType: "uint256", name: "acceptableMinMargin", type: "uint256" }, { indexed: false, internalType: "uint256", name: "executionFee", type: "uint256" }, { indexed: true, internalType: "uint128", name: "index", type: "uint128" } ], name: "IncreaseLiquidityPositionCreated", type: "event" }, { anonymous: false, inputs: [ { indexed: true, internalType: "uint128", name: "index", type: "uint128" }, { indexed: false, internalType: "address payable", name: "executionFeeReceiver", type: "address" } ], name: "IncreaseLiquidityPositionExecuted", type: "event" }, { anonymous: false, inputs: [ { indexed: true, internalType: "uint128", name: "index", type: "uint128" }, { indexed: false, internalType: "address payable", name: "executionFeeReceiver", type: "address" } ], name: "IncreasePositionCancelled", type: "event" }, { anonymous: false, inputs: [ { indexed: true, internalType: "address", name: "account", type: "address" }, { indexed: true, internalType: "contract IMarketDescriptor", name: "market", type: "address" }, { indexed: false, internalType: "Side", name: "side", type: "uint8" }, { indexed: false, internalType: "uint128", name: "marginDelta", type: "uint128" }, { indexed: false, internalType: "uint128", name: "sizeDelta", type: "uint128" }, { indexed: false, internalType: "uint160", name: "acceptableTradePriceX96", type: "uint160" }, { indexed: false, internalType: "uint256", name: "executionFee", type: "uint256" }, { indexed: true, internalType: "uint128", name: "index", type: "uint128" } ], name: "IncreasePositionCreated", type: "event" }, { anonymous: false, inputs: [ { indexed: true, internalType: "uint128", name: "index", type: "uint128" }, { indexed: false, internalType: "address payable", name: "executionFeeReceiver", type: "address" } ], name: "IncreasePositionExecuted", type: "event" }, { anonymous: false, inputs: [ { indexed: false, internalType: "uint256", name: "minExecutionFee", type: "uint256" } ], name: "MinExecutionFeeUpdated", type: "event" }, { anonymous: false, inputs: [ { indexed: true, internalType: "address", name: "account", type: "address" }, { indexed: false, internalType: "bool", name: "active", type: "bool" } ], name: "PositionExecutorUpdated", type: "event" }, { inputs: [ ], name: "acceptGov", outputs: [ ], stateMutability: "nonpayable", type: "function" }, { inputs: [ { internalType: "uint128", name: "_index", type: "uint128" }, { internalType: "address payable", name: "_executionFeeReceiver", type: "address" } ], name: "cancelDecreaseLiquidityPosition", outputs: [ { internalType: "bool", name: "", type: "bool" } ], stateMutability: "nonpayable", type: "function" }, { inputs: [ { internalType: "uint128", name: "_index", type: "uint128" }, { internalType: "address payable", name: "_executionFeeReceiver", type: "address" } ], name: "cancelDecreasePosition", outputs: [ { internalType: "bool", name: "", type: "bool" } ], stateMutability: "nonpayable", type: "function" }, { inputs: [ { internalType: "uint128", name: "_index", type: "uint128" }, { internalType: "address payable", name: "_executionFeeReceiver", type: "address" } ], name: "cancelIncreaseLiquidityPosition", outputs: [ { internalType: "bool", name: "", type: "bool" } ], stateMutability: "nonpayable", type: "function" }, { inputs: [ { internalType: "uint128", name: "_index", type: "uint128" }, { internalType: "address payable", name: "_executionFeeReceiver", type: "address" } ], name: "cancelIncreasePosition", outputs: [ { internalType: "bool", name: "", type: "bool" } ], stateMutability: "nonpayable", type: "function" }, { inputs: [ { internalType: "address", name: "_newGov", type: "address" } ], name: "changeGov", outputs: [ ], stateMutability: "nonpayable", type: "function" }, { inputs: [ { internalType: "contract IMarketDescriptor[]", name: "markets", type: "address[]" }, { internalType: "address", name: "receiver", type: "address" } ], name: "createCloseLiquidityPositionsBatch", outputs: [ { internalType: "uint128[]", name: "indices", type: "uint128[]" } ], stateMutability: "payable", type: "function" }, { inputs: [ { components: [ { internalType: "contract IMarketDescriptor", name: "market", type: "address" }, { internalType: "Side", name: "side", type: "uint8" } ], internalType: "struct IPositionRouter.ClosePositionParameter[]", name: "parameters", type: "tuple[]" }, { internalType: "address", name: "receiver", type: "address" } ], name: "createClosePositionsBatch", outputs: [ { internalType: "uint128[]", name: "indices", type: "uint128[]" } ], stateMutability: "payable", type: "function" }, { inputs: [ { internalType: "contract IMarketDescriptor", name: "_market", type: "address" }, { internalType: "uint128", name: "_marginDelta", type: "uint128" }, { internalType: "uint128", name: "_liquidityDelta", type: "uint128" }, { internalType: "uint128", name: "_acceptableMinMargin", type: "uint128" }, { internalType: "address", name: "_receiver", type: "address" } ], name: "createDecreaseLiquidityPosition", outputs: [ { internalType: "uint128", name: "index", type: "uint128" } ], stateMutability: "payable", type: "function" }, { inputs: [ { internalType: "contract IMarketDescriptor", name: "_market", type: "address" }, { internalType: "Side", name: "_side", type: "uint8" }, { internalType: "uint128", name: "_marginDelta", type: "uint128" }, { internalType: "uint128", name: "_sizeDelta", type: "uint128" }, { internalType: "uint160", name: "_acceptableTradePriceX96", type: "uint160" }, { internalType: "address", name: "_receiver", type: "address" } ], name: "createDecreasePosition", outputs: [ { internalType: "uint128", name: "index", type: "uint128" } ], stateMutability: "payable", type: "function" }, { inputs: [ { internalType: "contract IMarketDescriptor", name: "_market", type: "address" }, { internalType: "uint128", name: "_marginDelta", type: "uint128" }, { internalType: "uint128", name: "_liquidityDelta", type: "uint128" }, { internalType: "uint128", name: "_acceptableMinMargin", type: "uint128" } ], name: "createIncreaseLiquidityPosition", outputs: [ { internalType: "uint128", name: "index", type: "uint128" } ], stateMutability: "payable", type: "function" }, { inputs: [ { internalType: "contract IMarketDescriptor", name: "_market", type: "address" }, { internalType: "Side", name: "_side", type: "uint8" }, { internalType: "uint128", name: "_marginDelta", type: "uint128" }, { internalType: "uint128", name: "_sizeDelta", type: "uint128" }, { internalType: "uint160", name: "_acceptableTradePriceX96", type: "uint160" } ], name: "createIncreasePosition", outputs: [ { internalType: "uint128", name: "index", type: "uint128" } ], stateMutability: "payable", type: "function" }, { inputs: [ ], name: "decreaseLiquidityPositionIndex", outputs: [ { internalType: "uint128", name: "", type: "uint128" } ], stateMutability: "view", type: "function" }, { inputs: [ ], name: "decreaseLiquidityPositionIndexNext", outputs: [ { internalType: "uint128", name: "", type: "uint128" } ], stateMutability: "view", type: "function" }, { inputs: [ { internalType: "uint128", name: "", type: "uint128" } ], name: "decreaseLiquidityPositionRequests", outputs: [ { internalType: "address", name: "account", type: "address" }, { internalType: "contract IMarketDescriptor", name: "market", type: "address" }, { internalType: "uint128", name: "marginDelta", type: "uint128" }, { internalType: "uint128", name: "liquidityDelta", type: "uint128" }, { internalType: "uint128", name: "acceptableMinMargin", type: "uint128" }, { internalType: "uint256", name: "executionFee", type: "uint256" }, { internalType: "uint96", name: "blockNumber", type: "uint96" }, { internalType: "uint64", name: "blockTime", type: "uint64" }, { internalType: "address", name: "receiver", type: "address" } ], stateMutability: "view", type: "function" }, { inputs: [ ], name: "decreasePositionIndex", outputs: [ { internalType: "uint128", name: "", type: "uint128" } ], stateMutability: "view", type: "function" }, { inputs: [ ], name: "decreasePositionIndexNext", outputs: [ { internalType: "uint128", name: "", type: "uint128" } ], stateMutability: "view", type: "function" }, { inputs: [ { internalType: "uint128", name: "", type: "uint128" } ], name: "decreasePositionRequests", outputs: [ { internalType: "address", name: "account", type: "address" }, { internalType: "contract IMarketDescriptor", name: "market", type: "address" }, { internalType: "Side", name: "side", type: "uint8" }, { internalType: "uint128", name: "marginDelta", type: "uint128" }, { internalType: "uint128", name: "sizeDelta", type: "uint128" }, { internalType: "uint160", name: "acceptableTradePriceX96", type: "uint160" }, { internalType: "uint256", name: "executionFee", type: "uint256" }, { internalType: "uint96", name: "blockNumber", type: "uint96" }, { internalType: "uint64", name: "blockTime", type: "uint64" }, { internalType: "address", name: "receiver", type: "address" } ], stateMutability: "view", type: "function" }, { inputs: [ { internalType: "contract IMarketDescriptor", name: "_market", type: "address" }, { internalType: "uint128", name: "_liquidityDelta", type: "uint128" }, { internalType: "address", name: "_receiver", type: "address" } ], name: "executeDecreaseLiquidationFundPosition", outputs: [ ], stateMutability: "nonpayable", type: "function" }, { inputs: [ { internalType: "uint128", name: "_index", type: "uint128" }, { internalType: "address payable", name: "_executionFeeReceiver", type: "address" } ], name: "executeDecreaseLiquidityPosition", outputs: [ { internalType: "bool", name: "", type: "bool" } ], stateMutability: "nonpayable", type: "function" }, { inputs: [ { internalType: "uint128", name: "_endIndex", type: "uint128" }, { internalType: "address payable", name: "_executionFeeReceiver", type: "address" } ], name: "executeDecreaseLiquidityPositions", outputs: [ ], stateMutability: "nonpayable", type: "function" }, { inputs: [ { internalType: "uint128", name: "_index", type: "uint128" }, { internalType: "address payable", name: "_executionFeeReceiver", type: "address" } ], name: "executeDecreasePosition", outputs: [ { internalType: "bool", name: "", type: "bool" } ], stateMutability: "nonpayable", type: "function" }, { inputs: [ { internalType: "uint128", name: "_endIndex", type: "uint128" }, { internalType: "address payable", name: "_executionFeeReceiver", type: "address" } ], name: "executeDecreasePositions", outputs: [ ], stateMutability: "nonpayable", type: "function" }, { inputs: [ { internalType: "contract IMarketDescriptor", name: "_market", type: "address" }, { internalType: "uint128", name: "_liquidityDelta", type: "uint128" } ], name: "executeIncreaseLiquidationFundPosition", outputs: [ ], stateMutability: "nonpayable", type: "function" }, { inputs: [ { internalType: "uint128", name: "_index", type: "uint128" }, { internalType: "address payable", name: "_executionFeeReceiver", type: "address" } ], name: "executeIncreaseLiquidityPosition", outputs: [ { internalType: "bool", name: "", type: "bool" } ], stateMutability: "nonpayable", type: "function" }, { inputs: [ { internalType: "uint128", name: "_endIndex", type: "uint128" }, { internalType: "address payable", name: "_executionFeeReceiver", type: "address" } ], name: "executeIncreaseLiquidityPositions", outputs: [ ], stateMutability: "nonpayable", type: "function" }, { inputs: [ { internalType: "uint128", name: "_index", type: "uint128" }, { internalType: "address payable", name: "_executionFeeReceiver", type: "address" } ], name: "executeIncreasePosition", outputs: [ { internalType: "bool", name: "", type: "bool" } ], stateMutability: "nonpayable", type: "function" }, { inputs: [ { internalType: "uint128", name: "_endIndex", type: "uint128" }, { internalType: "address payable", name: "_executionFeeReceiver", type: "address" } ], name: "executeIncreasePositions", outputs: [ ], stateMutability: "nonpayable", type: "function" }, { inputs: [ ], name: "executionGasLimit", outputs: [ { internalType: "uint160", name: "", type: "uint160" } ], stateMutability: "view", type: "function" }, { inputs: [ ], name: "gov", outputs: [ { internalType: "address", name: "", type: "address" } ], stateMutability: "view", type: "function" }, { inputs: [ ], name: "increaseLiquidityPositionIndex", outputs: [ { internalType: "uint128", name: "", type: "uint128" } ], stateMutability: "view", type: "function" }, { inputs: [ ], name: "increaseLiquidityPositionIndexNext", outputs: [ { internalType: "uint128", name: "", type: "uint128" } ], stateMutability: "view", type: "function" }, { inputs: [ { internalType: "uint128", name: "", type: "uint128" } ], name: "increaseLiquidityPositionRequests", outputs: [ { internalType: "address", name: "account", type: "address" }, { internalType: "contract IMarketDescriptor", name: "market", type: "address" }, { internalType: "uint128", name: "marginDelta", type: "uint128" }, { internalType: "uint128", name: "liquidityDelta", type: "uint128" }, { internalType: "uint128", name: "acceptableMinMargin", type: "uint128" }, { internalType: "uint256", name: "executionFee", type: "uint256" }, { internalType: "uint96", name: "blockNumber", type: "uint96" }, { internalType: "uint64", name: "blockTime", type: "uint64" } ], stateMutability: "view", type: "function" }, { inputs: [ ], name: "increasePositionIndex", outputs: [ { internalType: "uint128", name: "", type: "uint128" } ], stateMutability: "view", type: "function" }, { inputs: [ ], name: "increasePositionIndexNext", outputs: [ { internalType: "uint128", name: "", type: "uint128" } ], stateMutability: "view", type: "function" }, { inputs: [ { internalType: "uint128", name: "", type: "uint128" } ], name: "increasePositionRequests", outputs: [ { internalType: "address", name: "account", type: "address" }, { internalType: "contract IMarketDescriptor", name: "market", type: "address" }, { internalType: "Side", name: "side", type: "uint8" }, { internalType: "uint128", name: "marginDelta", type: "uint128" }, { internalType: "uint128", name: "sizeDelta", type: "uint128" }, { internalType: "uint160", name: "acceptableTradePriceX96", type: "uint160" }, { internalType: "uint256", name: "executionFee", type: "uint256" }, { internalType: "uint96", name: "blockNumber", type: "uint96" }, { internalType: "uint64", name: "blockTime", type: "uint64" } ], stateMutability: "view", type: "function" }, { inputs: [ ], name: "marketManager", outputs: [ { internalType: "contract IMarketManager", name: "", type: "address" } ], stateMutability: "view", type: "function" }, { inputs: [ ], name: "maxTimeDelay", outputs: [ { internalType: "uint32", name: "", type: "uint32" } ], stateMutability: "view", type: "function" }, { inputs: [ ], name: "minBlockDelayExecutor", outputs: [ { internalType: "uint32", name: "", type: "uint32" } ], stateMutability: "view", type: "function" }, { inputs: [ ], name: "minExecutionFee", outputs: [ { internalType: "uint256", name: "", type: "uint256" } ], stateMutability: "view", type: "function" }, { inputs: [ ], name: "minTimeDelayPublic", outputs: [ { internalType: "uint32", name: "", type: "uint32" } ], stateMutability: "view", type: "function" }, { inputs: [ ], name: "pendingGov", outputs: [ { internalType: "address", name: "", type: "address" } ], stateMutability: "view", type: "function" }, { inputs: [ { internalType: "address", name: "", type: "address" } ], name: "positionExecutors", outputs: [ { internalType: "bool", name: "", type: "bool" } ], stateMutability: "view", type: "function" }, { inputs: [ ], name: "router", outputs: [ { internalType: "contract Router", name: "", type: "address" } ], stateMutability: "view", type: "function" }, { inputs: [ { internalType: "uint32", name: "_minBlockDelayExecutor", type: "uint32" }, { internalType: "uint32", name: "_minTimeDelayPublic", type: "uint32" }, { internalType: "uint32", name: "_maxTimeDelay", type: "uint32" } ], name: "updateDelayValues", outputs: [ ], stateMutability: "nonpayable", type: "function" }, { inputs: [ { internalType: "uint160", name: "_executionGasLimit", type: "uint160" } ], name: "updateExecutionGasLimit", outputs: [ ], stateMutability: "nonpayable", type: "function" }, { inputs: [ { internalType: "uint256", name: "_minExecutionFee", type: "uint256" } ], name: "updateMinExecutionFee", outputs: [ ], stateMutability: "nonpayable", type: "function" }, { inputs: [ { internalType: "address", name: "_account", type: "address" }, { internalType: "bool", name: "_active", type: "bool" } ], name: "updatePositionExecutor", outputs: [ ], stateMutability: "nonpayable", type: "function" }, { inputs: [ ], name: "usd", outputs: [ { internalType: "contract IERC20", name: "", type: "address" } ], stateMutability: "view", type: "function" } ]; var RouterABI = [ { inputs: [ { internalType: "contract IEFC", name: "_EFC", type: "address" }, { internalType: "contract IMarketManager", name: "_marketManager", type: "address" } ], stateMutability: "nonpayable", type: "constructor" }, { inputs: [ { internalType: "address", name: "target", type: "address" } ], name: "AddressEmptyCode", type: "error" }, { inputs: [ { internalType: "address", name: "account", type: "address" } ], name: "AddressInsufficientBalance", type: "error" }, { inputs: [ ], name: "CallerUnauthorized", type: "error" }, { inputs: [ ], name: "FailedInnerCall", type: "error" }, { inputs: [ ], name: "Forbidden", type: "error" }, { inputs: [ { internalType: "address", name: "liquidator", type: "address" } ], name: "LiquidatorAlreadyRegistered", type: "error" }, { inputs: [ { internalType: "address", name: "liquidator", type: "address" } ], name: "LiquidatorNotRegistered", type: "error" }, { inputs: [ { internalType: "address", name: "owner", type: "address" }, { internalType: "address", name: "expectedOwner", type: "address" } ], name: "OwnerMismatch", type: "error" }, { inputs: [ { internalType: "address", name: "sender", type: "address" }, { internalType: "address", name: "plugin", type: "address" } ], name: "PluginAlreadyApproved", type: "error" }, { inputs: [ { internalType: "address", name: "plugin", type: "address" } ], name: "PluginAlreadyRegistered", type: "error" }, { inputs: [ { internalType: "address", name: "sender", type: "address" }, { internalType: "address", name: "plugin", type: "address" } ], name: "PluginNotApproved", type: "error" }, { inputs: [ { internalType: "address", name: "plugin", type: "address" } ], name: "PluginNotRegistered", type: "error" }, { inputs: [ { internalType: "address", name: "token", type: "address" } ], name: "SafeERC20FailedOperation", type: "error" }, { anonymous: false, inputs: [ { indexed: true, internalType: "address", name: "previousGov", type: "address" }, { indexed: true, internalType: "address", name: "newGov", type: "address" } ], name: "ChangeGovStarted", type: "event" }, { anonymous: false, inputs: [ { indexed: true, internalType: "address", name: "previousGov", type: "address" }, { indexed: true, internalType: "address", name: "newGov", type: "address" }