Refactor Batch txs test

This commit is contained in:
IshaVenikar 2024-08-22 12:31:04 +05:30 committed by Prathamesh Musale
parent b7795b8327
commit d979b2c1c0
2 changed files with 34 additions and 29 deletions

View File

@ -1,7 +1,7 @@
import { Account } from './account'; import { Account } from './account';
import { DENOM } from './constants'; import { DENOM } from './constants';
import { Registry } from './index'; import { Registry } from './index';
import { getConfig } from './testing/helper'; import { createTestAccounts, getConfig } from './testing/helper';
const { chainId, rpcEndpoint, gqlEndpoint, privateKey, fee } = getConfig(); const { chainId, rpcEndpoint, gqlEndpoint, privateKey, fee } = getConfig();
@ -44,39 +44,31 @@ const registryTests = () => {
expect(quantity).toBe('10000'); expect(quantity).toBe('10000');
}); });
test('Send batch txs.', async () => { describe('Batch txs', () => {
const accounts = await createAccounts(10); let accounts: Account[];
beforeAll(async () => {
accounts = await createTestAccounts(10);
for (let i = 0; i < 10; i++) { for (let i = 0; i < 10; i++) {
const amount = (10 ** (15 - i)).toString(); const amount = (10 ** (15 - i)).toString();
const fromAccount = i === 0 ? privateKey : accounts[i - 1].getPrivateKey(); const fromAccount = i === 0 ? privateKey : accounts[i - 1].getPrivateKey();
await registry.sendCoins({ denom: DENOM, amount, destinationAddress: accounts[i].address }, fromAccount, fee); await registry.sendCoins({ denom: DENOM, amount, destinationAddress: accounts[i].address }, fromAccount, fee);
} }
});
test('All txs get included in a single block.', async () => {
await Promise.all(accounts.map((account) => await Promise.all(accounts.map((account) =>
registry.createBond({ denom: DENOM, amount: '100000' }, account.getPrivateKey(), fee) registry.createBond({ denom: DENOM, amount: '100000' }, account.getPrivateKey(), fee)
)); ));
const laconicClient = await registry.getLaconicClient(accounts[0]); const laconicClient = await registry.getLaconicClient(accounts[0]);
const bondTx = await laconicClient.searchTx('message.action="/cerc.bond.v1.MsgCreateBond"'); const bondTx = await laconicClient.searchTx("message.action='/cerc.bond.v1.MsgCreateBond'");
const expectedBlockHeight = bondTx[0].height; const expectedBlockHeight = bondTx[0].height;
expect(bondTx.every(tx => tx.height === expectedBlockHeight)).toBe(true); expect(bondTx.every(tx => tx.height === expectedBlockHeight)).toBe(true);
}); });
});
const createAccounts = async (numAccounts: number): Promise<Account[]> => {
const accounts: Account[] = [];
for (let i = 0; i < numAccounts; i++) {
const mnemonic = Account.generateMnemonic();
const account = await Account.generateFromMnemonic(mnemonic);
await account.init();
accounts.push(account);
}
return accounts;
};
}; };
describe('Registry', registryTests); describe('Registry', registryTests);

View File

@ -2,7 +2,7 @@ import assert from 'assert';
import yaml from 'node-yaml'; import yaml from 'node-yaml';
import semver from 'semver'; import semver from 'semver';
import { DEFAULT_CHAIN_ID } from '../index'; import { Account, DEFAULT_CHAIN_ID } from '../index';
export const ensureUpdatedConfig = async (path: string) => { export const ensureUpdatedConfig = async (path: string) => {
const conf = await yaml.read(path); const conf = await yaml.read(path);
@ -33,3 +33,16 @@ export const getConfig = () => {
} }
}; };
}; };
export const createTestAccounts = async (numAccounts: number): Promise<Account[]> => {
const accounts: Account[] = [];
for (let i = 0; i < numAccounts; i++) {
const mnemonic = Account.generateMnemonic();
const account = await Account.generateFromMnemonic(mnemonic);
await account.init();
accounts.push(account);
}
return accounts;
};