cosmos / cosmjs

The Swiss Army knife to power JavaScript based client solutions ranging from Web apps/explorers over browser extensions to server-side clients like faucets/scrapers.
https://cosmos.github.io/cosmjs/
Apache License 2.0
645 stars 330 forks source link

[Feature]: Allow extends functionality with withExtension method. #1476

Open j0nl1 opened 12 months ago

j0nl1 commented 12 months ago

Summary

CosmJS is the reference library in Cosmos ecosystem, due to its growth it is difficult for such a generic library to support all the features of different blockchains.

Ideal scenario is that each team could create their own extensions for the library, while keeping a good code base for other functionalities such rpc clients, parsers, etc... This make possible to replace specific parts in the clients or extends the client.

An additional request would be also let provide an external queryClient in the constructor requiring necessary extensions to make clients work as expected.

Proposed Feature

I will use SigningCosmWasmClient as an example but it could be any stargate client. Extensions could override a method but keeping the same interface (Liskov principle)

public withExtension<E>(extensions: E): SigningCosmWasmClient & E {
    return Object.assign(this, extensions);
}
const client = new SigningCosmWasmClient(tmClient, signer, options).withExtension({ 
    ...createProtocolExtension(),
    ...createFeatureExtension()
})