-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
Copy pathget-transaction-summary.ts
221 lines (180 loc) · 6.09 KB
/
get-transaction-summary.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
import { ErrorCode, FuelError } from '@fuel-ts/errors';
import { bn } from '@fuel-ts/math';
import { TransactionCoder } from '@fuel-ts/transactions';
import { arrayify } from '@fuel-ts/utils';
import type {
GqlGetTransactionsByOwnerQueryVariables,
GqlReceiptFragment,
} from '../__generated__/operations';
import type Provider from '../provider';
import { TRANSACTIONS_PAGE_SIZE_LIMIT, type PageInfo } from '../provider';
import type { TransactionRequest } from '../transaction-request';
import type { TransactionResult } from '../transaction-response';
import { validatePaginationArgs } from '../utils/validate-pagination-args';
import { assembleTransactionSummary } from './assemble-transaction-summary';
import { processGqlReceipt } from './receipt';
import { getTotalFeeFromStatus } from './status';
import type { AbiMap, TransactionSummary } from './types';
/** @hidden */
export interface GetTransactionSummaryParams {
id: string;
provider: Provider;
abiMap?: AbiMap;
}
export async function getTransactionSummary<TTransactionType = void>(
params: GetTransactionSummaryParams
): Promise<TransactionResult> {
const { id, provider, abiMap } = params;
const { transaction: gqlTransaction } = await provider.operations.getTransactionWithReceipts({
transactionId: id,
});
if (!gqlTransaction) {
throw new FuelError(
ErrorCode.TRANSACTION_NOT_FOUND,
`Transaction not found for given id: ${id}.`
);
}
const [decodedTransaction] = new TransactionCoder().decode(
arrayify(gqlTransaction.rawPayload),
0
);
let txReceipts: GqlReceiptFragment[] = [];
if (gqlTransaction?.status && 'receipts' in gqlTransaction.status) {
txReceipts = gqlTransaction.status.receipts;
}
const receipts = txReceipts.map(processGqlReceipt);
const {
consensusParameters: {
feeParameters: { gasPerByte, gasPriceFactor },
txParameters: { maxInputs, maxGasPerTx },
gasCosts,
},
} = await provider.getChain();
// If we have the total fee, we do not need to refetch the gas price
const totalFee = getTotalFeeFromStatus(gqlTransaction.status);
const gasPrice = totalFee ? bn(0) : await provider.getLatestGasPrice();
const baseAssetId = await provider.getBaseAssetId();
const transactionInfo = assembleTransactionSummary<TTransactionType>({
id: gqlTransaction.id,
receipts,
transaction: decodedTransaction,
transactionBytes: arrayify(gqlTransaction.rawPayload),
gqlTransactionStatus: gqlTransaction.status,
gasPerByte: bn(gasPerByte),
gasPriceFactor: bn(gasPriceFactor),
abiMap,
maxInputs,
gasCosts,
maxGasPerTx,
gasPrice,
baseAssetId,
});
return {
...transactionInfo,
};
}
export interface GetTransactionSummaryFromRequestParams {
transactionRequest: TransactionRequest;
provider: Provider;
abiMap?: AbiMap;
}
/** @hidden */
export async function getTransactionSummaryFromRequest<TTransactionType = void>(
params: GetTransactionSummaryFromRequestParams
): Promise<TransactionSummary<TTransactionType>> {
const { provider, transactionRequest, abiMap } = params;
const { receipts } = await provider.dryRun(transactionRequest);
const { gasPerByte, gasPriceFactor, gasCosts, maxGasPerTx } = await provider.getGasConfig();
const maxInputs = (await provider.getChain()).consensusParameters.txParameters.maxInputs;
const transaction = transactionRequest.toTransaction();
const transactionBytes = transactionRequest.toTransactionBytes();
const gasPrice = await provider.getLatestGasPrice();
const baseAssetId = await provider.getBaseAssetId();
const transactionSummary = assembleTransactionSummary<TTransactionType>({
id: transactionRequest.getTransactionId(await provider.getChainId()),
receipts,
transaction,
transactionBytes,
abiMap,
gasPerByte,
gasPriceFactor,
maxInputs,
gasCosts,
maxGasPerTx,
gasPrice,
baseAssetId,
});
return transactionSummary;
}
export interface GetTransactionsSummariesParams {
provider: Provider;
filters: GqlGetTransactionsByOwnerQueryVariables;
abiMap?: AbiMap;
}
export interface GetTransactionsSummariesReturns {
transactions: TransactionResult[];
pageInfo: PageInfo;
}
/**
* Gets transaction summaries for a given owner/address.
*
* @param params - The filters to apply to the query.
* @returns The transaction summaries.
*/
export async function getTransactionsSummaries(
params: GetTransactionsSummariesParams
): Promise<GetTransactionsSummariesReturns> {
const { filters, provider, abiMap } = params;
const { owner, ...inputArgs } = filters;
const validPaginationParams = validatePaginationArgs({
inputArgs,
paginationLimit: TRANSACTIONS_PAGE_SIZE_LIMIT,
});
const { transactionsByOwner } = await provider.operations.getTransactionsByOwner({
...validPaginationParams,
owner,
});
const { edges, pageInfo } = transactionsByOwner;
const {
consensusParameters: {
feeParameters: { gasPerByte, gasPriceFactor },
txParameters: { maxInputs, maxGasPerTx },
gasCosts,
},
} = await provider.getChain();
const gasPrice = await provider.getLatestGasPrice();
const baseAssetId = await provider.getBaseAssetId();
const transactions = edges.map((edge) => {
const { node: gqlTransaction } = edge;
const { id, rawPayload, status } = gqlTransaction;
const [decodedTransaction] = new TransactionCoder().decode(arrayify(rawPayload), 0);
let txReceipts: GqlReceiptFragment[] = [];
if (gqlTransaction?.status && 'receipts' in gqlTransaction.status) {
txReceipts = gqlTransaction.status.receipts;
}
const receipts = txReceipts.map(processGqlReceipt);
const transactionSummary = assembleTransactionSummary({
id,
receipts,
transaction: decodedTransaction,
transactionBytes: arrayify(rawPayload),
gqlTransactionStatus: status,
abiMap,
gasPerByte,
gasPriceFactor,
maxInputs,
gasCosts,
maxGasPerTx,
gasPrice,
baseAssetId,
});
const output: TransactionResult = {
...transactionSummary,
};
return output;
});
return {
transactions,
pageInfo,
};
}