Add simulate example
This commit is contained in:
parent
2a16375eab
commit
926767d1e1
48
packages/cli/examples/simulate.ts
Normal file
48
packages/cli/examples/simulate.ts
Normal file
@ -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();
|
||||
@ -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
|
||||
|
||||
Loading…
Reference in New Issue
Block a user