123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437 |
- import { Address, BitReader, BitString, Cell, TupleReader, beginCell, external, internal, parseTuple, storeMessage, toNano } from '@ton/core'
- import { KeyPair, getSecureRandomBytes, keyPairFromSeed, mnemonicToWalletKey } from '@ton/crypto'
- import axios from 'axios'
- // import { LiteClient, LiteRoundRobinEngine, LiteSingleEngine } from 'ton-lite-client'
- import { TonClient4 } from '@ton/ton';
- import { execSync } from 'child_process';
- import fs from 'fs'
- import { WalletContractV4 } from '@ton/ton';
- import dotenv from 'dotenv'
- import { GiversThings100, GiversThings1000, GiversThings10000, GiversThings200, GiversThings5000 } from './givers_things'
- import arg from 'arg'
- import { LiteClient, LiteSingleEngine, LiteRoundRobinEngine } from 'ton-lite-client';
- import { getLiteClient, getTon4Client, getTonCenterClient, getTonapiClient } from './client';
- import { HighloadWalletV2 } from '@scaleton/highload-wallet';
- import { OpenedContract } from '@ton/core';
- import { Api } from 'tonapi-sdk-js';
- dotenv.config({ path: 'config.txt.txt' })
- dotenv.config({ path: '.env.txt' })
- dotenv.config()
- dotenv.config({ path: 'config.txt' })
- type ApiObj = LiteClient | TonClient4 | Api<unknown>
- const args = arg({
- '--givers': Number, // 100 1000 10000
- '--api': String, // lite, tonhub, tonapi
- '--bin': String, // cuda, opencl or path to miner
- '--gpu': Number, // gpu id, default 0
- '--timeout': Number, // Timeout for mining in seconds
- '--allow-shards': Boolean, // if true - allows mining to other shards
- '-c': String, // blockchain config
- })
- let givers = GiversThings5000
- if (args['--givers']) {
- const val = args['--givers']
- const allowed = [100, 200, 1000, 5000, 10000]
- if (!allowed.includes(val)) {
- throw new Error('Invalid --givers argument')
- }
- switch (val) {
- case 100:
- givers = GiversThings100
- console.log('Using givers 100')
- break
- case 200:
- givers = GiversThings200
- console.log('Using givers 2000')
- break
- case 1000:
- givers = GiversThings1000
- console.log('Using givers 1 000')
- break
- case 5000:
- givers = GiversThings5000
- console.log('Using givers 5 000')
- break
- case 10000:
- givers = GiversThings10000
- console.log('Using givers 10 000')
- break
- }
- } else {
- console.log('Using givers 5 000')
- }
- let bin = '.\\pow-miner-cuda.exe'
- if (args['--bin']) {
- const argBin = args['--bin']
- if (argBin === 'cuda') {
- bin = '.\\pow-miner-cuda.exe'
- } else if (argBin === 'opencl' || argBin === 'amd') {
- bin = '.\\pow-miner-opencl.exe'
- } else {
- bin = argBin
- }
- }
- console.log('Using bin', bin)
- const gpu = args['--gpu'] ?? 0
- const timeout = args['--timeout'] ?? 5
- const allowShards = args['--allow-shards'] ?? false
- console.log('Using GPU', gpu)
- console.log('Using timeout', timeout)
- const mySeed = process.env.SEED as string
- const totalDiff = BigInt('115792089237277217110272752943501742914102634520085823245724998868298727686144')
- const envAddress = process.env.TARGET_ADDRESS
- let TARGET_ADDRESS: string | undefined = undefined
- if (envAddress) {
- try {
- TARGET_ADDRESS = Address.parse(envAddress).toString({ urlSafe: true, bounceable: false })
- }
- catch (e) {
- console.log('Couldnt parse target address')
- process.exit(1)
- }
- }
- let bestGiver: { address: string, coins: number } = { address: '', coins: 0 }
- async function updateBestGivers(liteClient: ApiObj, myAddress: Address) {
- const giver = givers[Math.floor(Math.random() * givers.length)]
- bestGiver = {
- address: giver.address,
- coins: giver.reward,
- }
- }
- async function getPowInfo(liteClient: ApiObj, address: Address): Promise<[bigint, bigint, bigint]> {
- if (liteClient instanceof TonClient4) {
- const lastInfo = await CallForSuccess(() => liteClient.getLastBlock())
- const powInfo = await CallForSuccess(() => liteClient.runMethod(lastInfo.last.seqno, address, 'get_pow_params', []))
- // console.log('pow info', powInfo, powInfo.result)
- const reader = new TupleReader(powInfo.result)
- const seed = reader.readBigNumber()
- const complexity = reader.readBigNumber()
- const iterations = reader.readBigNumber()
- return [seed, complexity, iterations]
- } else if (liteClient instanceof LiteClient) {
- const lastInfo = await liteClient.getMasterchainInfo()
- const powInfo = await liteClient.runMethod(address, 'get_pow_params', Buffer.from([]), lastInfo.last)
- const powStack = Cell.fromBase64(powInfo.result as string)
- const stack = parseTuple(powStack)
- const reader = new TupleReader(stack)
- const seed = reader.readBigNumber()
- const complexity = reader.readBigNumber()
- const iterations = reader.readBigNumber()
- return [seed, complexity, iterations]
- } else if (liteClient instanceof Api) {
- try {
- const powInfo = await CallForSuccess(
- () => liteClient.blockchain.execGetMethodForBlockchainAccount(address.toRawString(), 'get_pow_params', {}),
- 50,
- 300)
- const seed = BigInt(powInfo.stack[0].num as string)
- const complexity = BigInt(powInfo.stack[1].num as string)
- const iterations = BigInt(powInfo.stack[2].num as string)
- return [seed, complexity, iterations]
- } catch (e) {
- console.log('ls error', e)
- }
- }
- throw new Error('invalid client')
- }
- let go = true
- let i = 0
- let success = 0
- let lastMinedSeed: bigint = BigInt(0)
- let start = Date.now()
- async function main() {
- const minerOk = await testMiner()
- if (!minerOk) {
- console.log('Your miner is not working')
- console.log('Check if you use correct bin (cuda, amd).')
- console.log('If it doesn\'t help, try to run test_cuda or test_opencl script, to find out issue')
- process.exit(1)
- }
- let liteClient: ApiObj
- if (!args['--api']) {
- console.log('Using TonHub API')
- liteClient = await getTon4Client()
- } else {
- if (args['--api'] === 'lite') {
- console.log('Using LiteServer API')
- liteClient = await getLiteClient(args['-c'] ?? 'https://ton-blockchain.github.io/global.config.json')
- } else if (args['--api'] === 'tonapi') {
- console.log('Using TonApi')
- liteClient = await getTonapiClient()
- } else {
- console.log('Using TonHub API')
- liteClient = await getTon4Client()
- }
- }
- const keyPair = await mnemonicToWalletKey(mySeed.split(' '))
- const wallet = WalletContractV4.create({
- workchain: 0,
- publicKey: keyPair.publicKey
- })
- if (args['--wallet'] === 'highload') {
- console.log('Using highload wallet', wallet.address.toString({ bounceable: false, urlSafe: true }))
- } else {
- console.log('Using v4r2 wallet', wallet.address.toString({ bounceable: false, urlSafe: true }))
- }
- const targetAddress = TARGET_ADDRESS ?? wallet.address.toString({ bounceable: false, urlSafe: true })
- console.log('Target address:', targetAddress)
- console.log('Date, time, status, seed, attempts, successes, timespent')
- try {
- await updateBestGivers(liteClient, wallet.address)
- } catch (e) {
- console.log('error', e)
- throw Error('no givers')
- }
- setInterval(() => {
- updateBestGivers(liteClient, wallet.address)
- }, 5000)
- while (go) {
- const giverAddress = bestGiver.address
- const [seed, complexity, iterations] = await getPowInfo(liteClient, Address.parse(giverAddress))
- if (seed === lastMinedSeed) {
- // console.log('Wating for a new seed')
- updateBestGivers(liteClient, wallet.address)
- await delay(200)
- continue
- }
- const randomName = (await getSecureRandomBytes(8)).toString('hex') + '.boc'
- const path = `bocs/${randomName}`
- const command = `${bin} -g ${gpu} -F 128 -t ${timeout} ${targetAddress} ${seed} ${complexity} ${iterations} ${giverAddress} ${path}`
- try {
- const output = execSync(command, { encoding: 'utf-8', stdio: "pipe" }); // the default is 'buffer'
- } catch (e) {
- }
- let mined: Buffer | undefined = undefined
- try {
- mined = fs.readFileSync(path)
- lastMinedSeed = seed
- fs.rmSync(path)
- } catch (e) {
- //
- }
- if (!mined) {
- console.log(`${formatTime()}: not mined`, seed.toString(16).slice(0, 4), i++, success, Math.floor((Date.now() - start) / 1000))
- }
- if (mined) {
- const [newSeed] = await getPowInfo(liteClient, Address.parse(giverAddress))
- if (newSeed !== seed) {
- console.log('Mined already too late seed')
- continue
- }
- console.log(`${formatTime()}: mined`, seed.toString(16).slice(0, 4), i++, ++success, Math.floor((Date.now() - start) / 1000))
- let seqno = 0
- if (liteClient instanceof LiteClient || liteClient instanceof TonClient4) {
- let w = liteClient.open(wallet)
- try {
- seqno = await CallForSuccess(() => w.getSeqno())
- } catch (e) {
- //
- }
- } else {
- const res = await CallForSuccess(
- () => (liteClient as Api<unknown>).blockchain.execGetMethodForBlockchainAccount(wallet.address.toRawString(), "seqno", {}),
- 50,
- 250
- )
- if (res.success) {
- seqno = Number(BigInt(res.stack[0].num as string))
- }
- }
- await sendMinedBoc(wallet, seqno, keyPair, giverAddress, Cell.fromBoc(mined as Buffer)[0].asSlice().loadRef())
- }
- }
- }
- main()
- async function sendMinedBoc(
- wallet: WalletContractV4,
- seqno: number,
- keyPair: KeyPair,
- giverAddress: string,
- boc: Cell
- ) {
- if (args['--api'] === 'tonapi') {
- const tonapiClient = await getTonapiClient()
- const transfer = wallet.createTransfer({
- seqno,
- secretKey: keyPair.secretKey,
- messages: [internal({
- to: giverAddress,
- value: toNano('0.0678'),
- bounce: true,
- body: boc,
- })],
- sendMode: 3 as any,
- })
- const msg = beginCell().store(storeMessage(external({
- to: wallet.address,
- body: transfer
- }))).endCell()
- let k = 0
- let lastError: unknown
- while (k < 20) {
- try {
- await tonapiClient.blockchain.sendBlockchainMessage({
- boc: msg.toBoc().toString('base64'),
- })
- break
- // return res
- } catch (e: any) {
- // lastError = err
- k++
- if (e.status === 429) {
- await delay(200)
- } else {
- // console.log('tonapi error')
- k = 20
- break
- }
- }
- }
- return
- }
- const wallets: OpenedContract<WalletContractV4>[] = []
- const ton4Client = await getTon4Client()
- const tonOrbsClient = await getTon4Client()
- const w2 = ton4Client.open(wallet)
- const w3 = tonOrbsClient.open(wallet)
- wallets.push(w2)
- wallets.push(w3)
- if (args['--api'] === 'lite') {
- const liteServerClient = await getLiteClient(args['-c'] ?? 'https://ton-blockchain.github.io/global.config.json')
- const w1 = liteServerClient.open(wallet)
- wallets.push(w1)
- }
- for (let i = 0; i < 3; i++) {
- for (const w of wallets) {
- w.sendTransfer({
- seqno,
- secretKey: keyPair.secretKey,
- messages: [internal({
- to: giverAddress,
- value: toNano('0.0678'),
- bounce: true,
- body: boc,
- })],
- sendMode: 3 as any,
- }).catch(e => {
- //
- })
- }
- }
- }
- async function testMiner(): Promise<boolean> {
- const randomName = (await getSecureRandomBytes(8)).toString('hex') + '.boc'
- const path = `bocs/${randomName}`
- const command = `${bin} -g ${gpu} -F 128 -t ${timeout} kQBWkNKqzCAwA9vjMwRmg7aY75Rf8lByPA9zKXoqGkHi8SM7 229760179690128740373110445116482216837 53919893334301279589334030174039261347274288845081144962207220498400000000000 10000000000 kQBWkNKqzCAwA9vjMwRmg7aY75Rf8lByPA9zKXoqGkHi8SM7 ${path}`
- try {
- const output = execSync(command, { encoding: 'utf-8', stdio: "pipe" }); // the default is 'buffer'
- } catch (e) {
- }
- let mined: Buffer | undefined = undefined
- try {
- mined = fs.readFileSync(path)
- fs.rmSync(path)
- } catch (e) {
- //
- }
- if (!mined) {
- return false
- }
- return true
- }
- // Function to call ton api untill we get response.
- // Because testnet is pretty unstable we need to make sure response is final
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
- export async function CallForSuccess<T extends (...args: any[]) => any>(
- toCall: T,
- attempts = 20,
- delayMs = 100
- ): Promise<ReturnType<T>> {
- if (typeof toCall !== 'function') {
- throw new Error('unknown input')
- }
- let i = 0
- let lastError: unknown
- while (i < attempts) {
- try {
- const res = await toCall()
- return res
- } catch (err) {
- lastError = err
- i++
- await delay(delayMs)
- }
- }
- console.log('error after attempts', i)
- throw lastError
- }
- export function delay(ms: number) {
- return new Promise((resolve) => {
- setTimeout(resolve, ms)
- })
- }
- function formatTime() {
- return new Date().toLocaleTimeString('en-US', {
- hour12: false,
- hour: "numeric",
- minute: "numeric",
- day: "numeric",
- month: "numeric",
- year: "numeric",
- second: "numeric"
- });
- }
|