forked from huaigu/reth_miner
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmain.js
120 lines (104 loc) · 3.32 KB
/
main.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
const dotenv = require("dotenv");
dotenv.config();
const RPC_URL = process.env.RPC_URL;
const { ethers } = require("ethers");
const provider = new ethers.providers.JsonRpcProvider(RPC_URL);
const privateKey = process.env.PRIVATEKEY;
const wallet = new ethers.Wallet(privateKey, provider);
const account = wallet.address;
const currentChallenge = ethers.utils.formatBytes32String("rETH"); //0x7245544800000000000000000000000000000000000000000000000000000000
let solution;
const FgGreen = "\x1b[32m";
const FgYellow = "\x1b[33m";
const FgRed = "\x1b[31m";
const computeHash = () => {
while (1) {
const random_value = ethers.utils.randomBytes(32);
const potential_solution = ethers.utils.hexlify(random_value);
const hashed_solution = ethers.utils.keccak256(
ethers.utils.defaultAbiCoder.encode(
["bytes32", "bytes32"],
[potential_solution, currentChallenge]
)
);
if (hashed_solution.startsWith("0x77777")) {
logInfo(`solution found: ${hashed_solution}`);
solution = potential_solution;
break;
}
}
};
async function mine_rETH(idx) {
const jsonData = {
p: "rerc-20",
op: "mint",
tick: "rETH",
id: solution,
amt: "10000",
};
const dataHex = ethers.utils.hexlify(
ethers.utils.toUtf8Bytes(
"data:application/json," + JSON.stringify(jsonData)
)
);
const nonce = await provider.getTransactionCount(account);
const gasPrice = await provider.getGasPrice();
console.log(
FgYellow,
`=== Gas Price: ${(gasPrice / 1e9).toFixed(2)} gwei ===`
);
const ga = gasPrice.add(ethers.utils.parseUnits("3", "gwei"));
const tx = {
from: account,
to: account, // Self-transfer
nonce: nonce,
gasPrice: ga,
gasLimit: ethers.utils.hexlify(26000),
data: dataHex,
chainId: 1,
};
const signedTx = await wallet.signTransaction(tx);
const receipt = await provider.sendTransaction(signedTx);
//await to confirm
await provider.waitForTransaction(receipt.hash);
console.log(FgGreen, `Successful minted rETH: ${receipt.hash}`);
//async show gas consumption and balance
if (idx % 4 == 0) {
showGasConsumptionAndBalance(receipt.hash);
}
}
const sleep = (ms) => {
return new Promise((resolve) => setTimeout(resolve, ms));
};
const showGasConsumptionAndBalance = async (txHash) => {
const receipt = await provider.getTransactionReceipt(txHash);
const balance = await provider.getBalance(account);
console.log(FgYellow, `===Your balance: ${(balance / 1e18).toFixed(2)}===`);
// estimate how many reths you can mint base on current tx gas used and current balance
const estimateReth = Math.floor(
balance / receipt.gasUsed / receipt.effectiveGasPrice
);
console.log(
FgYellow,
`====You can mint rETH base on current balance: ${estimateReth}====`
);
};
const main = async () => {
let mintedCount = 0;
while (true) {
logInfo(`#-${mintedCount}: Calculating solution...`);
computeHash();
try {
await mine_rETH(mintedCount);
mintedCount++;
} catch (ex) {
console.error(ex);
logInfo(`#-${mintedCount}: Failed to mint rETH`);
}
}
};
const logInfo = (msg) => {
// log with datetime
console.log(`[${new Date().toLocaleString()}]: ${msg}`);
};
main();