-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathclaim.js
148 lines (128 loc) · 4.06 KB
/
claim.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
import { KeyPair, keyStores, connect, Near } from "near-api-js";
import { Twisters } from "twisters";
import BigNumber from "bignumber.js";
import { mainnetConfig } from "./rpc.js";
import { acc } from "./account.js";
const near = new Near(mainnetConfig);
const twisters = new Twisters();
const getAccount = (accountId, privateKey) =>
new Promise(async (resolve, reject) => {
try {
const keyStore = new keyStores.InMemoryKeyStore();
const keyPair = KeyPair.fromString(privateKey);
await keyStore.setKey(mainnetConfig.networkId, accountId, keyPair);
const connectionConfig = {
deps: {
keyStore,
},
...mainnetConfig,
};
const accountConnection = await connect(connectionConfig);
const account = await accountConnection.account(accountId);
resolve(account);
} catch (error) {
reject(error);
}
});
const getUser = async (near, accountId) => {
const argument = {
account_id: accountId,
};
const result = await near.connection.provider.query({
account_id: "game.hot.tg",
finality: "optimistic",
request_type: "call_function",
method_name: "get_user",
args_base64: Buffer.from(JSON.stringify(argument)).toString("base64"),
});
const detailUser = JSON.parse(Buffer.from(result.result).toString());
return detailUser;
};
const getNearBalance = async (accountId, privateKey) => {
const account = await getAccount(accountId, privateKey);
const Nearbalance = await account.getAccountBalance();
return new BigNumber(Nearbalance.total).dividedBy(1e24);
};
const processAccount = async (accountId, privateKey, delayInHours) => {
while (true) {
try {
const mineAndUpdate = async () => {
const NearBalanceUser = await getNearBalance(accountId, privateKey);
twisters.put(accountId, {
text: `
Account ID : ${accountId}
Near Balance :${NearBalanceUser}
Status : Claiming...
`,
});
let transactionHash = null;
while (transactionHash == null) {
try {
const account = await getAccount(accountId, privateKey);
const callContract = await account.functionCall({
contractId: "game.hot.tg",
methodName: "claim",
args: {},
});
transactionHash = callContract.transaction.hash;
twisters.put(accountId, {
text: `
Account ID : ${accountId}
Near Balance :${NearBalanceUser}
Status : Claimed ${callContract.transaction.hash}...
`,
});
await new Promise((resolve) => setTimeout(resolve, 5000));
twisters.put(accountId, {
active: false,
removed: true,
text: `
Account ID : ${accountId}
Near Balance :${NearBalanceUser}
Status : Claimed ${callContract.transaction.hash}...
`,
});
} catch (contractError) {
twisters.put(accountId, {
text: `
Account ID : ${accountId}
Near Balance :${NearBalanceUser}
Status : ${contractError}...
`,
});
await new Promise((resolve) => setTimeout(resolve, 5000));
}
}
twisters.put(accountId, {
text: `
Account ID : ${accountId}
Near Balance :${NearBalanceUser}
Status : Mining for ${delayInHours} Hours 5 Minutes...
`,
});
await new Promise((resolve) =>
setTimeout(resolve, delayInHours * 3600 * 1000 + 5 * 60 * 1000)
);
};
await mineAndUpdate();
} catch (error) {
twisters.put(accountId, {
text: `
Account ID : ${accountId}
Status : ${error.message} - ${error.cause ?? ""}...
`,
});
await new Promise((resolve) => setTimeout(resolve, 5000));
}
}
};
(async () => {
const allPromise = [];
const promises = acc.map(async (account) => {
const [accountId, privateKey, delayInHours] = account.split("|");
processAccount(accountId, privateKey, delayInHours);
});
for (const processAccount of promises) {
allPromise.push(await processAccount);
}
})();