-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
PP-778: implement the exchange cache (#87)
* feat: implement an exchange cache * feat(CachedExchangeApi): cached value retrieved from coingecko for 60 seconds * fix: change the pricer to use always the same exchange api instead of creating one for each request * style: apply format rules * chore: rename builders to exchanges
- Loading branch information
Showing
7 changed files
with
187 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
import log from 'loglevel'; | ||
import type { ExchangeApi } from './BaseExchangeApi'; | ||
import type { BigNumber as BigNumberJs } from 'bignumber.js'; | ||
|
||
export type RateWithExpiration = { | ||
rate: BigNumberJs; | ||
expirationTime: number; | ||
}; | ||
|
||
export class ExchangeApiCache implements ExchangeApi { | ||
constructor( | ||
private exchangeApi: ExchangeApi, | ||
private expirationTimeInMillisec: number, | ||
private cache: Map<string, RateWithExpiration> = new Map< | ||
string, | ||
RateWithExpiration | ||
>() | ||
) {} | ||
|
||
public async queryExchangeRate( | ||
sourceCurrency: string, | ||
targetCurrency: string | ||
): Promise<BigNumberJs> { | ||
const key = getKeyFromArgs(sourceCurrency, targetCurrency); | ||
const now = Date.now(); | ||
const cachedRate = this.cache.get(key); | ||
if (!cachedRate || cachedRate.expirationTime <= now) { | ||
log.debug( | ||
'CachedExchangeApi: value not available or expired', | ||
cachedRate | ||
); | ||
const rate = await this.exchangeApi.queryExchangeRate( | ||
sourceCurrency, | ||
targetCurrency | ||
); | ||
const expirationTime = now + this.expirationTimeInMillisec; | ||
this.cache.set(key, { expirationTime, rate }); | ||
log.debug('ExchangeApiCache: storing a new value', key, { | ||
expirationTime, | ||
rate, | ||
}); | ||
|
||
return rate; | ||
} | ||
log.debug( | ||
'ExchangeApiCache: value available in cache, API not called', | ||
cachedRate | ||
); | ||
|
||
return cachedRate.rate; | ||
} | ||
} | ||
|
||
export const getKeyFromArgs = ( | ||
sourceCurrency: string, | ||
targetCurrency: string | ||
) => `${sourceCurrency}->${targetCurrency}`; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
import BigNumber from 'bignumber.js'; | ||
import { expect } from 'chai'; | ||
import { | ||
RateWithExpiration, | ||
ExchangeApiCache, | ||
getKeyFromArgs, | ||
} from '../../../src/api/pricer/ExchangeApiCache'; | ||
import type { ExchangeApi } from 'src/api/pricer/BaseExchangeApi'; | ||
import sinon from 'sinon'; | ||
|
||
describe('ExchangeApiCache', function () { | ||
const sourceCurrency = 'source'; | ||
const targetCurrency = 'target'; | ||
const valueFromExchange = '1'; | ||
let fakeExchangeAPI: ExchangeApi; | ||
|
||
beforeEach(function () { | ||
fakeExchangeAPI = { | ||
queryExchangeRate: sinon.fake.resolves(new BigNumber(valueFromExchange)), | ||
}; | ||
}); | ||
|
||
it('should perform a call to the inner exchange if the value is not cached', async function () { | ||
const cache = new Map<string, RateWithExpiration>(); | ||
const cachedExchangeApi = new ExchangeApiCache(fakeExchangeAPI, 100, cache); | ||
const rate = await cachedExchangeApi.queryExchangeRate( | ||
sourceCurrency, | ||
targetCurrency | ||
); | ||
|
||
expect(rate.toString()).to.be.eq(valueFromExchange); | ||
expect(fakeExchangeAPI.queryExchangeRate).to.have.been.calledWithExactly( | ||
sourceCurrency, | ||
targetCurrency | ||
); | ||
}); | ||
|
||
it('should store the value if it is not cached', async function () { | ||
const cache = new Map<string, RateWithExpiration>(); | ||
const cachedExchangeApi = new ExchangeApiCache(fakeExchangeAPI, 100, cache); | ||
const rate = await cachedExchangeApi.queryExchangeRate( | ||
sourceCurrency, | ||
targetCurrency | ||
); | ||
const key = getKeyFromArgs(sourceCurrency, targetCurrency); | ||
|
||
expect(rate.toString()).to.be.eq(valueFromExchange); | ||
expect(cache.get(key)?.rate.toString()).to.be.eq(valueFromExchange); | ||
}); | ||
|
||
it('should perform a call to the inner exchange if the cached value is expired', async function () { | ||
const previouslyStoredValue = 100; | ||
const cache: Map<string, RateWithExpiration> = new Map(); | ||
const key = getKeyFromArgs(sourceCurrency, targetCurrency); | ||
// 1 second ago | ||
const expirationTime = Date.now() - 1_000; | ||
cache.set(key, { | ||
rate: new BigNumber(previouslyStoredValue), | ||
expirationTime, | ||
}); | ||
const cachedExchangeApi = new ExchangeApiCache(fakeExchangeAPI, 100, cache); | ||
const rate = await cachedExchangeApi.queryExchangeRate( | ||
sourceCurrency, | ||
targetCurrency | ||
); | ||
|
||
expect(rate.toString()).to.be.eq(valueFromExchange); | ||
|
||
expect(fakeExchangeAPI.queryExchangeRate).to.have.been.calledWithExactly( | ||
sourceCurrency, | ||
targetCurrency | ||
); | ||
expect(cache.get(key)?.rate.toString()).to.be.eq(valueFromExchange); | ||
}); | ||
|
||
it('should not perform a call to the inner exchange if the cached value is not expired', async function () { | ||
const cache: Map<string, RateWithExpiration> = new Map(); | ||
const key = getKeyFromArgs(sourceCurrency, targetCurrency); | ||
// in 10 seconds | ||
const expirationTime = Date.now() + 10_000; | ||
const cachedRate = new BigNumber(100); | ||
cache.set(key, { | ||
rate: cachedRate, | ||
expirationTime, | ||
}); | ||
const cachedExchangeApi = new ExchangeApiCache(fakeExchangeAPI, 100, cache); | ||
const rate = await cachedExchangeApi.queryExchangeRate( | ||
sourceCurrency, | ||
targetCurrency | ||
); | ||
|
||
expect(rate.toString()).to.be.eq(cachedRate.toString()); | ||
|
||
expect(fakeExchangeAPI.queryExchangeRate).not.to.have.been.called; | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters