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
636 stars 324 forks source link

Allow simulation of messages without private key access #1568

Open jmrossy opened 4 months ago

jmrossy commented 4 months ago

Currently, the simulate function isn't available on the StargateClient, only the SigningStargateClient. But signing clients require access to the key, which isn't always practical or possible.

I've been able to hack together a key-less version based on the signing client's simulate method but official support in the StargateClient would be preferable.

  const registry = new Registry([...defaultRegistryTypes, ...wasmTypes]);
  const encodedMsg = registry.encodeAsAny(message);
  const encodedPubkey = encodeSecp256k1Pubkey(Buffer.from(senderPubKey, 'hex'));
  const { sequence } = await stargateClient.getSequence(sender);
  const { gasInfo } = await stargateClient
    // @ts-ignore force access to protected method
    .forceGetQueryClient()
    .tx.simulate([encodedMsg], memo, encodedPubkey, sequence);
  const gasUnits = Uint53.fromString(gasInfo.gasUsed.toString()).toNumber();