Skip to content

Commit

Permalink
Merge pull request #298 from neutron-org/feat/improve-balances-query
Browse files Browse the repository at this point in the history
[NTRN-235] Feat/improve balances query
  • Loading branch information
pr0n00gler authored Jun 6, 2024
2 parents 4e4ad3b + 16b1f5a commit 8569ee7
Showing 1 changed file with 107 additions and 19 deletions.
126 changes: 107 additions & 19 deletions src/testcases/run_in_band/interchain_kv_query.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -171,20 +171,20 @@ const getQueryDelegatorDelegationsResult = (
},
});

const registerBalanceQuery = async (
const registerBalancesQuery = async (
cm: WalletWrapper,
contractAddress: string,
connectionId: string,
updatePeriod: number,
denom: string,
denoms: string[],
addr: cosmosclient.AccAddress,
) => {
const txResult = await cm.executeContract(
contractAddress,
JSON.stringify({
register_balance_query: {
register_balances_query: {
connection_id: connectionId,
denom: denom,
denoms: denoms,
addr: addr.toString(),
update_period: updatePeriod,
},
Expand Down Expand Up @@ -463,6 +463,7 @@ describe('Neutron / Interchain KV Query', () => {
2: 3,
3: 4,
4: 3,
5: 4,
};
let testState: TestStateLocalCosmosTestNet;
let neutronChain: CosmosWrapper;
Expand Down Expand Up @@ -526,9 +527,9 @@ describe('Neutron / Interchain KV Query', () => {
await neutronAccount.executeContract(
contractAddress,
JSON.stringify({
register_balance_query: {
register_balances_query: {
connection_id: connectionId,
denom: gaiaChain.denom,
denoms: [gaiaChain.denom],
addr: testState.wallets.cosmos.demo2.address.toString(),
update_period: 10,
},
Expand Down Expand Up @@ -588,12 +589,12 @@ describe('Neutron / Interchain KV Query', () => {
let balances = await neutronChain.queryBalances(contractAddress);
expect(balances.balances[0].amount).toEqual('1000000');

await registerBalanceQuery(
await registerBalancesQuery(
neutronAccount,
contractAddress,
connectionId,
10,
gaiaChain.denom,
[gaiaChain.denom],
testState.wallets.cosmos.demo2.address,
);

Expand All @@ -609,23 +610,23 @@ describe('Neutron / Interchain KV Query', () => {
});

test('register icq #2: balance', async () => {
await registerBalanceQuery(
await registerBalancesQuery(
neutronAccount,
contractAddress,
connectionId,
updatePeriods[2],
gaiaChain.denom,
[gaiaChain.denom],
testState.wallets.cosmos.demo2.address,
);
});

test('register icq #3: balance', async () => {
await registerBalanceQuery(
await registerBalancesQuery(
neutronAccount,
contractAddress,
connectionId,
updatePeriods[3],
gaiaChain.denom,
[gaiaChain.denom],
testState.wallets.cosmos.val1.address,
);
});
Expand All @@ -640,6 +641,17 @@ describe('Neutron / Interchain KV Query', () => {
[testState.wallets.cosmos.val1.address],
);
});

test('register icq #5: multiple balances', async () => {
await registerBalancesQuery(
neutronAccount,
contractAddress,
connectionId,
updatePeriods[5],
[gaiaChain.denom, 'nonexistentdenom'],
testState.wallets.cosmos.val1.address,
);
});
});
});

Expand Down Expand Up @@ -700,8 +712,34 @@ describe('Neutron / Interchain KV Query', () => {
);
});

test("registered icq #5 doesn't exist", async () => {
test('get registered icq #5: multiple balances', async () => {
const queryId = 5;
const queryResult = await getRegisteredQuery(
neutronChain,
contractAddress,
queryId,
);
expect(queryResult.registered_query.id).toEqual(queryId);
expect(queryResult.registered_query.owner).toEqual(contractAddress);
expect(queryResult.registered_query.keys.length).toEqual(2);
expect(queryResult.registered_query.keys[0].path).toEqual('bank');
expect(queryResult.registered_query.keys[0].key.length).toBeGreaterThan(
0,
);
expect(queryResult.registered_query.keys[1].path).toEqual('bank');
expect(queryResult.registered_query.keys[1].key.length).toBeGreaterThan(
0,
);
expect(queryResult.registered_query.query_type).toEqual('kv');
expect(queryResult.registered_query.transactions_filter).toEqual('');
expect(queryResult.registered_query.connection_id).toEqual(connectionId);
expect(queryResult.registered_query.update_period).toEqual(
updatePeriods[queryId],
);
});

test("registered icq #6 doesn't exist", async () => {
const queryId = 6;
await expect(
getRegisteredQuery(neutronChain, contractAddress, queryId),
).rejects.toThrow();
Expand Down Expand Up @@ -780,6 +818,56 @@ describe('Neutron / Interchain KV Query', () => {
(1500000).toString(),
);
});

test('perform icq #5: multiple balances', async () => {
const queryId = 5;
await waitForICQResultWithRemoteHeight(
neutronChain,
contractAddress,
queryId,
await getHeight(gaiaChain.sdk),
);

const interchainQueryResult = await getQueryBalanceResult(
neutronChain,
contractAddress,
queryId,
);
const directQueryResult = await cosmosclient.rest.bank.allBalances(
gaiaChain.sdk,
testState.wallets.cosmos.val1.address.toAccAddress().toString(),
);

expect(interchainQueryResult.balances.coins.length).toEqual(2);
expect(
interchainQueryResult.balances.coins.find(
(c) => c.denom == gaiaChain.denom,
),
).toBeDefined();
expect(
interchainQueryResult.balances.coins.find(
(c) => c.denom == gaiaChain.denom,
)?.amount,
).toEqual(
directQueryResult.data.balances?.find((c) => c.denom == gaiaChain.denom)
?.amount,
);
expect(
directQueryResult.data.balances?.find(
(c) => c.denom == 'nonexistentdenom',
),
).toEqual(undefined);
expect(
interchainQueryResult.balances.coins.find(
(c) => c.denom == 'nonexistentdenom',
),
).toBeDefined();
expect(
interchainQueryResult.balances.coins.find(
(c) => c.denom == 'nonexistentdenom',
)?.amount,
).toEqual('0');
});
});

// In this test suite we aim to ensure contract state gets correctly reverted
Expand Down Expand Up @@ -878,12 +966,12 @@ describe('Neutron / Interchain KV Query', () => {
test('should check query creation with governance parameters', async () => {
const params = await neutronChain.queryInterchainqueriesParams();

const queryId = await registerBalanceQuery(
const queryId = await registerBalancesQuery(
neutronAccount,
contractAddress,
connectionId,
2,
gaiaChain.denom,
[gaiaChain.denom],
gaiaAccount.wallet.address,
);

Expand Down Expand Up @@ -938,12 +1026,12 @@ describe('Neutron / Interchain KV Query', () => {
JSON.stringify(queryDepositParam),
);

const queryId = await registerBalanceQuery(
const queryId = await registerBalancesQuery(
neutronAccount,
contractAddress,
connectionId,
10,
gaiaChain.denom,
[gaiaChain.denom],
testState.wallets.cosmos.demo2.address,
);

Expand Down Expand Up @@ -994,12 +1082,12 @@ describe('Neutron / Interchain KV Query', () => {
balancesBeforeRegistration.balances as ICoin[],
);

const queryId = await registerBalanceQuery(
const queryId = await registerBalancesQuery(
neutronAccount,
contractAddress,
connectionId,
15,
gaiaChain.denom,
[gaiaChain.denom],
testState.wallets.cosmos.demo2.address,
);

Expand Down

0 comments on commit 8569ee7

Please sign in to comment.