getAccount returns undefined when address not found

This commit is contained in:
Ethan Frey 2020-02-16 19:13:53 +01:00 committed by Simon Warta
parent b39a54211b
commit d079f7673a
3 changed files with 24 additions and 8 deletions

View File

@ -74,7 +74,15 @@ describe("CosmWasmClient", () => {
});
});
it("returns zeros for missing accounts", async () => {
pendingWithoutCosmos();
const client = CosmWasmClient.makeReadOnly(httpUrl);
const missing = makeRandomAddress();
expect(await client.getNonce(missing)).toEqual({
accountNumber: 0,
sequence: 0,
});
});
});
describe("getAccount", () => {
@ -87,11 +95,18 @@ describe("CosmWasmClient", () => {
sequence: 0,
public_key: "",
coins: [
{denom: 'ucosm', amount: '1000000000'},
{denom: 'ustake', amount: '1000000000'},
{ denom: "ucosm", amount: "1000000000" },
{ denom: "ustake", amount: "1000000000" },
],
});
});
it("returns undefined for missing accounts", async () => {
pendingWithoutCosmos();
const client = CosmWasmClient.makeReadOnly(httpUrl);
const missing = makeRandomAddress();
expect(await client.getAccount(missing)).toBeUndefined();
});
});
describe("getBlock", () => {

View File

@ -153,14 +153,15 @@ export class CosmWasmClient {
public async getNonce(address?: string): Promise<GetNonceResult> {
const account = await this.getAccount(address);
return {
accountNumber: account.account_number,
sequence: account.sequence,
accountNumber: account ? account.account_number : 0,
sequence: account ? account.sequence : 0,
};
}
public async getAccount(address?: string): Promise<CosmosSdkAccount> {
public async getAccount(address?: string): Promise<CosmosSdkAccount | undefined> {
const account = await this.restClient.authAccounts(address || this.senderAddress);
return account.result.value;
const value = account.result.value;
return value.address === "" ? undefined : value;
}
/**

View File

@ -46,7 +46,7 @@ export declare class CosmWasmClient {
* @param address returns data for this address. When unset, the client's sender adddress is used.
*/
getNonce(address?: string): Promise<GetNonceResult>;
getAccount(address?: string): Promise<CosmosSdkAccount>;
getAccount(address?: string): Promise<CosmosSdkAccount | undefined>;
/**
* Gets block header and meta
*