Skip to content

Commit

Permalink
Merge pull request #160 from bancorprotocol/fix/modified-orders
Browse files Browse the repository at this point in the history
fixed order modified as side effect
  • Loading branch information
zavelevsky authored Sep 3, 2024
2 parents 4574270 + 70c8011 commit 56a18bb
Show file tree
Hide file tree
Showing 3 changed files with 203 additions and 135 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.98-DEV",
"version": "0.0.99-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.cjs",
"module": "dist/index.js",
Expand Down
28 changes: 26 additions & 2 deletions src/strategy-management/Toolkit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -990,12 +990,36 @@ export class Toolkit {
const newEncodedStrategy = encodeStrategy(newStrategy);

// step 3: to avoid changes due to rounding errors, we will override the new encoded strategy with selected values from the old encoded strategy:
// - if an order wasn't defined - it shouldn't be changed
// - if budget wasn't defined - we will use the old y
// - if no price was defined - we will use the old A and B
// - if any budget was defined - will set z according to MarginalPriceOptions
// - if any price was defined - we will reset z to y
// - if marginalPrice is a number - we will use it to calculate z
const encodedBN = encodedStrategyStrToBN(encoded);
if (
buyBudget === undefined &&
buyPriceLow === undefined &&
buyPriceHigh === undefined &&
buyPriceMarginal === undefined
) {
newEncodedStrategy.order1.y = encodedBN.order1.y;
newEncodedStrategy.order1.z = encodedBN.order1.z;
newEncodedStrategy.order1.A = encodedBN.order1.A;
newEncodedStrategy.order1.B = encodedBN.order1.B;
}
if (
sellBudget === undefined &&
sellPriceLow === undefined &&
sellPriceHigh === undefined &&
sellPriceMarginal === undefined
) {
newEncodedStrategy.order0.y = encodedBN.order0.y;
newEncodedStrategy.order0.z = encodedBN.order0.z;
newEncodedStrategy.order0.A = encodedBN.order0.A;
newEncodedStrategy.order0.B = encodedBN.order0.B;
}

if (buyBudget === undefined) {
newEncodedStrategy.order1.y = encodedBN.order1.y;
}
Expand All @@ -1018,13 +1042,13 @@ export class Toolkit {
// do nothing - z was already calculated and set
} else if (buyPriceMarginal === MarginalPriceOptions.maintain) {
if (encodedBN.order1.y.isZero()) {
// When depositing into an empty order and instructed to MAINTAIN - keep the old z, unless it's lower than the new y
// When depositing into an empty order and instructed to MAINTAIN - keep the old z, unless it's lower than the new y - in which case we set it to the new y
newEncodedStrategy.order1.z = BigNumberMax(
encodedBN.order1.z,
newEncodedStrategy.order1.y
);
} else {
// maintain the current ratio of y/z
// we're not depositing into an empty order and we're instructed to MAINTAIN - maintain the current ratio of y/z
newEncodedStrategy.order1.z = mulDiv(
encodedBN.order1.z,
newEncodedStrategy.order1.y,
Expand Down
Loading

0 comments on commit 56a18bb

Please sign in to comment.