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

Add IBlockchainApiManager for on-chain interactions #1623

Merged
merged 8 commits into from
Jun 12, 2024

Conversation

iamacook
Copy link
Member

@iamacook iamacook commented Jun 10, 2024

Summary

Smart contract wallets use EIP-1271 to sign messages. As we progress with SiWe-based authenticated, we will also need to add support for verification of messages signed with EIP-1271. Currently, our SiWe implementation assumes that signed messages come only from EOAs. In order to support verification for the above, we will need to make on-chain requests.

This adds a new IBlockchainManager, returning a client per chain. It can be used as a basis for future on-chain interactions, e.g. verification of EIP-1271 signatures.

It is important to note that w need a specific API key for the project.

Changes

  • Add relative configuration for Infura API key
  • Add IBlockchainApiManager and implementation with test coverage
  • Clear client on CHAIN_UPDATE
  • Add relevant test coverage

@iamacook iamacook self-assigned this Jun 10, 2024
@coveralls
Copy link

coveralls commented Jun 10, 2024

Pull Request Test Coverage Report for Build 9446016581

Details

  • 24 of 34 (70.59%) changed or added relevant lines in 4 files are covered.
  • No unchanged relevant lines lost coverage.
  • Overall coverage decreased (-0.04%) to 92.662%

Changes Missing Coverage Covered Lines Changed/Added Lines %
src/datasources/blockchain/blockchain-api.manager.ts 15 16 93.75%
src/domain/interfaces/blockchain-api.interface.ts 0 1 0.0%
src/datasources/blockchain/blockchain-api.service.ts 9 12 75.0%
src/domain/interfaces/blockchain-api.manager.interface.ts 0 5 0.0%
Totals Coverage Status
Change from base Build 9444680700: -0.04%
Covered Lines: 7154
Relevant Lines: 7419

💛 - Coveralls

@coveralls
Copy link

coveralls commented Jun 10, 2024

Pull Request Test Coverage Report for Build 9451506530

Details

  • 48 of 49 (97.96%) changed or added relevant lines in 8 files are covered.
  • 1 unchanged line in 1 file lost coverage.
  • Overall coverage increased (+0.05%) to 92.753%

Changes Missing Coverage Covered Lines Changed/Added Lines %
src/domain/interfaces/blockchain-api.interface.ts 0 1 0.0%
Files with Coverage Reduction New Missed Lines %
src/routes/transactions/mappers/common/transaction-info.mapper.ts 1 78.53%
Totals Coverage Status
Change from base Build 9444680700: 0.05%
Covered Lines: 7175
Relevant Lines: 7433

💛 - Coveralls

@iamacook iamacook marked this pull request as ready for review June 11, 2024 09:33
@iamacook iamacook requested a review from a team as a code owner June 11, 2024 09:33
@coveralls
Copy link

coveralls commented Jun 11, 2024

Pull Request Test Coverage Report for Build 9463145202

Details

  • 48 of 49 (97.96%) changed or added relevant lines in 8 files are covered.
  • 1 unchanged line in 1 file lost coverage.
  • Overall coverage increased (+0.02%) to 92.734%

Changes Missing Coverage Covered Lines Changed/Added Lines %
src/domain/interfaces/blockchain-api.interface.ts 0 1 0.0%
Files with Coverage Reduction New Missed Lines %
src/routes/transactions/entities/tests/human-description.builder.ts 1 90.0%
Totals Coverage Status
Change from base Build 9460927247: 0.02%
Covered Lines: 7206
Relevant Lines: 7471

💛 - Coveralls

@coveralls
Copy link

coveralls commented Jun 11, 2024

Pull Request Test Coverage Report for Build 9464947500

Details

  • 48 of 49 (97.96%) changed or added relevant lines in 8 files are covered.
  • No unchanged relevant lines lost coverage.
  • Overall coverage increased (+0.03%) to 92.744%

Changes Missing Coverage Covered Lines Changed/Added Lines %
src/domain/interfaces/blockchain-api.interface.ts 0 1 0.0%
Totals Coverage Status
Change from base Build 9460927247: 0.03%
Covered Lines: 7207
Relevant Lines: 7471

💛 - Coveralls

Comment on lines +33 to +36
destroyBlockchainApi(chainId: string): void {
if (this.blockchainApiMap?.[chainId]) {
delete this.blockchainApiMap[chainId];
}
Copy link
Member

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 given TransactionApi datasource. What do you think?

Copy link
Member Author

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.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I refactored the TransactionApiManager and BalancesApiManager to follow the same pattern with a common interface in #1640. When that is merged, I will propagate the changes into the BlockchainApiManager as well.

Comment on lines 22 to 27
getClient(): PublicClient {
return createPublicClient({
chain: this.getChain(),
transport: http(),
});
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not totally against this, but wanted to known the approach better: why do we expose the PublicClient outside this class? Wouldn't it be more inline with the other datasource classes (*-api.service.ts) to just expose the functions itself (even we are mostly wrapping the client in this case)?

I mean, instead of the having the clients of this class doing:

const blockchainClient = blockChainApi.getClient();
const blockNumber = await blockchainClient.getBlockNumber();

They would do something like:

const blockNumber = await blockchainApi.getBlockNumber();

Exposing the PublicClient seems a bit like defeating the purpose of having a datasource, which in my opinion has as one of its main benefits the encapsulation of the underlying implementation. What do you think?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point. IBlockchainApi is essentially redundant (it was a remanant from trialling the implementation). I removed it in e38f109 as per your suggestion.

constructor(
private readonly configurationService: IConfigurationService,
private readonly chain: DomainChain,
public readonly destroyClient: (chainId: string) => void,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's the purpose of this function? I understand BlockchainApiManager.destroyBlockchainApi is intended to delete the BlockChainApi entry in the map, but in this class this function doesn't do anything, right? It's intended to release some resources?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@coveralls
Copy link

coveralls commented Jun 11, 2024

Pull Request Test Coverage Report for Build 9467806260

Details

  • 41 of 41 (100.0%) changed or added relevant lines in 6 files are covered.
  • 1 unchanged line in 1 file lost coverage.
  • Overall coverage increased (+0.02%) to 92.728%

Files with Coverage Reduction New Missed Lines %
src/routes/transactions/entities/tests/human-description.builder.ts 1 80.0%
Totals Coverage Status
Change from base Build 9460927247: 0.02%
Covered Lines: 7198
Relevant Lines: 7463

💛 - Coveralls

@iamacook iamacook changed the title Add BlockchainApi with associated manager Add IBlockchainApiManager for on-chain interactions Jun 11, 2024
Copy link
Member

@hectorgomezv hectorgomezv left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👏🏻

@iamacook iamacook merged commit d932f70 into main Jun 12, 2024
16 checks passed
@iamacook iamacook deleted the blockchain-api-manager branch June 12, 2024 07:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants