generated from ton-community/ton-onboarding-challenge
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
118 lines (97 loc) · 3.66 KB
/
index.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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
import { BN } from 'bn.js';
import { Address, TonClient, toNano } from 'ton';
import { unixNow } from './src/lib/utils';
import { MineMessageParams, Queries } from './src/giver/NftGiver.data';
const qrcode = require('qrcode-terminal');
async function main() {
const wallet = Address.parse(
'EQAfsHnY2nTQDk4WTwXQfbCZ5GoJzD53qkMwKUOugLCgwFMZ',
);
console.log(wallet);
const collection = Address.parse(
'EQDk8N7xM5D669LC2YACrseBJtDyFqwtSPCNhRWXU7kjEptX',
);
const client = new TonClient({
endpoint: 'https://testnet.toncenter.com/api/v2/jsonRPC',
apiKey: 'API_KEY',
});
const miningData = await client.callGetMethod(collection, 'get_mining_data');
console.log(miningData);
const parseStackNum = (sn: any) => new BN(sn[1].substring(2), 'hex');
const complexity = parseStackNum(miningData.stack[0]);
const last_success = parseStackNum(miningData.stack[1]);
const seed = parseStackNum(miningData.stack[2]);
const target_delta = parseStackNum(miningData.stack[3]);
const min_cpl = parseStackNum(miningData.stack[4]);
const max_cpl = parseStackNum(miningData.stack[5]);
console.log('complexity', complexity);
console.log('last_success', last_success.toString());
console.log('seed', seed);
console.log('target_delta', target_delta.toString());
console.log('min_cpl', min_cpl.toString());
console.log('max_cpl', max_cpl.toString());
const mineParams: MineMessageParams = {
expire: unixNow() + 300, // 5 min is enough to make a transaction
mintTo: wallet, // your wallet
data1: new BN(0), // temp variable to increment in the miner
seed, // unique seed from get_mining_data
};
let msg = Queries.mine(mineParams);
let progress = 0;
while (new BN(msg.hash(), 'be').gt(complexity)) {
progress += 1;
console.clear();
console.log(
`Mining started: please, wait for 30-60 seconds to mine your NFT!`,
);
console.log(' ');
console.log(
`⛏ Mined ${progress} hashes! Last: `,
new BN(msg.hash(), 'be').toString(),
);
mineParams.expire = unixNow() + 300;
mineParams.data1.iaddn(1);
msg = Queries.mine(mineParams);
}
console.log(' ');
console.log('💎 Mission completed: msg_hash less than pow_complexity found!');
console.log(' ');
console.log('msg_hash: ', new BN(msg.hash(), 'be').toString());
console.log('pow_complexity: ', complexity.toString());
console.log(
'msg_hash < pow_complexity: ',
new BN(msg.hash(), 'be').lt(complexity),
);
console.log(' ');
console.log(
'💣 WARNING! As soon as you find the hash, you should quickly make a transaction.',
);
console.log(
'If someone else makes a transaction, the seed changes, and you have to find a hash again!',
);
console.log(' ');
// flags work only in user-friendly address form
const collectionAddr = collection.toFriendly({
urlSafe: true,
bounceable: true,
});
// we must convert TON to nanoTON
const amountToSend = toNano('0.05').toString();
// BOC means Bag Of Cells here
const preparedBodyCell = msg.toBoc().toString('base64url');
// final method to build a payment url
const tonDeepLink = (address: string, amount: string, body: string) => {
return `ton://transfer/${address}?amount=${amount}&bin=${body}`;
};
const link = tonDeepLink(collectionAddr, amountToSend, preparedBodyCell);
console.log('🚀 Link to receive an NFT:');
console.log(link);
qrcode.generate(link, { small: true }, function (qrcode: any) {
console.log('🚀 Link to mine your NFT (use Tonkeeper in testnet mode):');
console.log(qrcode);
console.log(
'* If QR is still too big, please run script from the terminal. (or make the font smaller)',
);
});
}
main();