cosmwasm-stargate: Adjust CosmWasmClient.getSequence

This commit is contained in:
willclarktech 2021-03-30 17:06:38 +02:00
parent 68dc0b45cf
commit 8b7ef45f3a
No known key found for this signature in database
GPG Key ID: 551A86E2E398ADF7
3 changed files with 31 additions and 30 deletions

View File

@ -82,24 +82,6 @@ describe("CosmWasmClient", () => {
});
});
describe("getSequence", () => {
it("works", async () => {
pendingWithoutWasmd();
const client = await CosmWasmClient.connect(wasmd.endpoint);
expect(await client.getSequence(unused.address)).toEqual({
accountNumber: unused.accountNumber,
sequence: unused.sequence,
});
});
it("returns null for missing accounts", async () => {
pendingWithoutWasmd();
const client = await CosmWasmClient.connect(wasmd.endpoint);
const missing = makeRandomAddress();
expect(await client.getSequence(missing)).toBeNull();
});
});
describe("getAccount", () => {
it("works", async () => {
pendingWithoutWasmd();
@ -120,6 +102,26 @@ describe("CosmWasmClient", () => {
});
});
describe("getSequence", () => {
it("works", async () => {
pendingWithoutWasmd();
const client = await CosmWasmClient.connect(wasmd.endpoint);
expect(await client.getSequence(unused.address)).toEqual({
accountNumber: unused.accountNumber,
sequence: unused.sequence,
});
});
it("rejects for missing accounts", async () => {
pendingWithoutWasmd();
const client = await CosmWasmClient.connect(wasmd.endpoint);
const missing = makeRandomAddress();
await expectAsync(client.getSequence(missing)).toBeRejectedWithError(
/account does not exist on chain/i,
);
});
});
describe("getBlock", () => {
it("works for latest block", async () => {
pendingWithoutWasmd();

View File

@ -120,14 +120,17 @@ export class CosmWasmClient {
}
}
public async getSequence(address: string): Promise<SequenceResponse | null> {
public async getSequence(address: string): Promise<SequenceResponse> {
const account = await this.getAccount(address);
return account
? {
accountNumber: account.accountNumber,
sequence: account.sequence,
}
: null;
if (!account) {
throw new Error(
"Account does not exist on chain. Send some tokens there before trying to query sequence.",
);
}
return {
accountNumber: account.accountNumber,
sequence: account.sequence,
};
}
public async getBlock(height?: number): Promise<Block> {

View File

@ -409,11 +409,7 @@ export class SigningCosmWasmClient extends CosmWasmClient {
throw new Error("Failed to retrieve account from signer");
}
const pubkey = encodePubkey(encodeSecp256k1Pubkey(accountFromSigner.pubkey));
const accountFromChain = await this.getAccount(signerAddress);
if (!accountFromChain) {
throw new Error("Account not found");
}
const { accountNumber, sequence } = accountFromChain;
const { accountNumber, sequence } = await this.getSequence(signerAddress);
const chainId = await this.getChainId();
const txBody = {
messages: messages,