-
-
Notifications
You must be signed in to change notification settings - Fork 188
/
transaction.ts
160 lines (140 loc) · 4.33 KB
/
transaction.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
import type {
TransactionError,
TransactionMeta,
TransactionParams,
} from '@metamask/transaction-controller';
import {
TransactionStatus,
UserFeeLevel,
} from '@metamask/transaction-controller';
import type { Hex } from '@metamask/utils';
import { add0x, remove0x } from '@metamask/utils';
import BN from 'bn.js';
import { EMPTY_BYTES, VALUE_ZERO } from '../constants';
import { UserOperationStatus } from '../types';
import type { UserOperationMetadata } from '../types';
/**
* Converts a user operation metadata object into a transaction metadata object.
* @param metadata - The user operation metadata object to convert.
* @returns The equivalent transaction metadata object.
*/
export function getTransactionMetadata(
metadata: UserOperationMetadata,
): TransactionMeta | undefined {
const {
actualGasCost,
actualGasUsed,
baseFeePerGas,
chainId,
error: rawError,
origin,
transactionHash,
id,
swapsMetadata,
time,
transactionParams,
transactionType,
userFeeLevel: rawUserFeeLevel,
userOperation,
} = metadata;
if (!transactionParams) {
return undefined;
}
// effectiveGasPrice = actualGasCost / actualGasUsed
const effectiveGasPrice =
actualGasCost && actualGasUsed
? add0x(
new BN(remove0x(actualGasCost), 16)
.div(new BN(remove0x(actualGasUsed), 16))
.toString(16),
)
: undefined;
const error = (
rawError
? {
name: rawError.name,
message: rawError.message,
stack: rawError.stack,
code: rawError.code,
rpc: rawError.rpc,
}
: undefined
) as TransactionError;
const status: TransactionStatus = {
[UserOperationStatus.Unapproved]: TransactionStatus.unapproved,
[UserOperationStatus.Approved]: TransactionStatus.approved,
[UserOperationStatus.Signed]: TransactionStatus.signed,
[UserOperationStatus.Submitted]: TransactionStatus.submitted,
[UserOperationStatus.Confirmed]: TransactionStatus.confirmed,
[UserOperationStatus.Failed]: TransactionStatus.failed,
}[metadata.status];
const gas = addHex(
userOperation.preVerificationGas,
userOperation.verificationGasLimit,
userOperation.callGasLimit,
);
const hasPaymaster = userOperation.paymasterAndData !== EMPTY_BYTES;
const maxFeePerGas = hasPaymaster ? VALUE_ZERO : userOperation.maxFeePerGas;
const maxPriorityFeePerGas = hasPaymaster
? VALUE_ZERO
: userOperation.maxPriorityFeePerGas;
const nonce =
userOperation.nonce === EMPTY_BYTES ? undefined : userOperation.nonce;
const txParams = {
...transactionParams,
from: userOperation.sender,
gas,
nonce,
maxFeePerGas,
maxPriorityFeePerGas,
} as TransactionParams;
// User operations only support EIP-1559 gas fee properties.
delete txParams.gasPrice;
const swaps = {
approvalTxId: swapsMetadata?.approvalTxId ?? undefined,
destinationTokenAddress:
swapsMetadata?.destinationTokenAddress ?? undefined,
destinationTokenDecimals:
swapsMetadata?.destinationTokenDecimals ?? undefined,
destinationTokenSymbol: swapsMetadata?.destinationTokenSymbol ?? undefined,
estimatedBaseFee: swapsMetadata?.estimatedBaseFee ?? undefined,
sourceTokenSymbol: swapsMetadata?.sourceTokenSymbol ?? undefined,
swapMetaData: swapsMetadata?.swapMetaData ?? undefined,
swapTokenValue: swapsMetadata?.swapTokenValue ?? undefined,
};
const userFeeLevel = hasPaymaster ? UserFeeLevel.CUSTOM : rawUserFeeLevel;
return {
baseFeePerGas: (baseFeePerGas as Hex) ?? undefined,
chainId: chainId as Hex,
error,
hash: transactionHash ?? undefined,
id,
isUserOperation: true,
origin,
status,
time,
txParams,
txReceipt: {
effectiveGasPrice: effectiveGasPrice ?? undefined,
gasUsed: actualGasUsed ?? undefined,
},
type: transactionType ?? undefined,
userFeeLevel: userFeeLevel as string,
...swaps,
};
}
/**
* Adds the given hexadecimal values together.
* @param values - The hexadecimal values to add together.
* @returns The sum of the given hexadecimal values.
*/
function addHex(...values: (string | undefined)[]) {
const total = new BN(0);
for (const value of values) {
if (!value) {
continue;
}
total.iadd(new BN(remove0x(value), 16));
}
return add0x(total.toString(16));
}