-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* ChainClientRouter and its integration * upd comment * pr feedback
- Loading branch information
Showing
4 changed files
with
80 additions
and
12 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
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,64 @@ | ||
import { CosmWasmClient } from '@cosmjs/cosmwasm-stargate' | ||
import { StargateClient } from '@cosmjs/stargate' | ||
|
||
type ChainClientRoutes<T> = { | ||
[rpcEndpoint: string]: T | ||
} | ||
|
||
type HandleConnect<T> = (rpcEndpoint: string) => Promise<T> | ||
|
||
/* | ||
* This is a workaround for `@cosmjs` clients to avoid connecting to the chain more than once. | ||
* | ||
* @example | ||
* export const stargateClientRouter = new ChainClientRouter({ | ||
* handleConnect: (rpcEndpoint: string) => StargateClient.connect(rpcEndpoint), | ||
* }) | ||
* | ||
* const client = await stargateClientRouter.connect(RPC_ENDPOINT); | ||
* | ||
* const queryResponse = await client.queryContractSmart(...); | ||
* */ | ||
class ChainClientRouter<T> { | ||
private readonly handleConnect: HandleConnect<T> | ||
private instances: ChainClientRoutes<T> = {} | ||
|
||
constructor({ handleConnect }: { handleConnect: HandleConnect<T> }) { | ||
this.handleConnect = handleConnect | ||
} | ||
|
||
/* | ||
* Connect to the chain and return the client | ||
* or return an existing instance of the client. | ||
* */ | ||
async connect(rpcEndpoint: string) { | ||
if (!this.getClientInstance(rpcEndpoint)) { | ||
const instance = await this.handleConnect(rpcEndpoint) | ||
this.setClientInstance(rpcEndpoint, instance) | ||
} | ||
|
||
return this.getClientInstance(rpcEndpoint) | ||
} | ||
|
||
private getClientInstance(rpcEndpoint: string) { | ||
return this.instances[rpcEndpoint] | ||
} | ||
|
||
private setClientInstance(rpcEndpoint: string, client: T) { | ||
this.instances[rpcEndpoint] = client | ||
} | ||
} | ||
|
||
/* | ||
* Router for connecting to `CosmWasmClient`. | ||
* */ | ||
export const cosmWasmClientRouter = new ChainClientRouter({ | ||
handleConnect: (rpcEndpoint: string) => CosmWasmClient.connect(rpcEndpoint), | ||
}) | ||
|
||
/* | ||
* Router for connecting to `StargateClient`. | ||
* */ | ||
export const stargateClientRouter = new ChainClientRouter({ | ||
handleConnect: (rpcEndpoint: string) => StargateClient.connect(rpcEndpoint), | ||
}) |