From 4b7b402e7447f09c6b3d9e5f74a42f5ce5c25a6d Mon Sep 17 00:00:00 2001 From: Justin Starry Date: Sun, 4 Apr 2021 18:02:36 +0800 Subject: [PATCH] fix: allow strings in transaction error validation (#16348) * fix: allow strings in transaction error validation * chore: make log tests more robust --- web3.js/src/connection.ts | 4 ++-- web3.js/test/connection.test.ts | 27 ++++++++++++++------------- 2 files changed, 16 insertions(+), 15 deletions(-) diff --git a/web3.js/src/connection.ts b/web3.js/src/connection.ts index ad8b31374b..0c7c93f11f 100644 --- a/web3.js/src/connection.ts +++ b/web3.js/src/connection.ts @@ -369,7 +369,7 @@ const GetLeaderScheduleResult = record(string(), array(number())); /** * Transaction error or null */ -const TransactionErrorResult = nullable(pick({})); +const TransactionErrorResult = nullable(union([pick({}), string()])); /** * Signature status for a transaction @@ -1536,7 +1536,7 @@ export type SignatureResult = { /** * Transaction error */ -export type TransactionError = {}; +export type TransactionError = {} | string; /** * Transaction confirmation status diff --git a/web3.js/test/connection.test.ts b/web3.js/test/connection.test.ts index 437fea11db..de96d3b85e 100644 --- a/web3.js/test/connection.test.ts +++ b/web3.js/test/connection.test.ts @@ -2281,24 +2281,25 @@ describe('Connection', () => { let listener: number | undefined; const owner = new Account(); const [logsRes, ctx] = await new Promise(resolve => { + let received = false; listener = connection.onLogs( - 'all', + owner.publicKey, (logs, ctx) => { - resolve([logs, ctx]); + if (!logs.err) { + received = true; + resolve([logs, ctx]); + } }, 'processed', ); - // Sleep to allow the subscription time to be setup. - // - // Without this, there's a race condition between setting up the log - // subscription and executing the transaction to trigger the log. - // If the transaction to trigger the log executes before the - // subscription is setup, the log event listener never fires and so the - // promise never resolves. - sleep(1000).then(() => { - // Execute a transaction so that we can pickup its logs. - connection.requestAirdrop(owner.publicKey, 1); - }); + + // Send transactions until the log subscription receives an event + (async () => { + while (!received) { + // Execute a transaction so that we can pickup its logs. + await connection.requestAirdrop(owner.publicKey, 1); + } + })(); }); expect(ctx.slot).to.be.greaterThan(0); expect(logsRes.logs.length).to.eq(2);