Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

calculateOverlappingStrategyPrices no longer needed in toolkit #131

Merged
merged 1 commit into from
Jan 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 0 additions & 48 deletions src/strategy-management/Toolkit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ import {
addFee,
buildStrategyObject,
calculateOverlappingBuyBudget,
calculateOverlappingPriceRanges,
calculateOverlappingSellBudget,
decodeStrategy,
encodeStrategy,
Expand Down Expand Up @@ -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.
*
Expand Down
57 changes: 49 additions & 8 deletions src/strategy-management/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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(
Expand All @@ -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,
Expand Down Expand Up @@ -464,6 +496,10 @@ export function calculateOverlappingSellBudget(

const sellBudget = formatUnits(sellLiquidity, baseTokenDecimals);

logger.debug('calculateOverlappingSellBudget info:', {
sellBudget,
});

return sellBudget;
}

Expand All @@ -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,
Expand Down Expand Up @@ -522,5 +559,9 @@ export function calculateOverlappingBuyBudget(

const buyBudget = formatUnits(buyLiquidity, quoteTokenDecimals);

logger.debug('calculateOverlappingBuyBudget info:', {
buyBudget,
});

return buyBudget;
}
21 changes: 0 additions & 21 deletions tests/toolkit.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
21 changes: 19 additions & 2 deletions tests/utils.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import {
addFee,
calculateOverlappingBuyBudget,
calculateOverlappingSellBudget,
calculateOverlappingPriceRanges,
calculateOverlappingPrices,
enforcePriceRange,
} from '../src/strategy-management';
import { isAlmostEqual } from './test-utils';
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down
Loading