telescope icon indicating copy to clipboard operation
telescope copied to clipboard

option for dependency injection of RpcClient

Open turadg opened this issue 11 months ago • 0 comments

Problem

The rpc.query.ts that Telescope generates is like,

export const createRPCQueryClient = async ({
  rpcEndpoint,
}: {
  rpcEndpoint: string | HttpEndpoint;
}) => {
  const tmClient = await Tendermint34Client.connect(rpcEndpoint);
  const client = new QueryClient(tmClient);

Such a construction depends on ambient authority of Tendermint34Client.connect.

This causes a couple problems:

Prototype lockdown

The module with connect imports axios and that fails to load in Hardened JS:

  • https://github.com/axios/axios/pull/6265

Ambient authority

fetch may not always be available, for example if run in a hardened compartment.

Mocking in tests

To test code with a fake RpcClient requires hacking into the module loader to intercept Axios.

Possible solution

You could move to just using Fetch API but that would still be an ambient authority.

Better would be an option for the createRPCQueryClient helper (or whatever it be called) to take an RpcClient instead of an endpoint. Then consumers can make their own RpcClient using whatever authorities are available, including a test mock.

Inside the function you can create the Tendermint client from the RpcClient argument,

const tmClient = await Tendermint34Client.create(rpcClient);

turadg avatar Nov 06 '24 23:11 turadg