Skip to content

Commit b0aa106

Browse files
authored
chore: add object parameter support to TransactionResponse constructor (#3911)
1 parent 365cfa8 commit b0aa106

File tree

5 files changed

+95
-11
lines changed

5 files changed

+95
-11
lines changed

.changeset/stale-news-go.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@fuel-ts/account": patch
3+
---
4+
5+
chore: add object parameter support to `TransactionResponse` constructor

packages/account/src/providers/provider.test.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1103,7 +1103,11 @@ describe('Provider', () => {
11031103
);
11041104

11051105
const chainId = await provider.getChainId();
1106-
const response = new TransactionResponse('invalid transaction id', provider, chainId);
1106+
const response = new TransactionResponse({
1107+
transactionRequestOrId: 'invalid transaction id',
1108+
provider,
1109+
chainId,
1110+
});
11071111

11081112
await expectToThrowFuelError(() => response.waitForResult(), {
11091113
code: FuelError.CODES.INVALID_REQUEST,

packages/account/src/providers/provider.ts

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1152,7 +1152,13 @@ export default class Provider {
11521152
);
11531153

11541154
const chainId = await this.getChainId();
1155-
return new TransactionResponse(transactionRequest, this, chainId, abis, subscription);
1155+
return new TransactionResponse({
1156+
transactionRequestOrId: transactionRequest,
1157+
provider: this,
1158+
chainId,
1159+
abis,
1160+
submitAndAwaitSubscription: subscription,
1161+
});
11561162
}
11571163

11581164
/**
@@ -2550,7 +2556,11 @@ export default class Provider {
25502556

25512557
async getTransactionResponse(transactionId: string): Promise<TransactionResponse> {
25522558
const chainId = await this.getChainId();
2553-
return new TransactionResponse(transactionId, this, chainId);
2559+
return new TransactionResponse({
2560+
transactionRequestOrId: transactionId,
2561+
provider: this,
2562+
chainId,
2563+
});
25542564
}
25552565

25562566
/**

packages/account/src/providers/transaction-response/transaction-response.ts

Lines changed: 67 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
/* eslint-disable @typescript-eslint/naming-convention */
2+
13
import { ErrorCode, FuelError } from '@fuel-ts/errors';
24
import type { BN } from '@fuel-ts/math';
35
import { bn } from '@fuel-ts/math';
@@ -115,6 +117,14 @@ export type TransactionResponseJson = {
115117
requestJson?: string;
116118
};
117119

120+
export type TransactionResponseParams = {
121+
transactionRequestOrId: string | TransactionRequest;
122+
provider: Provider;
123+
chainId: number;
124+
abis?: JsonAbisFromAllCalls;
125+
submitAndAwaitSubscription?: SubmitAndAwaitStatusSubscriptionIterable;
126+
};
127+
118128
/**
119129
* Represents a response for a transaction.
120130
*/
@@ -130,35 +140,85 @@ export class TransactionResponse {
130140
request?: TransactionRequest;
131141
status?: StatusChangeSubscription['statusChange'];
132142
abis?: JsonAbisFromAllCalls;
143+
private submitTxSubscription?: SubmitAndAwaitStatusSubscriptionIterable;
133144
preConfirmationStatus?: StatusChangeSubscription['statusChange'];
134145

135146
private waitingForStreamData = false;
136147
private statusResolvers: Map<StatusType, (() => void)[]> = new Map();
137148

138149
/**
139-
* Constructor for `TransactionResponse`.
150+
* Creates a new TransactionResponse instance.
140151
*
141-
* @param tx - The transaction ID or TransactionRequest.
152+
* @param transactionRequestOrId - The transaction ID or TransactionRequest.
142153
* @param provider - The provider.
154+
* @param chainId - The chain ID.
155+
* @param abis - The ABIs.
156+
* @param submitAndAwaitSubscription - The submit and await subscription.
157+
*
158+
* @deprecated Use the object-style constructor instead:
159+
* `new TransactionResponse({ ... })`
143160
*/
144161
constructor(
145-
tx: string | TransactionRequest,
162+
transactionRequestOrId: string | TransactionRequest,
146163
provider: Provider,
147164
chainId: number,
148165
abis?: JsonAbisFromAllCalls,
149-
private submitTxSubscription?: SubmitAndAwaitStatusSubscriptionIterable
166+
submitAndAwaitSubscription?: SubmitAndAwaitStatusSubscriptionIterable
167+
);
168+
169+
/**
170+
* Creates a new TransactionResponse instance.
171+
*
172+
* @param constructorParams - The constructor parameters.
173+
*/
174+
constructor(constructorParams: TransactionResponseParams);
175+
176+
/**
177+
* Constructor for `TransactionResponse`.
178+
*/
179+
constructor(
180+
constructorParams: string | TransactionRequest | TransactionResponseParams,
181+
provider?: Provider,
182+
chainId?: number,
183+
abis?: JsonAbisFromAllCalls,
184+
submitTxSubscription?: SubmitAndAwaitStatusSubscriptionIterable
150185
) {
186+
let tx: string | TransactionRequest;
187+
let _provider: Provider;
188+
let _chainId: number;
189+
let _abis: JsonAbisFromAllCalls | undefined;
190+
191+
if (
192+
typeof constructorParams === 'object' &&
193+
'provider' in constructorParams &&
194+
arguments.length === 1
195+
) {
196+
// Object-style usage
197+
tx = constructorParams.transactionRequestOrId;
198+
_provider = constructorParams.provider;
199+
_chainId = constructorParams.chainId;
200+
_abis = constructorParams.abis;
201+
this.submitTxSubscription = constructorParams.submitAndAwaitSubscription;
202+
} else {
203+
// Deprecated positional usage
204+
tx = constructorParams as string | TransactionRequest;
205+
_provider = provider as Provider;
206+
_chainId = chainId as number;
207+
_abis = abis;
208+
this.submitTxSubscription = submitTxSubscription;
209+
}
210+
151211
// Transaction Request was not provided
152212
if (typeof tx === 'string') {
153213
this.id = tx;
154214
} else {
155215
// Transaction Request was provided
156-
this.id = tx.getTransactionId(chainId);
216+
this.id = tx.getTransactionId(_chainId);
157217
this.request = tx;
158218
}
159219

160-
this.provider = provider;
161-
this.abis = abis;
220+
this.provider = _provider;
221+
this.abis = _abis;
162222
this.waitForResult = this.waitForResult.bind(this);
163223
this.waitForPreConfirmation = this.waitForPreConfirmation.bind(this);
164224
}

packages/account/src/providers/utils/transaction-response-serialization.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,12 @@ export const deserializeTransactionResponseJson = (json: TransactionResponseJson
4141
const provider = new Provider(providerUrl, { cache: providerCache });
4242
const { chainId } = providerCache.chain.consensusParameters;
4343

44-
const response = new TransactionResponse(id, provider, Number(chainId), abis);
44+
const response = new TransactionResponse({
45+
transactionRequestOrId: id,
46+
provider,
47+
chainId: Number(chainId),
48+
abis,
49+
});
4550

4651
if (requestJson) {
4752
response.request = transactionRequestify(JSON.parse(requestJson));

0 commit comments

Comments
 (0)