mirror of
https://github.com/cosmos/cosmjs.git
synced 2025-03-10 21:49:15 +00:00
Add delegation demo
This commit is contained in:
parent
ccd9a5db11
commit
8b535aa01a
38
packages/cli/examples/delegate.ts
Normal file
38
packages/cli/examples/delegate.ts
Normal file
@ -0,0 +1,38 @@
|
||||
const pen = await Secp256k1Pen.fromMnemonic("enlist hip relief stomach skate base shallow young switch frequent cry park");
|
||||
const senderAddress = pen.address("cosmos");
|
||||
|
||||
const client = new CosmosClient("http://localhost:1317");
|
||||
|
||||
const msg: MsgDelegate = {
|
||||
type: "cosmos-sdk/MsgDelegate",
|
||||
value: {
|
||||
delegator_address: senderAddress,
|
||||
// To get the proper validator address, start the demo chain (./scripts/wasmd/start.sh), then run:
|
||||
// curl http://localhost:1317/staking/validators | jq '.result[0].operator_address'
|
||||
validator_address: "cosmosvaloper1gjvanqxc774u6ed9thj4gpn9gj5zus5u32enqn",
|
||||
amount: coin(300000, "ustake"),
|
||||
}
|
||||
}
|
||||
const fee = {
|
||||
amount: coins(2000, "ucosm"),
|
||||
gas: "120000", // 120k
|
||||
};
|
||||
const memo = "Use your power wisely";
|
||||
|
||||
const chainId = await client.getChainId();
|
||||
console.log("Connected to chain:", chainId);
|
||||
|
||||
const { accountNumber, sequence } = await client.getNonce(senderAddress);
|
||||
console.log("Account/sequence:", accountNumber, sequence);
|
||||
|
||||
const signBytes = makeSignBytes([msg], fee, chainId, memo, accountNumber, sequence);
|
||||
const signature = await pen.sign(signBytes);
|
||||
const signedTx: StdTx = {
|
||||
msg: [msg],
|
||||
fee: fee,
|
||||
memo: memo,
|
||||
signatures: [signature],
|
||||
};
|
||||
|
||||
const result = await client.postTx(signedTx);
|
||||
console.log("Post result:", result);
|
Loading…
x
Reference in New Issue
Block a user