registry-sdk/src/index.test.ts
2024-09-05 19:20:33 +05:30

75 lines
2.7 KiB
TypeScript

import { Account } from './account';
import { DENOM } from './constants';
import { Registry } from './index';
import { createTestAccounts, getConfig } from './testing/helper';
const { chainId, rpcEndpoint, gqlEndpoint, privateKey, fee } = getConfig();
jest.setTimeout(90 * 1000);
const registryTests = () => {
let registry: Registry;
beforeAll(async () => {
registry = new Registry(gqlEndpoint, rpcEndpoint, chainId);
});
test('Get account info.', async () => {
const account = new Account(Buffer.from(privateKey, 'hex'));
await account.init();
const accounts = await registry.getAccounts([account.address]);
expect(accounts).toHaveLength(1);
const [accountObj] = accounts;
expect(accountObj.address).toBe(account.address);
expect(accountObj.pubKey).toBe(account.encodedPubkey);
expect(accountObj.number).toBe('0');
expect(accountObj.sequence).toBeDefined();
expect(accountObj.balance).toHaveLength(1);
const [{ type, quantity }] = accountObj.balance;
expect(type).toBe(DENOM);
expect(quantity).toBeDefined();
});
test('Get account balance.', async () => {
const mnenonic1 = Account.generateMnemonic();
const otherAccount = await Account.generateFromMnemonic(mnenonic1);
await otherAccount.init();
await registry.sendCoins({ denom: DENOM, amount: '10000', destinationAddress: otherAccount.address }, privateKey, fee);
const [accountObj] = await registry.getAccounts([otherAccount.address]);
expect(accountObj).toBeDefined();
expect(accountObj.address).toBe(otherAccount.address);
const [{ type, quantity }] = accountObj.balance;
expect(type).toBe(DENOM);
expect(quantity).toBe('10000');
});
describe('Batch txs', () => {
let accounts: Account[];
beforeAll(async () => {
accounts = await createTestAccounts(10);
for (let i = 0; i < 10; i++) {
const amount = (10 ** (15 - i)).toString();
const fromAccount = i === 0 ? privateKey : accounts[i - 1].getPrivateKey();
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) =>
registry.createBond({ denom: DENOM, amount: '100000' }, account.getPrivateKey(), fee)
));
const laconicClient = await registry.getLaconicClient(accounts[0]);
const bondTx = await laconicClient.searchTx("message.action='/cerc.bond.v1.MsgCreateBond'");
const expectedBlockHeight = bondTx[0].height;
expect(bondTx.every(tx => tx.height === expectedBlockHeight)).toBe(true);
});
});
};
describe('Registry', registryTests);