impermax-sdk 2.1.170 → 2.1.172

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/lib/index.d.ts CHANGED
@@ -46,4 +46,5 @@ import OffchainAccountLendingPoolV3 from "./offchain/account/lendingPool/offchai
46
46
  import OffchainAccountNftlpUniswapV3 from './offchain/account/lendingPool/nftlp/offchainAccountNftlpUniswapV3';
47
47
  import { MAX_TICK, MIN_TICK, formatPriceSqrtX96, uniV3TickToPrice } from "./utils/nftlpMath/uniswapV3General";
48
48
  import UniswapV3Position from './utils/position/uniswapV3';
49
- export { offchain, OffchainMultichain, Offchain, OffchainLendingPool, OffchainPoolToken, OffchainCollateral, OffchainBorrowable, OffchainVault, OffchainLeveragedVault, OffchainLendingVault, OffchainHedgedVault, OffchainConfigManager, OffchainPairConfig, OffchainProposal, OffchainMultichainAccount, OffchainAccount, OffchainAccountLendingPool, OffchainAccountPoolToken, OffchainAccountCollateral, OffchainAccountBorrowable, OffchainLeveragedPosition, OffchainAccountVault, OffchainPriceHelper, OffchainSolidexHelper, PoolTokenData, BorrowableData, CollateralData, BorrowablePastData, TokenData, RewardData, PairData, FarmingPoolData, LendingPoolPastData, LendingPoolData, CollateralPosition, SupplyPosition, BorrowPosition, UserData, TvlData, XimxData, LendingVaultData, VaultPosition, PairConfigData, ProposalData, ProposalMetadata, VaultRisk, VaultType, onchain, Onchain, OnchainLendingPool, OnchainPoolToken, OnchainBorrowable, OnchainCollateral, OnchainLendingVault, OnchainConfigManager, OnchainProposal, OnchainPairConfig, OnchainAccount, OnchainAccountLendingPool, OnchainAccountPoolToken, OnchainAccountBorrowable, OnchainAccountCollateral, OnchainAccountLendingVault, OnchainContractsHelper, OnchainImpermaxFactory, OnchainInteractions, OnchainInteractionsLendingPool, OnchainInteractionsPoolToken, OnchainInteractionsCollateral, OnchainInteractionsBorrowable, OnchainInteractionsLendingVault, OnchainInteractionsConfigManager, Contract, ApprovalType, PermitData, OnchainConfig, Changes, NO_CHANGES, Values, AirdropData, PendingRewardUI, CollateralConfig, BorrowableV2Config, BorrowableV3Config, PairConfig, CLAIM_AGGREGATOR, IMPERMAX_CHEF, IMX, IMPERMAX_FACTORY, getNetworkFactories, getFactoryByAddress, LENDING_VAULT_WATCHER, MERKLE_DISTRIBUTOR_IBEX, MERKLE_DISTRIBUTOR_IBEX_2, MERKLE_DISTRIBUTOR_ETH, ROUTER, SIMPLE_UNISWAP_ORACLE, WETH, MERKLE_URL_ETH, MERKLE_URL_IBEX, MERKLE_URL_IBEX_2, hrAmms, AMM_FACTORY, getAmmByFactory, AMM_SUBGRAPH_URLS, ADD_LIQUIDITY_URLS, AMM_LP_FEE, DEBANK_IDS, ETH_SYMBOL, ETH_NAME, SOLIDEX_FACTORIES, BORROW_FEE_FACTORIES, OLD_KINK_MULTIPLIER_FACTORIES, LIQUIDATION_FEE_FACTORIES, V2_FACTORIES, Farms, hrFarms, FarmIndex, STAKED_LP_FACTORY, getFarmByStakedLPFactory, NOT_SYNCED, DEADLINE, ZERO, APPROVE_AMOUNT, IMPERMAX_SUBGRAPH_URL, IMPERMAX_CHEF_SUBGRAPH_URL, BLOCKS_SUBGRAPH_URL, XIMX_SUBGRAPH_URL, Networks, Factory, Extension, PoolTokenType, Amms, Address, NetworkIndex, FactoryIndex, AddressIndex, NetworkFactoryIndex, LendingPoolIndex, AmmIndex, DistributorDetails, ProposalState, WhitelistState, VaultListOrderBy, VaultListParams, LendingPoolListOrderBy, LendingPoolListParams, SortDirection, LendingPoolVersion, PositionType, AccountPosition, PermitType, Permits, Permit, Borrowable, OnchainInteractionsLendingPoolV3, OnchainInteractionsNftlpUniswapV3, decimalToBalance, OffchainAccountLendingPoolV3, OffchainAccountNftlpUniswapV3, MAX_TICK, MIN_TICK, formatPriceSqrtX96, uniV3TickToPrice, isV3Factory, UniswapV3Position, OnchainAccountNftlpUniswapV3, OnchainAccountLendingPoolV3, OnchainAccountLendingPoolV2 };
49
+ import UniswapV2Position from './utils/position/uniswapV2';
50
+ export { offchain, OffchainMultichain, Offchain, OffchainLendingPool, OffchainPoolToken, OffchainCollateral, OffchainBorrowable, OffchainVault, OffchainLeveragedVault, OffchainLendingVault, OffchainHedgedVault, OffchainConfigManager, OffchainPairConfig, OffchainProposal, OffchainMultichainAccount, OffchainAccount, OffchainAccountLendingPool, OffchainAccountPoolToken, OffchainAccountCollateral, OffchainAccountBorrowable, OffchainLeveragedPosition, OffchainAccountVault, OffchainPriceHelper, OffchainSolidexHelper, PoolTokenData, BorrowableData, CollateralData, BorrowablePastData, TokenData, RewardData, PairData, FarmingPoolData, LendingPoolPastData, LendingPoolData, CollateralPosition, SupplyPosition, BorrowPosition, UserData, TvlData, XimxData, LendingVaultData, VaultPosition, PairConfigData, ProposalData, ProposalMetadata, VaultRisk, VaultType, onchain, Onchain, OnchainLendingPool, OnchainPoolToken, OnchainBorrowable, OnchainCollateral, OnchainLendingVault, OnchainConfigManager, OnchainProposal, OnchainPairConfig, OnchainAccount, OnchainAccountLendingPool, OnchainAccountLendingPoolV2, OnchainAccountLendingPoolV3, OnchainAccountNftlpUniswapV3, OnchainAccountPoolToken, OnchainAccountBorrowable, OnchainAccountCollateral, OnchainAccountLendingVault, OnchainContractsHelper, OnchainImpermaxFactory, OnchainInteractions, OnchainInteractionsLendingPool, OnchainInteractionsPoolToken, OnchainInteractionsCollateral, OnchainInteractionsBorrowable, OnchainInteractionsLendingVault, OnchainInteractionsConfigManager, Contract, ApprovalType, PermitData, OnchainConfig, Changes, NO_CHANGES, Values, AirdropData, PendingRewardUI, CollateralConfig, BorrowableV2Config, BorrowableV3Config, PairConfig, CLAIM_AGGREGATOR, IMPERMAX_CHEF, IMX, IMPERMAX_FACTORY, getNetworkFactories, getFactoryByAddress, LENDING_VAULT_WATCHER, MERKLE_DISTRIBUTOR_IBEX, MERKLE_DISTRIBUTOR_IBEX_2, MERKLE_DISTRIBUTOR_ETH, ROUTER, SIMPLE_UNISWAP_ORACLE, WETH, MERKLE_URL_ETH, MERKLE_URL_IBEX, MERKLE_URL_IBEX_2, hrAmms, AMM_FACTORY, getAmmByFactory, AMM_SUBGRAPH_URLS, ADD_LIQUIDITY_URLS, AMM_LP_FEE, DEBANK_IDS, ETH_SYMBOL, ETH_NAME, SOLIDEX_FACTORIES, BORROW_FEE_FACTORIES, OLD_KINK_MULTIPLIER_FACTORIES, LIQUIDATION_FEE_FACTORIES, V2_FACTORIES, Farms, hrFarms, FarmIndex, STAKED_LP_FACTORY, getFarmByStakedLPFactory, NOT_SYNCED, DEADLINE, ZERO, APPROVE_AMOUNT, IMPERMAX_SUBGRAPH_URL, IMPERMAX_CHEF_SUBGRAPH_URL, BLOCKS_SUBGRAPH_URL, XIMX_SUBGRAPH_URL, Networks, Factory, Extension, PoolTokenType, Amms, Address, NetworkIndex, FactoryIndex, AddressIndex, NetworkFactoryIndex, LendingPoolIndex, AmmIndex, DistributorDetails, ProposalState, WhitelistState, VaultListOrderBy, VaultListParams, LendingPoolListOrderBy, LendingPoolListParams, SortDirection, LendingPoolVersion, PositionType, AccountPosition, PermitType, Permits, Permit, Borrowable, OnchainInteractionsLendingPoolV3, OnchainInteractionsNftlpUniswapV3, decimalToBalance, OffchainAccountLendingPoolV3, OffchainAccountNftlpUniswapV3, MAX_TICK, MIN_TICK, formatPriceSqrtX96, uniV3TickToPrice, isV3Factory, UniswapV3Position, UniswapV2Position, };
package/lib/index.js CHANGED
@@ -26,9 +26,9 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
26
26
  return (mod && mod.__esModule) ? mod : { "default": mod };
