Skip to content

Commit

Permalink
add tests for ICQ module howtos
Browse files Browse the repository at this point in the history
  • Loading branch information
sotnikov-s committed Nov 8, 2024
1 parent d7e6016 commit 7fefd4e
Show file tree
Hide file tree
Showing 4 changed files with 430 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/helpers/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,11 @@ export const CONTRACTS = {
MARKETMAP: 'marketmap.wasm',
ORACLE: 'oracle.wasm',
IBC_RATE_LIMITER: 'rate_limiter.wasm',

// docs related contracts
HOWTO_REGISTER_KV_ICQ: 'howto_register_kv_icq.wasm',
HOWTO_REGISTER_CUSTOM_KV_ICQ: 'howto_register_custom_kv_icq.wasm',
HOWTO_REGISTER_TX_ICQ: 'howto_register_tx_icq.wasm',
};

export const NEUTRON_PREFIX = process.env.NEUTRON_ADDRESS_PREFIX || 'neutron';
Expand Down
129 changes: 129 additions & 0 deletions src/testcases/parallel/icq_howto_register_custom_kv_icq.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
import config from '../../config.json';
import { inject, RunnerTestSuite } from 'vitest';
import { LocalState } from '../../helpers/local_state';
import { Registry } from '@cosmjs/proto-signing';
import { Wallet } from '../../helpers/wallet';
import { defaultRegistryTypes, SigningStargateClient } from '@cosmjs/stargate';
import {
COSMOS_DENOM,
CONTRACTS,
NEUTRON_DENOM,
} from '../../helpers/constants';
import { GasPrice } from '@cosmjs/stargate/build/fee';
import { SigningNeutronClient } from '../../helpers/signing_neutron_client';
import { waitBlocks } from '@neutron-org/neutronjsplus/dist/wait';

// just a fresh test-specific address. don't use it in other parallel test cases to avoid races
const WATCHED_GAIA_ADDR = 'cosmos1yfhvt7uje9ztwr9mk6xnkg0thf83r2d53w38ja';

const CONNECTION_ID = 'connection-0';
const ICQ_UPDATE_PERIOD = 5;

describe(
'Neutron / docs / interchainqueries / howto / register KV ICQ',
{},
() => {
let testState: LocalState;
let gaiaClient: SigningStargateClient;
let gaiaAddress: string;
let neutronWallet: Wallet;
let neutronClient: SigningNeutronClient;

beforeAll(async (suite: RunnerTestSuite) => {
testState = await LocalState.create(config, inject('mnemonics'), suite);
neutronWallet = await testState.nextWallet('neutron');

const galaWallet = await testState.nextWallet('cosmos');
gaiaClient = await SigningStargateClient.connectWithSigner(
testState.rpcGaia,
galaWallet.directwallet,
{
registry: new Registry(defaultRegistryTypes),
gasPrice: GasPrice.fromString('0.05uatom'),
},
);
gaiaAddress = (
await galaWallet.directwallet.getAccounts()
)[0].address.toString();

neutronClient = await SigningNeutronClient.connectWithSigner(
testState.rpcNeutron,
neutronWallet.directwallet,
neutronWallet.address,
);

await gaiaClient.sendTokens(
gaiaAddress,
WATCHED_GAIA_ADDR,
[
{
amount: '1000000',
denom: COSMOS_DENOM,
},
],
300000,
);
});

let contractAddress: string;
describe('instantiate contract', () => {
let codeId: number;
test('store contract', async () => {
codeId = await neutronClient.upload(
CONTRACTS.HOWTO_REGISTER_CUSTOM_KV_ICQ,
);
expect(codeId).toBeGreaterThan(0);
});
test('instantiate contract', async () => {
contractAddress = await neutronClient.instantiate(
codeId,
{},
'howto_register_custom_kv_icq',
);
});
});

describe('register and execute KV ICQ', () => {
test('top up contract', async () => {
await neutronClient.sendTokens(
contractAddress,
[{ denom: NEUTRON_DENOM, amount: '1000000' }],
{
gas: '200000',
amount: [{ denom: NEUTRON_DENOM, amount: '1000' }],
},
);
});

test('register KV ICQ', async () => {
await neutronClient.execute(
contractAddress,
{
register_account_query: {
connection_id: CONNECTION_ID,
addr: WATCHED_GAIA_ADDR,
update_period: ICQ_UPDATE_PERIOD,
},
},
[],
{
amount: [{ denom: NEUTRON_DENOM, amount: '1000000' }],
gas: '2000000',
},
);
});

test('check ICQ submitted value result', async () => {
await waitBlocks(ICQ_UPDATE_PERIOD, neutronClient);

const queryResult = await neutronClient.queryContractSmart(
contractAddress,
{ account: { address: WATCHED_GAIA_ADDR } },
);
expect(queryResult.address).toEqual(WATCHED_GAIA_ADDR);
expect(+queryResult.sequence).toEqual(0);
expect(+queryResult.account_number).not.toEqual(0);
});
});
},
);
134 changes: 134 additions & 0 deletions src/testcases/parallel/icq_howto_register_kv_icq.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
import config from '../../config.json';
import { inject, RunnerTestSuite } from 'vitest';
import { LocalState } from '../../helpers/local_state';
import { Registry } from '@cosmjs/proto-signing';
import { Wallet } from '../../helpers/wallet';
import { defaultRegistryTypes, SigningStargateClient } from '@cosmjs/stargate';
import {
COSMOS_DENOM,
CONTRACTS,
NEUTRON_DENOM,
} from '../../helpers/constants';
import { GasPrice } from '@cosmjs/stargate/build/fee';
import { SigningNeutronClient } from '../../helpers/signing_neutron_client';
import { waitBlocks } from '@neutron-org/neutronjsplus/dist/wait';

