-
Notifications
You must be signed in to change notification settings - Fork 5
/
main.ts
96 lines (77 loc) · 2.61 KB
/
main.ts
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
import { TonClient, WalletContractV4, internal } from "ton";
import { Cell, } from "ton-core";
import { mnemonicToPrivateKey } from "ton-crypto";
import { readFileSync } from 'fs';
import { exec } from 'child_process';
import { promisify } from "util";
import dotenv from 'dotenv';
dotenv.config();
const MY_ADDRESS = process.env.MY_ADDRESS!
const MINTER_ADDRESS = process.env.MINTER_ADDRESS!
const MNEMONIC = (process.env.MNEMONIC!).split(' ');
const execAsync = promisify(exec);
const client = new TonClient({
endpoint: 'https://toncenter.com/api/v2/jsonRPC',
apiKey: process.env.TONCENTER_API_KEY
});
async function getParams(address: string) {
const res = await fetch(
`https://tonapi.io/v2/blockchain/accounts/${address}/methods/get_pow_params`,
{
headers: {
accept: "application/json",
Authorization: "Bearer " + process.env.TONCONSOLE_BEARER,
},
}
);
return (await res.json()).stack;
}
function parseParams(params: any[]) {
let paramsString = ''
for (let i = 0; i < params.length - 1; i++) {
if (params[i].type == 'num') {
paramsString += BigInt(params[i].num).toString() + ' '
}
}
return paramsString
}
async function runCommandAndHandleResult(): Promise<void> {
try {
const params = parseParams(await getParams(MINTER_ADDRESS));
const command = `crypto/pow-miner -vv -w30 -t500 ${MY_ADDRESS} ${params} ${MINTER_ADDRESS} mined.boc`;
console.log("[Starting mining]")
const { stdout, stderr } = await execAsync(command, { timeout: 1000 * 1000 }); // 100 seconds timeout
if (stderr && !stderr.includes("bytes of serialized external message into file `mined.boc`")) {
console.log("[Error in command]")
throw new Error(`Error in command execution: ${stderr}`);
}
const buffer = readFileSync('./mined.boc');
const cell = Cell.fromBoc(buffer)[0].asSlice().loadRef()
let keyPair = await mnemonicToPrivateKey(MNEMONIC);
let wallet = WalletContractV4.create({ workchain: 0, publicKey: keyPair.publicKey });
let contract = client.open(wallet);
console.log(contract.address.toString({ urlSafe: true, bounceable: false }))
let seqno: number = await contract.getSeqno();
let transfer = contract.createTransfer({
seqno,
secretKey: keyPair.secretKey,
messages: [internal({
value: '0.05',
to: MINTER_ADDRESS,
body: cell
})]
});
console.log("[Sending transaction]")
contract.send(transfer)
} catch (error) {
console.log(error)
}
}
async function main() {
while (true) {
await runCommandAndHandleResult();
}
}
(async () => {
await main();
})();