-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
transaction-response.ts
287 lines (250 loc) · 7.98 KB
/
transaction-response.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
import { ErrorCode, FuelError } from '@fuel-ts/errors';
import type { BN } from '@fuel-ts/math';
import { bn } from '@fuel-ts/math';
import type {
ReceiptCall,
ReceiptLog,
ReceiptLogData,
ReceiptPanic,
ReceiptReturn,
ReceiptReturnData,
ReceiptRevert,
ReceiptTransfer,
ReceiptTransferOut,
ReceiptScriptResult,
ReceiptMessageOut,
Transaction,
ReceiptMint,
ReceiptBurn,
} from '@fuel-ts/transactions';
import { TransactionCoder } from '@fuel-ts/transactions';
import { arrayify } from '@fuel-ts/utils';
import type Provider from '../provider';
import type { JsonAbisFromAllCalls } from '../transaction-request';
import { assembleTransactionSummary } from '../transaction-summary/assemble-transaction-summary';
import { processGqlReceipt } from '../transaction-summary/receipt';
import type { TransactionSummary, GqlTransaction, AbiMap } from '../transaction-summary/types';
import { extractTxError } from '../utils';
import { getDecodedLogs } from './getDecodedLogs';
/** @hidden */
export type TransactionResultCallReceipt = ReceiptCall;
/** @hidden */
export type TransactionResultReturnReceipt = ReceiptReturn;
/** @hidden */
export type TransactionResultReturnDataReceipt = ReceiptReturnData & { data: string };
/** @hidden */
export type TransactionResultPanicReceipt = ReceiptPanic;
/** @hidden */
export type TransactionResultRevertReceipt = ReceiptRevert;
/** @hidden */
export type TransactionResultLogReceipt = ReceiptLog;
/** @hidden */
export type TransactionResultLogDataReceipt = ReceiptLogData & { data: string };
/** @hidden */
export type TransactionResultTransferReceipt = ReceiptTransfer;
/** @hidden */
export type TransactionResultTransferOutReceipt = ReceiptTransferOut;
/** @hidden */
export type TransactionResultScriptResultReceipt = ReceiptScriptResult;
/** @hidden */
export type TransactionResultMessageOutReceipt = ReceiptMessageOut;
export type TransactionResultMintReceipt = ReceiptMint;
export type TransactionResultBurnReceipt = ReceiptBurn;
/** @hidden */
export type TransactionResultReceipt =
| ReceiptCall
| ReceiptReturn
| (ReceiptReturnData & { data: string })
| ReceiptPanic
| ReceiptRevert
| ReceiptLog
| (ReceiptLogData & { data: string })
| ReceiptTransfer
| ReceiptTransferOut
| ReceiptScriptResult
| ReceiptMessageOut
| TransactionResultMintReceipt
| TransactionResultBurnReceipt;
/** @hidden */
export type TransactionResult<TTransactionType = void> = TransactionSummary<TTransactionType> & {
gqlTransaction: GqlTransaction;
logs?: Array<unknown>;
};
/**
* Represents a response for a transaction.
*/
export class TransactionResponse {
/** Transaction ID */
id: string;
/** Current provider */
provider: Provider;
/** Gas used on the transaction */
gasUsed: BN = bn(0);
/** The graphql Transaction with receipts object. */
gqlTransaction?: GqlTransaction;
abis?: JsonAbisFromAllCalls;
/**
* Constructor for `TransactionResponse`.
*
* @param id - The transaction ID.
* @param provider - The provider.
*/
constructor(id: string, provider: Provider, abis?: JsonAbisFromAllCalls) {
this.id = id;
this.provider = provider;
this.abis = abis;
}
/**
* 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,
abis?: JsonAbisFromAllCalls
): Promise<TransactionResponse> {
const response = new TransactionResponse(id, provider, abis);
await response.fetch();
return response;
}
/**
* Fetch the transaction with receipts from the provider.
*
* @returns Transaction with receipts query result.
*/
async fetch(): Promise<GqlTransaction> {
const response = await this.provider.operations.getTransactionWithReceipts({
transactionId: this.id,
});
if (!response.transaction) {
const subscription = this.provider.operations.statusChange({
transactionId: this.id,
});
for await (const { statusChange } of subscription) {
if (statusChange) {
break;
}
}
return this.fetch();
}
this.gqlTransaction = response.transaction;
return response.transaction;
}
/**
* Decode the raw payload of the transaction.
*
* @param transactionWithReceipts - The transaction with receipts object.
* @returns The decoded transaction.
*/
decodeTransaction<TTransactionType = void>(transactionWithReceipts: GqlTransaction) {
return new TransactionCoder().decode(
arrayify(transactionWithReceipts.rawPayload),
0
)?.[0] as Transaction<TTransactionType>;
}
/**
* Retrieves the TransactionSummary. If the `gqlTransaction` is not set, it will
* fetch it from the provider
*
* @param contractsAbiMap - The contracts ABI map.
* @returns
*/
async getTransactionSummary<TTransactionType = void>(
contractsAbiMap?: AbiMap
): Promise<TransactionSummary<TTransactionType>> {
let transaction = this.gqlTransaction;
if (!transaction) {
transaction = await this.fetch();
}
const decodedTransaction = this.decodeTransaction<TTransactionType>(
transaction
) as Transaction<TTransactionType>;
const receipts = transaction.receipts?.map(processGqlReceipt) || [];
const { gasPerByte, gasPriceFactor, gasCosts } = this.provider.getGasConfig();
const maxInputs = this.provider.getChain().consensusParameters.maxInputs;
const transactionSummary = assembleTransactionSummary<TTransactionType>({
id: this.id,
receipts,
transaction: decodedTransaction,
transactionBytes: arrayify(transaction.rawPayload),
gqlTransactionStatus: transaction.status,
gasPerByte,
gasPriceFactor,
abiMap: contractsAbiMap,
maxInputs,
gasCosts,
});
return transactionSummary;
}
private async waitForStatusChange() {
const status = this.gqlTransaction?.status?.type;
if (status && status !== 'SubmittedStatus') {
return;
}
const subscription = this.provider.operations.statusChange({
transactionId: this.id,
});
for await (const { statusChange } of subscription) {
if (statusChange.type === 'SqueezedOutStatus') {
throw new FuelError(
ErrorCode.TRANSACTION_SQUEEZED_OUT,
`Transaction Squeezed Out with reason: ${statusChange.reason}`
);
}
if (statusChange.type !== 'SubmittedStatus') {
break;
}
}
await this.fetch();
}
/**
* Waits for transaction to complete and returns the result.
*
* @returns The completed transaction result
*/
async waitForResult<TTransactionType = void>(
contractsAbiMap?: AbiMap
): Promise<TransactionResult<TTransactionType>> {
await this.waitForStatusChange();
const transactionSummary = await this.getTransactionSummary<TTransactionType>(contractsAbiMap);
const transactionResult: TransactionResult<TTransactionType> = {
gqlTransaction: this.gqlTransaction as GqlTransaction,
...transactionSummary,
};
let logs: Array<unknown> = [];
if (this.abis) {
logs = getDecodedLogs(
transactionSummary.receipts,
this.abis.main,
this.abis.otherContractsAbis
);
transactionResult.logs = logs;
}
if (transactionResult.isStatusFailure) {
const {
receipts,
gqlTransaction: { status },
} = transactionResult;
throw extractTxError({
receipts,
status,
logs,
});
}
return transactionResult;
}
/**
* Waits for transaction to complete and returns the result.
*
* @param contractsAbiMap - The contracts ABI map.
*/
async wait<TTransactionType = void>(
contractsAbiMap?: AbiMap
): Promise<TransactionResult<TTransactionType>> {
return this.waitForResult<TTransactionType>(contractsAbiMap);
}
}