-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdelegatevalidator.ts
133 lines (107 loc) · 3.33 KB
/
delegatevalidator.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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
// import { ethToEvmos, evmosToEth } from "@tharsis/address-converter";
import {
createTransactionWithMultipleMessages,
createMsgSend as protoMsgSend,
createMsgDelegate as protoMsgDelegate,
createMsgCreateValidator as protoMsgCreateValidator,
} from "@evmos/proto";
import { Wallet } from "@ethersproject/wallet";
import {
broadcast,
getSender,
signTransaction,
LOCALNET_CHAIN,
LOCALNET_FEE,
signTransactionUsingEIP712,
} from "@hanchon/evmos-ts-wallet";
import {
createEIP712,
createMsgSend,
generateFee,
generateMessageWithMultipleTransactions,
generateTypes,
MSG_SEND_TYPES,
createMsgDelegate,
MSG_DELEGATE_TYPES,
createMsgCreateValidator,
MSG_CREATE_VALIDATOR_TYPES,
} from "@evmos/eip712";
async function prepareTx(wallet: Wallet) {
const fee = LOCALNET_FEE;
fee.gas = "3000000";
fee.amount = "300000";
const sender = await getSender(wallet, "http://localhost:1317");
console.log(sender.pubkey);
const feeObject = generateFee(
fee.amount,
fee.denom,
fee.gas,
sender.accountAddress
);
var types = generateTypes(MSG_DELEGATE_TYPES);
const msgs: any[] = [];
var pubkey = wallet.publicKey.split("0x")[1];
var ba64 = Buffer.from(pubkey, "hex").toString("base64");
const max = "0.400000000000000000";
const change = "0.000000000000000001";
const rate = "0.100000000000000000";
msgs.push(
createMsgDelegate(sender.accountAddress,
"evmosvaloper1can8phtx6t6aq73p3c2ad2l3dwhmewxe0tegst",
"1000000000000000001",
"stake",)
);
const messages = generateMessageWithMultipleTransactions(
sender.accountNumber.toString(),
sender.sequence.toString(),
LOCALNET_CHAIN.cosmosChainId,
"",
feeObject,
msgs
);
const eipToSign = createEIP712(types, LOCALNET_CHAIN.chainId, messages);
const protoMsgs: any[] = [];
protoMsgs.push(
protoMsgDelegate(sender.accountAddress,
"evmosvaloper1can8phtx6t6aq73p3c2ad2l3dwhmewxe0tegst",
"1000000000000000001",
"stake",)
);
const tx = createTransactionWithMultipleMessages(
protoMsgs,
"",
fee.amount,
fee.denom,
parseInt(fee.gas, 10),
"ethsecp256",
sender.pubkey,
sender.sequence,
sender.accountNumber,
LOCALNET_CHAIN.cosmosChainId
);
return {
sender,
txSimple: {
signDirect: tx.signDirect,
legacyAmino: tx.legacyAmino,
eipToSign,
},
};
}
(async () => {
const seed =
"attitude pepper pattern poem acquire fetch alpha panda deer accuse knock ahead whip alarm mountain fossil pottery vessel cupboard situate parrot resist sock winter";
const wallet = Wallet.fromMnemonic(seed);
const msgMM = await prepareTx(wallet);
const resMM = await signTransactionUsingEIP712(
wallet,
msgMM.sender.accountAddress,
msgMM.txSimple
);
const broadcastRes = await broadcast(resMM, "http://localhost:1317");
if (broadcastRes.tx_response.code === 0) {
console.log("Success sign transaction");
} else {
console.log(`Error payload signature: ${JSON.stringify(broadcastRes)}`);
}
})();