-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
96 lines (82 loc) · 2.44 KB
/
index.ts
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
import { Wallet } from "@ethersproject/wallet";
import {
broadcast,
getSender,
MAINNET_CHAIN,
signTransaction,
} from "@hanchon/evmos-ts-wallet";
import {
createTransaction,
createMsgWithdrawValidatorCommission,
createAnyMessage,
} from "@evmos/proto";
import * as authz from "@evmos/proto/dist/proto/cosmos/authz/v1beta1/tx";
import { Sender, TxGenerated } from "@evmos/transactions";
import * as dotenv from "dotenv";
dotenv.config();
export function sleep(milliseconds: number) {
return new Promise((resolve) => setTimeout(resolve, milliseconds));
}
// Constants
const MNEMONIC = process.env.MNEMONIC;
const VALIDATOR_ADDRESS = process.env.VALIDATOR_ADDRESS;
const ENDPOINT = "https://rest.bd.evmos.org:1317";
// Create the transaction that needs to be sent
function createTx(sender: Sender) {
let protos = [];
protos.push(
createAnyMessage(createMsgWithdrawValidatorCommission(VALIDATOR_ADDRESS))
);
const msgExec = new authz.cosmos.authz.v1beta1.MsgExec({
grantee: sender.accountAddress,
msgs: protos,
});
const message = { message: msgExec, path: "cosmos.authz.v1beta1.MsgExec" };
return createTransaction(
message,
"",
"4000000000000000",
"aevmos",
150000,
"ethsecp256",
sender.pubkey,
sender.sequence,
sender.accountNumber,
MAINNET_CHAIN.cosmosChainId
);
}
const main = async () => {
console.log("Getting sender");
const wallet = Wallet.fromMnemonic(MNEMONIC);
let sender: Sender;
try {
sender = await getSender(wallet, ENDPOINT);
} catch (e) {
console.error("Couldn't get the sender");
return;
}
console.log(`Sending transaction with sequence: ${sender.sequence}`);
const msg = createTx(sender);
const signedTransaction = await signTransaction(wallet, msg as TxGenerated);
const RETRY_MAX = 10;
let retry = RETRY_MAX;
while (retry > 0) {
const broadcastRes = await broadcast(signedTransaction, ENDPOINT);
if (broadcastRes.tx_response.code === 0) {
// Transaction success
console.log(`Success: ${broadcastRes.tx_response.txhash}`);
break;
} else if (broadcastRes.tx_response.code === 19) {
// Already broadcasted
console.log(`Already boarcasted: ${broadcastRes.tx_response.txhash}`);
break;
} else {
// Retry
console.log(`Error: ${JSON.stringify(broadcastRes.tx_response)}`);
retry = retry - 1;
await sleep(7000);
}
}
console.log("Script finished");
};
(() => main())();