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

Get strategy by ID #139

Merged
merged 2 commits into from
Mar 21, 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
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,16 @@
"name": "@bancor/carbon-sdk",
"type": "module",
"source": "src/index.ts",
"version": "0.0.95-DEV",
"version": "0.0.96-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",
"exports": {
".":{
".": {
"import": "./dist/index.js",
"require": "./dist/index.cjs"
},
"./contracts-api":{
"./contracts-api": {
"import": "./dist/contracts-api/index.js",
"require": "./dist/contracts-api/index.cjs"
},
Expand Down
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
Loading