Skip to content

Commit

Permalink
exposing marginal price in strategy object
Browse files Browse the repository at this point in the history
  • Loading branch information
zavelevsky committed Oct 4, 2023
1 parent 3218e8e commit 4d17fd1
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 3 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "@bancor/carbon-sdk",
"type": "module",
"source": "src/index.ts",
"version": "0.0.84-DEV",
"version": "0.0.85-DEV",
"description": "The SDK is a READ-ONLY tool, intended to facilitate working with Carbon contracts. It's a convenient wrapper around our matching algorithm, allowing programs and users get a ready to use transaction data that will allow them to manage strategies and fulfill trades",
"main": "dist/index.js",
"module": "dist/index.js",
Expand Down
12 changes: 11 additions & 1 deletion src/common/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,9 +118,11 @@ export type Strategy = {
baseToken: string;
quoteToken: string;
buyPriceLow: string; // in quote tkn per 1 base tkn
buyPriceMarginal: string; // in quote tkn per 1 base tkn
buyPriceHigh: string; // in quote tkn per 1 base tkn
buyBudget: string; // in quote tkn
sellPriceLow: string; // in quote tkn per 1 base tkn
sellPriceMarginal: string; // in quote tkn per 1 base tkn
sellPriceHigh: string; // in quote tkn per 1 base tkn
sellBudget: string; // in base tkn
encoded: EncodedStrategyBNStr; // the encoded strategy
Expand All @@ -133,7 +135,15 @@ export type AtLeastOneOf<T> = {
}[keyof T];

export type StrategyUpdate = AtLeastOneOf<
Omit<Strategy, 'id' | 'encoded' | 'baseToken' | 'quoteToken'>
Omit<
Strategy,
| 'id'
| 'encoded'
| 'baseToken'
| 'quoteToken'
| 'buyPriceMarginal'
| 'sellPriceMarginal'
>
>;

export type BlockMetadata = {
Expand Down
14 changes: 14 additions & 0 deletions src/strategy-management/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,12 +82,22 @@ export async function parseStrategy(
const decimals0 = await decimals.fetchDecimals(token0);
const decimals1 = await decimals.fetchDecimals(token1);
const buyPriceLow = normalizeRate(order1.lowestRate, decimals0, decimals1);
const buyPriceMarginal = normalizeRate(
order1.marginalRate,
decimals0,
decimals1
);
const buyPriceHigh = normalizeRate(order1.highestRate, decimals0, decimals1);
const sellPriceLow = normalizeInvertedRate(
order0.highestRate,
decimals1,
decimals0
);
const sellPriceMarginal = normalizeInvertedRate(
order0.marginalRate,
decimals1,
decimals0
);
const sellPriceHigh = normalizeInvertedRate(
order0.lowestRate,
decimals1,
Expand All @@ -109,9 +119,11 @@ export async function parseStrategy(
baseToken: token0,
quoteToken: token1,
buyPriceLow,
buyPriceMarginal,
buyPriceHigh,
buyBudget,
sellPriceLow,
sellPriceMarginal,
sellPriceHigh,
sellBudget,
encoded: strEncoded,
Expand All @@ -122,9 +134,11 @@ export async function parseStrategy(
baseToken: token0,
quoteToken: token1,
buyPriceLow,
buyPriceMarginal,
buyPriceHigh,
buyBudget,
sellPriceLow,
sellPriceMarginal,
sellPriceHigh,
sellBudget,
encoded: strEncoded,
Expand Down
17 changes: 16 additions & 1 deletion tests/encoders.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -573,9 +573,11 @@ describe('encoders', () => {
baseToken: '0x6b175474e89094c44da98b954eedeac495271d0f',
quoteToken: '0x57ab1e02fee23774580c119740129eac7081e9d3',
buyPriceLow: '1.5',
buyPriceMarginal: '2',
buyPriceHigh: '2',
buyBudget: '200',
sellPriceLow: '0.5',
sellPriceMarginal: '0.5',
sellPriceHigh: '0.6',
sellBudget: '100',
};
Expand Down Expand Up @@ -614,10 +616,23 @@ describe('encoders', () => {
expect(Strategy.baseToken).to.equal(expectedStrategy.baseToken);
expect(Strategy.quoteToken).to.equal(expectedStrategy.quoteToken);
expect(Strategy.buyPriceLow).to.equal(expectedStrategy.buyPriceLow);
expect(Strategy.buyPriceMarginal).to.equal(
expectedStrategy.buyPriceMarginal
);
expect(Strategy.buyPriceHigh).to.equal(expectedStrategy.buyPriceHigh);
expect(Strategy.buyBudget).to.equal(expectedStrategy.buyBudget);
expect(Strategy.sellPriceLow).to.equal(expectedStrategy.sellPriceLow);
expect(+Strategy.sellPriceHigh).to.equal(+expectedStrategy.sellPriceHigh);
expect(Strategy.sellPriceMarginal).to.equal(
expectedStrategy.sellPriceMarginal
);
expect(
...isAlmostEqual(
Strategy.sellPriceHigh,
expectedStrategy.sellPriceHigh,
'1e-18',
'0'
)
).to.be.true;
expect(Strategy.sellBudget).to.equal(expectedStrategy.sellBudget);
});
});
Expand Down
5 changes: 5 additions & 0 deletions tests/toolkit.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,11 @@ const expectedStrategies: Strategy[] = [
baseToken: 'abc',
quoteToken: 'xyz',
buyPriceLow: '0',
buyPriceMarginal: '0',
buyPriceHigh: '0',
buyBudget: '0',
sellPriceLow: '0',
sellPriceMarginal: '0',
sellPriceHigh: '0',
sellBudget: '0',
encoded: encodedStrategyBNToStr(encodedStrategies[0]),
Expand All @@ -62,10 +64,13 @@ const expectedStrategies: Strategy[] = [
quoteToken: 'abc',
buyPriceLow:
'0.000000000000000000000000000012621774483536188886587657044524579674771302961744368076324462890625',
buyPriceMarginal:
'0.0000000000000000000000000000504870979341447555463506281780983186990852118469774723052978515625',
buyPriceHigh:
'0.0000000000000000000000000000504870979341447555463506281780983186990852118469774723052978515625',
buyBudget: '0.000000000000000001',
sellPriceLow: '19807040628566084398385987584',
sellPriceMarginal: '19807040628566084398385987584',
sellPriceHigh: '79228162514264337593543950336',
sellBudget: '0.000000000000000001',
encoded: encodedStrategyBNToStr(encodedStrategies[1]),
Expand Down

0 comments on commit 4d17fd1

Please sign in to comment.