registry-sdk/src/index.test.ts

83 lines
3.2 KiB
TypeScript
Raw Normal View History

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 () => {
// Fund 5 new accounts for the test
accounts = await createTestAccounts(5);
for (let i = 0; i < accounts.length; i++) {
await registry.sendCoins({ denom: DENOM, amount: '1000000', destinationAddress: accounts[i].address }, privateKey, fee);
}
});
test('Multiple txs get included in a block.', async () => {
// Send a bond creation tx from each account (send from different accounts to avoid sequence errors)
await Promise.all(accounts.map((account) =>
registry.createBond({ denom: DENOM, amount: '100000' }, account.getPrivateKey(), fee)
));
const laconicClient = await registry.getLaconicClient(accounts[0]);
const bondCreationTxHeights = await Promise.all(accounts.map(async (account) => {
// Get the bond creation tx for each account
const [tx] = await laconicClient.searchTx(`message.sender='${account.address}' AND message.action='/cerc.bond.v1.MsgCreateBond'`);
return tx.height;
}));
bondCreationTxHeights.forEach((txHeight, i) => {
console.log('tx', accounts[i].address, txHeight);
});
// Check that all txs are within two blocks
const expectedBlockHeight = bondCreationTxHeights.sort()[0];
expect(bondCreationTxHeights.every(txHeight => txHeight === expectedBlockHeight || txHeight === expectedBlockHeight + 1)).toBe(true);
});
});
};
describe('Registry', registryTests);