From 507fac06eea014bd7cbe2e4c9f2fc741ef246ea0 Mon Sep 17 00:00:00 2001 From: Tyera Eulberg Date: Wed, 10 Apr 2019 14:40:49 -0700 Subject: [PATCH] fix: Appease flow --- web3.js/module.flow.js | 14 ++++----- web3.js/src/connection.js | 31 ++++++++++--------- .../util/send-and-confirm-raw-transaction.js | 10 +++--- .../src/util/send-and-confirm-transaction.js | 10 +++--- web3.js/test/connection.test.js | 14 ++++----- web3.js/test/token-program.test.js | 6 ++-- 6 files changed, 44 insertions(+), 41 deletions(-) diff --git a/web3.js/module.flow.js b/web3.js/module.flow.js index 311bbdb42d..3e03c3e80e 100644 --- a/web3.js/module.flow.js +++ b/web3.js/module.flow.js @@ -53,12 +53,12 @@ declare module '@solana/web3.js' { keyedAccountInfo: KeyedAccountInfo, ) => void; - declare export type SignatureStatus = - | 'Confirmed' - | 'AccountInUse' - | 'SignatureNotFound' - | 'ProgramRuntimeError' - | 'GenericFailure'; + declare export type SignatureSuccess = {| + Ok: null, + |}; + declare export type TransactionError = {| + Err: Object, + |}; declare export class Connection { constructor(endpoint: string): Connection; @@ -67,7 +67,7 @@ declare module '@solana/web3.js' { confirmTransaction(signature: TransactionSignature): Promise; getSignatureStatus( signature: TransactionSignature, - ): Promise; + ): Promise; getTransactionCount(): Promise; getRecentBlockhash(): Promise; requestAirdrop( diff --git a/web3.js/src/connection.js b/web3.js/src/connection.js index e476d83d26..98ca7010a5 100644 --- a/web3.js/src/connection.js +++ b/web3.js/src/connection.js @@ -126,11 +126,8 @@ const ConfirmTransactionRpcResult = jsonRpcResult('boolean'); const GetSignatureStatusRpcResult = jsonRpcResult( struct.union([ 'null', - struct.union([ - struct({Ok: 'null'}), - struct({Err: 'string'}) - ]) - ]) + struct.union([struct({Ok: 'null'}), struct({Err: 'object'})]), + ]), ); /** @@ -212,16 +209,22 @@ type ProgramAccountSubscriptionInfo = { }; /** - * Possible signature status values + * Signature status: Success * - * @typedef {string} SignatureStatus + * @typedef {Object} SignatureSuccess */ -export type SignatureStatus = - | 'Confirmed' - | 'AccountInUse' - | 'SignatureNotFound' - | 'ProgramRuntimeError' - | 'GenericFailure'; +export type SignatureSuccess = {| + Ok: null, +|}; + +/** + * Signature status: TransactionError + * + * @typedef {Object} TransactionError + */ +export type TransactionError = {| + Err: Object, +|}; /** * A connection to a fullnode JSON RPC endpoint @@ -338,7 +341,7 @@ export class Connection { */ async getSignatureStatus( signature: TransactionSignature, - ): Promise { + ): Promise { const unsafeRes = await this._rpcRequest('getSignatureStatus', [signature]); const res = GetSignatureStatusRpcResult(unsafeRes); if (res.error) { diff --git a/web3.js/src/util/send-and-confirm-raw-transaction.js b/web3.js/src/util/send-and-confirm-raw-transaction.js index b73df5c82a..befe712a39 100644 --- a/web3.js/src/util/send-and-confirm-raw-transaction.js +++ b/web3.js/src/util/send-and-confirm-raw-transaction.js @@ -16,11 +16,11 @@ export async function sendAndConfirmRawTransaction( let signature = await connection.sendRawTransaction(rawTransaction); // Wait up to a couple slots for a confirmation - let status = ''; + let status = null; let statusRetries = 6; for (;;) { status = await connection.getSignatureStatus(signature); - if (status !== 'SignatureNotFound') { + if (status) { break; } @@ -32,14 +32,14 @@ export async function sendAndConfirmRawTransaction( throw new Error( `Raw Transaction '${signature}' was not confirmed in ${duration.toFixed( 2, - )} seconds (${status})`, + )} seconds (${JSON.stringify(status)})`, ); } } - if (status === 'Confirmed') { + if (status && 'Ok' in status) { return signature; } - throw new Error(`Raw transaction ${signature} failed (${status})`); + throw new Error(`Raw transaction ${signature} failed (${JSON.stringify(status)})`); } diff --git a/web3.js/src/util/send-and-confirm-transaction.js b/web3.js/src/util/send-and-confirm-transaction.js index 8844c19baa..299bb93b70 100644 --- a/web3.js/src/util/send-and-confirm-transaction.js +++ b/web3.js/src/util/send-and-confirm-transaction.js @@ -24,7 +24,7 @@ export async function sendAndConfirmTransaction( signature = await connection.sendTransaction(transaction, ...signers); // Wait up to a couple slots for a confirmation - let status = 'SignatureNotFound'; + let status = null; let statusRetries = 6; for (;;) { status = await connection.getSignatureStatus(signature); @@ -39,7 +39,7 @@ export async function sendAndConfirmTransaction( await sleep((500 * DEFAULT_TICKS_PER_SLOT) / NUM_TICKS_PER_SECOND); } - if ('Ok' in status) { + if (status && 'Ok' in status) { break; } if (--sendRetries <= 0) { @@ -47,12 +47,12 @@ export async function sendAndConfirmTransaction( throw new Error( `Transaction '${signature}' was not confirmed in ${duration.toFixed( 2, - )} seconds (${status})`, + )} seconds (${JSON.stringify(status)})`, ); } - if ('Err' in status && !status.includes('AccountInUse')) { - throw new Error(`Transaction ${signature} failed (${status})`); + if (status && status.Err && !('AccountInUse' in status.Err)) { + throw new Error(`Transaction ${signature} failed (${JSON.stringify(status)})`); } // Retry in 0..100ms to try to avoid another AccountInUse collision diff --git a/web3.js/test/connection.test.js b/web3.js/test/connection.test.js index b3aaea14f8..1493d73d1f 100644 --- a/web3.js/test/connection.test.js +++ b/web3.js/test/connection.test.js @@ -338,12 +338,12 @@ test('transaction', async () => { }, { error: null, - result: 'Confirmed', + result: {Ok: null}, }, ]); - await expect(connection.getSignatureStatus(signature)).resolves.toEqual( - {'Ok': null}, - ); + await expect(connection.getSignatureStatus(signature)).resolves.toEqual({ + Ok: null, + }); mockRpc.push([ url, @@ -410,9 +410,9 @@ test('multi-instruction transaction', async () => { expect(++i).toBeLessThan(10); await sleep(500); } - await expect(connection.getSignatureStatus(signature)).resolves.toEqual( - {'Ok': null}, - ); + await expect(connection.getSignatureStatus(signature)).resolves.toEqual({ + Ok: null, + }); expect(await connection.getBalance(accountFrom.publicKey)).toBe(12); expect(await connection.getBalance(accountTo.publicKey)).toBe(21); diff --git a/web3.js/test/token-program.test.js b/web3.js/test/token-program.test.js index 5becc905af..e2e37b2f8c 100644 --- a/web3.js/test/token-program.test.js +++ b/web3.js/test/token-program.test.js @@ -11,7 +11,7 @@ import {sleep} from '../src/util/sleep'; // The default of 5 seconds is too slow for live testing sometimes jest.setTimeout(60000); -function mockGetSignatureStatus(result: string = 'Confirmed') { +function mockGetSignatureStatus(result: Object = {Ok: null}) { mockRpc.push([ url, { @@ -58,7 +58,7 @@ test('create new token', async () => { // mock Token.newAccount() transaction mockSendTransaction(); - mockGetSignatureStatus('SignatureNotFound'); + mockGetSignatureStatus(null); mockGetSignatureStatus(); // mock SystemProgram.createAccount transaction for Token.createNewToken() @@ -67,7 +67,7 @@ test('create new token', async () => { // mock Token.createNewToken() transaction mockSendTransaction(); - mockGetSignatureStatus('SignatureNotFound'); + mockGetSignatureStatus(null); mockGetSignatureStatus(); }