feat: return null when account info not found
This commit is contained in:
committed by
Michael Vines
parent
0fcb1f6f56
commit
7989c10b7a
4
web3.js/module.d.ts
vendored
4
web3.js/module.d.ts
vendored
@ -163,11 +163,11 @@ declare module '@solana/web3.js' {
|
||||
getAccountInfoAndContext(
|
||||
publicKey: PublicKey,
|
||||
commitment?: Commitment,
|
||||
): Promise<RpcResponseAndContext<AccountInfo>>;
|
||||
): Promise<RpcResponseAndContext<AccountInfo | null>>;
|
||||
getAccountInfo(
|
||||
publicKey: PublicKey,
|
||||
commitment?: Commitment,
|
||||
): Promise<AccountInfo>;
|
||||
): Promise<AccountInfo | null>;
|
||||
getProgramAccounts(
|
||||
programId: PublicKey,
|
||||
commitment?: Commitment,
|
||||
|
@ -176,11 +176,11 @@ declare module '@solana/web3.js' {
|
||||
getAccountInfoAndContext(
|
||||
publicKey: PublicKey,
|
||||
commitment: ?Commitment,
|
||||
): Promise<RpcResponseAndContext<AccountInfo>>;
|
||||
): Promise<RpcResponseAndContext<AccountInfo | null>>;
|
||||
getAccountInfo(
|
||||
publicKey: PublicKey,
|
||||
commitment: ?Commitment,
|
||||
): Promise<AccountInfo>;
|
||||
): Promise<AccountInfo | null>;
|
||||
getProgramAccounts(
|
||||
programId: PublicKey,
|
||||
commitment: ?Commitment,
|
||||
|
@ -866,7 +866,7 @@ export class Connection {
|
||||
async getAccountInfoAndContext(
|
||||
publicKey: PublicKey,
|
||||
commitment: ?Commitment,
|
||||
): Promise<RpcResponseAndContext<AccountInfo>> {
|
||||
): Promise<RpcResponseAndContext<AccountInfo | null>> {
|
||||
const args = this._argsWithCommitment([publicKey.toBase58()], commitment);
|
||||
const unsafeRes = await this._rpcRequest('getAccountInfo', args);
|
||||
const res = GetAccountInfoAndContextRpcResult(unsafeRes);
|
||||
@ -875,18 +875,17 @@ export class Connection {
|
||||
}
|
||||
assert(typeof res.result !== 'undefined');
|
||||
|
||||
if (!res.result.value) {
|
||||
throw new Error('Invalid request');
|
||||
let value = null;
|
||||
if (res.result.value) {
|
||||
const {executable, owner, lamports, data} = res.result.value;
|
||||
value = {
|
||||
executable,
|
||||
owner: new PublicKey(owner),
|
||||
lamports,
|
||||
data: bs58.decode(data),
|
||||
};
|
||||
}
|
||||
|
||||
const {executable, owner, lamports, data} = res.result.value;
|
||||
const value = {
|
||||
executable,
|
||||
owner: new PublicKey(owner),
|
||||
lamports,
|
||||
data: bs58.decode(data),
|
||||
};
|
||||
|
||||
return {
|
||||
context: {
|
||||
slot: res.result.context.slot,
|
||||
@ -901,7 +900,7 @@ export class Connection {
|
||||
async getAccountInfo(
|
||||
publicKey: PublicKey,
|
||||
commitment: ?Commitment,
|
||||
): Promise<AccountInfo> {
|
||||
): Promise<AccountInfo | null> {
|
||||
return await this.getAccountInfoAndContext(publicKey, commitment)
|
||||
.then(x => x.value)
|
||||
.catch(e => {
|
||||
@ -1235,29 +1234,19 @@ export class Connection {
|
||||
async getNonceAndContext(
|
||||
nonceAccount: PublicKey,
|
||||
commitment: ?Commitment,
|
||||
): Promise<RpcResponseAndContext<NonceAccount>> {
|
||||
const args = this._argsWithCommitment(
|
||||
[nonceAccount.toBase58()],
|
||||
): Promise<RpcResponseAndContext<NonceAccount | null>> {
|
||||
const {context, value: accountInfo} = await this.getAccountInfoAndContext(
|
||||
nonceAccount,
|
||||
commitment,
|
||||
);
|
||||
const unsafeRes = await this._rpcRequest('getAccountInfo', args);
|
||||
const res = GetAccountInfoAndContextRpcResult(unsafeRes);
|
||||
if (res.error) {
|
||||
throw new Error(res.error.message);
|
||||
}
|
||||
assert(typeof res.result !== 'undefined');
|
||||
if (!res.result.value) {
|
||||
throw new Error('Invalid request');
|
||||
}
|
||||
|
||||
const value = NonceAccount.fromAccountData(
|
||||
bs58.decode(res.result.value.data),
|
||||
);
|
||||
let value = null;
|
||||
if (accountInfo !== null) {
|
||||
value = NonceAccount.fromAccountData(accountInfo.data);
|
||||
}
|
||||
|
||||
return {
|
||||
context: {
|
||||
slot: res.result.context.slot,
|
||||
},
|
||||
context,
|
||||
value,
|
||||
};
|
||||
}
|
||||
@ -1268,7 +1257,7 @@ export class Connection {
|
||||
async getNonce(
|
||||
nonceAccount: PublicKey,
|
||||
commitment: ?Commitment,
|
||||
): Promise<NonceAccount> {
|
||||
): Promise<NonceAccount | null> {
|
||||
return await this.getNonceAndContext(nonceAccount, commitment)
|
||||
.then(x => x.value)
|
||||
.catch(e => {
|
||||
|
@ -25,7 +25,7 @@ const errorResponse = {
|
||||
result: undefined,
|
||||
};
|
||||
|
||||
test('get account info - error', () => {
|
||||
test('get account info - not found', async () => {
|
||||
const account = new Account();
|
||||
const connection = new Connection(url);
|
||||
|
||||
@ -35,12 +35,18 @@ test('get account info - error', () => {
|
||||
method: 'getAccountInfo',
|
||||
params: [account.publicKey.toBase58()],
|
||||
},
|
||||
errorResponse,
|
||||
{
|
||||
error: null,
|
||||
result: {
|
||||
context: {
|
||||
slot: 11,
|
||||
},
|
||||
value: null,
|
||||
},
|
||||
},
|
||||
]);
|
||||
|
||||
return expect(connection.getAccountInfo(account.publicKey)).rejects.toThrow(
|
||||
errorMessage,
|
||||
);
|
||||
expect(await connection.getAccountInfo(account.publicKey)).toBeNull();
|
||||
});
|
||||
|
||||
test('get program accounts', async () => {
|
||||
@ -820,6 +826,10 @@ test('request airdrop', async () => {
|
||||
]);
|
||||
|
||||
const accountInfo = await connection.getAccountInfo(account.publicKey);
|
||||
if (accountInfo === null) {
|
||||
expect(accountInfo).not.toBeNull();
|
||||
return;
|
||||
}
|
||||
expect(accountInfo.lamports).toBe(minimumAmount + 42);
|
||||
expect(accountInfo.data).toHaveLength(0);
|
||||
expect(accountInfo.owner).toEqual(SystemProgram.programId);
|
||||
|
@ -129,6 +129,10 @@ test('create and query nonce account', async () => {
|
||||
nonceAccount.publicKey,
|
||||
'recent',
|
||||
);
|
||||
if (nonceAccountData === null) {
|
||||
expect(nonceAccountData).not.toBeNull();
|
||||
return;
|
||||
}
|
||||
expect(nonceAccountData.authorizedPubkey).toEqual(from.publicKey);
|
||||
expect(bs58.decode(nonceAccountData.nonce).length).toBeGreaterThan(30);
|
||||
});
|
||||
|
@ -224,7 +224,17 @@ test('live Nonce actions', async () => {
|
||||
expect(nonceBalance).toEqual(minimumAmount);
|
||||
|
||||
const nonceQuery1 = await connection.getNonce(nonceAccount.publicKey);
|
||||
if (nonceQuery1 === null) {
|
||||
expect(nonceQuery1).not.toBeNull();
|
||||
return;
|
||||
}
|
||||
|
||||
const nonceQuery2 = await connection.getNonce(nonceAccount.publicKey);
|
||||
if (nonceQuery2 === null) {
|
||||
expect(nonceQuery2).not.toBeNull();
|
||||
return;
|
||||
}
|
||||
|
||||
expect(nonceQuery1.nonce).toEqual(nonceQuery2.nonce);
|
||||
|
||||
// Wait for blockhash to advance
|
||||
@ -238,6 +248,10 @@ test('live Nonce actions', async () => {
|
||||
);
|
||||
await sendAndConfirmRecentTransaction(connection, advanceNonce, from);
|
||||
const nonceQuery3 = await connection.getNonce(nonceAccount.publicKey);
|
||||
if (nonceQuery3 === null) {
|
||||
expect(nonceQuery3).not.toBeNull();
|
||||
return;
|
||||
}
|
||||
expect(nonceQuery1.nonce).not.toEqual(nonceQuery3.nonce);
|
||||
const nonce = nonceQuery3.nonce;
|
||||
|
||||
|
Reference in New Issue
Block a user