Skip to content

Commit

Permalink
Identity com bugfix/robust fetch nullable (#2301)
Browse files Browse the repository at this point in the history
* ts: Fixed `.fetchNullable()` to be robust towards accounts only holding a balance

* update changelog to new PR id

* prettier

Co-authored-by: Martin Riedel <web@riedel-it.de>
Co-authored-by: henrye <henry@notanemail>
  • Loading branch information
3 people authored Dec 7, 2022
1 parent 7d7747c commit 09b829d
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 4 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ The minor version will be incremented upon a breaking change and the patch versi
- event: Fix multiple event listeners with the same name ([#2165](https://github.com/coral-xyz/anchor/pull/2165)).
- lang: Prevent the payer account from being initialized as a program account ([#2284](https://github.com/coral-xyz/anchor/pull/2284)).
- ts: Fixing breaking change where null or undefined wallet throws an error ([#2303](https://github.com/coral-xyz/anchor/pull/2303)).
- ts: Fixed `.fetchNullable()` to be robust towards accounts only holding a balance ([#2301](https://github.com/coral-xyz/anchor/pull/2301)).

### Breaking

Expand Down
14 changes: 14 additions & 0 deletions tests/misc/tests/misc/misc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -787,6 +787,20 @@ describe("misc", () => {
assert.strictEqual(account2.idata.toNumber(), 3);
});

it("Can use fetchNullable() on accounts with only a balance", async () => {
const account = anchor.web3.Keypair.generate();

// Airdrop 1 SOL to the account.
const signature = await program.provider.connection.requestAirdrop(
account.publicKey,
anchor.web3.LAMPORTS_PER_SOL
);
await program.provider.connection.confirmTransaction(signature);

const data = await program.account.data.fetchNullable(account.publicKey);
assert.isNull(data);
});

describe("associated_token constraints", () => {
let associatedToken = null;
// apparently cannot await here so doing it in the 'it' statements
Expand Down
11 changes: 7 additions & 4 deletions ts/packages/anchor/src/program/namespace/account.ts
Original file line number Diff line number Diff line change
Expand Up @@ -154,9 +154,10 @@ export class AccountClient<
);
const { value, context } = accountInfo;
return {
data: value
? this._coder.accounts.decode<T>(this._idlAccount.name, value.data)
: null,
data:
value && value.data.length !== 0
? this._coder.accounts.decode<T>(this._idlAccount.name, value.data)
: null,
context,
};
}
Expand All @@ -169,7 +170,9 @@ export class AccountClient<
async fetch(address: Address, commitment?: Commitment): Promise<T> {
const { data } = await this.fetchNullableAndContext(address, commitment);
if (data === null) {
throw new Error(`Account does not exist ${address.toString()}`);
throw new Error(
`Account does not exist or has no data ${address.toString()}`
);
}
return data;
}
Expand Down

1 comment on commit 09b829d

@vercel
Copy link

@vercel vercel bot commented on 09b829d Dec 7, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

anchor-docs – ./

anchor-docs-git-master-200ms.vercel.app
anchor-docs-200ms.vercel.app
www.anchor-lang.com
anchor-lang.com

Please sign in to comment.