git.delta.rocks / unique-network / refs/commits / 3833b2da5ea7

difftreelog

source

tests/src/util/playgrounds/feedAlices.ts3.0 KiBsourcehistory
1// Copyright 2019-2022 Unique Network (Gibraltar) Ltd.2// SPDX-License-Identifier: Apache-2.034import * 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    // batching is actually undesired, it takes away the time while all the transactions actually succeed30    const batchSize = 300;31    let balanceGrantedCounter = 0;32    for (let b = 0; b < filenames.length; b += batchSize) {33      const tx = [];34      let batchBalanceGrantedCounter = 0;35      for (let i = 0; batchBalanceGrantedCounter < batchSize && b + i < filenames.length; i++) {36        const f = filenames[b + i];37        if (!f.endsWith('.test.ts') || f.includes('.outdated')) continue;38        const account = await privateKey({filename: f, ignoreFundsPresence: true});39        const aliceBalance = await helper.balance.getSubstrate(account.address);4041        if (aliceBalance < 100_000n * oneToken) {42          tx.push(helper.executeExtrinsic(43            alice, 44            'api.tx.balances.transfer',45            [account.address, 1_000_000n * 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(e => {69      console.error(e);70      console.error(`Some transactions might have failed. ${retriesLeft > 1 ? 'Retrying...' : 'Something is wrong.'}\n`);71      return fundFilenamesWithRetries(--retriesLeft);72    });73};7475fundFilenamesWithRetries(3).then((result) => process.exit(result ? 0 : 1)).catch(e => {76  console.error(e);77  process.exit(1);78});