Skip to content

Commit

Permalink
feat: add static method create to transaction response (FuelLabs#1206)
Browse files Browse the repository at this point in the history
  • Loading branch information
Torres-ssf authored Aug 30, 2023
1 parent c267a00 commit 35364da
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .changeset/happy-spoons-knock.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@fuel-ts/providers": minor
---

add static method create to TransactionReponse class
76 changes: 76 additions & 0 deletions packages/fuel-gauge/src/transaction-response.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
import { generateTestWallet } from '@fuel-ts/wallet/test-utils';
import type { WalletUnlocked } from 'fuels';
import { FUEL_NETWORK_URL, Provider, TransactionResponse, Wallet } from 'fuels';

describe('TransactionSummary', () => {
let provider: Provider;
let adminWallet: WalletUnlocked;

beforeAll(async () => {
provider = new Provider(FUEL_NETWORK_URL);
adminWallet = await generateTestWallet(provider, [[1_000]]);
});

it('should ensure create method waits till a transaction response is given', async () => {
const destination = Wallet.generate();

const { id: transactionId } = await adminWallet.transfer(destination.address, 100);

const response = await TransactionResponse.create(transactionId, provider);

expect(response.gqlTransaction).toBeDefined();
expect(response.gqlTransaction?.status).toBeDefined();
expect(response.gqlTransaction?.id).toBe(transactionId);
});

it('should ensure getTransactionSummary fetchs a transaction and assembles transaction summary', async () => {
const destination = Wallet.generate();

const { id: transactionId } = await adminWallet.transfer(destination.address, 100);

const response = new TransactionResponse(transactionId, provider);

expect(response.gqlTransaction).toBeUndefined();

const transactionSummary = await response.getTransactionSummary();

expect(transactionSummary.id).toBeDefined();
expect(transactionSummary.fee).toBeDefined();
expect(transactionSummary.gasUsed).toBeDefined();
expect(transactionSummary.operations).toBeDefined();
expect(transactionSummary.type).toBeDefined();
expect(transactionSummary.blockId).toBeDefined();
expect(transactionSummary.time).toBeDefined();
expect(transactionSummary.status).toBeDefined();
expect(transactionSummary.receipts).toBeDefined();
expect(transactionSummary.mintedAssets).toBeDefined();
expect(transactionSummary.burnedAssets).toBeDefined();
expect(transactionSummary.isTypeMint).toBeDefined();
expect(transactionSummary.isTypeCreate).toBeDefined();
expect(transactionSummary.isTypeScript).toBeDefined();
expect(transactionSummary.isStatusFailure).toBeDefined();
expect(transactionSummary.isStatusSuccess).toBeDefined();
expect(transactionSummary.isStatusPending).toBeDefined();
expect(transactionSummary.transaction).toBeDefined();

expect(response.gqlTransaction).toBeDefined();
expect(response.gqlTransaction?.status).toBeDefined();
expect(response.gqlTransaction?.id).toBe(transactionId);
});

it('should ensure waitForResult always waits for the transaction to be processed', async () => {
const destination = Wallet.generate();

const { id: transactionId } = await adminWallet.transfer(destination.address, 100);

const response = new TransactionResponse(transactionId, provider);

expect(response.gqlTransaction).toBeUndefined();

await response.waitForResult();

expect(response.gqlTransaction?.status?.type).toBeDefined();
expect(response.gqlTransaction?.status?.type).not.toEqual('SubmittedStatus');
expect(response.gqlTransaction?.id).toBe(transactionId);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,20 @@ export class TransactionResponse {
this.provider = provider;
}

/**
* Async constructor for `TransactionResponse`. This method can be used to create
* an instance of `TransactionResponse` and wait for the transaction to be fetched
* from the chain, ensuring that the `gqlTransaction` property is set.
*
* @param id - The transaction ID.
* @param provider - The provider.
*/
static async create(id: string, provider: Provider): Promise<TransactionResponse> {
const response = new TransactionResponse(id, provider);
await response.fetch();
return response;
}

/**
* Fetch the transaction with receipts from the provider.
*
Expand Down

0 comments on commit 35364da

Please sign in to comment.