Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: types of tx response and the network calls involved #3568

Merged
merged 2 commits into from
Jan 9, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .changeset/sweet-geese-count.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
---
---
101 changes: 101 additions & 0 deletions packages/fuel-gauge/src/transaction-response.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,107 @@ function getSubscriptionStreamFromFetch(streamHolder: { stream: ReadableStream<U
* @group browser
*/
describe('TransactionResponse', () => {
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();

Expand Down
Loading