-
Notifications
You must be signed in to change notification settings - Fork 135
/
Copy pathindex.js
164 lines (139 loc) · 4.49 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
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
const { ethers } = require("ethers");
const config = require("./config")
const { v4: uuidv4 } = require('uuid');
const Handlebars = require('handlebars');
const axios = require("axios");
const crypto = require("crypto")
// 连接到结点
const provider = new ethers.providers.JsonRpcProvider(config.rpcUrl);
// 创建钱包
const wallet = new ethers.Wallet(config.privateKey.trim(), provider);
async function sleep(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}
function randomInteger(minimum, maximum) {
if (maximum === undefined) {
maximum = minimum;
minimum = 0;
}
if (typeof minimum !== 'number' || typeof maximum !== 'number') {
throw new TypeError('Expected all arguments to be numbers');
}
return Math.floor(
(Math.random() * (maximum - minimum + 1)) + minimum
);
}
async function sha256(message) {
const msgBuffer = new TextEncoder().encode(message);
const hashBuffer = await crypto.subtle.digest('SHA-256', msgBuffer);
const hashArray = Array.from(new Uint8Array(hashBuffer));
const hashHex = hashArray.map(b => ('00' + b.toString(16)).slice(-2)).join('');
return hashHex;
}
async function checkAvaible(tokenJson){
let hash = await sha256(tokenJson)
let url = 'https://mainnet-api.ethscriptions.com/api/ethscriptions/exists/' + hash
try {
let response = await axios.get(url)
return response.data.result === false;
} catch (error) {
console.log(error)
}
return false
}
// 转成16进制
const convertToHexa = (str = '') =>{
return `0x${Buffer.from(str).toString('hex')}`
}
// 获取当前账户的 nonce
async function getCurrentNonce(wallet) {
try {
const nonce = await wallet.getTransactionCount("pending");
console.log("Nonce:", nonce);
return nonce;
} catch (error) {
console.error("Error fetching nonce:", error.message);
throw error;
}
}
// 获取当前主网 gas 价格
async function getGasPrice() {
const gasPrice = await provider.getGasPrice();
return gasPrice;
}
// 获取链上实时 gasLimit
async function getGasLimit(hexData, address) {
const gasLimit = await provider.estimateGas({
to: address,
value: ethers.utils.parseEther("0"),
data: hexData,
});
return gasLimit.toNumber();
}
// 转账交易
async function sendTransaction(nonce) {
let uuid = uuidv4()
let hexData = config.tokenDataHex
let tokenJson = config.tokenJson
if (config.tokenJson !== '') {
let template = Handlebars.compile(config.tokenJson.trim());
let templateData = {"uuid": `${uuid}`}
tokenJson = template(templateData);
let id = randomInteger(config.tokenMinId, config.tokenMaxId)
templateData = {"id": `${id}`}
tokenJson = template(templateData);
console.log("铭文json数据", tokenJson)
if (config.tokenJson.indexOf("{{id}}") > 0) {
let avaible = await checkAvaible(tokenJson)
if (!avaible) {
return
}
}
hexData = convertToHexa(tokenJson);
console.log("铭文16进制数据", hexData)
}
// 获取实时 gasPrice
const currentGasPrice = await getGasPrice();
// 在当前 gasPrice 上增加 一定倍数
const gasMultiple = parseInt(String(config.increaseGas * 100))
const increasedGasPrice = currentGasPrice.div(100).mul(gasMultiple);
// 获取钱包地址
let address = await wallet.getAddress();
if (config.receiveAddress !== "") {
address = config.receiveAddress;
}
// 获取当前 gasLimit 限制
const gasLimit = await getGasLimit(hexData, address);
// 付费金额
const payPrice = config.payPrice
console.log("正在打铭文数据的16进制数据", hexData)
const transaction = {
to: address,
// 替换为你要转账的金额
value: ethers.utils.parseEther(payPrice),
// 十六进制数据
data: hexData,
// 设置 nonce
nonce: nonce,
// 设置 gas 价格
gasPrice: increasedGasPrice,
// 限制gasLimit,根据当前网络转账的设置,不知道设置多少的去区块浏览器看别人转账成功的是多少
gasLimit: gasLimit,
};
try {
const tx = await wallet.sendTransaction(transaction);
console.log(`Transaction with nonce ${nonce} hash:`, tx.hash);
} catch (error) {
console.error(`Error in transaction with nonce ${nonce}:`, error.message);
}
}
// 发送多次交易
async function sendTransactions() {
const currentNonce = await getCurrentNonce(wallet);
const sleepTime = config.sleepTime
for (let i = 0; i < config.repeatCount; i++) {
await sendTransaction(currentNonce + i);
await sleep(sleepTime)
}
}
sendTransactions();