feat: return null when account info not found

This commit is contained in:
Justin Starry
2020-04-05 16:18:45 +08:00
committed by Michael Vines
parent 0fcb1f6f56
commit 7989c10b7a
6 changed files with 57 additions and 40 deletions

4
web3.js/module.d.ts vendored
View File

@ -163,11 +163,11 @@ declare module '@solana/web3.js' {
getAccountInfoAndContext( getAccountInfoAndContext(
publicKey: PublicKey, publicKey: PublicKey,
commitment?: Commitment, commitment?: Commitment,
): Promise<RpcResponseAndContext<AccountInfo>>; ): Promise<RpcResponseAndContext<AccountInfo | null>>;
getAccountInfo( getAccountInfo(
publicKey: PublicKey, publicKey: PublicKey,
commitment?: Commitment, commitment?: Commitment,
): Promise<AccountInfo>; ): Promise<AccountInfo | null>;
getProgramAccounts( getProgramAccounts(
programId: PublicKey, programId: PublicKey,
commitment?: Commitment, commitment?: Commitment,

View File

@ -176,11 +176,11 @@ declare module '@solana/web3.js' {
getAccountInfoAndContext( getAccountInfoAndContext(
publicKey: PublicKey, publicKey: PublicKey,
commitment: ?Commitment, commitment: ?Commitment,
): Promise<RpcResponseAndContext<AccountInfo>>; ): Promise<RpcResponseAndContext<AccountInfo | null>>;
getAccountInfo( getAccountInfo(
publicKey: PublicKey, publicKey: PublicKey,
commitment: ?Commitment, commitment: ?Commitment,
): Promise<AccountInfo>; ): Promise<AccountInfo | null>;
getProgramAccounts( getProgramAccounts(
programId: PublicKey, programId: PublicKey,
commitment: ?Commitment, commitment: ?Commitment,

View File

@ -866,7 +866,7 @@ export class Connection {
async getAccountInfoAndContext( async getAccountInfoAndContext(
publicKey: PublicKey, publicKey: PublicKey,
commitment: ?Commitment, commitment: ?Commitment,
): Promise<RpcResponseAndContext<AccountInfo>> { ): Promise<RpcResponseAndContext<AccountInfo | null>> {
const args = this._argsWithCommitment([publicKey.toBase58()], commitment); const args = this._argsWithCommitment([publicKey.toBase58()], commitment);
const unsafeRes = await this._rpcRequest('getAccountInfo', args); const unsafeRes = await this._rpcRequest('getAccountInfo', args);
const res = GetAccountInfoAndContextRpcResult(unsafeRes); const res = GetAccountInfoAndContextRpcResult(unsafeRes);
@ -875,18 +875,17 @@ export class Connection {
} }
assert(typeof res.result !== 'undefined'); assert(typeof res.result !== 'undefined');
if (!res.result.value) { let value = null;
throw new Error('Invalid request'); 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 { return {
context: { context: {
slot: res.result.context.slot, slot: res.result.context.slot,
@ -901,7 +900,7 @@ export class Connection {
async getAccountInfo( async getAccountInfo(
publicKey: PublicKey, publicKey: PublicKey,
commitment: ?Commitment, commitment: ?Commitment,
): Promise<AccountInfo> { ): Promise<AccountInfo | null> {
return await this.getAccountInfoAndContext(publicKey, commitment) return await this.getAccountInfoAndContext(publicKey, commitment)
.then(x => x.value) .then(x => x.value)
.catch(e => { .catch(e => {
@ -1235,29 +1234,19 @@ export class Connection {
async getNonceAndContext( async getNonceAndContext(
nonceAccount: PublicKey, nonceAccount: PublicKey,
commitment: ?Commitment, commitment: ?Commitment,
): Promise<RpcResponseAndContext<NonceAccount>> { ): Promise<RpcResponseAndContext<NonceAccount | null>> {
const args = this._argsWithCommitment( const {context, value: accountInfo} = await this.getAccountInfoAndContext(
[nonceAccount.toBase58()], nonceAccount,
commitment, 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( let value = null;
bs58.decode(res.result.value.data), if (accountInfo !== null) {
); value = NonceAccount.fromAccountData(accountInfo.data);
}
return { return {
context: { context,
slot: res.result.context.slot,
},
value, value,
}; };
} }
@ -1268,7 +1257,7 @@ export class Connection {
async getNonce( async getNonce(
nonceAccount: PublicKey, nonceAccount: PublicKey,
commitment: ?Commitment, commitment: ?Commitment,
): Promise<NonceAccount> { ): Promise<NonceAccount | null> {
return await this.getNonceAndContext(nonceAccount, commitment) return await this.getNonceAndContext(nonceAccount, commitment)
.then(x => x.value) .then(x => x.value)
.catch(e => { .catch(e => {

View File

@ -25,7 +25,7 @@ const errorResponse = {
result: undefined, result: undefined,
}; };
test('get account info - error', () => { test('get account info - not found', async () => {
const account = new Account(); const account = new Account();
const connection = new Connection(url); const connection = new Connection(url);
@ -35,12 +35,18 @@ test('get account info - error', () => {
method: 'getAccountInfo', method: 'getAccountInfo',
params: [account.publicKey.toBase58()], params: [account.publicKey.toBase58()],
}, },
errorResponse, {
error: null,
result: {
context: {
slot: 11,
},
value: null,
},
},
]); ]);
return expect(connection.getAccountInfo(account.publicKey)).rejects.toThrow( expect(await connection.getAccountInfo(account.publicKey)).toBeNull();
errorMessage,
);
}); });
test('get program accounts', async () => { test('get program accounts', async () => {
@ -820,6 +826,10 @@ test('request airdrop', async () => {
]); ]);
const accountInfo = await connection.getAccountInfo(account.publicKey); const accountInfo = await connection.getAccountInfo(account.publicKey);
if (accountInfo === null) {
expect(accountInfo).not.toBeNull();
return;
}
expect(accountInfo.lamports).toBe(minimumAmount + 42); expect(accountInfo.lamports).toBe(minimumAmount + 42);
expect(accountInfo.data).toHaveLength(0); expect(accountInfo.data).toHaveLength(0);
expect(accountInfo.owner).toEqual(SystemProgram.programId); expect(accountInfo.owner).toEqual(SystemProgram.programId);

View File

@ -129,6 +129,10 @@ test('create and query nonce account', async () => {
nonceAccount.publicKey, nonceAccount.publicKey,
'recent', 'recent',
); );
if (nonceAccountData === null) {
expect(nonceAccountData).not.toBeNull();
return;
}
expect(nonceAccountData.authorizedPubkey).toEqual(from.publicKey); expect(nonceAccountData.authorizedPubkey).toEqual(from.publicKey);
expect(bs58.decode(nonceAccountData.nonce).length).toBeGreaterThan(30); expect(bs58.decode(nonceAccountData.nonce).length).toBeGreaterThan(30);
}); });

View File

@ -224,7 +224,17 @@ test('live Nonce actions', async () => {
expect(nonceBalance).toEqual(minimumAmount); expect(nonceBalance).toEqual(minimumAmount);
const nonceQuery1 = await connection.getNonce(nonceAccount.publicKey); const nonceQuery1 = await connection.getNonce(nonceAccount.publicKey);
if (nonceQuery1 === null) {
expect(nonceQuery1).not.toBeNull();
return;
}
const nonceQuery2 = await connection.getNonce(nonceAccount.publicKey); const nonceQuery2 = await connection.getNonce(nonceAccount.publicKey);
if (nonceQuery2 === null) {
expect(nonceQuery2).not.toBeNull();
return;
}
expect(nonceQuery1.nonce).toEqual(nonceQuery2.nonce); expect(nonceQuery1.nonce).toEqual(nonceQuery2.nonce);
// Wait for blockhash to advance // Wait for blockhash to advance
@ -238,6 +248,10 @@ test('live Nonce actions', async () => {
); );
await sendAndConfirmRecentTransaction(connection, advanceNonce, from); await sendAndConfirmRecentTransaction(connection, advanceNonce, from);
const nonceQuery3 = await connection.getNonce(nonceAccount.publicKey); const nonceQuery3 = await connection.getNonce(nonceAccount.publicKey);
if (nonceQuery3 === null) {
expect(nonceQuery3).not.toBeNull();
return;
}
expect(nonceQuery1.nonce).not.toEqual(nonceQuery3.nonce); expect(nonceQuery1.nonce).not.toEqual(nonceQuery3.nonce);
const nonce = nonceQuery3.nonce; const nonce = nonceQuery3.nonce;