Skip to content

Commit

Permalink
refactor simulate; remove unused comment
Browse files Browse the repository at this point in the history
  • Loading branch information
nil-amrutlal committed Jan 24, 2024
1 parent 123853e commit 257eb17
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -161,25 +161,23 @@ export async function simulateCoin({
counter = 0;
}

const balance = await getAccountBalance({
account: account.account,
chainId: account.chainId || defaultChain,
networkHost: network.host,
});

// using a random number safety gap to avoid underflowing the account
const amountWithSafetyGap = amount + getRandomNumber(seededRandomNo, 1);
if (amountWithSafetyGap > parseFloat(balance)) {
console.warn(
`Insufficient funds for ${account.account}\nFunds necessary: ${amountWithSafetyGap}\nFunds available: ${balance}`,
);
console.log('Skipping transfer');
// If not enough balance, continue
if (
!(await validateBalance(
account,
amount,
network.host,
defaultChain,
seededRandomNo,
))
) {
continue;
}

// Generate seeded random number based on the previous number
seededRandomNo = seedRandom(`${seededRandomNo}`);

// Randomly choose next account
let nextAccount =
accounts[getRandomNumber(seededRandomNo, accounts.length)];

Expand All @@ -196,6 +194,7 @@ export async function simulateCoin({
transferType === 'cross-chain-transfer' &&
simulationDefaults.CHAIN_COUNT > 1
) {
// Make sure the chain id is different
if (account.chainId === nextAccount.chainId) {
nextAccount = {
...nextAccount,
Expand Down Expand Up @@ -283,3 +282,29 @@ export async function simulateCoin({
throw error;
}
}

async function validateBalance(
account: IAccount,
amount: number,
networkHost: string,
defaultChain: ChainId,
seededRandomNo: number,
): Promise<boolean> {
const balance = await getAccountBalance({
account: account.account,
chainId: account.chainId || defaultChain,
networkHost: networkHost,
});
// using a random number safety gap to avoid underflowing the account
const amountWithSafetyGap = amount + getRandomNumber(seededRandomNo, 1);

if (amountWithSafetyGap > parseFloat(balance)) {
console.warn(
`Insufficient funds for ${account.account}\nFunds necessary: ${amountWithSafetyGap}\nFunds available: ${balance}`,
);
console.log('Skipping transfer');
return false;
}

return true;
}
Original file line number Diff line number Diff line change
Expand Up @@ -53,5 +53,4 @@ export async function appendToLogFile(
): Promise<void> {
const dataString = Object.values(data).join(',');
await services.filesystem.appendFile(filepath, `${dataString}\n`);
// fs.appendFileSync(filepath, `${dataString}\n`);
}

0 comments on commit 257eb17

Please sign in to comment.