diff --git a/web3.js/module.d.ts b/web3.js/module.d.ts index fbeba9eae8..86fdb5b93e 100644 --- a/web3.js/module.d.ts +++ b/web3.js/module.d.ts @@ -163,11 +163,11 @@ declare module '@solana/web3.js' { getAccountInfoAndContext( publicKey: PublicKey, commitment?: Commitment, - ): Promise>; + ): Promise>; getAccountInfo( publicKey: PublicKey, commitment?: Commitment, - ): Promise; + ): Promise; getProgramAccounts( programId: PublicKey, commitment?: Commitment, diff --git a/web3.js/module.flow.js b/web3.js/module.flow.js index 92f0245810..6484ccd8ab 100644 --- a/web3.js/module.flow.js +++ b/web3.js/module.flow.js @@ -176,11 +176,11 @@ declare module '@solana/web3.js' { getAccountInfoAndContext( publicKey: PublicKey, commitment: ?Commitment, - ): Promise>; + ): Promise>; getAccountInfo( publicKey: PublicKey, commitment: ?Commitment, - ): Promise; + ): Promise; getProgramAccounts( programId: PublicKey, commitment: ?Commitment, diff --git a/web3.js/src/connection.js b/web3.js/src/connection.js index 83a5771d33..e541ed779e 100644 --- a/web3.js/src/connection.js +++ b/web3.js/src/connection.js @@ -866,7 +866,7 @@ export class Connection { async getAccountInfoAndContext( publicKey: PublicKey, commitment: ?Commitment, - ): Promise> { + ): Promise> { 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 { + ): Promise { 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> { - const args = this._argsWithCommitment( - [nonceAccount.toBase58()], + ): Promise> { + 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 { + ): Promise { return await this.getNonceAndContext(nonceAccount, commitment) .then(x => x.value) .catch(e => { diff --git a/web3.js/test/connection.test.js b/web3.js/test/connection.test.js index 0bb283518b..f89ab87cce 100644 --- a/web3.js/test/connection.test.js +++ b/web3.js/test/connection.test.js @@ -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); diff --git a/web3.js/test/nonce.test.js b/web3.js/test/nonce.test.js index 45b4879bd3..12214ea2ee 100644 --- a/web3.js/test/nonce.test.js +++ b/web3.js/test/nonce.test.js @@ -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); }); diff --git a/web3.js/test/system-program.test.js b/web3.js/test/system-program.test.js index cb90410207..f30024d831 100644 --- a/web3.js/test/system-program.test.js +++ b/web3.js/test/system-program.test.js @@ -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;