-
Notifications
You must be signed in to change notification settings - Fork 30
Expand file tree
/
Copy path3-delegate.js
More file actions
75 lines (64 loc) · 2.9 KB
/
3-delegate.js
File metadata and controls
75 lines (64 loc) · 2.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
import {
QueryClient, setupDistributionExtension, setupBankExtension, setupStakingExtension, setupTxExtension, setupGovExtension
} from "@cosmjs/stargate";
import { Tendermint34Client } from '@cosmjs/tendermint-rpc';
import fs from "fs";
import SigningClient from "./utils/SigningClient.js";
import { coin } from '@cosmjs/launchpad';
import { getSigner } from "./utils/helpers.js";
const loadJSON = (path) => JSON.parse(fs.readFileSync(new URL(path, import.meta.url)));
const chainsMap = loadJSON('./assets/chains.json');
async function getQueryClient(rpcEndpoint) {
const tendermint34Client = await Tendermint34Client.connect(rpcEndpoint);
const queryClient = QueryClient.withExtensions(
tendermint34Client,
setupBankExtension,
setupStakingExtension,
setupTxExtension,
setupGovExtension,
setupDistributionExtension
);
return queryClient;
}
async function delegate(client, address, validator, amount) {
let ops = [];
ops.push({
typeUrl: "/cosmos.staking.v1beta1.MsgDelegate",
value: {
delegatorAddress: address,
validatorAddress: validator,
amount: amount
},
});
let result = await client.signAndBroadcast(address, ops, '', '');
return result;
}
async function start(chain, mnemonicOrKey, validatorAddress, delegationAmount) {
try {
const rpcEndpoint = chain.rpc;
let wallet = await getSigner(chain, mnemonicOrKey);
let client = new SigningClient(chain, wallet);
const [account] = await wallet.getAccounts();
const queryClient = await getQueryClient(rpcEndpoint);
let balances = await queryClient.bank.balance(account.address, chain.denom);
console.log(`${account.address} has ${balances.amount / Math.pow(10, chain.exponent)} ${chain.symbol}`);
if (balances.amount > 0) {
//Delegation
let result = await delegate(client, account.address, validatorAddress, coin("" + delegationAmount * Math.pow(10, chain.exponent), chain.denom));
let code = result.code;
if (code == 0) {
console.log(`${account.address} delegated ${delegationAmount} ${chain.symbol} to ${validatorAddress}: ${result.transactionHash}`)
} else {
console.log(`${account.address} FAILED to delegate ${delegationAmount} ${chain.symbol} to ${validatorAddress}. Reason: ${result.rawLog}`);
}
}
} catch (err) {
console.log(err)
console.log("Error in start: " + err);
}
}
const chainName = 'cosmos'; //Get the chain name from the chains.json
const mnemonicOrKey = 'Put mnemonic or private key here';
const validatorAddress = ''; //Validator address
const delegationAmount = 1; //Delegation amount
start(chainsMap[chainName], mnemonicOrKey, validatorAddress, delegationAmount);