Merge pull request #142 from CosmWasm/chain-id
Cache chainID in CosmWasmClient
This commit is contained in:
commit
b0d084be5e
@ -94,7 +94,7 @@ export class CosmWasmConnection implements BlockchainConnection {
|
||||
}
|
||||
|
||||
private static async initialize(cosmWasmClient: CosmWasmClient): Promise<ChainId> {
|
||||
const rawChainId = await cosmWasmClient.chainId();
|
||||
const rawChainId = await cosmWasmClient.getChainId();
|
||||
return Caip5.encode(rawChainId);
|
||||
}
|
||||
|
||||
|
||||
@ -11,8 +11,8 @@ import {
|
||||
fromOneElementArray,
|
||||
makeRandomAddress,
|
||||
pendingWithoutWasmd,
|
||||
wasmd,
|
||||
wasmdEnabled,
|
||||
wasmdEndpoint,
|
||||
} from "./testutils.spec";
|
||||
import { Coin, CosmosSdkTx, isMsgExecuteContract, isMsgInstantiateContract, isMsgSend } from "./types";
|
||||
|
||||
@ -39,7 +39,7 @@ describe("CosmWasmClient.searchTx", () => {
|
||||
beforeAll(async () => {
|
||||
if (wasmdEnabled()) {
|
||||
const pen = await Secp256k1Pen.fromMnemonic(faucet.mnemonic);
|
||||
const client = new SigningCosmWasmClient(wasmdEndpoint, faucet.address, signBytes =>
|
||||
const client = new SigningCosmWasmClient(wasmd.endpoint, faucet.address, signBytes =>
|
||||
pen.sign(signBytes),
|
||||
);
|
||||
|
||||
@ -51,7 +51,7 @@ describe("CosmWasmClient.searchTx", () => {
|
||||
};
|
||||
const result = await client.sendTokens(recipient, [transferAmount]);
|
||||
await sleep(50); // wait until tx is indexed
|
||||
const txDetails = await new RestClient(wasmdEndpoint).txsById(result.transactionHash);
|
||||
const txDetails = await new RestClient(wasmd.endpoint).txsById(result.transactionHash);
|
||||
postedSend = {
|
||||
sender: faucet.address,
|
||||
recipient: recipient,
|
||||
@ -71,7 +71,7 @@ describe("CosmWasmClient.searchTx", () => {
|
||||
};
|
||||
const result = await client.execute(hashInstance, msg);
|
||||
await sleep(50); // wait until tx is indexed
|
||||
const txDetails = await new RestClient(wasmdEndpoint).txsById(result.transactionHash);
|
||||
const txDetails = await new RestClient(wasmd.endpoint).txsById(result.transactionHash);
|
||||
postedExecute = {
|
||||
sender: faucet.address,
|
||||
contract: hashInstance,
|
||||
@ -87,7 +87,7 @@ describe("CosmWasmClient.searchTx", () => {
|
||||
it("can search by ID", async () => {
|
||||
pendingWithoutWasmd();
|
||||
assert(postedSend, "value must be set in beforeAll()");
|
||||
const client = new CosmWasmClient(wasmdEndpoint);
|
||||
const client = new CosmWasmClient(wasmd.endpoint);
|
||||
const result = await client.searchTx({ id: postedSend.hash });
|
||||
expect(result.length).toEqual(1);
|
||||
expect(result[0]).toEqual(
|
||||
@ -101,7 +101,7 @@ describe("CosmWasmClient.searchTx", () => {
|
||||
|
||||
it("can search by ID (non existent)", async () => {
|
||||
pendingWithoutWasmd();
|
||||
const client = new CosmWasmClient(wasmdEndpoint);
|
||||
const client = new CosmWasmClient(wasmd.endpoint);
|
||||
const nonExistentId = "0000000000000000000000000000000000000000000000000000000000000000";
|
||||
const result = await client.searchTx({ id: nonExistentId });
|
||||
expect(result.length).toEqual(0);
|
||||
@ -110,7 +110,7 @@ describe("CosmWasmClient.searchTx", () => {
|
||||
it("can search by ID and filter by minHeight", async () => {
|
||||
pendingWithoutWasmd();
|
||||
assert(postedSend);
|
||||
const client = new CosmWasmClient(wasmdEndpoint);
|
||||
const client = new CosmWasmClient(wasmd.endpoint);
|
||||
const query = { id: postedSend.hash };
|
||||
|
||||
{
|
||||
@ -139,7 +139,7 @@ describe("CosmWasmClient.searchTx", () => {
|
||||
it("can search by height", async () => {
|
||||
pendingWithoutWasmd();
|
||||
assert(postedSend, "value must be set in beforeAll()");
|
||||
const client = new CosmWasmClient(wasmdEndpoint);
|
||||
const client = new CosmWasmClient(wasmd.endpoint);
|
||||
const result = await client.searchTx({ height: postedSend.height });
|
||||
expect(result.length).toEqual(1);
|
||||
expect(result[0]).toEqual(
|
||||
@ -156,7 +156,7 @@ describe("CosmWasmClient.searchTx", () => {
|
||||
it("can search by sender", async () => {
|
||||
pendingWithoutWasmd();
|
||||
assert(postedSend, "value must be set in beforeAll()");
|
||||
const client = new CosmWasmClient(wasmdEndpoint);
|
||||
const client = new CosmWasmClient(wasmd.endpoint);
|
||||
const results = await client.searchTx({ sentFromOrTo: postedSend.sender });
|
||||
expect(results.length).toBeGreaterThanOrEqual(1);
|
||||
|
||||
@ -182,7 +182,7 @@ describe("CosmWasmClient.searchTx", () => {
|
||||
it("can search by recipient", async () => {
|
||||
pendingWithoutWasmd();
|
||||
assert(postedSend, "value must be set in beforeAll()");
|
||||
const client = new CosmWasmClient(wasmdEndpoint);
|
||||
const client = new CosmWasmClient(wasmd.endpoint);
|
||||
const results = await client.searchTx({ sentFromOrTo: postedSend.recipient });
|
||||
expect(results.length).toBeGreaterThanOrEqual(1);
|
||||
|
||||
@ -208,7 +208,7 @@ describe("CosmWasmClient.searchTx", () => {
|
||||
it("can search by recipient and filter by minHeight", async () => {
|
||||
pendingWithoutWasmd();
|
||||
assert(postedSend);
|
||||
const client = new CosmWasmClient(wasmdEndpoint);
|
||||
const client = new CosmWasmClient(wasmd.endpoint);
|
||||
const query = { sentFromOrTo: postedSend.recipient };
|
||||
|
||||
{
|
||||
@ -235,7 +235,7 @@ describe("CosmWasmClient.searchTx", () => {
|
||||
it("can search by recipient and filter by maxHeight", async () => {
|
||||
pendingWithoutWasmd();
|
||||
assert(postedSend);
|
||||
const client = new CosmWasmClient(wasmdEndpoint);
|
||||
const client = new CosmWasmClient(wasmd.endpoint);
|
||||
const query = { sentFromOrTo: postedSend.recipient };
|
||||
|
||||
{
|
||||
@ -264,7 +264,7 @@ describe("CosmWasmClient.searchTx", () => {
|
||||
it("can search by transfer.recipient", async () => {
|
||||
pendingWithoutWasmd();
|
||||
assert(postedSend, "value must be set in beforeAll()");
|
||||
const client = new CosmWasmClient(wasmdEndpoint);
|
||||
const client = new CosmWasmClient(wasmd.endpoint);
|
||||
const results = await client.searchTx({
|
||||
tags: [{ key: "transfer.recipient", value: postedSend.recipient }],
|
||||
});
|
||||
@ -290,7 +290,7 @@ describe("CosmWasmClient.searchTx", () => {
|
||||
it("can search by message.contract_address", async () => {
|
||||
pendingWithoutWasmd();
|
||||
assert(postedExecute, "value must be set in beforeAll()");
|
||||
const client = new CosmWasmClient(wasmdEndpoint);
|
||||
const client = new CosmWasmClient(wasmd.endpoint);
|
||||
const results = await client.searchTx({
|
||||
tags: [{ key: "message.contract_address", value: postedExecute.contract }],
|
||||
});
|
||||
@ -332,7 +332,7 @@ describe("CosmWasmClient.searchTx", () => {
|
||||
it("can search by message.contract_address + message.action", async () => {
|
||||
pendingWithoutWasmd();
|
||||
assert(postedExecute, "value must be set in beforeAll()");
|
||||
const client = new CosmWasmClient(wasmdEndpoint);
|
||||
const client = new CosmWasmClient(wasmd.endpoint);
|
||||
const results = await client.searchTx({
|
||||
tags: [
|
||||
{ key: "message.contract_address", value: postedExecute.contract },
|
||||
|
||||
@ -17,8 +17,8 @@ import {
|
||||
makeRandomAddress,
|
||||
pendingWithoutWasmd,
|
||||
tendermintIdMatcher,
|
||||
wasmd,
|
||||
wasmdEnabled,
|
||||
wasmdEndpoint,
|
||||
} from "./testutils.spec";
|
||||
import { MsgSend, StdFee } from "./types";
|
||||
|
||||
@ -43,23 +43,35 @@ interface HackatomInstance {
|
||||
describe("CosmWasmClient", () => {
|
||||
describe("makeReadOnly", () => {
|
||||
it("can be constructed", () => {
|
||||
const client = new CosmWasmClient(wasmdEndpoint);
|
||||
const client = new CosmWasmClient(wasmd.endpoint);
|
||||
expect(client).toBeTruthy();
|
||||
});
|
||||
});
|
||||
|
||||
describe("chainId", () => {
|
||||
describe("getChainId", () => {
|
||||
it("works", async () => {
|
||||
pendingWithoutWasmd();
|
||||
const client = new CosmWasmClient(wasmdEndpoint);
|
||||
expect(await client.chainId()).toEqual("testing");
|
||||
const client = new CosmWasmClient(wasmd.endpoint);
|
||||
expect(await client.getChainId()).toEqual(wasmd.expectedChainId);
|
||||
});
|
||||
|
||||
it("caches chain ID", async () => {
|
||||
pendingWithoutWasmd();
|
||||
const client = new CosmWasmClient(wasmd.endpoint);
|
||||
const openedClient = (client as unknown) as PrivateCosmWasmClient;
|
||||
const getCodeSpy = spyOn(openedClient.restClient, "nodeInfo").and.callThrough();
|
||||
|
||||
expect(await client.getChainId()).toEqual(wasmd.expectedChainId); // from network
|
||||
expect(await client.getChainId()).toEqual(wasmd.expectedChainId); // from cache
|
||||
|
||||
expect(getCodeSpy).toHaveBeenCalledTimes(1);
|
||||
});
|
||||
});
|
||||
|
||||
describe("getHeight", () => {
|
||||
it("gets height via last block", async () => {
|
||||
pendingWithoutWasmd();
|
||||
const client = new CosmWasmClient(wasmdEndpoint);
|
||||
const client = new CosmWasmClient(wasmd.endpoint);
|
||||
const openedClient = (client as unknown) as PrivateCosmWasmClient;
|
||||
const blockLatestSpy = spyOn(openedClient.restClient, "blocksLatest").and.callThrough();
|
||||
|
||||
@ -74,7 +86,7 @@ describe("CosmWasmClient", () => {
|
||||
|
||||
it("gets height via authAccount once an address is known", async () => {
|
||||
pendingWithoutWasmd();
|
||||
const client = new CosmWasmClient(wasmdEndpoint);
|
||||
const client = new CosmWasmClient(wasmd.endpoint);
|
||||
|
||||
const openedClient = (client as unknown) as PrivateCosmWasmClient;
|
||||
const blockLatestSpy = spyOn(openedClient.restClient, "blocksLatest").and.callThrough();
|
||||
@ -99,7 +111,7 @@ describe("CosmWasmClient", () => {
|
||||
describe("getNonce", () => {
|
||||
it("works", async () => {
|
||||
pendingWithoutWasmd();
|
||||
const client = new CosmWasmClient(wasmdEndpoint);
|
||||
const client = new CosmWasmClient(wasmd.endpoint);
|
||||
expect(await client.getNonce(unused.address)).toEqual({
|
||||
accountNumber: 5,
|
||||
sequence: 0,
|
||||
@ -108,7 +120,7 @@ describe("CosmWasmClient", () => {
|
||||
|
||||
it("throws for missing accounts", async () => {
|
||||
pendingWithoutWasmd();
|
||||
const client = new CosmWasmClient(wasmdEndpoint);
|
||||
const client = new CosmWasmClient(wasmd.endpoint);
|
||||
const missing = makeRandomAddress();
|
||||
await client.getNonce(missing).then(
|
||||
() => fail("this must not succeed"),
|
||||
@ -120,7 +132,7 @@ describe("CosmWasmClient", () => {
|
||||
describe("getAccount", () => {
|
||||
it("works", async () => {
|
||||
pendingWithoutWasmd();
|
||||
const client = new CosmWasmClient(wasmdEndpoint);
|
||||
const client = new CosmWasmClient(wasmd.endpoint);
|
||||
expect(await client.getAccount(unused.address)).toEqual({
|
||||
address: unused.address,
|
||||
accountNumber: 5,
|
||||
@ -135,7 +147,7 @@ describe("CosmWasmClient", () => {
|
||||
|
||||
it("returns undefined for missing accounts", async () => {
|
||||
pendingWithoutWasmd();
|
||||
const client = new CosmWasmClient(wasmdEndpoint);
|
||||
const client = new CosmWasmClient(wasmd.endpoint);
|
||||
const missing = makeRandomAddress();
|
||||
expect(await client.getAccount(missing)).toBeUndefined();
|
||||
});
|
||||
@ -144,7 +156,7 @@ describe("CosmWasmClient", () => {
|
||||
describe("getBlock", () => {
|
||||
it("works for latest block", async () => {
|
||||
pendingWithoutWasmd();
|
||||
const client = new CosmWasmClient(wasmdEndpoint);
|
||||
const client = new CosmWasmClient(wasmd.endpoint);
|
||||
const response = await client.getBlock();
|
||||
|
||||
// id
|
||||
@ -152,7 +164,7 @@ describe("CosmWasmClient", () => {
|
||||
|
||||
// header
|
||||
expect(response.header.height).toBeGreaterThanOrEqual(1);
|
||||
expect(response.header.chainId).toEqual(await client.chainId());
|
||||
expect(response.header.chainId).toEqual(await client.getChainId());
|
||||
expect(new ReadonlyDate(response.header.time).getTime()).toBeLessThan(ReadonlyDate.now());
|
||||
expect(new ReadonlyDate(response.header.time).getTime()).toBeGreaterThanOrEqual(
|
||||
ReadonlyDate.now() - 5_000,
|
||||
@ -164,7 +176,7 @@ describe("CosmWasmClient", () => {
|
||||
|
||||
it("works for block by height", async () => {
|
||||
pendingWithoutWasmd();
|
||||
const client = new CosmWasmClient(wasmdEndpoint);
|
||||
const client = new CosmWasmClient(wasmd.endpoint);
|
||||
const height = (await client.getBlock()).header.height;
|
||||
const response = await client.getBlock(height - 1);
|
||||
|
||||
@ -173,7 +185,7 @@ describe("CosmWasmClient", () => {
|
||||
|
||||
// header
|
||||
expect(response.header.height).toEqual(height - 1);
|
||||
expect(response.header.chainId).toEqual(await client.chainId());
|
||||
expect(response.header.chainId).toEqual(await client.getChainId());
|
||||
expect(new ReadonlyDate(response.header.time).getTime()).toBeLessThan(ReadonlyDate.now());
|
||||
expect(new ReadonlyDate(response.header.time).getTime()).toBeGreaterThanOrEqual(
|
||||
ReadonlyDate.now() - 5_000,
|
||||
@ -187,7 +199,7 @@ describe("CosmWasmClient", () => {
|
||||
describe("getIdentifier", () => {
|
||||
it("works", async () => {
|
||||
pendingWithoutWasmd();
|
||||
const client = new CosmWasmClient(wasmdEndpoint);
|
||||
const client = new CosmWasmClient(wasmd.endpoint);
|
||||
expect(await client.getIdentifier(cosmoshub.tx)).toEqual(cosmoshub.id);
|
||||
});
|
||||
});
|
||||
@ -196,7 +208,7 @@ describe("CosmWasmClient", () => {
|
||||
it("works", async () => {
|
||||
pendingWithoutWasmd();
|
||||
const pen = await Secp256k1Pen.fromMnemonic(faucet.mnemonic);
|
||||
const client = new CosmWasmClient(wasmdEndpoint);
|
||||
const client = new CosmWasmClient(wasmd.endpoint);
|
||||
|
||||
const memo = "My first contract on chain";
|
||||
const sendMsg: MsgSend = {
|
||||
@ -223,7 +235,7 @@ describe("CosmWasmClient", () => {
|
||||
gas: "890000",
|
||||
};
|
||||
|
||||
const chainId = await client.chainId();
|
||||
const chainId = await client.getChainId();
|
||||
const { accountNumber, sequence } = await client.getNonce(faucet.address);
|
||||
const signBytes = makeSignBytes([sendMsg], fee, chainId, memo, accountNumber, sequence);
|
||||
const signature = await pen.sign(signBytes);
|
||||
@ -243,7 +255,7 @@ describe("CosmWasmClient", () => {
|
||||
describe("getCodes", () => {
|
||||
it("works", async () => {
|
||||
pendingWithoutWasmd();
|
||||
const client = new CosmWasmClient(wasmdEndpoint);
|
||||
const client = new CosmWasmClient(wasmd.endpoint);
|
||||
const result = await client.getCodes();
|
||||
expect(result.length).toBeGreaterThanOrEqual(1);
|
||||
const [first] = result;
|
||||
@ -260,7 +272,7 @@ describe("CosmWasmClient", () => {
|
||||
describe("getCodeDetails", () => {
|
||||
it("works", async () => {
|
||||
pendingWithoutWasmd();
|
||||
const client = new CosmWasmClient(wasmdEndpoint);
|
||||
const client = new CosmWasmClient(wasmd.endpoint);
|
||||
const result = await client.getCodeDetails(1);
|
||||
|
||||
const expectedInfo: Code = {
|
||||
@ -279,7 +291,7 @@ describe("CosmWasmClient", () => {
|
||||
|
||||
it("caches downloads", async () => {
|
||||
pendingWithoutWasmd();
|
||||
const client = new CosmWasmClient(wasmdEndpoint);
|
||||
const client = new CosmWasmClient(wasmd.endpoint);
|
||||
const openedClient = (client as unknown) as PrivateCosmWasmClient;
|
||||
const getCodeSpy = spyOn(openedClient.restClient, "getCode").and.callThrough();
|
||||
|
||||
@ -294,7 +306,7 @@ describe("CosmWasmClient", () => {
|
||||
describe("getContracts", () => {
|
||||
it("works", async () => {
|
||||
pendingWithoutWasmd();
|
||||
const client = new CosmWasmClient(wasmdEndpoint);
|
||||
const client = new CosmWasmClient(wasmd.endpoint);
|
||||
const result = await client.getContracts(1);
|
||||
expect(result.length).toBeGreaterThanOrEqual(3);
|
||||
const [hash, isa, jade] = result;
|
||||
@ -322,7 +334,7 @@ describe("CosmWasmClient", () => {
|
||||
describe("getContract", () => {
|
||||
it("works for HASH instance", async () => {
|
||||
pendingWithoutWasmd();
|
||||
const client = new CosmWasmClient(wasmdEndpoint);
|
||||
const client = new CosmWasmClient(wasmd.endpoint);
|
||||
const hash = await client.getContract("cosmos18vd8fpwxzck93qlwghaj6arh4p7c5n89uzcee5");
|
||||
expect(hash).toEqual({
|
||||
address: "cosmos18vd8fpwxzck93qlwghaj6arh4p7c5n89uzcee5",
|
||||
@ -361,7 +373,7 @@ describe("CosmWasmClient", () => {
|
||||
if (wasmdEnabled()) {
|
||||
pendingWithoutWasmd();
|
||||
const pen = await Secp256k1Pen.fromMnemonic(faucet.mnemonic);
|
||||
const client = new SigningCosmWasmClient(wasmdEndpoint, faucet.address, signBytes =>
|
||||
const client = new SigningCosmWasmClient(wasmd.endpoint, faucet.address, signBytes =>
|
||||
pen.sign(signBytes),
|
||||
);
|
||||
const { codeId } = await client.upload(getRandomizedHackatom());
|
||||
@ -375,7 +387,7 @@ describe("CosmWasmClient", () => {
|
||||
pendingWithoutWasmd();
|
||||
assert(contract);
|
||||
|
||||
const client = new CosmWasmClient(wasmdEndpoint);
|
||||
const client = new CosmWasmClient(wasmd.endpoint);
|
||||
const raw = await client.queryContractRaw(contract.address, configKey);
|
||||
assert(raw, "must get result");
|
||||
expect(JSON.parse(fromUtf8(raw))).toEqual({
|
||||
@ -389,7 +401,7 @@ describe("CosmWasmClient", () => {
|
||||
pendingWithoutWasmd();
|
||||
assert(contract);
|
||||
|
||||
const client = new CosmWasmClient(wasmdEndpoint);
|
||||
const client = new CosmWasmClient(wasmd.endpoint);
|
||||
const raw = await client.queryContractRaw(contract.address, otherKey);
|
||||
expect(raw).toBeNull();
|
||||
});
|
||||
@ -399,7 +411,7 @@ describe("CosmWasmClient", () => {
|
||||
assert(contract);
|
||||
|
||||
const nonExistentAddress = makeRandomAddress();
|
||||
const client = new CosmWasmClient(wasmdEndpoint);
|
||||
const client = new CosmWasmClient(wasmd.endpoint);
|
||||
await client.queryContractRaw(nonExistentAddress, configKey).then(
|
||||
() => fail("must not succeed"),
|
||||
error => expect(error).toMatch(`No contract found at address "${nonExistentAddress}"`),
|
||||
@ -414,7 +426,7 @@ describe("CosmWasmClient", () => {
|
||||
if (wasmdEnabled()) {
|
||||
pendingWithoutWasmd();
|
||||
const pen = await Secp256k1Pen.fromMnemonic(faucet.mnemonic);
|
||||
const client = new SigningCosmWasmClient(wasmdEndpoint, faucet.address, signBytes =>
|
||||
const client = new SigningCosmWasmClient(wasmd.endpoint, faucet.address, signBytes =>
|
||||
pen.sign(signBytes),
|
||||
);
|
||||
const { codeId } = await client.upload(getRandomizedHackatom());
|
||||
@ -428,7 +440,7 @@ describe("CosmWasmClient", () => {
|
||||
pendingWithoutWasmd();
|
||||
assert(contract);
|
||||
|
||||
const client = new CosmWasmClient(wasmdEndpoint);
|
||||
const client = new CosmWasmClient(wasmd.endpoint);
|
||||
const verifier = await client.queryContractSmart(contract.address, { verifier: {} });
|
||||
expect(fromAscii(verifier)).toEqual(contract.initMsg.verifier);
|
||||
});
|
||||
@ -437,7 +449,7 @@ describe("CosmWasmClient", () => {
|
||||
pendingWithoutWasmd();
|
||||
assert(contract);
|
||||
|
||||
const client = new CosmWasmClient(wasmdEndpoint);
|
||||
const client = new CosmWasmClient(wasmd.endpoint);
|
||||
await client.queryContractSmart(contract.address, { broken: {} }).then(
|
||||
() => fail("must not succeed"),
|
||||
error => expect(error).toMatch(/Error parsing QueryMsg/i),
|
||||
@ -448,7 +460,7 @@ describe("CosmWasmClient", () => {
|
||||
pendingWithoutWasmd();
|
||||
|
||||
const nonExistentAddress = makeRandomAddress();
|
||||
const client = new CosmWasmClient(wasmdEndpoint);
|
||||
const client = new CosmWasmClient(wasmd.endpoint);
|
||||
await client.queryContractSmart(nonExistentAddress, { verifier: {} }).then(
|
||||
() => fail("must not succeed"),
|
||||
error => expect(error).toMatch(`No contract found at address "${nonExistentAddress}"`),
|
||||
|
||||
@ -148,14 +148,21 @@ export class CosmWasmClient {
|
||||
protected anyValidAddress: string | undefined;
|
||||
|
||||
private readonly codesCache = new Map<number, CodeDetails>();
|
||||
private chainId: string | undefined;
|
||||
|
||||
public constructor(url: string, broadcastMode = BroadcastMode.Block) {
|
||||
this.restClient = new RestClient(url, broadcastMode);
|
||||
}
|
||||
|
||||
public async chainId(): Promise<string> {
|
||||
const response = await this.restClient.nodeInfo();
|
||||
return response.node_info.network;
|
||||
public async getChainId(): Promise<string> {
|
||||
if (!this.chainId) {
|
||||
const response = await this.restClient.nodeInfo();
|
||||
const chainId = response.node_info.network;
|
||||
if (!chainId) throw new Error("Chain ID must not be empty");
|
||||
this.chainId = chainId;
|
||||
}
|
||||
|
||||
return this.chainId;
|
||||
}
|
||||
|
||||
public async getHeight(): Promise<number> {
|
||||
|
||||
@ -24,8 +24,8 @@ import {
|
||||
tendermintIdMatcher,
|
||||
tendermintOptionalIdMatcher,
|
||||
tendermintShortHashMatcher,
|
||||
wasmd,
|
||||
wasmdEnabled,
|
||||
wasmdEndpoint,
|
||||
} from "./testutils.spec";
|
||||
import {
|
||||
Coin,
|
||||
@ -166,7 +166,7 @@ async function executeContract(
|
||||
|
||||
describe("RestClient", () => {
|
||||
it("can be constructed", () => {
|
||||
const client = new RestClient(wasmdEndpoint);
|
||||
const client = new RestClient(wasmd.endpoint);
|
||||
expect(client).toBeTruthy();
|
||||
});
|
||||
|
||||
@ -175,7 +175,7 @@ describe("RestClient", () => {
|
||||
describe("authAccounts", () => {
|
||||
it("works for unused account without pubkey", async () => {
|
||||
pendingWithoutWasmd();
|
||||
const client = new RestClient(wasmdEndpoint);
|
||||
const client = new RestClient(wasmd.endpoint);
|
||||
const { height, result } = await client.authAccounts(unusedAccount.address);
|
||||
expect(height).toMatch(tendermintHeightMatcher);
|
||||
expect(result).toEqual({
|
||||
@ -202,7 +202,7 @@ describe("RestClient", () => {
|
||||
// This fails in the first test run if you forget to run `./scripts/wasmd/init.sh`
|
||||
it("has correct pubkey for faucet", async () => {
|
||||
pendingWithoutWasmd();
|
||||
const client = new RestClient(wasmdEndpoint);
|
||||
const client = new RestClient(wasmd.endpoint);
|
||||
const { result } = await client.authAccounts(faucet.address);
|
||||
expect(result.value).toEqual(
|
||||
jasmine.objectContaining({
|
||||
@ -214,7 +214,7 @@ describe("RestClient", () => {
|
||||
// This property is used by CosmWasmClient.getAccount
|
||||
it("returns empty address for non-existent account", async () => {
|
||||
pendingWithoutWasmd();
|
||||
const client = new RestClient(wasmdEndpoint);
|
||||
const client = new RestClient(wasmd.endpoint);
|
||||
const nonExistentAccount = makeRandomAddress();
|
||||
const { result } = await client.authAccounts(nonExistentAccount);
|
||||
expect(result).toEqual({
|
||||
@ -229,7 +229,7 @@ describe("RestClient", () => {
|
||||
describe("blocksLatest", () => {
|
||||
it("works", async () => {
|
||||
pendingWithoutWasmd();
|
||||
const client = new RestClient(wasmdEndpoint);
|
||||
const client = new RestClient(wasmd.endpoint);
|
||||
const response = await client.blocksLatest();
|
||||
|
||||
// id
|
||||
@ -262,7 +262,7 @@ describe("RestClient", () => {
|
||||
describe("blocks", () => {
|
||||
it("works for block by height", async () => {
|
||||
pendingWithoutWasmd();
|
||||
const client = new RestClient(wasmdEndpoint);
|
||||
const client = new RestClient(wasmd.endpoint);
|
||||
const height = parseInt((await client.blocksLatest()).block.header.height, 10);
|
||||
const response = await client.blocks(height - 1);
|
||||
|
||||
@ -298,7 +298,7 @@ describe("RestClient", () => {
|
||||
describe("nodeInfo", () => {
|
||||
it("works", async () => {
|
||||
pendingWithoutWasmd();
|
||||
const client = new RestClient(wasmdEndpoint);
|
||||
const client = new RestClient(wasmd.endpoint);
|
||||
const { node_info, application_version } = await client.nodeInfo();
|
||||
|
||||
expect(node_info).toEqual({
|
||||
@ -339,7 +339,7 @@ describe("RestClient", () => {
|
||||
beforeAll(async () => {
|
||||
if (wasmdEnabled()) {
|
||||
const pen = await Secp256k1Pen.fromMnemonic(faucet.mnemonic);
|
||||
const client = new SigningCosmWasmClient(wasmdEndpoint, faucet.address, signBytes =>
|
||||
const client = new SigningCosmWasmClient(wasmd.endpoint, faucet.address, signBytes =>
|
||||
pen.sign(signBytes),
|
||||
);
|
||||
|
||||
@ -353,7 +353,7 @@ describe("RestClient", () => {
|
||||
const result = await client.sendTokens(recipient, transferAmount);
|
||||
|
||||
await sleep(50); // wait until tx is indexed
|
||||
const txDetails = await new RestClient(wasmdEndpoint).txsById(result.transactionHash);
|
||||
const txDetails = await new RestClient(wasmd.endpoint).txsById(result.transactionHash);
|
||||
posted = {
|
||||
sender: faucet.address,
|
||||
recipient: recipient,
|
||||
@ -367,7 +367,7 @@ describe("RestClient", () => {
|
||||
it("can query transactions by height", async () => {
|
||||
pendingWithoutWasmd();
|
||||
assert(posted);
|
||||
const client = new RestClient(wasmdEndpoint);
|
||||
const client = new RestClient(wasmd.endpoint);
|
||||
const result = await client.txsQuery(`tx.height=${posted.height}&limit=26`);
|
||||
expect(parseInt(result.count, 10)).toEqual(1);
|
||||
expect(parseInt(result.limit, 10)).toEqual(26);
|
||||
@ -380,7 +380,7 @@ describe("RestClient", () => {
|
||||
it("can query transactions by ID", async () => {
|
||||
pendingWithoutWasmd();
|
||||
assert(posted);
|
||||
const client = new RestClient(wasmdEndpoint);
|
||||
const client = new RestClient(wasmd.endpoint);
|
||||
const result = await client.txsQuery(`tx.hash=${posted.hash}&limit=26`);
|
||||
expect(parseInt(result.count, 10)).toEqual(1);
|
||||
expect(parseInt(result.limit, 10)).toEqual(26);
|
||||
@ -393,7 +393,7 @@ describe("RestClient", () => {
|
||||
it("can query transactions by sender", async () => {
|
||||
pendingWithoutWasmd();
|
||||
assert(posted);
|
||||
const client = new RestClient(wasmdEndpoint);
|
||||
const client = new RestClient(wasmd.endpoint);
|
||||
const result = await client.txsQuery(`message.sender=${posted.sender}&limit=200`);
|
||||
expect(parseInt(result.count, 10)).toBeGreaterThanOrEqual(1);
|
||||
expect(parseInt(result.limit, 10)).toEqual(200);
|
||||
@ -407,7 +407,7 @@ describe("RestClient", () => {
|
||||
it("can query transactions by recipient", async () => {
|
||||
pendingWithoutWasmd();
|
||||
assert(posted);
|
||||
const client = new RestClient(wasmdEndpoint);
|
||||
const client = new RestClient(wasmd.endpoint);
|
||||
const result = await client.txsQuery(`transfer.recipient=${posted.recipient}&limit=200`);
|
||||
expect(parseInt(result.count, 10)).toEqual(1);
|
||||
expect(parseInt(result.limit, 10)).toEqual(200);
|
||||
@ -422,7 +422,7 @@ describe("RestClient", () => {
|
||||
pending("This combination is broken 🤷♂️. Handle client-side at higher level.");
|
||||
pendingWithoutWasmd();
|
||||
assert(posted);
|
||||
const client = new RestClient(wasmdEndpoint);
|
||||
const client = new RestClient(wasmd.endpoint);
|
||||
const hashQuery = `tx.hash=${posted.hash}`;
|
||||
|
||||
{
|
||||
@ -449,7 +449,7 @@ describe("RestClient", () => {
|
||||
it("can filter by recipient and tx.minheight", async () => {
|
||||
pendingWithoutWasmd();
|
||||
assert(posted);
|
||||
const client = new RestClient(wasmdEndpoint);
|
||||
const client = new RestClient(wasmd.endpoint);
|
||||
const recipientQuery = `transfer.recipient=${posted.recipient}`;
|
||||
|
||||
{
|
||||
@ -476,7 +476,7 @@ describe("RestClient", () => {
|
||||
it("can filter by recipient and tx.maxheight", async () => {
|
||||
pendingWithoutWasmd();
|
||||
assert(posted);
|
||||
const client = new RestClient(wasmdEndpoint);
|
||||
const client = new RestClient(wasmd.endpoint);
|
||||
const recipientQuery = `transfer.recipient=${posted.recipient}`;
|
||||
|
||||
{
|
||||
@ -503,7 +503,7 @@ describe("RestClient", () => {
|
||||
it("can query by tags (module + code_id)", async () => {
|
||||
pendingWithoutWasmd();
|
||||
assert(posted);
|
||||
const client = new RestClient(wasmdEndpoint);
|
||||
const client = new RestClient(wasmd.endpoint);
|
||||
const result = await client.txsQuery(`message.module=wasm&message.code_id=${deployedErc20.codeId}`);
|
||||
expect(parseInt(result.count, 10)).toBeGreaterThanOrEqual(4);
|
||||
|
||||
@ -549,7 +549,7 @@ describe("RestClient", () => {
|
||||
it("can query by tags (module + code_id + action)", async () => {
|
||||
pendingWithoutWasmd();
|
||||
assert(posted);
|
||||
const client = new RestClient(wasmdEndpoint);
|
||||
const client = new RestClient(wasmd.endpoint);
|
||||
|
||||
{
|
||||
const uploads = await client.txsQuery(
|
||||
@ -606,7 +606,7 @@ describe("RestClient", () => {
|
||||
describe("encodeTx", () => {
|
||||
it("works for cosmoshub example", async () => {
|
||||
pendingWithoutWasmd();
|
||||
const client = new RestClient(wasmdEndpoint);
|
||||
const client = new RestClient(wasmd.endpoint);
|
||||
expect(await client.encodeTx(cosmoshub.tx)).toEqual(fromBase64(cosmoshub.tx_data));
|
||||
});
|
||||
});
|
||||
@ -641,7 +641,7 @@ describe("RestClient", () => {
|
||||
gas: "890000",
|
||||
};
|
||||
|
||||
const client = new RestClient(wasmdEndpoint);
|
||||
const client = new RestClient(wasmd.endpoint);
|
||||
const { account_number, sequence } = (await client.authAccounts(faucet.address)).result.value;
|
||||
|
||||
const signBytes = makeSignBytes([theMsg], fee, defaultNetworkId, memo, account_number, sequence);
|
||||
@ -655,7 +655,7 @@ describe("RestClient", () => {
|
||||
it("can upload, instantiate and execute wasm", async () => {
|
||||
pendingWithoutWasmd();
|
||||
const pen = await Secp256k1Pen.fromMnemonic(faucet.mnemonic);
|
||||
const client = new RestClient(wasmdEndpoint);
|
||||
const client = new RestClient(wasmd.endpoint);
|
||||
|
||||
const transferAmount: readonly Coin[] = [
|
||||
{
|
||||
@ -729,7 +729,7 @@ describe("RestClient", () => {
|
||||
it("can list upload code", async () => {
|
||||
pendingWithoutWasmd();
|
||||
const pen = await Secp256k1Pen.fromMnemonic(faucet.mnemonic);
|
||||
const client = new RestClient(wasmdEndpoint);
|
||||
const client = new RestClient(wasmd.endpoint);
|
||||
|
||||
// check with contracts were here first to compare
|
||||
const existingInfos = await client.listCodeInfo();
|
||||
@ -769,7 +769,7 @@ describe("RestClient", () => {
|
||||
it("can list contracts and get info", async () => {
|
||||
pendingWithoutWasmd();
|
||||
const pen = await Secp256k1Pen.fromMnemonic(faucet.mnemonic);
|
||||
const client = new RestClient(wasmdEndpoint);
|
||||
const client = new RestClient(wasmd.endpoint);
|
||||
const beneficiaryAddress = makeRandomAddress();
|
||||
const transferAmount: readonly Coin[] = [
|
||||
{
|
||||
@ -830,7 +830,7 @@ describe("RestClient", () => {
|
||||
});
|
||||
|
||||
describe("contract state", () => {
|
||||
const client = new RestClient(wasmdEndpoint);
|
||||
const client = new RestClient(wasmd.endpoint);
|
||||
const noContract = makeRandomAddress();
|
||||
const expectedKey = toAscii("config");
|
||||
let contractAddress: string | undefined;
|
||||
|
||||
@ -144,7 +144,7 @@ export class SigningCosmWasmClient extends CosmWasmClient {
|
||||
};
|
||||
const fee = this.fees.upload;
|
||||
const { accountNumber, sequence } = await this.getNonce();
|
||||
const chainId = await this.chainId();
|
||||
const chainId = await this.getChainId();
|
||||
const signBytes = makeSignBytes([storeCodeMsg], fee, chainId, memo, accountNumber, sequence);
|
||||
const signature = await this.signCallback(signBytes);
|
||||
const signedTx = {
|
||||
@ -189,7 +189,7 @@ export class SigningCosmWasmClient extends CosmWasmClient {
|
||||
};
|
||||
const fee = this.fees.init;
|
||||
const { accountNumber, sequence } = await this.getNonce();
|
||||
const chainId = await this.chainId();
|
||||
const chainId = await this.getChainId();
|
||||
const signBytes = makeSignBytes([instantiateMsg], fee, chainId, memo, accountNumber, sequence);
|
||||
|
||||
const signature = await this.signCallback(signBytes);
|
||||
@ -227,7 +227,7 @@ export class SigningCosmWasmClient extends CosmWasmClient {
|
||||
};
|
||||
const fee = this.fees.exec;
|
||||
const { accountNumber, sequence } = await this.getNonce();
|
||||
const chainId = await this.chainId();
|
||||
const chainId = await this.getChainId();
|
||||
const signBytes = makeSignBytes([executeMsg], fee, chainId, memo, accountNumber, sequence);
|
||||
const signature = await this.signCallback(signBytes);
|
||||
const signedTx = {
|
||||
@ -261,7 +261,7 @@ export class SigningCosmWasmClient extends CosmWasmClient {
|
||||
};
|
||||
const fee = this.fees.send;
|
||||
const { accountNumber, sequence } = await this.getNonce();
|
||||
const chainId = await this.chainId();
|
||||
const chainId = await this.getChainId();
|
||||
const signBytes = makeSignBytes([sendMsg], fee, chainId, memo, accountNumber, sequence);
|
||||
const signature = await this.signCallback(signBytes);
|
||||
const signedTx = {
|
||||
|
||||
@ -78,7 +78,10 @@ export const deployedErc20 = {
|
||||
],
|
||||
};
|
||||
|
||||
export const wasmdEndpoint = "http://localhost:1317";
|
||||
export const wasmd = {
|
||||
endpoint: "http://localhost:1317",
|
||||
expectedChainId: "testing",
|
||||
};
|
||||
|
||||
export const faucet = {
|
||||
mnemonic:
|
||||
|
||||
3
packages/sdk/types/cosmwasmclient.d.ts
vendored
3
packages/sdk/types/cosmwasmclient.d.ts
vendored
@ -112,8 +112,9 @@ export declare class CosmWasmClient {
|
||||
/** Any address the chain considers valid (valid bech32 with proper prefix) */
|
||||
protected anyValidAddress: string | undefined;
|
||||
private readonly codesCache;
|
||||
private chainId;
|
||||
constructor(url: string, broadcastMode?: BroadcastMode);
|
||||
chainId(): Promise<string>;
|
||||
getChainId(): Promise<string>;
|
||||
getHeight(): Promise<number>;
|
||||
/**
|
||||
* Returns a 32 byte upper-case hex transaction hash (typically used as the transaction ID)
|
||||
|
||||
Loading…
Reference in New Issue
Block a user