2022-04-22 10:15:47 +00:00
|
|
|
import { Account } from './account';
|
2024-03-07 13:15:50 +00:00
|
|
|
import { DENOM } from './constants';
|
2022-04-22 10:15:47 +00:00
|
|
|
import { Registry } from './index';
|
2024-09-06 09:07:57 +00:00
|
|
|
import { createTestAccounts, getConfig } from './testing/helper';
|
2022-04-22 10:15:47 +00:00
|
|
|
|
2024-04-01 14:23:34 +00:00
|
|
|
const { chainId, rpcEndpoint, gqlEndpoint, privateKey, fee } = getConfig();
|
2022-04-22 10:15:47 +00:00
|
|
|
|
|
|
|
jest.setTimeout(90 * 1000);
|
|
|
|
|
|
|
|
const registryTests = () => {
|
|
|
|
let registry: Registry;
|
|
|
|
|
|
|
|
beforeAll(async () => {
|
2024-09-06 09:07:57 +00:00
|
|
|
registry = new Registry(gqlEndpoint, rpcEndpoint, { chainId });
|
2022-04-22 10:15:47 +00:00
|
|
|
});
|
|
|
|
|
2024-03-07 04:17:05 +00:00
|
|
|
test('Get account info.', async () => {
|
2022-04-22 10:15:47 +00:00
|
|
|
const account = new Account(Buffer.from(privateKey, 'hex'));
|
2024-03-07 13:15:50 +00:00
|
|
|
await account.init();
|
|
|
|
const accounts = await registry.getAccounts([account.address]);
|
2024-03-07 04:17:05 +00:00
|
|
|
expect(accounts).toHaveLength(1);
|
2022-04-22 10:15:47 +00:00
|
|
|
const [accountObj] = accounts;
|
2024-03-07 13:15:50 +00:00
|
|
|
expect(accountObj.address).toBe(account.address);
|
2022-04-22 10:15:47 +00:00
|
|
|
expect(accountObj.pubKey).toBe(account.encodedPubkey);
|
|
|
|
expect(accountObj.number).toBe('0');
|
|
|
|
expect(accountObj.sequence).toBeDefined();
|
|
|
|
expect(accountObj.balance).toHaveLength(1);
|
2024-03-07 04:17:05 +00:00
|
|
|
const [{ type, quantity }] = accountObj.balance;
|
2024-03-07 13:15:50 +00:00
|
|
|
expect(type).toBe(DENOM);
|
2022-04-22 10:15:47 +00:00
|
|
|
expect(quantity).toBeDefined();
|
2024-03-07 04:17:05 +00:00
|
|
|
});
|
2022-04-22 10:15:47 +00:00
|
|
|
|
2024-03-07 04:17:05 +00:00
|
|
|
test('Get account balance.', async () => {
|
2022-04-22 10:15:47 +00:00
|
|
|
const mnenonic1 = Account.generateMnemonic();
|
|
|
|
const otherAccount = await Account.generateFromMnemonic(mnenonic1);
|
2024-03-07 13:15:50 +00:00
|
|
|
await otherAccount.init();
|
|
|
|
await registry.sendCoins({ denom: DENOM, amount: '10000', destinationAddress: otherAccount.address }, privateKey, fee);
|
2022-04-22 10:15:47 +00:00
|
|
|
|
2024-03-07 13:15:50 +00:00
|
|
|
const [accountObj] = await registry.getAccounts([otherAccount.address]);
|
2022-04-22 10:15:47 +00:00
|
|
|
expect(accountObj).toBeDefined();
|
2024-03-07 13:15:50 +00:00
|
|
|
expect(accountObj.address).toBe(otherAccount.address);
|
2024-03-07 04:17:05 +00:00
|
|
|
const [{ type, quantity }] = accountObj.balance;
|
2024-03-07 13:15:50 +00:00
|
|
|
expect(type).toBe(DENOM);
|
|
|
|
expect(quantity).toBe('10000');
|
2024-03-07 04:17:05 +00:00
|
|
|
});
|
2024-09-06 09:07:57 +00:00
|
|
|
|
|
|
|
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);
|
|
|
|
});
|
|
|
|
});
|
2024-03-07 04:17:05 +00:00
|
|
|
};
|
2022-04-22 10:15:47 +00:00
|
|
|
|
|
|
|
describe('Registry', registryTests);
|