Skip to content

Commit

Permalink
Migrate to cometClient in query tests
Browse files Browse the repository at this point in the history
  • Loading branch information
webmaster128 committed Oct 26, 2023
1 parent 8db0ef9 commit e3ee1a2
Show file tree
Hide file tree
Showing 13 changed files with 244 additions and 254 deletions.
6 changes: 3 additions & 3 deletions packages/cosmwasm-stargate/src/testutils.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import {
setupAuthExtension,
setupBankExtension,
} from "@cosmjs/stargate";
import { Tendermint34Client } from "@cosmjs/tendermint-rpc";
import { connectComet } from "@cosmjs/tendermint-rpc";
import { assertDefinedAndNotNull } from "@cosmjs/utils";
import { SignMode } from "cosmjs-types/cosmos/tx/signing/v1beta1/signing";
import { AuthInfo, SignDoc, TxBody } from "cosmjs-types/cosmos/tx/v1beta1/tx";
Expand Down Expand Up @@ -170,8 +170,8 @@ export function fromOneElementArray<T>(elements: ArrayLike<T>): T {
export async function makeWasmClient(
endpoint: string,
): Promise<QueryClient & AuthExtension & BankExtension & WasmExtension> {
const tmClient = await Tendermint34Client.connect(endpoint);
return QueryClient.withExtensions(tmClient, setupAuthExtension, setupBankExtension, setupWasmExtension);
const cometClient = await connectComet(endpoint);
return QueryClient.withExtensions(cometClient, setupAuthExtension, setupBankExtension, setupWasmExtension);
}

/**
Expand Down
22 changes: 10 additions & 12 deletions packages/stargate/src/modules/auth/queries.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* eslint-disable @typescript-eslint/naming-convention */
import { encodePubkey } from "@cosmjs/proto-signing";
import { Tendermint34Client } from "@cosmjs/tendermint-rpc";
import { CometClient, connectComet } from "@cosmjs/tendermint-rpc";
import { assert } from "@cosmjs/utils";
import { BaseAccount } from "cosmjs-types/cosmos/auth/v1beta1/auth";
import { Any } from "cosmjs-types/google/protobuf/any";
Expand All @@ -9,18 +9,16 @@ import { QueryClient } from "../../queryclient";
import { nonExistentAddress, pendingWithoutSimapp, simapp, unused, validator } from "../../testutils.spec";
import { AuthExtension, setupAuthExtension } from "./queries";

async function makeClientWithAuth(
rpcUrl: string,
): Promise<[QueryClient & AuthExtension, Tendermint34Client]> {
const tmClient = await Tendermint34Client.connect(rpcUrl);
return [QueryClient.withExtensions(tmClient, setupAuthExtension), tmClient];
async function makeClientWithAuth(rpcUrl: string): Promise<[QueryClient & AuthExtension, CometClient]> {
const cometClient = await connectComet(rpcUrl);
return [QueryClient.withExtensions(cometClient, setupAuthExtension), cometClient];
}

describe("AuthExtension", () => {
describe("account", () => {
it("works for unused account", async () => {
pendingWithoutSimapp();
const [client, tmClient] = await makeClientWithAuth(simapp.tendermintUrl);
const [client, cometClient] = await makeClientWithAuth(simapp.tendermintUrl);
const account = await client.auth.account(unused.address);
assert(account);

Expand All @@ -33,12 +31,12 @@ describe("AuthExtension", () => {
}),
);

tmClient.disconnect();
cometClient.disconnect();
});

it("works for account with pubkey and non-zero sequence", async () => {
pendingWithoutSimapp();
const [client, tmClient] = await makeClientWithAuth(simapp.tendermintUrl);
const [client, cometClient] = await makeClientWithAuth(simapp.tendermintUrl);
const account = await client.auth.account(validator.delegatorAddress);
assert(account);

Expand All @@ -50,18 +48,18 @@ describe("AuthExtension", () => {
sequence: BigInt(validator.sequence),
});

tmClient.disconnect();
cometClient.disconnect();
});

it("rejects for non-existent address", async () => {
pendingWithoutSimapp();
const [client, tmClient] = await makeClientWithAuth(simapp.tendermintUrl);
const [client, cometClient] = await makeClientWithAuth(simapp.tendermintUrl);

await expectAsync(client.auth.account(nonExistentAddress)).toBeRejectedWithError(
/account cosmos1p79apjaufyphcmsn4g07cynqf0wyjuezqu84hd not found/i,
);

tmClient.disconnect();
cometClient.disconnect();
});
});
});
22 changes: 10 additions & 12 deletions packages/stargate/src/modules/authz/queries.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { makeCosmoshubPath } from "@cosmjs/amino";
import { coins, DirectSecp256k1HdWallet } from "@cosmjs/proto-signing";
import { Tendermint34Client } from "@cosmjs/tendermint-rpc";
import { CometClient, connectComet } from "@cosmjs/tendermint-rpc";
import { assertDefined, sleep } from "@cosmjs/utils";
import { GenericAuthorization } from "cosmjs-types/cosmos/authz/v1beta1/authz";

Expand All @@ -18,11 +18,9 @@ import {
} from "../../testutils.spec";
import { AuthzExtension, setupAuthzExtension } from "./queries";

async function makeClientWithAuthz(
rpcUrl: string,
): Promise<[QueryClient & AuthzExtension, Tendermint34Client]> {
const tmClient = await Tendermint34Client.connect(rpcUrl);
return [QueryClient.withExtensions(tmClient, setupAuthzExtension), tmClient];
async function makeClientWithAuthz(rpcUrl: string): Promise<[QueryClient & AuthzExtension, CometClient]> {
const cometClient = await connectComet(rpcUrl);
return [QueryClient.withExtensions(cometClient, setupAuthzExtension), cometClient];
}

describe("AuthzExtension", () => {
Expand Down Expand Up @@ -81,7 +79,7 @@ describe("AuthzExtension", () => {
describe("grants", () => {
it("works", async () => {
pendingWithoutSimapp();
const [client, tmClient] = await makeClientWithAuthz(simapp.tendermintUrl);
const [client, cometClient] = await makeClientWithAuthz(simapp.tendermintUrl);
const response = await client.authz.grants(granter1Address, grantee1Address, "");
expect(response.grants.length).toEqual(1);
const grant = response.grants[0];
Expand All @@ -98,14 +96,14 @@ describe("AuthzExtension", () => {
// Check if it's the same one then we granted
expect(msgDecoded).toEqual(grantedMsg);

tmClient.disconnect();
cometClient.disconnect();
});
});

describe("granter grants", () => {
it("works", async () => {
pendingWithoutSimapp46OrHigher();
const [client, tmClient] = await makeClientWithAuthz(simapp.tendermintUrl);
const [client, cometClient] = await makeClientWithAuthz(simapp.tendermintUrl);
const response = await client.authz.granterGrants(granter1Address);
expect(response.grants.length).toBeGreaterThanOrEqual(1);
const grant = response.grants.find(
Expand All @@ -129,14 +127,14 @@ describe("AuthzExtension", () => {
// Check if it's the same one then we granted
expect(msgDecoded).toEqual(grantedMsg);

tmClient.disconnect();
cometClient.disconnect();
});
});

describe("grantee grants", () => {
it("works", async () => {
pendingWithoutSimapp46OrHigher();
const [client, tmClient] = await makeClientWithAuthz(simapp.tendermintUrl);
const [client, cometClient] = await makeClientWithAuthz(simapp.tendermintUrl);
const response = await client.authz.granteeGrants(grantee1Address);
expect(response.grants.length).toEqual(1);
const grant = response.grants[0];
Expand All @@ -157,7 +155,7 @@ describe("AuthzExtension", () => {
// Check if it's the same one then we granted
expect(msgDecoded).toEqual(grantedMsg);

tmClient.disconnect();
cometClient.disconnect();
});
});
});
54 changes: 26 additions & 28 deletions packages/stargate/src/modules/bank/queries.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Tendermint34Client } from "@cosmjs/tendermint-rpc";
import { CometClient, connectComet } from "@cosmjs/tendermint-rpc";

import { QueryClient } from "../../queryclient";
import {
Expand All @@ -10,18 +10,16 @@ import {
} from "../../testutils.spec";
import { BankExtension, setupBankExtension } from "./queries";

async function makeClientWithBank(
rpcUrl: string,
): Promise<[QueryClient & BankExtension, Tendermint34Client]> {
const tmClient = await Tendermint34Client.connect(rpcUrl);
return [QueryClient.withExtensions(tmClient, setupBankExtension), tmClient];
async function makeClientWithBank(rpcUrl: string): Promise<[QueryClient & BankExtension, CometClient]> {
const cometClient = await connectComet(rpcUrl);
return [QueryClient.withExtensions(cometClient, setupBankExtension), cometClient];
}

describe("BankExtension", () => {
describe("balance", () => {
it("works for different existing balances", async () => {
pendingWithoutSimapp();
const [client, tmClient] = await makeClientWithBank(simapp.tendermintUrl);
const [client, cometClient] = await makeClientWithBank(simapp.tendermintUrl);

const response1 = await client.bank.balance(unused.address, simapp.denomFee);
expect(response1).toEqual({
Expand All @@ -34,40 +32,40 @@ describe("BankExtension", () => {
denom: simapp.denomStaking,
});

tmClient.disconnect();
cometClient.disconnect();
});

it("returns zero for non-existent balance", async () => {
pendingWithoutSimapp();
const [client, tmClient] = await makeClientWithBank(simapp.tendermintUrl);
const [client, cometClient] = await makeClientWithBank(simapp.tendermintUrl);

const response = await client.bank.balance(unused.address, "gintonic");
expect(response).toEqual({
amount: "0",
denom: "gintonic",
});

tmClient.disconnect();
cometClient.disconnect();
});

it("returns zero for non-existent address", async () => {
pendingWithoutSimapp();
const [client, tmClient] = await makeClientWithBank(simapp.tendermintUrl);
const [client, cometClient] = await makeClientWithBank(simapp.tendermintUrl);

const response = await client.bank.balance(nonExistentAddress, simapp.denomFee);
expect(response).toEqual({
amount: "0",
denom: simapp.denomFee,
});

tmClient.disconnect();
cometClient.disconnect();
});
});

describe("allBalances", () => {
it("returns all balances for unused account", async () => {
pendingWithoutSimapp();
const [client, tmClient] = await makeClientWithBank(simapp.tendermintUrl);
const [client, cometClient] = await makeClientWithBank(simapp.tendermintUrl);

const balances = await client.bank.allBalances(unused.address);
expect(balances).toEqual([
Expand All @@ -81,24 +79,24 @@ describe("BankExtension", () => {
},
]);

tmClient.disconnect();
cometClient.disconnect();
});

it("returns an empty list for non-existent account", async () => {
pendingWithoutSimapp();
const [client, tmClient] = await makeClientWithBank(simapp.tendermintUrl);
const [client, cometClient] = await makeClientWithBank(simapp.tendermintUrl);

const balances = await client.bank.allBalances(nonExistentAddress);
expect(balances).toEqual([]);

tmClient.disconnect();
cometClient.disconnect();
});
});

describe("totalSupply", () => {
it("works", async () => {
pendingWithoutSimapp();
const [client, tmClient] = await makeClientWithBank(simapp.tendermintUrl);
const [client, cometClient] = await makeClientWithBank(simapp.tendermintUrl);

const { supply } = await client.bank.totalSupply();
expect(supply).toEqual([
Expand All @@ -112,42 +110,42 @@ describe("BankExtension", () => {
},
]);

tmClient.disconnect();
cometClient.disconnect();
});
});

describe("supplyOf", () => {
it("works for existing denom", async () => {
pendingWithoutSimapp();
const [client, tmClient] = await makeClientWithBank(simapp.tendermintUrl);
const [client, cometClient] = await makeClientWithBank(simapp.tendermintUrl);

const response = await client.bank.supplyOf(simapp.denomFee);
expect(response).toEqual({
amount: simapp.totalSupply.toString(),
denom: simapp.denomFee,
});

tmClient.disconnect();
cometClient.disconnect();
});

it("returns zero for non-existent denom", async () => {
pendingWithoutSimapp();
const [client, tmClient] = await makeClientWithBank(simapp.tendermintUrl);
const [client, cometClient] = await makeClientWithBank(simapp.tendermintUrl);

const response = await client.bank.supplyOf("gintonic");
expect(response).toEqual({
amount: "0",
denom: "gintonic",
});

tmClient.disconnect();
cometClient.disconnect();
});
});

describe("denomMetadata", () => {
it("works for existent denom", async () => {
pendingWithoutSimapp();
const [client, tmClient] = await makeClientWithBank(simapp.tendermintUrl);
const [client, cometClient] = await makeClientWithBank(simapp.tendermintUrl);

const metadata = await client.bank.denomMetadata("ucosm");
expect(metadata).toEqual(
Expand All @@ -172,23 +170,23 @@ describe("BankExtension", () => {
}),
);

tmClient.disconnect();
cometClient.disconnect();
});

it("works for non-existent denom", async () => {
pendingWithoutSimapp();
const [client, tmClient] = await makeClientWithBank(simapp.tendermintUrl);
const [client, cometClient] = await makeClientWithBank(simapp.tendermintUrl);

await expectAsync(client.bank.denomMetadata("nothere")).toBeRejectedWithError(/code = NotFound/i);

tmClient.disconnect();
cometClient.disconnect();
});
});

describe("denomsMetadata", () => {
it("works", async () => {
pendingWithoutSimapp();
const [client, tmClient] = await makeClientWithBank(simapp.tendermintUrl);
const [client, cometClient] = await makeClientWithBank(simapp.tendermintUrl);

const metadatas = await client.bank.denomsMetadata();
expect(metadatas.length).toEqual(1);
Expand All @@ -214,7 +212,7 @@ describe("BankExtension", () => {
}),
);

tmClient.disconnect();
cometClient.disconnect();
});
});
});
Loading

0 comments on commit e3ee1a2

Please sign in to comment.