27
27
  };
28
28
  Object.defineProperty(exports, "__esModule", { value: true });
29
- exports.OnchainInteractionsLendingVault = exports.OnchainInteractionsBorrowable = exports.OnchainInteractionsCollateral = exports.OnchainInteractionsPoolToken = exports.OnchainInteractionsLendingPool = exports.OnchainInteractions = exports.OnchainImpermaxFactory = exports.OnchainContractsHelper = exports.OnchainAccountLendingVault = exports.OnchainAccountCollateral = exports.OnchainAccountBorrowable = exports.OnchainAccountPoolToken = exports.OnchainAccountLendingPool = exports.OnchainAccount = exports.OnchainPairConfig = exports.OnchainProposal = exports.OnchainConfigManager = exports.OnchainLendingVault = exports.OnchainCollateral = exports.OnchainBorrowable = exports.OnchainPoolToken = exports.OnchainLendingPool = exports.Onchain = exports.onchain = exports.VaultType = exports.VaultRisk = exports.OffchainSolidexHelper = exports.OffchainPriceHelper = exports.OffchainAccountVault = exports.OffchainLeveragedPosition = exports.OffchainAccountBorrowable = exports.OffchainAccountCollateral = exports.OffchainAccountPoolToken = exports.OffchainAccountLendingPool = exports.OffchainAccount = exports.OffchainMultichainAccount = exports.OffchainProposal = exports.OffchainPairConfig = exports.OffchainConfigManager = exports.OffchainHedgedVault = exports.OffchainLendingVault = exports.OffchainLeveragedVault = exports.OffchainVault = exports.OffchainBorrowable = exports.OffchainCollateral = exports.OffchainPoolToken = exports.OffchainLendingPool = exports.Offchain = exports.OffchainMultichain = exports.offchain = void 0;
30
- exports.Amms = exports.PoolTokenType = exports.Extension = exports.Factory = exports.Networks = exports.XIMX_SUBGRAPH_URL = exports.BLOCKS_SUBGRAPH_URL = exports.IMPERMAX_CHEF_SUBGRAPH_URL = exports.IMPERMAX_SUBGRAPH_URL = exports.APPROVE_AMOUNT = exports.ZERO = exports.DEADLINE = exports.NOT_SYNCED = exports.getFarmByStakedLPFactory = exports.STAKED_LP_FACTORY = exports.hrFarms = exports.Farms = exports.V2_FACTORIES = exports.LIQUIDATION_FEE_FACTORIES = exports.OLD_KINK_MULTIPLIER_FACTORIES = exports.BORROW_FEE_FACTORIES = exports.SOLIDEX_FACTORIES = exports.ETH_NAME = exports.ETH_SYMBOL = exports.DEBANK_IDS = exports.AMM_LP_FEE = exports.ADD_LIQUIDITY_URLS = exports.AMM_SUBGRAPH_URLS = exports.getAmmByFactory = exports.AMM_FACTORY = exports.hrAmms = exports.MERKLE_URL_IBEX_2 = exports.MERKLE_URL_IBEX = exports.MERKLE_URL_ETH = exports.WETH = exports.SIMPLE_UNISWAP_ORACLE = exports.ROUTER = exports.MERKLE_DISTRIBUTOR_ETH = exports.MERKLE_DISTRIBUTOR_IBEX_2 = exports.MERKLE_DISTRIBUTOR_IBEX = exports.LENDING_VAULT_WATCHER = exports.getFactoryByAddress = exports.getNetworkFactories = exports.IMPERMAX_FACTORY = exports.IMX = exports.IMPERMAX_CHEF = exports.CLAIM_AGGREGATOR = exports.NO_CHANGES = exports.ApprovalType = exports.OnchainInteractionsConfigManager = void 0;
31
- exports.OnchainAccountLendingPoolV2 = exports.OnchainAccountLendingPoolV3 = exports.OnchainAccountNftlpUniswapV3 = exports.UniswapV3Position = exports.isV3Factory = exports.uniV3TickToPrice = exports.formatPriceSqrtX96 = exports.MIN_TICK = exports.MAX_TICK = exports.OffchainAccountNftlpUniswapV3 = exports.OffchainAccountLendingPoolV3 = exports.decimalToBalance = exports.OnchainInteractionsNftlpUniswapV3 = exports.OnchainInteractionsLendingPoolV3 = exports.Borrowable = exports.PermitType = exports.PositionType = exports.LendingPoolVersion = exports.SortDirection = exports.LendingPoolListOrderBy = exports.VaultListOrderBy = exports.WhitelistState = exports.ProposalState = void 0;
29
+ exports.OnchainInteractionsPoolToken = exports.OnchainInteractionsLendingPool = exports.OnchainInteractions = exports.OnchainImpermaxFactory = exports.OnchainContractsHelper = exports.OnchainAccountLendingVault = exports.OnchainAccountCollateral = exports.OnchainAccountBorrowable = exports.OnchainAccountPoolToken = exports.OnchainAccountNftlpUniswapV3 = exports.OnchainAccountLendingPoolV3 = exports.OnchainAccountLendingPoolV2 = exports.OnchainAccountLendingPool = exports.OnchainAccount = exports.OnchainPairConfig = exports.OnchainProposal = exports.OnchainConfigManager = exports.OnchainLendingVault = exports.OnchainCollateral = exports.OnchainBorrowable = exports.OnchainPoolToken = exports.OnchainLendingPool = exports.Onchain = exports.onchain = exports.VaultType = exports.VaultRisk = exports.OffchainSolidexHelper = exports.OffchainPriceHelper = exports.OffchainAccountVault = exports.OffchainLeveragedPosition = exports.OffchainAccountBorrowable = exports.OffchainAccountCollateral = exports.OffchainAccountPoolToken = exports.OffchainAccountLendingPool = exports.OffchainAccount = exports.OffchainMultichainAccount = exports.OffchainProposal = exports.OffchainPairConfig = exports.OffchainConfigManager = exports.OffchainHedgedVault = exports.OffchainLendingVault = exports.OffchainLeveragedVault = exports.OffchainVault = exports.OffchainBorrowable = exports.OffchainCollateral = exports.OffchainPoolToken = exports.OffchainLendingPool = exports.Offchain = exports.OffchainMultichain = exports.offchain = void 0;
30
+ exports.Factory = exports.Networks = exports.XIMX_SUBGRAPH_URL = exports.BLOCKS_SUBGRAPH_URL = exports.IMPERMAX_CHEF_SUBGRAPH_URL = exports.IMPERMAX_SUBGRAPH_URL = exports.APPROVE_AMOUNT = exports.ZERO = exports.DEADLINE = exports.NOT_SYNCED = exports.getFarmByStakedLPFactory = exports.STAKED_LP_FACTORY = exports.hrFarms = exports.Farms = exports.V2_FACTORIES = exports.LIQUIDATION_FEE_FACTORIES = exports.OLD_KINK_MULTIPLIER_FACTORIES = exports.BORROW_FEE_FACTORIES = exports.SOLIDEX_FACTORIES = exports.ETH_NAME = exports.ETH_SYMBOL = exports.DEBANK_IDS = exports.AMM_LP_FEE = exports.ADD_LIQUIDITY_URLS = exports.AMM_SUBGRAPH_URLS = exports.getAmmByFactory = exports.AMM_FACTORY = exports.hrAmms = exports.MERKLE_URL_IBEX_2 = exports.MERKLE_URL_IBEX = exports.MERKLE_URL_ETH = exports.WETH = exports.SIMPLE_UNISWAP_ORACLE = exports.ROUTER = exports.MERKLE_DISTRIBUTOR_ETH = exports.MERKLE_DISTRIBUTOR_IBEX_2 = exports.MERKLE_DISTRIBUTOR_IBEX = exports.LENDING_VAULT_WATCHER = exports.getFactoryByAddress = exports.getNetworkFactories = exports.IMPERMAX_FACTORY = exports.IMX = exports.IMPERMAX_CHEF = exports.CLAIM_AGGREGATOR = exports.NO_CHANGES = exports.ApprovalType = exports.OnchainInteractionsConfigManager = exports.OnchainInteractionsLendingVault = exports.OnchainInteractionsBorrowable = exports.OnchainInteractionsCollateral = void 0;
31
+ exports.UniswapV2Position = exports.UniswapV3Position = exports.isV3Factory = exports.uniV3TickToPrice = exports.formatPriceSqrtX96 = exports.MIN_TICK = exports.MAX_TICK = exports.OffchainAccountNftlpUniswapV3 = exports.OffchainAccountLendingPoolV3 = exports.decimalToBalance = exports.OnchainInteractionsNftlpUniswapV3 = exports.OnchainInteractionsLendingPoolV3 = exports.Borrowable = exports.PermitType = exports.PositionType = exports.LendingPoolVersion = exports.SortDirection = exports.LendingPoolListOrderBy = exports.VaultListOrderBy = exports.WhitelistState = exports.ProposalState = exports.Amms = exports.PoolTokenType = exports.Extension = void 0;
32
32
  const offchain_1 = __importStar(require("./offchain"));
