forked from qzz0518/coss
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
68 lines (59 loc) · 2.75 KB
/
index.js
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
require('dotenv').config();
const { SigningStargateClient, GasPrice, coins } = require("@cosmjs/stargate");
const { DirectSecp256k1Wallet } = require('@cosmjs/proto-signing');
const { readFileSync } = require("fs");
const {base64FromBytes} = require("cosmjs-types/helpers");
async function performTransaction(walletInfo, numberOfTimes) {
const rpcEndpoint = process.env.NODE_URL;
const gasPrice = GasPrice.fromString("0.025uatom");
const wallet = await DirectSecp256k1Wallet.fromKey(Buffer.from(walletInfo.privateKey, "hex"), "cosmos");
const client = await SigningStargateClient.connectWithSigner(rpcEndpoint, wallet, { gasPrice: gasPrice });
const fee = {
amount: coins(405, "uatom"),
gas: "81000",
};
let successCount = 0;
let attemptCount = 0;
while (successCount < numberOfTimes) {
try {
const [account] = await wallet.getAccounts();
const amount = coins(1, "uatom");
const memo = 'data:,{"op":"mint","amt":10000,"tick":"coss","p":"crc-20"}';
const result = await client.sendTokens(account.address, account.address, amount, fee, base64FromBytes(Buffer.from(memo, 'utf8')));
console.log(`${account.address}, 第 ${successCount + 1} 次操作成功: ${'https://www.mintscan.io/cosmos/tx/' + result.transactionHash}`);
successCount++;
} catch (error) {
console.error(`尝试次数 ${attemptCount + 1} 失败: `, error);
await new Promise(resolve => setTimeout(resolve, 1000)); // 失败后等待一秒
}
attemptCount++;
}
console.log(`总共尝试次数: ${attemptCount}, 成功次数: ${successCount}`);
}
async function main() {
let walletData = [];
try {
walletData = JSON.parse(readFileSync('cosmos_wallets.json', 'utf-8'));
} catch (e) {
console.log('未找到 cosmos_wallets.json,使用配置的主钱包');
}
const privateKey = process.env.PRIVATE_KEY;
const wallet = await DirectSecp256k1Wallet.fromKey(Buffer.from(privateKey, "hex"), "cosmos");
const [account] = await wallet.getAccounts();
const walletAddress = account.address;
const client = await SigningStargateClient.connectWithSigner(process.env.NODE_URL, wallet);
const balance = await client.getBalance(walletAddress, "uatom");
console.log(`地址: ${walletAddress} 余额: ${parseFloat(balance.amount) / 1000000}`);
walletData.push( {
"address": walletAddress,
"privateKey": privateKey
});
Promise.all(walletData.map(wallet => performTransaction(wallet, 110)))
.then(() => {
console.log("所有操作完成");
})
.catch(error => {
console.error("操作中有错误发生: ", error);
});
}
main();