forked from safe-global/safe-client-gateway
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add SafeBalancesApi (safe-global#1151)
- Moves `TransactionApi` balances-related functions (`getBalances`, `clearBalances`, `getCollectibles`, `clearCollectibles`) to `SafeBalancesApi`. - Adds `SafeBalancesApi` routing (between different chain-specific Transaction Services) to `BalancesApiManager` (heavily inspired by `TransactionApiManager`). - Unifies naming to stick to the interface (between `clearLocalBalances` and `clearBalances`). - Simplifies both `BalancesRepository` and `CollectiblesRepository`, as some complexity is pushed to `BalancesApiManager`.
- Loading branch information
1 parent
4fa2d6d
commit e0ab5ef
Showing
13 changed files
with
282 additions
and
202 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,42 +1,72 @@ | ||
import { IConfigurationService } from '@/config/configuration.service.interface'; | ||
import { SafeBalancesApi } from '@/datasources/balances-api/safe-balances-api.service'; | ||
import { IZerionBalancesApi } from '@/datasources/balances-api/zerion-balances-api.service'; | ||
import { CacheFirstDataSource } from '@/datasources/cache/cache.first.data.source'; | ||
import { | ||
CacheService, | ||
ICacheService, | ||
} from '@/datasources/cache/cache.service.interface'; | ||
import { HttpErrorFactory } from '@/datasources/errors/http-error-factory'; | ||
import { IBalancesApi } from '@/domain/interfaces/balances-api.interface'; | ||
import { IBalancesApiManager } from '@/domain/interfaces/balances-api.manager.interface'; | ||
import { IConfigApi } from '@/domain/interfaces/config-api.interface'; | ||
import { Inject, Injectable } from '@nestjs/common'; | ||
|
||
@Injectable() | ||
export class BalancesApiManager implements IBalancesApiManager { | ||
private readonly zerionBalancesChainIds: string[]; | ||
private safeBalancesApiMap: Record<string, SafeBalancesApi> = {}; | ||
private readonly zerionChainIds: string[]; | ||
private readonly zerionBalancesApi: IBalancesApi; | ||
private readonly useVpcUrl: boolean; | ||
|
||
constructor( | ||
@Inject(IConfigurationService) | ||
private readonly configurationService: IConfigurationService, | ||
@Inject(IConfigApi) private readonly configApi: IConfigApi, | ||
private readonly dataSource: CacheFirstDataSource, | ||
@Inject(CacheService) private readonly cacheService: ICacheService, | ||
private readonly httpErrorFactory: HttpErrorFactory, | ||
@Inject(IZerionBalancesApi) zerionBalancesApi: IBalancesApi, | ||
) { | ||
this.zerionBalancesChainIds = this.configurationService.getOrThrow< | ||
string[] | ||
>('features.zerionBalancesChainIds'); | ||
this.zerionChainIds = this.configurationService.getOrThrow<string[]>( | ||
'features.zerionBalancesChainIds', | ||
); | ||
this.useVpcUrl = this.configurationService.getOrThrow<boolean>( | ||
'safeTransaction.useVpcUrl', | ||
); | ||
|
||
this.zerionBalancesApi = zerionBalancesApi; | ||
} | ||
|
||
useExternalApi(chainId: string): boolean { | ||
return this.zerionBalancesChainIds.includes(chainId); | ||
return this.zerionChainIds.includes(chainId); | ||
} | ||
|
||
getBalancesApi(chainId: string): IBalancesApi { | ||
async getBalancesApi(chainId: string): Promise<IBalancesApi> { | ||
if (this._isSupportedByZerion(chainId)) { | ||
return this.zerionBalancesApi; | ||
} | ||
throw new Error(`Chain ID ${chainId} balances provider is not configured`); | ||
|
||
const safeBalancesApi = this.safeBalancesApiMap[chainId]; | ||
if (safeBalancesApi !== undefined) return safeBalancesApi; | ||
|
||
const chain = await this.configApi.getChain(chainId); | ||
this.safeBalancesApiMap[chainId] = new SafeBalancesApi( | ||
chainId, | ||
this.useVpcUrl ? chain.vpcTransactionService : chain.transactionService, | ||
this.dataSource, | ||
this.cacheService, | ||
this.configurationService, | ||
this.httpErrorFactory, | ||
); | ||
return this.safeBalancesApiMap[chainId]; | ||
} | ||
|
||
getFiatCodes(): string[] { | ||
return this.zerionBalancesApi.getFiatCodes().sort(); | ||
} | ||
|
||
private _isSupportedByZerion(chainId: string): boolean { | ||
return this.zerionBalancesChainIds.includes(chainId); | ||
return this.zerionChainIds.includes(chainId); | ||
} | ||
} |
117 changes: 117 additions & 0 deletions
117
src/datasources/balances-api/safe-balances-api.service.ts
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,117 @@ | ||
import { IConfigurationService } from '@/config/configuration.service.interface'; | ||
import { CacheFirstDataSource } from '@/datasources/cache/cache.first.data.source'; | ||
import { CacheRouter } from '@/datasources/cache/cache.router'; | ||
import { ICacheService } from '@/datasources/cache/cache.service.interface'; | ||
import { HttpErrorFactory } from '@/datasources/errors/http-error-factory'; | ||
import { Balance } from '@/domain/balances/entities/balance.entity'; | ||
import { Collectible } from '@/domain/collectibles/entities/collectible.entity'; | ||
import { Page } from '@/domain/entities/page.entity'; | ||
import { IBalancesApi } from '@/domain/interfaces/balances-api.interface'; | ||
import { Injectable } from '@nestjs/common'; | ||
|
||
@Injectable() | ||
export class SafeBalancesApi implements IBalancesApi { | ||
private readonly defaultExpirationTimeInSeconds: number; | ||
private readonly defaultNotFoundExpirationTimeSeconds: number; | ||
|
||
constructor( | ||
private readonly chainId: string, | ||
private readonly baseUrl: string, | ||
private readonly dataSource: CacheFirstDataSource, | ||
private readonly cacheService: ICacheService, | ||
private readonly configurationService: IConfigurationService, | ||
private readonly httpErrorFactory: HttpErrorFactory, | ||
) { | ||
this.defaultExpirationTimeInSeconds = | ||
this.configurationService.getOrThrow<number>( | ||
'expirationTimeInSeconds.default', | ||
); | ||
this.defaultNotFoundExpirationTimeSeconds = | ||
this.configurationService.getOrThrow<number>( | ||
'expirationTimeInSeconds.notFound.default', | ||
); | ||
} | ||
|
||
async getBalances(args: { | ||
safeAddress: string; | ||
trusted?: boolean; | ||
excludeSpam?: boolean; | ||
}): Promise<Balance[]> { | ||
try { | ||
const cacheDir = CacheRouter.getBalancesCacheDir({ | ||
chainId: this.chainId, | ||
...args, | ||
}); | ||
const url = `${this.baseUrl}/api/v1/safes/${args.safeAddress}/balances/`; | ||
return await this.dataSource.get({ | ||
cacheDir, | ||
url, | ||
notFoundExpireTimeSeconds: this.defaultNotFoundExpirationTimeSeconds, | ||
networkRequest: { | ||
params: { | ||
trusted: args.trusted, | ||
exclude_spam: args.excludeSpam, | ||
}, | ||
}, | ||
expireTimeSeconds: this.defaultExpirationTimeInSeconds, | ||
}); | ||
} catch (error) { | ||
throw this.httpErrorFactory.from(error); | ||
} | ||
} | ||
|
||
async clearBalances(args: { safeAddress: string }): Promise<void> { | ||
const key = CacheRouter.getBalancesCacheKey({ | ||
chainId: this.chainId, | ||
safeAddress: args.safeAddress, | ||
}); | ||
await this.cacheService.deleteByKey(key); | ||
} | ||
|
||
async getCollectibles(args: { | ||
safeAddress: string; | ||
limit?: number; | ||
offset?: number; | ||
trusted?: boolean; | ||
excludeSpam?: boolean; | ||
}): Promise<Page<Collectible>> { | ||
try { | ||
const cacheDir = CacheRouter.getCollectiblesCacheDir({ | ||
chainId: this.chainId, | ||
...args, | ||
}); | ||
const url = `${this.baseUrl}/api/v2/safes/${args.safeAddress}/collectibles/`; | ||
return await this.dataSource.get({ | ||
cacheDir, | ||
url, | ||
notFoundExpireTimeSeconds: this.defaultNotFoundExpirationTimeSeconds, | ||
networkRequest: { | ||
params: { | ||
limit: args.limit, | ||
offset: args.offset, | ||
trusted: args.trusted, | ||
exclude_spam: args.excludeSpam, | ||
}, | ||
}, | ||
expireTimeSeconds: this.defaultExpirationTimeInSeconds, | ||
}); | ||
} catch (error) { | ||
throw this.httpErrorFactory.from(error); | ||
} | ||
} | ||
|
||
async clearCollectibles(args: { safeAddress: string }): Promise<void> { | ||
const key = CacheRouter.getCollectiblesKey({ | ||
chainId: this.chainId, | ||
safeAddress: args.safeAddress, | ||
}); | ||
await this.cacheService.deleteByKey(key); | ||
} | ||
|
||
/** | ||
* No fiat prices are available from this provider. | ||
*/ | ||
getFiatCodes(): string[] { | ||
return []; | ||
} | ||
} |
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
Oops, something went wrong.