registry-sdk/src/index.test.ts
Prathamesh Musale 6a17b1b175 Add support for using auto fee calculation (#22)
Part of [Create a public laconicd testnet](https://www.notion.so/Create-a-public-laconicd-testnet-896a11bdd8094eff8f1b49c0be0ca3b8)

- Add an option for setting gas price
- Use fees as gas estimation multiplier when `gasPrice` is provided (following `cosmjs` pattern)
  - Default gas estimation multiplier: `2`
- Add a method to query bonds by owners and fix tests
- Add a test for inclusion of batch of txs sent at once in the same block

Co-authored-by: IshaVenikar <ishavenikar7@gmail.com>
Co-authored-by: Nabarun <nabarun@deepstacksoft.com>
Reviewed-on: cerc-io/registry-sdk#22
Co-authored-by: Prathamesh Musale <prathamesh.musale0@gmail.com>
Co-committed-by: Prathamesh Musale <prathamesh.musale0@gmail.com>
2024-09-06 09:07:57 +00:00

83 lines
3.2 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 () => {
// 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);