Let CosmWasmClient.getNonce throw

This commit is contained in:
Simon Warta 2020-02-17 12:31:59 +01:00
parent 03f01f54d6
commit 28ee35217e
3 changed files with 17 additions and 8 deletions

View File

@ -74,14 +74,14 @@ describe("CosmWasmClient", () => {
});
});
it("returns zeros for missing accounts", async () => {
it("throws for missing accounts", async () => {
pendingWithoutCosmos();
const client = CosmWasmClient.makeReadOnly(httpUrl);
const missing = makeRandomAddress();
expect(await client.getNonce(missing)).toEqual({
accountNumber: 0,
sequence: 0,
});
await client.getNonce(missing).then(
() => fail("this must not succeed"),
error => expect(error).toMatch(/account does not exist on chain/i),
);
});
});

View File

@ -148,13 +148,20 @@ export class CosmWasmClient {
/**
* Returns account number and sequence.
*
* Throws if the account does not exist on chain.
*
* @param address returns data for this address. When unset, the client's sender adddress is used.
*/
public async getNonce(address?: string): Promise<GetNonceResult> {
const account = await this.getAccount(address);
if (!account) {
throw new Error(
"Account does not exist on chain. Send some tokens there before trying to query nonces.",
);
}
return {
accountNumber: account ? account.account_number : 0,
sequence: account ? account.sequence : 0,
accountNumber: account.account_number,
sequence: account.sequence,
};
}

View File

@ -1,6 +1,6 @@
import { Log } from "./logs";
import { BlockResponse, TxsResponse } from "./restclient";
import { Coin, CosmosSdkTx, StdSignature, CosmosSdkAccount } from "./types";
import { Coin, CosmosSdkAccount, CosmosSdkTx, StdSignature } from "./types";
export interface SigningCallback {
(signBytes: Uint8Array): Promise<StdSignature>;
}
@ -43,6 +43,8 @@ export declare class CosmWasmClient {
/**
* Returns account number and sequence.
*
* Throws if the account does not exist on chain.
*
* @param address returns data for this address. When unset, the client's sender adddress is used.
*/
getNonce(address?: string): Promise<GetNonceResult>;