registry-sdk/src/config.test.ts

104 lines
3.7 KiB
TypeScript
Raw Normal View History

import { GasPrice } from '@cosmjs/stargate';
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 configTests = () => {
let registry: Registry;
let testAccount: Account;
beforeAll(async () => {
registry = new Registry(gqlEndpoint, rpcEndpoint, { chainId });
// Fund a new account for the test
[testAccount] = await createTestAccounts(1);
await registry.sendCoins({ denom: DENOM, amount: '10000000', destinationAddress: testAccount.address }, privateKey, fee);
});
test('StdFee fees with gas price not set', async () => {
const testFees = {
amount: [{ denom: 'alnt', amount: '400000' }],
gas: '400000'
};
// Send a bond creation tx
await registry.createBond({ denom: DENOM, amount: '100000' }, testAccount.getPrivateKey(), testFees);
// Check that bond gets created
const [result] = await registry.queryBondsByOwners([testAccount.address]);
expect(result.bonds).toHaveLength(1);
});
test('StdFee fees with gas price set', async () => {
const testFees = {
amount: [{ denom: 'alnt', amount: '400000' }],
gas: '400000'
};
// Set gas price lower than min gas price
const testGasPrice = GasPrice.fromString(String('0.00001alnt'));
const registry = new Registry(gqlEndpoint, rpcEndpoint, { chainId, gasPrice: testGasPrice });
// Send a bond creation tx
await registry.createBond({ denom: DENOM, amount: '100000' }, testAccount.getPrivateKey(), testFees);
// Check that bond gets created (gas price ignored)
const [result] = await registry.queryBondsByOwners([testAccount.address]);
expect(result.bonds).toHaveLength(2);
});
test('Gas price with fees not set (default gas estimation multiplier)', async () => {
// Set gas price
const testGasPrice = GasPrice.fromString('1alnt');
const registry = new Registry(gqlEndpoint, rpcEndpoint, { chainId, gasPrice: testGasPrice });
// Send a bond creation tx
await registry.createBond({ denom: DENOM, amount: '100000' }, testAccount.getPrivateKey());
// Check that bond gets created (gas price ignored)
const [result] = await registry.queryBondsByOwners([testAccount.address]);
expect(result.bonds).toHaveLength(3);
});
test('Gas price with fees set (fees as the gas estimation multiplier)', async () => {
const testFees = 2.1;
// Set gas price
const testGasPrice = GasPrice.fromString('1alnt');
const registry = new Registry(gqlEndpoint, rpcEndpoint, { chainId, gasPrice: testGasPrice });
// Send a bond creation tx
await registry.createBond({ denom: DENOM, amount: '100000' }, testAccount.getPrivateKey(), testFees);
// Check that bond gets created (gas price ignored)
const [result] = await registry.queryBondsByOwners([testAccount.address]);
expect(result.bonds).toHaveLength(4);
});
test('Error on fees and gas price both not set', async () => {
const errorMsg = 'Gas price must be set in the client options when auto gas is used';
// Create registry without gasPrice
const registry = new Registry(gqlEndpoint, rpcEndpoint, { chainId });
// Send a bond creation tx
try {
await registry.createBond({ denom: DENOM, amount: '100000' }, testAccount.getPrivateKey());
} catch (error: any) {
expect(error.toString()).toContain(errorMsg);
}
// Check that bond doesn't get created
const [result] = await registry.queryBondsByOwners([testAccount.address]);
expect(result.bonds).toHaveLength(4);
});
};
describe('Config', configTests);