forked from mxnster/martian-NFT-bot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
146 lines (123 loc) · 4.57 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
import { AptosClient, AptosAccount, CoinClient } from "aptos";
import { Buffer } from "buffer";
import { config } from "./config.js";
import consoleStamp from 'console-stamp';
import fs from 'fs'
consoleStamp(console, { format: ':date(HH:MM:ss)' });
const pk = config.mainAccountPrivateKey.slice(2, config.mainAccountPrivateKey.length);
const client = new AptosClient(config.nodeUrl);
const coinClient = new CoinClient(client)
const mainAccount = new AptosAccount(Uint8Array.from(Buffer.from(pk, 'hex')));
const generateRandomAmount = (min, max) => Math.random() * (max - min) + min;
const timeout = ms => new Promise(res => setTimeout(res, ms))
async function sendTransaction(sender, payload, nonce) {
try {
const txnRequest = await client.generateTransaction(sender.address(), payload, {
gas_unit_price: config.gasUnitPrice,
max_gas_amount: config.maxGasAmount,
sequence_number: nonce
});
const signedTxn = await client.signTransaction(sender, txnRequest);
const transactionRes = await client.submitTransaction(signedTxn);
return await client.waitForTransactionWithResult(transactionRes.hash, { checkSuccess: true })
} catch (err) {
try {
console.log('[ERROR]', JSON.parse(err?.message).message)
} catch { console.log('[ERROR]', err.message) }
await timeout(60000)
}
}
async function sendAptos(from, to, amount) {
amount = amount.toFixed(7);
console.log(`Sending ${amount} APT to ${to.address()}`);
return await sendTransaction(from, {
type: "entry_function_payload",
function: "0x1::aptos_account::transfer",
type_arguments: [],
arguments: [to.address().hex(), (amount * 100000000).toFixed(0)]
})
}
async function approveNFT(sender, label) {
console.log(`Approving ${label}`);
return await sendTransaction(sender, {
type: "entry_function_payload",
function: "0x3::token::create_collection_script",
type_arguments: [],
arguments: [
label,
"Martian Testnet NFT",
"https://aptos.dev",
"9007199254740991",
[false, false, false]
],
}, generator.next().value)
}
async function mintNFT(sender, label, name) {
console.log(`Minting ${name}`);
return await sendTransaction(sender, {
type: "entry_function_payload",
function: "0x3::token::create_token_script",
type_arguments: [],
arguments: [
label,
name,
"OG Martian",
"1",
"9007199254740991",
"https://gateway.pinata.cloud/ipfs/QmXiSJPXJ8mf9LHijv6xFH1AtGef4h8v5VPEKZgjR4nzvM",
sender.address(),
"0",
"0",
[!1, !1, !1, !1, !1],
[],
[],
[]
]
}, generator.next().value)
}
async function nftCreationHandler(sender) {
let number = (Math.floor(9e4 * Math.random()) + 1e4).toString();
let label = "Martian Testnet" + number;
let name = "Martian NFT #" + number;
await approveNFT(sender, label)
await mintNFT(sender, label, name)
}
async function checkBalance(account) {
try {
let balance = Number(await coinClient.checkBalance(account)) / 100000000
console.log(`Balance ${balance} APT`);
return balance
} catch (err) {
try {
console.log('[ERROR]', JSON.parse(err?.message).message)
} catch {
console.log('[ERROR]', err.message)
}
await timeout(60000)
}
return 1
}
function savePrivateKey(privateKey) {
fs.appendFileSync("keys.txt", `${privateKey}\n`, "utf8");
}
let generator;
function* nonceMaker(i) {
let nonce = i;
while (true) {
yield nonce++;
}
}
(async () => {
while (await checkBalance(mainAccount) > config.aptosAmountPerAccount.max) {
const sandwich = new AptosAccount();
let randomAmount = generateRandomAmount(config.aptosAmountPerAccount.min, config.aptosAmountPerAccount.max)
await sendAptos(mainAccount, sandwich, randomAmount);
const nftAccount = new AptosAccount();
savePrivateKey(nftAccount.toPrivateKeyObject().privateKeyHex)
await sendAptos(sandwich, nftAccount, randomAmount - generateRandomAmount(0.003, 0.005));
generator = nonceMaker(0);
await Promise.all(Array(config.nftCountPerAccount).fill(0).map(() => nftCreationHandler(nftAccount)))
console.log(`Check NFTs: https://explorer.aptoslabs.com/account/${nftAccount.address()}/tokens`);
console.log("-".repeat(130));
}
})()