diff --git a/src/config.test.ts b/src/config.test.ts new file mode 100644 index 0000000..b18d500 --- /dev/null +++ b/src/config.test.ts @@ -0,0 +1,111 @@ +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 accounts: Account[]; + + beforeAll(async () => { + registry = new Registry(gqlEndpoint, rpcEndpoint, { chainId }); + + // Fund 4 new accounts for the test + accounts = await createTestAccounts(4); + for (let i = 0; i < accounts.length; i++) { + await registry.sendCoins({ denom: DENOM, amount: '1000000', destinationAddress: accounts[i].address }, privateKey, fee); + } + }); + + test('StdFee fees with gas price not set', async () => { + const testAccount = accounts[0]; + 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.queryBondsByOwner([testAccount.address]); + expect(result.bonds).toHaveLength(1); + }); + + test('StdFee fees with gas price set', async () => { + const testAccount = accounts[0]; + 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.queryBondsByOwner([testAccount.address]); + expect(result.bonds).toHaveLength(2); + }); + + test('Gas price with fees not set (default gas estimation multiplier)', async () => { + const testAccount = accounts[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()); + + // Check that bond gets created (gas price ignored) + const [result] = await registry.queryBondsByOwner([testAccount.address]); + expect(result.bonds).toHaveLength(1); + }); + + test('Gas price with fees set (fees as the gas estimation multiplier)', async () => { + const testAccount = accounts[2]; + 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.queryBondsByOwner([testAccount.address]); + expect(result.bonds).toHaveLength(1); + }); + + test('Fees and gas price both not set', async () => { + const testAccount = accounts[3]; + 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.queryBondsByOwner([testAccount.address]); + expect(result.bonds).toHaveLength(0); + }); +}; + +describe('Config', configTests);