Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Identity com bugfix/robust fetch nullable #2301

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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