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-03-11 11:27:27 +00:00
|
|
|
import { 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-04-01 14:23:34 +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-08-22 06:10:44 +00:00
|
|
|
|
|
|
|
test('Get transaction info.', async () => {
|
|
|
|
const bondAmount = '100000';
|
|
|
|
|
|
|
|
const accounts = await createAccounts(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);
|
|
|
|
}
|
|
|
|
const bondPromises = accounts.map((account) =>
|
|
|
|
registry.createBond({ denom: DENOM, amount: bondAmount }, account.getPrivateKey(), fee)
|
|
|
|
);
|
|
|
|
|
|
|
|
await Promise.all(bondPromises);
|
|
|
|
});
|
|
|
|
|
|
|
|
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;
|
|
|
|
};
|
2024-03-07 04:17:05 +00:00
|
|
|
};
|
2022-04-22 10:15:47 +00:00
|
|
|
|
|
|
|
describe('Registry', registryTests);
|