1234import * as path from 'path';5import {promises as fs} from 'fs';6import {usingPlaygrounds} from '.';78async function getFiles(rootPath: string): Promise<string[]> {9 const files = await fs.readdir(rootPath, {withFileTypes: true});10 const filenames: string[] = [];11 for (const entry of files) {12 const res = path.resolve(rootPath, entry.name);13 if (entry.isDirectory()) {14 filenames.push(...await getFiles(res));15 } else {16 filenames.push(res);17 }18 }19 return filenames;20}2122const fundFilenames = async () => {23 await usingPlaygrounds(async (helper, privateKey) => {24 const oneToken = helper.balance.getOneTokenNominal();25 const alice = await privateKey('//Alice');26 const nonce = await helper.chain.getNonce(alice.address);27 const filenames = await getFiles(path.resolve(__dirname, '../..'));2829 console.log(`Main Alice address: ${alice.address}, with balance:`, await helper.balance.getSubstrate(alice.address));3031 const batchSize = 20;32 let balanceGrantedCounter = 0;33 for (let b = 0; b < filenames.length; b += batchSize) {34 const tx = [];35 let batchBalanceGrantedCounter = 0;36 for (const f of filenames.slice(b, b + batchSize)) {37 if (!f.endsWith('.test.ts')) continue;38 const account = await privateKey({filename: f});39 const aliceBalance = await helper.balance.getSubstrate(account.address);4041 if (aliceBalance < 5000n * oneToken) {42 tx.push(helper.executeExtrinsic(43 alice, 44 'api.tx.balances.transfer',45 [account.address, 10000n * oneToken],46 true,47 {nonce: nonce + balanceGrantedCounter++},48 ).then(() => true).catch(() => {console.error(`Transaction to ${path.basename(f)} registered as failed. Strange.`); return false;}));49 batchBalanceGrantedCounter++;50 }51 }5253 if(tx.length > 0) {54 console.log(`Granting funds to ${batchBalanceGrantedCounter} filename accounts.`);55 const result = await Promise.all(tx);56 if (result && result.lastIndexOf(false) > -1) throw new Error('The transactions actually probably succeeded, should check the balances.');57 }58 }5960 if (balanceGrantedCounter == 0) console.log('No account needs additional funding.');61 });62};6364const fundFilenamesWithRetries = async (retriesLeft: number): Promise<boolean> => {65 if (retriesLeft <= 0) return Promise.resolve(false);66 return fundFilenames()67 .then(() => Promise.resolve(true))68 .catch(() => {69 console.error(`Some transactions have failed. ${retriesLeft >= 1 ? 'Retrying...' : 'Something is wrong.'}`);70 return fundFilenamesWithRetries(--retriesLeft);71 });72};7374fundFilenamesWithRetries(2).then((result) => process.exit(result ? 0 : 1)).catch(e => {75 console.error(e);76 process.exit(1);77});