-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbenchmark_complex_mint.js
129 lines (105 loc) · 4.19 KB
/
benchmark_complex_mint.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
import { KeyPair, keyStores, connect, Contract, utils } from 'near-api-js';
import { globalConfig } from './config.js';
import { v4 as uuidv4 } from 'uuid';
import chalk from 'chalk';
import fs from "fs";
if (process.argv.length < 3) {
console.error('Usage: node benchmark_simple_mint.js <path/to/account_list.csv>');
process.exit(1);
}
const csvFilePath = process.argv[2];
const actionList = ["token", "nft"];
const startMintComplex = async (userAddress, userPrivateKey, action) => {
// console.log("MINT ALL global config ", globalConfig);
console.log(chalk.blue("MINT ALL global config "), globalConfig);
console.log(chalk.white.bold.bgRed("Action: [" + action + "] for user: " + userAddress));
const gas = "300000000000000";
const attachedDeposit = utils.format.parseNearAmount("0.1");
const keyPair = KeyPair.fromString(userPrivateKey);
const keyStore = new keyStores.InMemoryKeyStore();
const nearConfig = {
...globalConfig, ...{
keyStore: keyStore
}
}
await keyStore.setKey(nearConfig.networkId, userAddress, keyPair);
const nearConnection = await connect(nearConfig);
const account = await nearConnection.account(userAddress);
const contractToken = new Contract(
account,
nearConfig.contractToken,
{
viewMethods: ['ft_balance_of'],
changeMethods: ['storage_deposit'],
}
);
const contractNFT = new Contract(
account,
nearConfig.contractNFT,
{
viewMethods: ['nft_tokens_for_owner'],
changeMethods: [],
}
);
const contractAccount = new Contract(
account,
nearConfig.contractMain,
{
viewMethods: ['getUserPoints', 'getAllUserPoints'],
changeMethods: ['mintAll'],
}
);
while (true) {
try {
let waitTime = Math.floor(Math.random() * (3000 - 1000 + 1) + 1000);
await new Promise(resolve => setTimeout(resolve, waitTime));
var startTime = new Date();
/**
* cross contracat call token mint, 6 receiept each time (https://testnet.nearblocks.io/txns/85rdHwCLNn2Th9gjw6ZEqNxfjzMbs5t1Amcg8rTyYDTK?tab=summary)
*/
var result = "";
if (action == "token") {
await contractToken.storage_deposit({ "account_id": userAddress }, gas, attachedDeposit); //it's stress test so just spam the network anyway
result = await contractAccount.mintAll({ action: "token" }, gas, attachedDeposit);
}
/**
* cross contract call mint NFT, 8 receipts each time (https://testnet.nearblocks.io/txns/7ZRMyBnXHQpdHYxEMGEwQmNtmbs74smkRj4cWRCmmKWB?tab=summary)
*/
if (action == "nft") {
result = await contractAccount.mintAll({ action: "nft", tokenId: uuidv4() }, gas, attachedDeposit);
}
console.log("Result: ", result);
console.log(chalk.white.bgGreen(
"Done in " + (new Date() - startTime) / 1000 + " seconds, with " + (action == "nft" ? 8 : 6) + " receipts (shard's transactions) , NEAR balance: ")
);
} catch (e) {
console.log("❌ Error, retry after few secs~~:", e);
let waitTime = Math.floor(Math.random() * (5000 - 1000 + 1) + 1000);
await new Promise(resolve => setTimeout(resolve, waitTime));
}
}
};
// startMintComplex(userAddress, userPrivateKey, "token");
const run = async () => {
const randomAction = actionList[Math.floor(Math.random() * actionList.length)];
fs.readFile(csvFilePath, 'utf8', async (err, data) => {
if (err) {
console.error('Error reading CSV file:', err);
return;
}
const rows = data.trim().split('\n');
const result = [];
for (let i = 1; i < rows.length; i++) {
const [key, value] = rows[i].split(',');
result.push({ key, value });
startMintComplex(key, value, randomAction);
}
console.log(result);
});
};
run()
.then(() => {
})
.catch((error) => {
console.error('An error occurred:', error);
});