Skip to content

Commit

Permalink
Merge pull request #54 from bancorprotocol/fix/duplicate-strategies
Browse files Browse the repository at this point in the history
  • Loading branch information
zavelevsky authored Aug 9, 2023
2 parents 65e0801 + 02f69cb commit e16836c
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 4 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.80-DEV",
"version": "0.0.81-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
6 changes: 3 additions & 3 deletions src/chain-cache/ChainCache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import {
import { Logger } from '../common/logger';
const logger = new Logger('ChainCache.ts');

const schemeVersion = 4; // bump this when the serialization format changes
const schemeVersion = 5; // bump this when the serialization format changes

type PairToStrategiesMap = { [key: string]: EncodedStrategy[] };
type StrategyById = { [key: string]: EncodedStrategy };
Expand Down Expand Up @@ -458,7 +458,7 @@ export class ChainCache extends (EventEmitter as new () => TypedEventEmitter<Cac
}
const key = toPairKey(strategy.token0, strategy.token1);
const strategies = (this._strategiesByPair[key] || []).filter(
(s) => s.id !== strategy.id
(s) => !s.id.eq(strategy.id)
);
strategies.push(strategy);
this._strategiesByPair[key] = strategies;
Expand All @@ -479,7 +479,7 @@ export class ChainCache extends (EventEmitter as new () => TypedEventEmitter<Cac
const key = toPairKey(strategy.token0, strategy.token1);
delete this._strategiesById[strategy.id.toString()];
const strategies = (this._strategiesByPair[key] || []).filter(
(s) => s.id !== strategy.id
(s) => !s.id.eq(strategy.id)
);
this._strategiesByPair[key] = strategies;
this._removeStrategyOrders(strategy);
Expand Down
11 changes: 11 additions & 0 deletions tests/ChainCache.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,17 @@ describe('ChainCache', () => {
cache.applyBatchedUpdates(5, [], [], [], []);
expect(affectedPairs).to.deep.equal([['abc', 'xyz']]);
});
it('should contain a single copy of a strategy that was updated', async () => {
const cache = new ChainCache();
const encodedStrategy1_mod = {
...encodedStrategy1,
id: BigNumber.from(encodedStrategy1.id.toString()),
};
cache.addPair('abc', 'xyz', [encodedStrategy1]);
cache.applyBatchedUpdates(10, [], [], [encodedStrategy1_mod], []);
const strategies = await cache.getStrategiesByPair('abc', 'xyz');
expect(strategies).to.have.length(1);
});
});
describe('cache miss', () => {
it('getStrategiesByPair call miss handler when pair is not cached', async () => {
Expand Down

0 comments on commit e16836c

Please sign in to comment.