Skip to content

Commit

Permalink
Merge branch 'identity-com-bugfix/robust-fetchNullable' of github.com…
Browse files Browse the repository at this point in the history
…:Henry-E/anchor into Henry-E-identity-com-bugfix/robust-fetchNullable
  • Loading branch information
henrye committed Dec 5, 2022
2 parents 98f26ce + 207241e commit 18f361a
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ The minor version will be incremented upon a breaking change and the patch versi
- ts: Update seeds inference to allow nested user defined structs within the seeds ([#2198](https://github.com/coral-xyz/anchor/pull/2198))
- 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: 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
4 changes: 2 additions & 2 deletions ts/packages/anchor/src/program/namespace/account.ts
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ export class AccountClient<
);
const { value, context } = accountInfo;
return {
data: value
data: value && value.data.length !== 0
? this._coder.accounts.decode<T>(this._idlAccount.name, value.data)
: null,
context,
Expand All @@ -169,7 +169,7 @@ 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

0 comments on commit 18f361a

Please sign in to comment.