-
Notifications
You must be signed in to change notification settings - Fork 73
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
Add IBlockchainApiManager
for on-chain interactions
#1623
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
2e69eba
Add `BlockchainApi` with associated manager
iamacook c5b5369
Fix lint
iamacook e8f8cd9
Clear client after chain update
iamacook bb6be01
Add test
iamacook 2b1b0fe
Merge branch 'main' into blockchain-api-manager
iamacook 512f8f1
Remove comment
iamacook e38f109
Return client directly from manager
iamacook 4e1082a
Add dummy API key for E2E tests
iamacook File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,49 @@ | ||
import { FakeConfigurationService } from '@/config/__tests__/fake.configuration.service'; | ||
import { BlockchainApiManager } from '@/datasources/blockchain/blockchain-api.manager'; | ||
import { chainBuilder } from '@/domain/chains/entities/__tests__/chain.builder'; | ||
import { IConfigApi } from '@/domain/interfaces/config-api.interface'; | ||
import { faker } from '@faker-js/faker'; | ||
|
||
const configApiMock = jest.mocked({ | ||
getChain: jest.fn(), | ||
} as jest.MockedObjectDeep<IConfigApi>); | ||
|
||
describe('BlockchainApiManager', () => { | ||
let target: BlockchainApiManager; | ||
|
||
beforeEach(() => { | ||
jest.resetAllMocks(); | ||
|
||
const fakeConfigurationService = new FakeConfigurationService(); | ||
fakeConfigurationService.set( | ||
'blockchain.infura.apiKey', | ||
faker.string.hexadecimal({ length: 32 }), | ||
); | ||
target = new BlockchainApiManager(fakeConfigurationService, configApiMock); | ||
}); | ||
|
||
describe('getBlockchainApi', () => { | ||
it('caches the API', async () => { | ||
const chain = chainBuilder().build(); | ||
configApiMock.getChain.mockResolvedValue(chain); | ||
|
||
const api = await target.getBlockchainApi(chain.chainId); | ||
const cachedApi = await target.getBlockchainApi(chain.chainId); | ||
|
||
expect(api).toBe(cachedApi); | ||
}); | ||
}); | ||
|
||
describe('destroyBlockchainApi', () => { | ||
it('destroys the API', async () => { | ||
const chain = chainBuilder().build(); | ||
configApiMock.getChain.mockResolvedValue(chain); | ||
|
||
const api = await target.getBlockchainApi(chain.chainId); | ||
target.destroyBlockchainApi(chain.chainId); | ||
const cachedApi = await target.getBlockchainApi(chain.chainId); | ||
|
||
expect(api).not.toBe(cachedApi); | ||
}); | ||
}); | ||
}); |
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,67 @@ | ||
import { IConfigurationService } from '@/config/configuration.service.interface'; | ||
import { Chain as DomainChain } from '@/domain/chains/entities/chain.entity'; | ||
import { RpcUriAuthentication } from '@/domain/chains/entities/rpc-uri-authentication.entity'; | ||
import { IBlockchainApiManager } from '@/domain/interfaces/blockchain-api.manager.interface'; | ||
import { IConfigApi } from '@/domain/interfaces/config-api.interface'; | ||
import { Inject, Injectable } from '@nestjs/common'; | ||
import { Chain, PublicClient, createPublicClient, http } from 'viem'; | ||
|
||
@Injectable() | ||
export class BlockchainApiManager implements IBlockchainApiManager { | ||
private readonly blockchainApiMap: Record<string, PublicClient> = {}; | ||
private readonly infuraApiKey: string; | ||
|
||
constructor( | ||
@Inject(IConfigurationService) | ||
private readonly configurationService: IConfigurationService, | ||
@Inject(IConfigApi) private readonly configApi: IConfigApi, | ||
) { | ||
this.infuraApiKey = this.configurationService.getOrThrow<string>( | ||
'blockchain.infura.apiKey', | ||
); | ||
} | ||
|
||
async getBlockchainApi(chainId: string): Promise<PublicClient> { | ||
const blockchainApi = this.blockchainApiMap[chainId]; | ||
if (blockchainApi) { | ||
return blockchainApi; | ||
} | ||
|
||
const chain = await this.configApi.getChain(chainId); | ||
this.blockchainApiMap[chainId] = this.createClient(chain); | ||
|
||
return this.blockchainApiMap[chainId]; | ||
} | ||
|
||
destroyBlockchainApi(chainId: string): void { | ||
if (this.blockchainApiMap?.[chainId]) { | ||
delete this.blockchainApiMap[chainId]; | ||
} | ||
} | ||
|
||
private createClient(chain: DomainChain): PublicClient { | ||
return createPublicClient({ | ||
chain: this.formatChain(chain), | ||
transport: http(), | ||
}); | ||
} | ||
|
||
private formatChain(chain: DomainChain): Chain { | ||
return { | ||
id: Number(chain.chainId), | ||
name: chain.chainName, | ||
nativeCurrency: chain.nativeCurrency, | ||
rpcUrls: { | ||
default: { | ||
http: [this.formatRpcUri(chain.rpcUri)], | ||
}, | ||
}, | ||
}; | ||
} | ||
|
||
private formatRpcUri(rpcUri: DomainChain['rpcUri']): string { | ||
return rpcUri.authentication === RpcUriAuthentication.ApiKeyPath | ||
? rpcUri.value + this.infuraApiKey | ||
: rpcUri.value; | ||
} | ||
} |
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,18 @@ | ||
import { BlockchainRepository } from '@/domain/blockchain/blockchain.repository'; | ||
import { BlockchainApiManagerModule } from '@/domain/interfaces/blockchain-api.manager.interface'; | ||
import { Module } from '@nestjs/common'; | ||
|
||
export const IBlockchainRepository = Symbol('IBlockchainRepository'); | ||
|
||
export interface IBlockchainRepository { | ||
clearClient(chainId: string): void; | ||
} | ||
|
||
@Module({ | ||
imports: [BlockchainApiManagerModule], | ||
providers: [ | ||
{ provide: IBlockchainRepository, useClass: BlockchainRepository }, | ||
], | ||
exports: [IBlockchainRepository], | ||
}) | ||
export class BlockchainRepositoryModule {} |
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,15 @@ | ||
import { IBlockchainRepository } from '@/domain/blockchain/blockchain.repository.interface'; | ||
import { IBlockchainApiManager } from '@/domain/interfaces/blockchain-api.manager.interface'; | ||
import { Inject, Injectable } from '@nestjs/common'; | ||
|
||
@Injectable() | ||
export class BlockchainRepository implements IBlockchainRepository { | ||
constructor( | ||
@Inject(IBlockchainApiManager) | ||
private readonly blockchainApiManager: IBlockchainApiManager, | ||
) {} | ||
|
||
clearClient(chainId: string): void { | ||
this.blockchainApiManager.destroyBlockchainApi(chainId); | ||
} | ||
} |
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,21 @@ | ||
import { BlockchainApiManager } from '@/datasources/blockchain/blockchain-api.manager'; | ||
import { ConfigApiModule } from '@/datasources/config-api/config-api.module'; | ||
import { PublicClient } from 'viem'; | ||
import { Module } from '@nestjs/common'; | ||
|
||
export const IBlockchainApiManager = Symbol('IBlockchainApiManager'); | ||
|
||
export interface IBlockchainApiManager { | ||
getBlockchainApi(chainId: string): Promise<PublicClient>; | ||
|
||
destroyBlockchainApi(chainId: string): void; | ||
} | ||
|
||
@Module({ | ||
imports: [ConfigApiModule], | ||
providers: [ | ||
{ provide: IBlockchainApiManager, useClass: BlockchainApiManager }, | ||
], | ||
exports: [IBlockchainApiManager], | ||
}) | ||
export class BlockchainApiManagerModule {} |
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
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is great. In fact, being strict we should implement the same destroy mechanism in
transaction-api.manager.ts
, as the Transaction Service URLs can also change (even is it's pretty unusual). Now it requires a CGW restart to get a new configuration for a givenTransactionApi
datasource. What do you think?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Agree that it's unusual but I think it would be a good pattern to copy. We could even consider creating a specific interface for these managers that we implement in both.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I refactored the
TransactionApiManager
andBalancesApiManager
to follow the same pattern with a common interface in #1640. When that is merged, I will propagate the changes into theBlockchainApiManager
as well.