Skip to content

Commit

Permalink
revert refact
Browse files Browse the repository at this point in the history
oring to the fallback and cache factories
  • Loading branch information
yvesfracari committed Oct 11, 2024
1 parent f4f4dc6 commit 6a95691
Show file tree
Hide file tree
Showing 16 changed files with 1,256 additions and 547 deletions.
95 changes: 35 additions & 60 deletions apps/api/src/app/inversify.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,21 @@ import {
CacheRepository,
CacheRepositoryMemory,
CacheRepositoryRedis,
CacheRepositoryFactory,
Erc20Repository,
Erc20RepositoryCache,
Erc20RepositoryViem,
FallbackRepositoryFactory,
SimulationRepository,
SimulationRepositoryTenderly,
TokenHolderRepository,
TokenHolderRepositoryCache,
TokenHolderRepositoryEthplorer,
TokenHolderRepositoryFallback,
TokenHolderRepositoryGoldRush,
UsdRepository,
UsdRepositoryCache,
UsdRepositoryCoingecko,
UsdRepositoryCow,
UsdRepositoryFallback,
cacheRepositorySymbol,
cowApiClients,
erc20RepositorySymbol,
Expand All @@ -21,10 +25,6 @@ import {
tokenHolderRepositorySymbol,
usdRepositorySymbol,
viemClients,
serializePricePoints,
deserializePricePoints,
TokenHolderPoint,
SimulationRepository,
} from '@cowprotocol/repositories';

const DEFAULT_CACHE_VALUE_SECONDS = ms('2min') / 1000; // 2min cache time by default for values
Expand All @@ -34,33 +34,26 @@ const CACHE_TOKEN_INFO_SECONDS = ms('24h') / 1000; // 24h

import { Container } from 'inversify';
import {
SimulationService,
SlippageService,
SlippageServiceMain,
SimulationService,
TokenHolderService,
TokenHolderServiceMain,
UsdService,
UsdServiceMain,
slippageServiceSymbol,
simulationServiceSymbol,
slippageServiceSymbol,
tokenHolderServiceSymbol,
usdServiceSymbol,
} from '@cowprotocol/services';
import ms from 'ms';

function getErc20Repository(cacheRepository: CacheRepository): Erc20Repository {
return CacheRepositoryFactory.create<Erc20Repository>(
return new Erc20RepositoryCache(
new Erc20RepositoryViem(viemClients),
cacheRepository,
'erc20',
CACHE_TOKEN_INFO_SECONDS,
CACHE_TOKEN_INFO_SECONDS,
{
get: {
serialize: (data) => JSON.stringify(data),
deserialize: (data) => JSON.parse(data),
},
}
CACHE_TOKEN_INFO_SECONDS
);
}

Expand All @@ -72,89 +65,69 @@ function getCacheRepository(_apiContainer: Container): CacheRepository {
return new CacheRepositoryMemory();
}

const usdPriceConvertFns = {
getUsdPrice: {
serialize: (data: number) => data.toString(),
deserialize: parseFloat,
},
getUsdPrices: {
serialize: serializePricePoints,
deserialize: deserializePricePoints,
},
};

function getUsdRepositoryCow(
cacheRepository: CacheRepository,
erc20Repository: Erc20Repository
): UsdRepository {
return CacheRepositoryFactory.create<UsdRepository>(
return new UsdRepositoryCache(
new UsdRepositoryCow(cowApiClients, erc20Repository),
cacheRepository,
'usdCow',
DEFAULT_CACHE_VALUE_SECONDS,
DEFAULT_CACHE_NULL_SECONDS,
usdPriceConvertFns
DEFAULT_CACHE_NULL_SECONDS
);
}

function getUsdRepositoryCoingecko(
cacheRepository: CacheRepository
): UsdRepository {
return CacheRepositoryFactory.create<UsdRepository>(
return new UsdRepositoryCache(
new UsdRepositoryCoingecko(),
cacheRepository,
'usdCoingecko',
DEFAULT_CACHE_VALUE_SECONDS,
DEFAULT_CACHE_NULL_SECONDS,
usdPriceConvertFns
DEFAULT_CACHE_NULL_SECONDS
);
}

function getUsdRepository(
cacheRepository: CacheRepository,
erc20Repository: Erc20Repository
): UsdRepository {
return FallbackRepositoryFactory.create<UsdRepository>([
return new UsdRepositoryFallback([
getUsdRepositoryCoingecko(cacheRepository),
getUsdRepositoryCow(cacheRepository, erc20Repository),
]);
}

const tokenHolderConvertFns = {
getTopTokenHolders: {
serialize: (data: TokenHolderPoint[]) => JSON.stringify(data),
deserialize: (data: string) => JSON.parse(data),
},
};

function getTokenHolderRepositoryGoldRush(
function getTokenHolderRepositoryEthplorer(
cacheRepository: CacheRepository
): TokenHolderRepository {
return CacheRepositoryFactory.create<TokenHolderRepository>(
new TokenHolderRepositoryGoldRush(),
return new TokenHolderRepositoryCache(
new TokenHolderRepositoryEthplorer(),
cacheRepository,
'tokenHolderGoldRush',
CACHE_TOKEN_INFO_SECONDS,
DEFAULT_CACHE_NULL_SECONDS,
tokenHolderConvertFns
'tokenHolderEthplorer',
DEFAULT_CACHE_VALUE_SECONDS,
DEFAULT_CACHE_NULL_SECONDS
);
}

function getTokenHolderRepositoryEthplorer(
function getTokenHolderRepositoryGoldRush(
cacheRepository: CacheRepository
): TokenHolderRepository {
return CacheRepositoryFactory.create<TokenHolderRepository>(
new TokenHolderRepositoryEthplorer(),
return new TokenHolderRepositoryCache(
new TokenHolderRepositoryGoldRush(),
cacheRepository,
'tokenHolderEthplorer',
CACHE_TOKEN_INFO_SECONDS,
DEFAULT_CACHE_NULL_SECONDS,
tokenHolderConvertFns
'tokenHolderGoldRush',
DEFAULT_CACHE_VALUE_SECONDS,
DEFAULT_CACHE_NULL_SECONDS
);
}

function getTokenHolderRepository(cacheRepository: CacheRepository) {
return FallbackRepositoryFactory.create<TokenHolderRepository>([
function getTokenHolderRepository(
cacheRepository: CacheRepository
): TokenHolderRepository {
return new TokenHolderRepositoryFallback([
getTokenHolderRepositoryGoldRush(cacheRepository),
getTokenHolderRepositoryEthplorer(cacheRepository),
]);
Expand All @@ -165,14 +138,16 @@ function getApiContainer(): Container {
// Repositories
const cacheRepository = getCacheRepository(apiContainer);
const erc20Repository = getErc20Repository(cacheRepository);
const simulationRepository = new SimulationRepositoryTenderly();
const tokenHolderRepository = getTokenHolderRepository(cacheRepository);

apiContainer
.bind<Erc20Repository>(erc20RepositorySymbol)
.toConstantValue(erc20Repository);

apiContainer
.bind<SimulationRepository>(tenderlyRepositorySymbol)
.toConstantValue(new SimulationRepositoryTenderly());
.toConstantValue(simulationRepository);

apiContainer
.bind<CacheRepository>(cacheRepositorySymbol)
Expand All @@ -184,7 +159,7 @@ function getApiContainer(): Container {

apiContainer
.bind<TokenHolderRepository>(tokenHolderRepositorySymbol)
.toConstantValue(getTokenHolderRepository(cacheRepository));
.toConstantValue(tokenHolderRepository);

// Services
apiContainer
Expand Down
188 changes: 0 additions & 188 deletions libs/repositories/src/CacheRepository/CacheRepositoryFactory.spec.ts

This file was deleted.

Loading

0 comments on commit 6a95691

Please sign in to comment.