From a5bb56fb05b16d8e0dc5e9b12656562f4e53d1bc Mon Sep 17 00:00:00 2001 From: Daniel Bate Date: Thu, 9 Jan 2025 09:35:49 +0000 Subject: [PATCH] chore: types of tx response and the network calls involved (#3568) * chore: tests for tx response network calls * chore: changeset --- .changeset/sweet-geese-count.md | 2 + .../src/transaction-response.test.ts | 101 ++++++++++++++++++ 2 files changed, 103 insertions(+) create mode 100644 .changeset/sweet-geese-count.md diff --git a/.changeset/sweet-geese-count.md b/.changeset/sweet-geese-count.md new file mode 100644 index 00000000000..a845151cc84 --- /dev/null +++ b/.changeset/sweet-geese-count.md @@ -0,0 +1,2 @@ +--- +--- diff --git a/packages/fuel-gauge/src/transaction-response.test.ts b/packages/fuel-gauge/src/transaction-response.test.ts index d4fb20427bf..9a407670566 100644 --- a/packages/fuel-gauge/src/transaction-response.test.ts +++ b/packages/fuel-gauge/src/transaction-response.test.ts @@ -71,6 +71,107 @@ function getSubscriptionStreamFromFetch(streamHolder: { stream: ReadableStream { + beforeEach(() => { + vi.resetAllMocks(); + }); + + it('builds tx response from active sendAndAwaitStatus subscription', async () => { + using launched = await launchTestNode(); + + const { + provider, + wallets: [adminWallet], + } = launched; + + const submitAndAwaitStatusSpy = vi.spyOn(provider.operations, 'submitAndAwaitStatus'); + const statusChangeSpy = vi.spyOn(provider.operations, 'statusChange'); + const getTransactionWithReceiptsSpy = vi.spyOn( + provider.operations, + 'getTransactionWithReceipts' + ); + + const transferTx = await adminWallet.transfer( + Wallet.generate().address, + 100, + await provider.getBaseAssetId() + ); + const result = await transferTx.waitForResult(); + + expect(transferTx.id).toEqual(result.id); + expect(result.receipts.length).toBe(2); + + expect(submitAndAwaitStatusSpy).toHaveBeenCalledTimes(1); // sends tx and gets result + expect(statusChangeSpy).toHaveBeenCalledTimes(0); // we already have the status + expect(getTransactionWithReceiptsSpy).toHaveBeenCalledTimes(0); // we already have the raw tx + }); + + it('builds tx response from transaction ID', async () => { + using launched = await launchTestNode(); + + const { + provider, + wallets: [adminWallet], + } = launched; + + const submitAndAwaitStatusSpy = vi.spyOn(provider.operations, 'submitAndAwaitStatus'); + const statusChangeSpy = vi.spyOn(provider.operations, 'statusChange'); + const getTransactionWithReceiptsSpy = vi.spyOn( + provider.operations, + 'getTransactionWithReceipts' + ); + + const transferTx = await adminWallet.transfer( + Wallet.generate().address, + 100, + await provider.getBaseAssetId() + ); + const response = new TransactionResponse(transferTx.id, provider, await provider.getChainId()); + const result = await response.waitForResult(); + + expect(transferTx.id).toEqual(result.id); + expect(result.receipts.length).toBe(2); + + expect(submitAndAwaitStatusSpy).toHaveBeenCalledTimes(1); // sends tx + expect(statusChangeSpy).toHaveBeenCalledTimes(1); // awaits result (can get receipts) + expect(getTransactionWithReceiptsSpy).toHaveBeenCalledTimes(1); // gets raw transaction and receipts + }); + + it('builds tx response from transaction request instance', async () => { + using launched = await launchTestNode(); + + const { + provider, + wallets: [adminWallet], + } = launched; + + const submitAndAwaitStatusSpy = vi.spyOn(provider.operations, 'submitAndAwaitStatus'); + const statusChangeSpy = vi.spyOn(provider.operations, 'statusChange'); + const getTransactionWithReceiptsSpy = vi.spyOn( + provider.operations, + 'getTransactionWithReceipts' + ); + + const transferTxRequest = await adminWallet.createTransfer( + Wallet.generate().address, + 100, + await provider.getBaseAssetId() + ); + const transferTx = await adminWallet.sendTransaction(transferTxRequest); + const response = new TransactionResponse( + transferTxRequest, + provider, + await provider.getChainId() + ); + const result = await response.waitForResult(); + + expect(transferTx.id).toEqual(result.id); + expect(result.receipts.length).toBe(2); + + expect(submitAndAwaitStatusSpy).toHaveBeenCalledTimes(1); // sends tx + expect(statusChangeSpy).toHaveBeenCalledTimes(1); // awaits result and gets receipts + expect(getTransactionWithReceiptsSpy).toHaveBeenCalledTimes(0); // we already have raw tx + }); + it('should ensure create method waits till a transaction response is given', async () => { using launched = await launchTestNode();