diff --git a/packages/cli/examples/simulate.ts b/packages/cli/examples/simulate.ts new file mode 100644 index 00000000..19bb8724 --- /dev/null +++ b/packages/cli/examples/simulate.ts @@ -0,0 +1,48 @@ +import { makeCosmoshubPath } from "@cosmjs/amino"; +import { DirectSecp256k1HdWallet } from "@cosmjs/proto-signing"; +import { + assertIsBroadcastTxSuccess, + calculateFee, + GasPrice, + MsgSendEncodeObject, + SigningStargateClient, +} from "@cosmjs/stargate"; + +// Wallet +const mnemonic = + "economy stock theory fatal elder harbor betray wasp final emotion task crumble siren bottom lizard educate guess current outdoor pair theory focus wife stone"; +const path = makeCosmoshubPath(3); +const prefix = "cosmos"; +const wallet = await DirectSecp256k1HdWallet.fromMnemonic(mnemonic, { hdPaths: [path], prefix: prefix }); +const [account] = await wallet.getAccounts(); +console.log("Signer address:", account.address); + +// Network config +const rpcEndpoint = "ws://localhost:26658"; +const gasPrice = GasPrice.fromString("0.025ucosm"); + +// Setup client +const client = await SigningStargateClient.connectWithSigner(rpcEndpoint, wallet); + +// Send transaction (using simulate) +const recipient = "cosmos1xv9tklw7d82sezh9haa573wufgy59vmwe6xxe5"; +const amount = { + denom: "ucosm", + amount: "1234567", +}; +const sendMsg: MsgSendEncodeObject = { + typeUrl: "/cosmos.bank.v1beta1.MsgSend", + value: { + fromAddress: account.address, + toAddress: recipient, + amount: [amount], + }, +}; +const memo = "With simulate"; +const gasEstimation = await client.simulate(account.address, [sendMsg], memo); +const fee = calculateFee(Math.floor(gasEstimation * 1.3), gasPrice); +const result = await client.sendTokens(account.address, recipient, [amount], fee, memo); +assertIsBroadcastTxSuccess(result); +console.log("Successfully broadcasted:", result); + +client.disconnect(); diff --git a/packages/cli/run_examples.sh b/packages/cli/run_examples.sh index 897442ca..c8fcd5c6 100755 --- a/packages/cli/run_examples.sh +++ b/packages/cli/run_examples.sh @@ -15,4 +15,5 @@ yarn node ./bin/cosmwasm-cli --init examples/mask.ts --code "process.exit(0)" yarn node ./bin/cosmwasm-cli --init examples/multisig_address.ts --code "process.exit(0)" if [ -n "${SIMAPP42_ENABLED:-}" ]; then yarn node ./bin/cosmwasm-cli --init examples/stargate.ts --code "process.exit(0)" + yarn node ./bin/cosmwasm-cli --init examples/simulate.ts --code "process.exit(0)" fi