Skip to content

Commit

Permalink
add getStrategyBuyId
Browse files Browse the repository at this point in the history
  • Loading branch information
GrandSchtroumpf committed Mar 20, 2024
1 parent fe2fee2 commit c0605cf
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 0 deletions.
37 changes: 37 additions & 0 deletions src/strategy-management/Toolkit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,43 @@ export class Toolkit {
return maxSourceAmount;
}

/**
* Gets the strategy by its id
*
* If the cache is synced, it will return the strategy from the cache.
* Otherwise, it will fetch the strategy from the chain.
*
* @param {string} id - ID of the strategy to fetch.
*/
public async getStrategyById(id: string): Promise<Strategy> {
logger.debug('getStrategyById called', arguments);

let encodedStrategy: EncodedStrategy | undefined;

if (this._cache) {
encodedStrategy = await this._cache.getStrategyById(id);
}

if (encodedStrategy) {
logger.debug('getStrategyById fetched from cache');
} else {
logger.debug('getStrategyById fetching from chain');
encodedStrategy = await this._api.reader.strategy(BigNumber.from(id));
}
const decodedStrategy = decodeStrategy(encodedStrategy);

const strategy = await parseStrategy(decodedStrategy, this._decimals);

logger.debug('getStrategyById info:', {
id,
encodedStrategy,
decodedStrategy,
strategy,
});

return strategy;
}

/**
* Gets all the strategies that belong to the given pair
*
Expand Down
25 changes: 25 additions & 0 deletions tests/toolkit.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ describe('Toolkit', () => {
reader: {
getDecimalsByAddress: sinon.stub(),
strategiesByPair: sinon.stub(),
strategy: sinon.stub(),
},
composer: {
updateStrategy: sinon.stub(),
Expand Down Expand Up @@ -581,6 +582,30 @@ describe('Toolkit', () => {
});
});

describe('getStrategyById', () => {
it('should fetch strategies from cache if available', async () => {
cacheMock.getStrategyById.resolves(encodedStrategies[0]);

const toolkit = new Toolkit(apiMock, cacheMock, decimalFetcher);
const strategy = await toolkit.getStrategyById('0');

expect(cacheMock.getStrategyById.calledWith('0')).to.be.true;
expect(strategy).to.deep.equal(expectedStrategies[0]);
});

it('should fetch strategies from the chain if not available in cache', async () => {
cacheMock.getStrategyById.resolves(undefined);

apiMock.reader.strategy.resolves(encodedStrategies[0]);

const toolkit = new Toolkit(apiMock, cacheMock, decimalFetcher);
const strategies = await toolkit.getStrategyById('0');

expect(apiMock.reader.strategy.calledWith(BigNumber.from('0'))).to.be.true;
expect(strategies).to.deep.equal(expectedStrategies[0]);
});
});

describe('getStrategiesByPair', () => {
it('should fetch strategies from cache if available', async () => {
cacheMock.getStrategiesByPair.resolves(encodedStrategies);
Expand Down

0 comments on commit c0605cf

Please sign in to comment.