33
33
  exports.Offchain = offchain_1.default;
34
34
  Object.defineProperty(exports, "OffchainAccount", { enumerable: true, get: function () { return offchain_1.OffchainAccount; } });
@@ -203,3 +203,5 @@ Object.defineProperty(exports, "formatPriceSqrtX96", { enumerable: true, get: fu
203
203
  Object.defineProperty(exports, "uniV3TickToPrice", { enumerable: true, get: function () { return uniswapV3General_1.uniV3TickToPrice; } });
204
204
  const uniswapV3_1 = __importDefault(require("./utils/position/uniswapV3"));
205
205
  exports.UniswapV3Position = uniswapV3_1.default;
206
+ const uniswapV2_1 = __importDefault(require("./utils/position/uniswapV2"));
207
+ exports.UniswapV2Position = uniswapV2_1.default;
@@ -1 +1,2 @@
1
1
  export * from './uniswapV3';
2
+ export * from './uniswapV2';
@@ -15,3 +15,4 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
15
15
  };
16
16
  Object.defineProperty(exports, "__esModule", { value: true });
17
17
  __exportStar(require("./uniswapV3"), exports);
18
+ __exportStar(require("./uniswapV2"), exports);
@@ -0,0 +1,98 @@
1
+ import type { Position } from "../interface";
2
+ export declare class UniswapV2Position implements Position {
3
+ lockStateChange: boolean;
4
+ state: number;
5
+ /**
6
+ * NOTICE all values inside this class are normalized (no decimals or mantissa to take into account)
7
+ */
8
+ liquidity: number;
9
+ debtX: number;
10
+ debtY: number;
11
+ initialLiquidity: number;
12
+ initialDebtX: number;
13
+ initialDebtY: number;
14
+ marketPrice: number;
15
+ oraclePrice: number;
16
+ safetyMargin: number;
17
+ liquidationPenalty: number;
18
+ constructor(_liquidity: number, // lp amount fixed by exchangeRate and k
19
+ _debtX: number, _debtY: number, _marketPrice: number, _oraclePrice: number, _safetyMargin: number, _liquidationPenalty: number, _lockStateChange: boolean);
20
+ private checkLock;
21
+ /**
22
+ * PRIVATE SETTERS
23
+ */
24
+ private setRealXRealY;
25
+ private setRealX;
26
+ private setRealY;
27
+ private setDebtX;
28
+ private setDebtY;
29
+ /**
30
+ * PRIVATE GETTERS
31
+ */
32
+ private getRealXGivenLiquidityAndPrice;
33
+ private getRealYGivenLiquidityAndPrice;
34
+ private getRealXGivenLiquidity;
35
+ private getRealYGivenLiquidity;
36
+ private getRealXGivenPrice;
37
+ private getRealYGivenPrice;
38
+ private getRealX;
39
+ private getRealY;
40
+ private getInitialRealX;
41
+ private getInitialRealY;
42
+ private getValueGivenPriceAndAmounts;
43
+ private getValueGivenAmounts;
44
+ private getAmountXGivenValue;
45
+ private getAmountYGivenValue;
46
+ private getDebtValueGivenPrice;
47
+ private getCollateralValueGivenPrice;
48
+ private getEquityValueGivenPrice;
49
+ private getDebtValue;
50
+ private getCollateralValue;
51
+ private getEquityValue;
52
+ private getLiquidityPostLiquidationValueGivenPriceAndDebt;
53
+ private isLiquidatableGivenPriceAndDebt;
54
+ private isLiquidatableGivenDebt;
55
+ private isLiquidatableGivenPrice;
56
+ private isUnderwaterGivenPrice;
57
+ private getLiquidationPriceInRange;
58
+ private getMaxDeltaDebtInRange;
59
+ /**
60
+ * PUBLIC SETTERS
61
+ */
62
+ depositX(amount: number): void;
63
+ depositY(amount: number): void;
64
+ withdrawX(amount: number): void;
65
+ withdrawY(amount: number): void;
66
+ borrowX(amount: number): void;
67
+ borrowY(amount: number): void;
68
+ repayX(amount: number): void;
69
+ repayY(amount: number): void;
70
+ /**
71
+ * PUBLIC GETTERS
72
+ */
73
+ getDepositedX(): number;
74
+ getDepositedY(): number;
75
+ getNetX(): number;
76
+ getNetY(): number;
77
+ isLiquidatable(): boolean;
78
+ isUnderwater(): boolean;
79
+ getLiquidationRange(): {
80
+ priceA: number;
81
+ priceB: number;
82
+ } | undefined;
83
+ getLeverage(): number;
84
+ getOptimalLiquidity(amountX: number, amountY: number): {
85
+ liquidity: number;
86
+ amountX: number;
87
+ amountY: number;
88
+ };
89
+ getMaxLeverage(): number;
90
+ getMinLeverage(): number;
91
+ getMaxWithdrawable(): {
92
+ amountX: number;
93
+ amountY: number;
94
+ };
95
+ getMaxBorrowableX(): number;
96
+ getMaxBorrowableY(): number;
97
+ }
98
+ export default UniswapV2Position;
@@ -1,310 +1,332 @@
1
- // import type { Position } from "../interface";
2
- //
3
- // function getVirtualX(liquidity: number, price: number) {
4
- // return liquidity / Math.sqrt(price);
5
- // }
6
- // function getVirtualY(liquidity: number, price: number) {
7
- // return liquidity * Math.sqrt(price);
8
- // }
9
- // function getRealX(liquidity: number, price: number, priceA: number, priceB: number) {
10
- // const surplusX = getVirtualX(liquidity, priceB);
11
- // const virtualX = getVirtualX(liquidity, Math.max(price, priceA));
12
- // return Math.max(virtualX - surplusX, 0);
13
- // }
14
- // function getRealY(liquidity: number, price: number, priceA: number, priceB: number) {
15
- // const surplusY = getVirtualY(liquidity, priceA);
16
- // const virtualY = getVirtualY(liquidity, Math.min(price, priceB));
17
- // return Math.max(virtualY - surplusY, 0);
18
- // }
19
- //
20
- // const LOWEST_PRICE = 1 / 1e18;
21
- // const HIGHEST_PRICE = 1e18;
22
- //
23
- // export class UniswapV3Position implements Position {
24
- // public lockStateChange: boolean;
25
- //
26
- // /**
27
- // * NOTICE all values inside this class are normalized (no decimals or mantissa to take into account)
28
- // */
29
- // public liquidity: number;
30
- // public debtX: number;
31
- // public debtY: number;
32
- // public initialLiquidity: number;
33
- // public initialDebtX: number;
34
- // public initialDebtY: number;
35
- // public marketPrice: number;
36
- // public oraclePrice: number;
37
- // public priceA: number;
38
- // public priceB: number;
39
- // public safetyMargin: number;
40
- // public liquidationPenalty: number;
41
- //
42
- // // TODO add fee logic
43
- //
44
- // constructor(
45
- // _liquidity: number,
46
- // _debtX: number,
47
- // _debtY: number,
48
- // _marketPrice: number,
49
- // _oraclePrice: number,
50
- // _priceA: number,
51
- // _priceB: number,
52
- // _safetyMargin: number,
53
- // _liquidationPenalty: number,
54
- // _lockStateChange: boolean,
55
- // ) {
56
- // this.liquidity = _liquidity;
57
- // this.debtX = _debtX;
58
- // this.debtY = _debtY;
59
- // this.initialLiquidity = _liquidity;
60
- // this.initialDebtX = _debtX;
61
- // this.initialDebtY = _debtY;
62
- // this.marketPrice = _marketPrice;
63
- // this.oraclePrice = _oraclePrice;
64
- // this.priceA = _priceA;
65
- // this.priceB = _priceB;
66
- // this.safetyMargin = _safetyMargin;
67
- // this.liquidationPenalty = _liquidationPenalty;
68
- // this.lockStateChange = _lockStateChange;
69
- // }
70
- //
71
- // private checkLock() {
72
- // if (this.lockStateChange) throw Error("Can't change state of original position object");
73
- // else this.state++;
74
- // }
75
- //
76
- // /**
77
- // * PRIVATE
78
- // */
79
- //
80
- // private getRealXGivenLiquidityAndPrice(liquidity: number, price: number) {
81
- // return getRealX(liquidity, price, this.priceA, this.priceB);
82
- // }
83
- // private getRealYGivenLiquidityAndPrice(liquidity: number, price: number) {
84
- // return getRealY(liquidity, price, this.priceA, this.priceB);
85
- // }
86
- // private getRealXGivenLiquidity(liquidity: number) {
87
- // return this.getRealXGivenLiquidityAndPrice(liquidity, this.marketPrice);
88
- // }
89
- // private getRealYGivenLiquidity(liquidity: number) {
90
- // return this.getRealYGivenLiquidityAndPrice(liquidity, this.marketPrice);
91
- // }
92
- // private getRealXGivenPrice(price: number) {
93
- // return this.getRealXGivenLiquidityAndPrice(this.liquidity, price);
94
- // }
95
- // private getRealYGivenPrice(price: number) {
96
- // return this.getRealYGivenLiquidityAndPrice(this.liquidity, price);
97
- // }
98
- // private getInitialRealX() {
99
- // return this.getRealXGivenLiquidityAndPrice(this.initialLiquidity, this.marketPrice);
100
- // }
101
- // private getInitialRealY() {
102
- // return this.getRealYGivenLiquidityAndPrice(this.initialLiquidity, this.marketPrice);
103
- // }
104
- //
105
- // private getValueGivenPriceAndAmounts(price: number, amountX: number, amountY: number) : number {
106
- // return amountX * price + amountY;
107
- // }
108
- // private getValueGivenAmounts(amountX: number, amountY: number) : number {
109
- // return amountX * this.marketPrice + amountY;
110
- // }
111
- // private getDebtValueGivenPrice(price: number) {
112
- // return this.getValueGivenPriceAndAmounts(price, this.debtX, this.debtY);
113
- // }
114
- // private getCollateralValueGivenPrice(price: number) {
115
- // return this.getValueGivenPriceAndAmounts(price, this.getRealXGivenPrice(price), this.getRealYGivenPrice(price));
116
- // }
117
- // private getEquityValueGivenPrice(price: number) {
118
- // return this.getCollateralValueGivenPrice(price) - this.getDebtValueGivenPrice(price);
119
- // }
120
- // private getDebtValue() {
121
- // return this.getDebtValueGivenPrice(this.marketPrice);
122
- // }
123
- // private getCollateralValue() {
124
- // return this.getCollateralValueGivenPrice(this.marketPrice);
125
- // }
126
- // private getEquityValue() {
127
- // return this.getEquityValueGivenPrice(this.marketPrice);
128
- // }
129
- //
130
- //
131
- // private isLiquidatableGivenPrice(price: number) : boolean {
132
- // return this.getEquityValueGivenPrice(price / this.safetyMargin) < 0 || this.getEquityValueGivenPrice(price * this.safetyMargin) < 0;
133
- // }
134
- // private isUnderwaterGivenPrice(price: number) : boolean {
135
- // return this.getEquityValueGivenPrice(price) < 0;
136
- // }
137
- //
138
- // private getLiquidityGivenAmounts(amountX: number, amountY: number) : number {
139
- // if (amountX == 0) {
140
- // return amountY / Math.sqrt(this.marketPrice);
141
- // }
142
- // if (amountY == 0) {
143
- // return amountX * Math.sqrt(this.marketPrice);
144
- // }
145
- // return amountX / (1 / Math.sqrt(this.marketPrice) - 1 / Math.sqrt(this.priceB));
146
- // }
147
- //
148
- // private getLiquidationPriceInRange(lowPrice: number, highPrice: number, lowIsLiquidatable: boolean, highIsLiquidatable: boolean) : number | undefined {
149
- // if (lowIsLiquidatable == highIsLiquidatable) return undefined;
150
- // const avgPrice = Math.sqrt(lowPrice * highPrice);
151
- // if (lowPrice / highPrice > 0.9999) return avgPrice;
152
- // const avgIsLiquidatable = this.isLiquidatableGivenPrice(avgPrice);
153
- // return this.getLiquidationPriceInRange(lowPrice, avgPrice, lowIsLiquidatable, avgIsLiquidatable)
154
- // ?? this.getLiquidationPriceInRange(avgPrice, highPrice, avgIsLiquidatable, highIsLiquidatable);
155
- // }
156
- //
157
- // /**
158
- // * PUBLIC SETTERS
159
- // */
160
- //
161
- // // Setters
162
- // public setLiquidity(_liquidity: number) {
163
- // this.checkLock();
164
- // this.liquidity = _liquidity;
165
- // }
166
- // public setRealXRealY(amountX: number, amountY: number) {
167
- // this.checkLock();
168
- // const { liquidity } = this.getOptimalLiquidity(amountX, amountY);
169
- // this.liquidity = liquidity;
170
- // }
171
- // public setRealX(amountX: number) {
172
- // this.checkLock();
173
- // const { liquidity } = this.getOptimalLiquidity(amountX, Infinity);
174
- // this.liquidity = liquidity;
175
- // }
176
- // public setRealY(amountY: number) {
177
- // this.checkLock();
178
- // const { liquidity } = this.getOptimalLiquidity(Infinity, amountY);
179
- // this.liquidity = liquidity;
180
- // }
181
- // public setDebtX(_debtX: number) {
182
- // this.checkLock();
183
- // this.debtX = _debtX;
184
- // }
185
- // public setDebtY(_debtY: number) {
186
- // this.checkLock();
187
- // this.debtY = _debtY;
188
- // }
189
- //
190
- // // Actions
191
- // public depositX(amount : number) {
192
- // this.setRealX(this.getInitialRealX() + amount);
193
- // }
194
- // public depositY(amount : number) {
195
- // this.setRealY(this.getInitialRealY() + amount);
196
- // }
197
- // public withdrawX(amount : number) {
198
- // this.setRealX(Math.max(this.getInitialRealX() - amount, 0));
199
- // }
200
- // public withdrawY(amount : number) {
201
- // this.setRealY(Math.max(this.getInitialRealY() - amount, 0));
202
- // }
203
- // public borrowX(amount: number) {
204
- // this.setDebtX(this.initialDebtX + amount);
205
- // }
206
- // public borrowY(amount: number) {
207
- // this.setDebtY(this.initialDebtY + amount);
208
- // }
209
- // public repayX(amount: number) {
210
- // this.setDebtX(Math.max(this.initialDebtX - amount, 0));
211
- // }
212
- // public repayY(amount: number) {
213
- // this.setDebtY(Math.max(this.initialDebtY - amount, 0));
214
- // }
215
- //
216
- // /**
217
- // * PUBLIC GETTERS
218
- // */
219
- //
220
- // public getRealX() {
221
- // return this.getRealXGivenLiquidityAndPrice(this.liquidity, this.marketPrice);
222
- // }
223
- // public getRealY() {
224
- // return this.getRealYGivenLiquidityAndPrice(this.liquidity, this.marketPrice);
225
- // }
226
- //
227
- // public getNetX() {
228
- // return this.getRealX() - this.debtX;
229
- // }
230
- // public getNetY() {
231
- // return this.getRealY() - this.debtY;
232
- // }
233
- //
234
- // public isLiquidatable() {
235
- // return this.isLiquidatableGivenPrice(this.oraclePrice);
236
- // }
237
- // public isUnderwater() {
238
- // return this.isUnderwaterGivenPrice(this.marketPrice);
239
- // }
240
- //
241
- // public getLiquidationRange() : {
242
- // priceA: number,
243
- // priceB: number
244
- // } | undefined {
245
- // const isLiquidatable = this.isLiquidatable();
246
- // if (isLiquidatable) return undefined;
247
- // const lowIsLiquidatable = this.isLiquidatableGivenPrice(LOWEST_PRICE);
248
- // const highIsLiquidatable = this.isLiquidatableGivenPrice(HIGHEST_PRICE);
249
- // const priceA = this.getLiquidationPriceInRange(LOWEST_PRICE, this.marketPrice, lowIsLiquidatable, isLiquidatable);
250
- // const priceB = this.getLiquidationPriceInRange(this.marketPrice, HIGHEST_PRICE, isLiquidatable, highIsLiquidatable);
251
- // return {
252
- // priceA: priceA ? priceA : 0,
253
- // priceB: priceB ? priceB : Infinity,
254
- // }
255
- // }
256
- //
257
- // public getLeverage() {
258
- // return this.getCollateralValue() / this.getEquityValue();
259
- // }
260
- //
261
- // // amountX and amountY are expected to have the same sign
262
- // public getOptimalLiquidity(amountX: number, amountY: number) : {
263
- // liquidity: number,
264
- // amountX: number,
265
- // amountY: number
266
- // } {
267
- // if (amountX != 0 && amountY != 0) {
268
- // const sampleX = this.getRealXGivenLiquidity(1);
269
- // const sampleY = this.getRealYGivenLiquidity(1);
270
- // if (sampleX == 0) amountX = 0;
271
- // else if (sampleY == 0) amountY = 0;
272
- // else {
273
- // if (Math.abs(amountX) / sampleX > Math.abs(amountY) / sampleY) amountX = amountY / sampleY * sampleX;
274
- // else amountY = amountX / sampleX * sampleY;
275
- // }
276
- // }
277
- // const liquidity = this.getLiquidityGivenAmounts(amountX, amountY)
278
- // return {liquidity, amountX, amountY};
279
- // }
280
- //
281
- // public getMaxLeverage() : number {
282
- // // TODO binary search
283
- // return 1;
284
- // }
285
- // public getMinLeverage() : number {
286
- // const realX = this.getRealX();
287
- // const realY = this.getRealY();
288
- // if (realX > this.debtX && realY > this.debtY) return 0;
289
- // // TODO binary search
290
- // return 0;
291
- // }
292
- // public getMaxWithdrawable() : {
293
- // amountX: number,
294
- // amountY: number
295
- // } {
296
- // let amountX = 0, amountY = 0;
297
- // // TODO binary search
298
- // return {amountX, amountY};
299
- // }
300
- // public getMaxBorrowableX() : number {
301
- // // TODO binary search
302
- // return 0;
303
- // }
304
- // public getMaxBorrowableY() : number {
305
- // // TODO binary search
306
- // return 0;
307
- // }
308
- // }
309
- //
310
- // export default UniswapV3Position;
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.UniswapV2Position = void 0;
4
+ function getRealX(liquidity, price) {
5
+ return liquidity / Math.sqrt(price);
6
+ }
7
+ function getRealY(liquidity, price) {
8
+ return liquidity * Math.sqrt(price);
9
+ }
10
+ const LOWEST_PRICE = 1 / 1e18;
11
+ const HIGHEST_PRICE = 1e18;
12
+ class UniswapV2Position {
13
+ constructor(_liquidity, // lp amount fixed by exchangeRate and k
14
+ _debtX, _debtY, _marketPrice, _oraclePrice, _safetyMargin, _liquidationPenalty, _lockStateChange) {
15
+ this.liquidity = _liquidity;
16
+ this.debtX = _debtX;
17
+ this.debtY = _debtY;
18
+ this.initialLiquidity = _liquidity;
19
+ this.initialDebtX = _debtX;
20
+ this.initialDebtY = _debtY;
21
+ this.marketPrice = _marketPrice;
22
+ this.oraclePrice = _oraclePrice;
23
+ this.safetyMargin = _safetyMargin;
24
+ this.liquidationPenalty = _liquidationPenalty;
25
+ this.lockStateChange = _lockStateChange;
26
+ this.state = 0;
27
+ }
28
+ checkLock() {
29
+ if (this.lockStateChange)
30
+ throw Error("Can't change state of original position object");
31
+ else
32
+ this.state++;
33
+ }
34
+ /**
35
+ * PRIVATE SETTERS
36
+ */
37
+ setRealXRealY(amountX, amountY) {
38
+ if (Number.isNaN(amountX))
39
+ return;
40
+ if (Number.isNaN(amountY))
41
+ return;
42
+ this.checkLock();
43
+ const { liquidity } = this.getOptimalLiquidity(amountX, amountY);
44
+ this.liquidity = liquidity;
45
+ }
46
+ setRealX(amountX) {
47
+ if (Number.isNaN(amountX))
48
+ return;
49
+ this.checkLock();
50
+ const { liquidity } = this.getOptimalLiquidity(amountX, Infinity);
51
+ this.liquidity = liquidity;
52
+ }
53
+ setRealY(amountY) {
54
+ if (Number.isNaN(amountY))
55
+ return;
56
+ this.checkLock();
57
+ const { liquidity } = this.getOptimalLiquidity(Infinity, amountY);
58
+ this.liquidity = liquidity;
59
+ }
60
+ setDebtX(_debtX) {
61
+ if (Number.isNaN(_debtX))
62
+ return;
63
+ this.checkLock();
64
+ this.debtX = _debtX;
65
+ }
66
+ setDebtY(_debtY) {
67
+ if (Number.isNaN(_debtY))
68
+ return;
69
+ this.checkLock();
70
+ this.debtY = _debtY;
71
+ }
72
+ /**
73
+ * PRIVATE GETTERS
74
+ */
75
+ getRealXGivenLiquidityAndPrice(liquidity, price) {
76
+ return getRealX(liquidity, price);
77
+ }
78
+ getRealYGivenLiquidityAndPrice(liquidity, price) {
79
+ return getRealY(liquidity, price);
80
+ }
81
+ getRealXGivenLiquidity(liquidity) {
82
+ return this.getRealXGivenLiquidityAndPrice(liquidity, this.marketPrice);
83
+ }
84
+ getRealYGivenLiquidity(liquidity) {
85
+ return this.getRealYGivenLiquidityAndPrice(liquidity, this.marketPrice);
86
+ }
87
+ getRealXGivenPrice(price) {
88
+ return this.getRealXGivenLiquidityAndPrice(this.liquidity, price);
89
+ }
90
+ getRealYGivenPrice(price) {
91
+ return this.getRealYGivenLiquidityAndPrice(this.liquidity, price);
92
+ }
93
+ getRealX() {
94
+ return this.getRealXGivenLiquidityAndPrice(this.liquidity, this.marketPrice);
95
+ }
96
+ getRealY() {
97
+ return this.getRealYGivenLiquidityAndPrice(this.liquidity, this.marketPrice);
98
+ }
99
+ getInitialRealX() {
100
+ return this.getRealXGivenLiquidityAndPrice(this.initialLiquidity, this.marketPrice);
101
+ }
102
+ getInitialRealY() {
103
+ return this.getRealYGivenLiquidityAndPrice(this.initialLiquidity, this.marketPrice);
104
+ }
105
+ getValueGivenPriceAndAmounts(price, amountX, amountY) {
106
+ return amountX * price + amountY;
107
+ }
108
+ getValueGivenAmounts(amountX, amountY) {
109
+ return amountX * this.marketPrice + amountY;
110
+ }
111
+ getAmountXGivenValue(value) {
112
+ return value / this.marketPrice;
113
+ }
114
+ getAmountYGivenValue(value) {
115
+ return value;
116
+ }
117
+ getDebtValueGivenPrice(price) {
118
+ return this.getValueGivenPriceAndAmounts(price, this.debtX, this.debtY);
119
+ }
120
+ getCollateralValueGivenPrice(price) {
121
+ return this.getValueGivenPriceAndAmounts(price, this.getRealXGivenPrice(price), this.getRealYGivenPrice(price));
122
+ }
123
+ getEquityValueGivenPrice(price) {
124
+ return this.getCollateralValueGivenPrice(price) - this.getDebtValueGivenPrice(price);
125
+ }
126
+ getDebtValue() {
127
+ return this.getDebtValueGivenPrice(this.marketPrice);
128
+ }
129
+ getCollateralValue() {
130
+ return this.getCollateralValueGivenPrice(this.marketPrice);
131
+ }
132
+ getEquityValue() {
133
+ return this.getEquityValueGivenPrice(this.marketPrice);
134
+ }
135
+ getLiquidityPostLiquidationValueGivenPriceAndDebt(price, debtX, debtY) {
136
+ const debtValue = this.getValueGivenPriceAndAmounts(price, debtX, debtY);
137
+ const collateralValue = this.getCollateralValueGivenPrice(price);
138
+ const collateralNeeded = debtValue * this.liquidationPenalty;
139
+ return collateralValue - collateralNeeded;
140
+ }
141
+ isLiquidatableGivenPriceAndDebt(price, debtX, debtY) {
142
+ return this.getLiquidityPostLiquidationValueGivenPriceAndDebt(price / this.safetyMargin, debtX, debtY) < 0
143
+ || this.getLiquidityPostLiquidationValueGivenPriceAndDebt(price * this.safetyMargin, debtX, debtY) < 0;
144
+ }
145
+ isLiquidatableGivenDebt(debtX, debtY) {
146
+ return this.isLiquidatableGivenPriceAndDebt(this.marketPrice, debtX, debtY);
147
+ }
148
+ isLiquidatableGivenPrice(price) {
149
+ return this.getEquityValueGivenPrice(price / this.safetyMargin) < 0 || this.getEquityValueGivenPrice(price * this.safetyMargin) < 0;
150
+ }
151
+ isUnderwaterGivenPrice(price) {
152
+ return this.getEquityValueGivenPrice(price) < 0;
153
+ }
154
+ getLiquidationPriceInRange(lowPrice, highPrice, lowIsLiquidatable, highIsLiquidatable) {
155
+ if (lowIsLiquidatable == highIsLiquidatable)
156
+ return undefined;
157
+ const avgPrice = Math.sqrt(lowPrice * highPrice);
158
+ if (lowPrice / highPrice > 0.9999)
159
+ return avgPrice;
160
+ const avgIsLiquidatable = this.isLiquidatableGivenPrice(avgPrice);
161
+ return this.getLiquidationPriceInRange(lowPrice, avgPrice, lowIsLiquidatable, avgIsLiquidatable)
162
+ ?? this.getLiquidationPriceInRange(avgPrice, highPrice, avgIsLiquidatable, highIsLiquidatable);
163
+ }
164
+ getMaxDeltaDebtInRange(lowDeltaX, lowDeltaY, highDeltaX, highDeltaY) {
165
+ if (Number.isNaN(lowDeltaX) || Number.isNaN(lowDeltaY) || Number.isNaN(highDeltaX) || Number.isNaN(highDeltaY))
166
+ return [0, 0];
167
+ if (highDeltaX == 0 && highDeltaY == 0)
168
+ return [0, 0];
169
+ if (Math.abs(highDeltaX / lowDeltaX) < 1.001 || Math.abs(highDeltaY / lowDeltaY) < 1.001)
170
+ return [lowDeltaX, lowDeltaY];
171
+ const avgDeltaX = (lowDeltaX + highDeltaX) / 2;
172
+ const avgDeltaY = (lowDeltaY + highDeltaY) / 2;
173
+ if (this.isLiquidatableGivenDebt(this.debtX + avgDeltaX, this.debtY + avgDeltaY))
174
+ return this.getMaxDeltaDebtInRange(lowDeltaX, lowDeltaY, avgDeltaX, avgDeltaY);
175
+ else
176
+ return this.getMaxDeltaDebtInRange(avgDeltaX, avgDeltaY, highDeltaX, highDeltaY);
177
+ }
178
+ /**
179
+ * PUBLIC SETTERS
180
+ */
181
+ depositX(amount) {
182
+ this.setRealX(this.getInitialRealX() + amount);
183
+ }
184
+ depositY(amount) {
185
+ this.setRealY(this.getInitialRealY() + amount);
186
+ }
187
+ withdrawX(amount) {
188
+ this.setRealX(Math.max(this.getInitialRealX() - amount, 0));
189
+ }
190
+ withdrawY(amount) {
191
+ this.setRealY(Math.max(this.getInitialRealY() - amount, 0));
192
+ }
193
+ borrowX(amount) {
194
+ this.setDebtX(this.initialDebtX + amount);
195
+ }
196
+ borrowY(amount) {
197
+ this.setDebtY(this.initialDebtY + amount);
198
+ }
199
+ repayX(amount) {
200
+ this.setDebtX(Math.max(this.initialDebtX - amount, 0));
201
+ }
202
+ repayY(amount) {
203
+ this.setDebtY(Math.max(this.initialDebtY - amount, 0));
204
+ }
205
+ /**
206
+ * PUBLIC GETTERS
207
+ */
208
+ getDepositedX() {
209
+ return this.getRealX();
210
+ }
211
+ getDepositedY() {
212
+ return this.getRealY();
213
+ }
214
+ getNetX() {
215
+ return this.getDepositedX() - this.debtX;
216
+ }
217
+ getNetY() {
218
+ return this.getDepositedY() - this.debtY;
219
+ }
220
+ isLiquidatable() {
221
+ return this.isLiquidatableGivenPrice(this.oraclePrice);
222
+ }
223
+ isUnderwater() {
224
+ return this.isUnderwaterGivenPrice(this.marketPrice);
225
+ }
226
+ getLiquidationRange() {
227
+ const isLiquidatable = this.isLiquidatable();
228
+ if (isLiquidatable)
229
+ return undefined;
230
+ const lowIsLiquidatable = this.isLiquidatableGivenPrice(LOWEST_PRICE);
231
+ const highIsLiquidatable = this.isLiquidatableGivenPrice(HIGHEST_PRICE);
232
+ const priceA = this.getLiquidationPriceInRange(LOWEST_PRICE, this.marketPrice, lowIsLiquidatable, isLiquidatable);
233
+ const priceB = this.getLiquidationPriceInRange(this.marketPrice, HIGHEST_PRICE, isLiquidatable, highIsLiquidatable);
234
+ return {
235
+ priceA: priceA && priceA > LOWEST_PRICE ? priceA : 0,
236
+ priceB: priceB && priceB < HIGHEST_PRICE ? priceB : Infinity,
237
+ };
238
+ }
239
+ getLeverage() {
240
+ const leverage = this.getCollateralValue() / this.getEquityValue();
241
+ if (!leverage)
242
+ return 1;
243
+ if (Number.isNaN(leverage))
244
+ return 1;
245
+ if (leverage < 1)
246
+ return 1;
247
+ return leverage;
248
+ }
249
+ // amountX and amountY are expected to have the same sign
250
+ getOptimalLiquidity(amountX, amountY) {
251
+ const liquidityA = amountX * Math.sqrt(this.marketPrice);
252
+ const liquidityB = amountY / Math.sqrt(this.marketPrice);
253
+ const liquidity = Math.min(liquidityA, liquidityB);
254
+ return {
255
+ liquidity: Number.isNaN(liquidity) ? 0 : liquidity ?? 0,
256
+ amountX: Number.isNaN(amountX) ? 0 : amountX ?? 0,
257
+ amountY: Number.isNaN(amountY) ? 0 : amountY ?? 0,
258
+ };
259
+ }
260
+ getMaxLeverage() {
261
+ const currentLeverage = this.getLeverage();
262
+ if (this.isLiquidatable())
263
+ return currentLeverage;
264
+ const highLeverage = 100; // we assume the position is liquidatable with this leverage
265
+ let setLiquidityToZero = false;
266
+ if (this.liquidity === 0) {
267
+ this.liquidity = 0.000001;
268
+ setLiquidityToZero = true;
269
+ }
270
+ const realX = this.getRealX();
271
+ const realY = this.getRealY();
272
+ const projectedRealX = realX * highLeverage / currentLeverage;
273
+ const projectedRealY = realY * highLeverage / currentLeverage;
274
+ const projectedDebtX = this.debtX + projectedRealX - realX;
275
+ const projectedDebtY = this.debtY + projectedRealY - realY;
276
+ const normalizedDeltaDebtX = projectedDebtX * currentLeverage / highLeverage - this.debtX;
277
+ const normalizedDeltaDebtY = projectedDebtY * currentLeverage / highLeverage - this.debtY;
278
+ const [deltaDebtX, deltaDebtY] = this.getMaxDeltaDebtInRange(0, 0, normalizedDeltaDebtX, normalizedDeltaDebtY);
279
+ const collateralValue = this.getCollateralValue();
280
+ const debtValue = this.getValueGivenAmounts(this.debtX + deltaDebtX, this.debtY + deltaDebtY);
281
+ const equityValue = collateralValue - debtValue;
282
+ if (setLiquidityToZero)
283
+ this.liquidity = 0;
284
+ return collateralValue / equityValue;
285
+ }
286
+ getMinLeverage() {
287
+ const depositedX = this.getDepositedX();
288
+ const depositedY = this.getDepositedY();
289
+ if (depositedX >= this.debtX && depositedY >= this.debtY)
290
+ return 0;
291
+ // TODO binary search
292
+ return 0;
293
+ }
294
+ getMaxWithdrawable() {
295
+ const depositedX = this.getDepositedX();
296
+ const depositedY = this.getDepositedY();
297
+ let percentage = 0;
298
+ if (!this.isLiquidatable()) {
299
+ if (this.debtX == 0 && this.debtY == 0) {
300
+ percentage = 1;
301
+ }
302
+ else {
303
+ const highRatio = this.getEquityValue() / this.getDebtValue();
304
+ const [deltaDebtX, deltaDebtY] = this.getMaxDeltaDebtInRange(0, 0, this.debtX * highRatio, this.debtY * highRatio);
305
+ const ratio = this.debtX > 0 ? deltaDebtX / this.debtX : deltaDebtY / this.debtY;
306
+ percentage = 1 / (1 / ratio + 1);
307
+ }
308
+ }
309
+ return {
310
+ amountX: depositedX * percentage,
311
+ amountY: depositedY * percentage,
312
+ };
313
+ }
314
+ getMaxBorrowableX() {
315
+ if (this.isLiquidatable())
316
+ return 0;
317
+ const equityValue = this.getEquityValue();
318
+ const highDebtX = this.getAmountXGivenValue(equityValue);
319
+ const [debtX,] = this.getMaxDeltaDebtInRange(0, 0, highDebtX, 0);
320
+ return debtX + this.debtX - this.initialDebtX;
321
+ }
322
+ getMaxBorrowableY() {
323
+ if (this.isLiquidatable())
324
+ return 0;
325
+ const equityValue = this.getEquityValue();
326
+ const highDebtY = this.getAmountYGivenValue(equityValue);
327
+ const [, debtY] = this.getMaxDeltaDebtInRange(0, 0, 0, highDebtY);
328
+ return debtY + this.debtY - this.initialDebtY;
329
+ }
330
+ }
331
+ exports.UniswapV2Position = UniswapV2Position;
332
+ exports.default = UniswapV2Position;
@@ -51,6 +51,7 @@ export declare class UniswapV3Position implements Position {
51
51
  private getDebtValueGivenPrice;
52
52
  private getCollateralValueGivenPrice;
53
53
  private getWeightedCollateralValueGivenPrice;
54
+ private getWeightedCollateralValueGivenPriceAndDeltaLeverage;
54
55
  private getEquityValueGivenPrice;
55
56
  private getDebtValue;
56
57
  private getCollateralValue;
@@ -21,7 +21,6 @@ const LOWEST_PRICE = 1 / 1e18;
21
21
  const HIGHEST_PRICE = 1e18;
22
22
  const FEE_COLLECTED_WEIGHT = 0.95;
23
23
  class UniswapV3Position {
24
- // TODO add fee logic
25
24
  constructor(_liquidity, _uncollectedFeesX, _uncollectedFeesY, _debtX, _debtY, _marketPrice, _oraclePrice, _priceA, _priceB, _safetyMargin, _liquidationPenalty, _lockStateChange) {
26
25
  this.liquidity = _liquidity;
27
26
  this.uncollectedFeesX = _uncollectedFeesX;
@@ -140,6 +139,10 @@ class UniswapV3Position {
140
139
  getWeightedCollateralValueGivenPrice(price) {
141
140
  return this.getValueGivenPriceAndAmounts(price, this.getRealXGivenPrice(price) + this.uncollectedFeesX * FEE_COLLECTED_WEIGHT, this.getRealYGivenPrice(price) + this.uncollectedFeesY * FEE_COLLECTED_WEIGHT);
142
141
  }
142
+ getWeightedCollateralValueGivenPriceAndDeltaLeverage(price, deltaX, deltaY) {
143
+ const liquidity = this.getLiquidityGivenAmounts(deltaX, deltaY);
144
+ return this.getValueGivenPriceAndAmounts(price, this.getRealXGivenLiquidityAndPrice(this.liquidity + liquidity, price) + this.uncollectedFeesX * FEE_COLLECTED_WEIGHT, this.getRealYGivenLiquidityAndPrice(this.liquidity + liquidity, price) + this.uncollectedFeesY * FEE_COLLECTED_WEIGHT);
145
+ }
143
146
  getEquityValueGivenPrice(price) {
144
147
  return this.getCollateralValueGivenPrice(price) - this.getDebtValueGivenPrice(price);
145
148
  }
@@ -165,9 +168,8 @@ class UniswapV3Position {
165
168
  return collateralValue - collateralNeeded;
166
169
  }
167
170
  getWeightedLiquidityPostLiquidationValueGivenPriceAndDeltaLeverage(price, deltaX, deltaY) {
168
- const deltaValue = this.getValueGivenPriceAndAmounts(price, deltaX, deltaY);
169
- const debtValue = this.getDebtValueGivenPrice(price) + deltaValue;
170
- const collateralValue = this.getWeightedCollateralValueGivenPrice(price) + deltaValue;
171
+ const debtValue = this.getDebtValueGivenPrice(price) + this.getValueGivenPriceAndAmounts(price, deltaX, deltaY);
172
+ const collateralValue = this.getWeightedCollateralValueGivenPriceAndDeltaLeverage(price, deltaX, deltaY);
171
173
  const collateralNeeded = debtValue * this.liquidationPenalty;
172
174
  return collateralValue - collateralNeeded;
173
175
  }
@@ -233,7 +235,7 @@ class UniswapV3Position {
233
235
  return [lowDeltaX, lowDeltaY];
234
236
  const avgDeltaX = (lowDeltaX + highDeltaX) / 2;
235
237
  const avgDeltaY = (lowDeltaY + highDeltaY) / 2;
236
- if (this.isLiquidatableGivenDeltaLeverage(this.debtX + avgDeltaX, this.debtY + avgDeltaY))
238
+ if (this.isLiquidatableGivenDeltaLeverage(avgDeltaX, avgDeltaY))
237
239
  return this.getMaxDeltaLeverageInRange(lowDeltaX, lowDeltaY, avgDeltaX, avgDeltaY);
238
240
  else
239
241
  return this.getMaxDeltaLeverageInRange(avgDeltaX, avgDeltaY, highDeltaX, highDeltaY);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "impermax-sdk",
3
- "version": "2.1.170",
3
+ "version": "2.1.172",
4
4
  "description": "",
5
5
  "main": "./lib/index.js",
6
6
  "module": "./lib/index.js",