// just a fresh test-specific address. don't use it in other parallel test cases to avoid races
const WATCHED_GAIA_ADDR = 'cosmos1gdzru2fzdn7czxn89phu9ergn7v8c7zpladz6f';

const WATCHED_GAIA_ADDR_BALANCE_UATOM = 100000;
const CONNECTION_ID = 'connection-0';
const ICQ_UPDATE_PERIOD = 5;

describe(
'Neutron / docs / interchainqueries / howto / register KV ICQ',
{},
() => {
let testState: LocalState;
let gaiaClient: SigningStargateClient;
let gaiaAddress: string;
let neutronWallet: Wallet;
let neutronClient: SigningNeutronClient;

beforeAll(async (suite: RunnerTestSuite) => {
testState = await LocalState.create(config, inject('mnemonics'), suite);
neutronWallet = await testState.nextWallet('neutron');

const galaWallet = await testState.nextWallet('cosmos');
gaiaClient = await SigningStargateClient.connectWithSigner(
testState.rpcGaia,
galaWallet.directwallet,
{
registry: new Registry(defaultRegistryTypes),
gasPrice: GasPrice.fromString('0.05uatom'),
},
);
gaiaAddress = (
await galaWallet.directwallet.getAccounts()
)[0].address.toString();

neutronClient = await SigningNeutronClient.connectWithSigner(
testState.rpcNeutron,
neutronWallet.directwallet,
neutronWallet.address,
);

await gaiaClient.sendTokens(
gaiaAddress,
WATCHED_GAIA_ADDR,
[
{
amount: WATCHED_GAIA_ADDR_BALANCE_UATOM.toString(),
denom: COSMOS_DENOM,
},
],
300000,
);
});

let contractAddress: string;
describe('instantiate contract', () => {
let codeId: number;
test('store contract', async () => {
codeId = await neutronClient.upload(CONTRACTS.HOWTO_REGISTER_KV_ICQ);
expect(codeId).toBeGreaterThan(0);
});
test('instantiate contract', async () => {
contractAddress = await neutronClient.instantiate(
codeId,
{},
'howto_register_kv_icq',
);
});
});

describe('register and execute KV ICQ', () => {
test('top up contract', async () => {
await neutronClient.sendTokens(
contractAddress,
[{ denom: NEUTRON_DENOM, amount: '1000000' }],
{
gas: '200000',
amount: [{ denom: NEUTRON_DENOM, amount: '1000' }],
},
);
});

test('register KV ICQ', async () => {
await neutronClient.execute(
contractAddress,
{
register_balances_query: {
connection_id: CONNECTION_ID,
denoms: [COSMOS_DENOM],
addr: WATCHED_GAIA_ADDR,
update_period: ICQ_UPDATE_PERIOD,
},
},
[],
{
amount: [{ denom: NEUTRON_DENOM, amount: '1000000' }],
gas: '2000000',
},
);
});

test('check ICQ submitted value result', async () => {
await waitBlocks(ICQ_UPDATE_PERIOD, neutronClient);

const queryResult = await neutronClient.queryContractSmart(
contractAddress,
{ balances: { address: WATCHED_GAIA_ADDR } },
);
expect(queryResult).toEqual({
coins: [
{
denom: COSMOS_DENOM,
amount: WATCHED_GAIA_ADDR_BALANCE_UATOM.toString(),
},
],
});
});
});
},
);
Loading

0 comments on commit 7fefd4e

Please sign in to comment.