cosmjs/packages/cli/examples/delegate.ts
2023-04-06 18:02:44 +02:00

49 lines
1.5 KiB
TypeScript

import { DirectSecp256k1HdWallet } from "@cosmjs/proto-signing";
import {
coin,
MsgDelegateEncodeObject,
SigningStargateClient,
calculateFee,
assertIsDeliverTxSuccess,
GasPrice,
} from "@cosmjs/stargate";
// Wallet
const wallet = await DirectSecp256k1HdWallet.fromMnemonic(
"enlist hip relief stomach skate base shallow young switch frequent cry park",
);
const [{ address: signerAddress }] = await wallet.getAccounts();
console.log("Signer address:", signerAddress);
// Network config
const rpcEndpoint = "ws://localhost:26658";
const gasPrice = GasPrice.fromString("0.025ucosm");
// Setup client
const client = await SigningStargateClient.connectWithSigner(rpcEndpoint, wallet);
// Send delegate transaction
const msg: MsgDelegateEncodeObject = {
typeUrl: "/cosmos.staking.v1beta1.MsgDelegate",
value: {
delegatorAddress: signerAddress,
// To get the proper validator address, start the demo chain (./scripts/simapp44/start.sh), then run:
// curl http://localhost:1318/staking/validators | jq '.result[0].operator_address'
validatorAddress: "cosmosvaloper12nt2hqjps8r065wc02qks88tvqzdeua06e982h",
amount: coin(300000, "ustake"),
},
};
const fee = calculateFee(180_000, gasPrice);
const memo = "Use your power wisely";
const chainId = await client.getChainId();
console.log("Connected to chain:", chainId);
const result = await client.signAndBroadcast(signerAddress, [msg], fee, memo);
console.log("Broadcast result:", result);
assertIsDeliverTxSuccess(result);
console.log("Successfully broadcasted:", result);
client.disconnect();