From 57b5793717db76ea861e26c0ffe6f3e39e62eb39 Mon Sep 17 00:00:00 2001 From: Doron Zavelevsky Date: Mon, 29 Jan 2024 10:22:32 +0000 Subject: [PATCH] calculateOverlappingStrategyPrices no longer needed in toolkit --- src/strategy-management/Toolkit.ts | 48 ------------------------- src/strategy-management/utils.ts | 57 +++++++++++++++++++++++++----- tests/toolkit.spec.ts | 21 ----------- tests/utils.spec.ts | 21 +++++++++-- 4 files changed, 68 insertions(+), 79 deletions(-) diff --git a/src/strategy-management/Toolkit.ts b/src/strategy-management/Toolkit.ts index a25830c..75ccc15 100644 --- a/src/strategy-management/Toolkit.ts +++ b/src/strategy-management/Toolkit.ts @@ -50,7 +50,6 @@ import { addFee, buildStrategyObject, calculateOverlappingBuyBudget, - calculateOverlappingPriceRanges, calculateOverlappingSellBudget, decodeStrategy, encodeStrategy, @@ -710,53 +709,6 @@ export class Toolkit { ); } - /** - * Calculate the overlapping strategy prices. Returns it with correct decimals - * - * @param {string} buyPriceLow - The minimum buy price for the strategy, in in `quoteToken` per 1 `baseToken`, as a string. - * @param {string} sellPriceHigh - The maximum sell price for the strategy, in `quoteToken` per 1 `baseToken`, as a string. - * @param {string} marketPrice - The market price, in `quoteToken` per 1 `baseToken`, as a string. - * @param {string} spreadPercentage - The spread percentage, e.g. for 10%, enter `10`. - * @return {Promise<{ - * buyPriceLow: string; - * buyPriceHigh: string; - * buyPriceMarginal: string; - * sellPriceLow: string; - * sellPriceHigh: string; - * sellPriceMarginal: string; - * marketPrice: string; - * }>} The calculated overlapping strategy prices. - */ - public async calculateOverlappingStrategyPrices( - buyPriceLow: string, - sellPriceHigh: string, - marketPrice: string, - spreadPercentage: string - ): Promise<{ - buyPriceLow: string; - buyPriceHigh: string; - buyPriceMarginal: string; - sellPriceLow: string; - sellPriceHigh: string; - sellPriceMarginal: string; - marketPrice: string; - }> { - logger.debug('calculateOverlappingStrategyPrices called', arguments); - - const prices = calculateOverlappingPriceRanges( - buyPriceLow, - sellPriceHigh, - marketPrice, - spreadPercentage - ); - - logger.debug('calculateOverlappingStrategyPrices info:', { - prices, - }); - - return { buyPriceLow, marketPrice, sellPriceHigh, ...prices }; - } - /** * Calculates the sell budget given a buy budget of an overlapping strategy. * diff --git a/src/strategy-management/utils.ts b/src/strategy-management/utils.ts index de9d965..a6672cd 100644 --- a/src/strategy-management/utils.ts +++ b/src/strategy-management/utils.ts @@ -372,17 +372,39 @@ export function enforcePriceRange( return marginalPrice; } -export function calculateOverlappingPriceRanges( - buyPriceLow: string, // in quote tkn per 1 base tkn - sellPriceHigh: string, // in quote tkn per 1 base tkn - marketPrice: string, // in quote tkn per 1 base tkn - spreadPercentage: string // e.g. for 0.1% pass '0.1' +/** + * Calculate the overlapping strategy prices. Returns it with correct decimals + * + * @param {string} buyPriceLow - The minimum buy price for the strategy, in in `quoteToken` per 1 `baseToken`, as a string. + * @param {string} sellPriceHigh - The maximum sell price for the strategy, in `quoteToken` per 1 `baseToken`, as a string. + * @param {string} marketPrice - The market price, in `quoteToken` per 1 `baseToken`, as a string. + * @param {string} spreadPercentage - The spread percentage, e.g. for 10%, enter `10`. + * @return {{ + * buyPriceLow: string; + * buyPriceHigh: string; + * buyPriceMarginal: string; + * sellPriceLow: string; + * sellPriceHigh: string; + * sellPriceMarginal: string; + * marketPrice: string; + * }} The calculated overlapping strategy prices. + */ +export function calculateOverlappingPrices( + buyPriceLow: string, + sellPriceHigh: string, + marketPrice: string, + spreadPercentage: string ): { + buyPriceLow: string; buyPriceHigh: string; buyPriceMarginal: string; sellPriceLow: string; + sellPriceHigh: string; sellPriceMarginal: string; + marketPrice: string; } { + logger.debug('calculateOverlappingPrices called', arguments); + const spreadFactor = new Decimal(spreadPercentage).div(100).plus(1); const buyPriceHigh = new Decimal(sellPriceHigh).div(spreadFactor); const sellPriceLow = new Decimal(buyPriceLow).mul(spreadFactor); @@ -401,12 +423,21 @@ export function calculateOverlappingPriceRanges( new Decimal(marketPrice).mul(spreadFactor.sqrt()) ); - return { + const prices = { buyPriceHigh: buyPriceHigh.toString(), buyPriceMarginal: buyPriceMarginal.toString(), sellPriceLow: sellPriceLow.toString(), sellPriceMarginal: sellPriceMarginal.toString(), + buyPriceLow, + sellPriceHigh, + marketPrice, }; + + logger.debug('calculateOverlappingPrices info:', { + prices, + }); + + return prices; } export function calculateOverlappingSellBudget( @@ -418,11 +449,12 @@ export function calculateOverlappingSellBudget( spreadPercentage: string, // e.g. for 0.1% pass '0.1' buyBudget: string // in quote tkn ): string { + logger.debug('calculateOverlappingSellBudget called', arguments); // zero buy budget means zero sell budget if (buyBudget === '0') return '0'; const { buyPriceHigh, sellPriceLow, sellPriceMarginal, buyPriceMarginal } = - calculateOverlappingPriceRanges( + calculateOverlappingPrices( buyPriceLow, sellPriceHigh, marketPrice, @@ -464,6 +496,10 @@ export function calculateOverlappingSellBudget( const sellBudget = formatUnits(sellLiquidity, baseTokenDecimals); + logger.debug('calculateOverlappingSellBudget info:', { + sellBudget, + }); + return sellBudget; } @@ -476,11 +512,12 @@ export function calculateOverlappingBuyBudget( spreadPercentage: string, // e.g. for 0.1% pass '0.1' sellBudget: string // in base tkn ): string { + logger.debug('calculateOverlappingBuyBudget called', arguments); // zero sell budget means zero buy budget if (sellBudget === '0') return '0'; const { sellPriceLow, buyPriceHigh, sellPriceMarginal, buyPriceMarginal } = - calculateOverlappingPriceRanges( + calculateOverlappingPrices( buyPriceLow, sellPriceHigh, marketPrice, @@ -522,5 +559,9 @@ export function calculateOverlappingBuyBudget( const buyBudget = formatUnits(buyLiquidity, quoteTokenDecimals); + logger.debug('calculateOverlappingBuyBudget info:', { + buyBudget, + }); + return buyBudget; } diff --git a/tests/toolkit.spec.ts b/tests/toolkit.spec.ts index fbcd319..b9a5177 100644 --- a/tests/toolkit.spec.ts +++ b/tests/toolkit.spec.ts @@ -491,27 +491,6 @@ describe('Toolkit', () => { }); describe('overlappingStrategies', () => { - it('should calculate strategy prices', async () => { - const toolkit = new Toolkit(apiMock, cacheMock, decimalFetcher); - const result = await toolkit.calculateOverlappingStrategyPrices( - '1500.0000000000000000001', - '2000', - '1845', - '1' - ); - expect(result).to.deep.equal({ - buyPriceLow: '1500.0000000000000000001', - buyPriceHigh: - '1980.19801980198019801980198019801980198019801980198019801980198019801980198019801980198019801980198', - buyPriceMarginal: - '1835.843615937429955302430075647665154941937455663234267436323585007036269526077757760470083423519019', - sellPriceLow: '1515.000000000000000000101', - sellPriceHigh: '2000', - sellPriceMarginal: - '1854.202052096804254855454376404141806491356830219866610110686820857106632221338535338074784257754209', - marketPrice: '1845', - }); - }); it('should calculate strategy sell budget', async () => { const toolkit = new Toolkit(apiMock, cacheMock, decimalFetcher); const result = await toolkit.calculateOverlappingStrategySellBudget( diff --git a/tests/utils.spec.ts b/tests/utils.spec.ts index 6377467..d6a64b8 100644 --- a/tests/utils.spec.ts +++ b/tests/utils.spec.ts @@ -12,7 +12,7 @@ import { addFee, calculateOverlappingBuyBudget, calculateOverlappingSellBudget, - calculateOverlappingPriceRanges, + calculateOverlappingPrices, enforcePriceRange, } from '../src/strategy-management'; import { isAlmostEqual } from './test-utils'; @@ -61,6 +61,23 @@ describe('utils', () => { '1854.202052096804254855454376404141806491356830219866610110686820857106632221338535338074784257754209', sellBudget: '0.021054379648026716', }, + { + baseTokenDecimals: 18, + quoteTokenDecimals: 6, + buyPriceLow: '1500.0000000000000000001', + sellPriceHigh: '2000', + marketPrice: '1845', + spreadPercentage: '1', + buyBudget: '100', + buyPriceHigh: + '1980.19801980198019801980198019801980198019801980198019801980198019801980198019801980198019801980198', + sellPriceLow: '1515.000000000000000000101', + buyPriceMarginal: + '1835.843615937429955302430075647665154941937455663234267436323585007036269526077757760470083423519019', + sellPriceMarginal: + '1854.202052096804254855454376404141806491356830219866610110686820857106632221338535338074784257754209', + sellBudget: '0.021054379648026716', + }, { baseTokenDecimals: 18, quoteTokenDecimals: 6, @@ -100,7 +117,7 @@ describe('utils', () => { buyPriceLow: ${buyPriceLow}, sellPriceHigh: ${sellPriceHigh}, marketPrice: ${marketPrice}, spreadPercentage: ${spreadPercentage}, buyBudget: ${buyBudget}`, () => { - const prices = calculateOverlappingPriceRanges( + const prices = calculateOverlappingPrices( buyPriceLow, sellPriceHigh, marketPrice,