Skip to content

Commit dc44835

Browse files
authored
[feat]: Enhance wallet port communication (#30)
* fix: Variable name * feat: Better structure wallet communication * feat: Add 'getFeeData' query to wallet worker and overhaul gas calculation logic
1 parent b86a097 commit dc44835

8 files changed

+295
-118
lines changed

src/submitter/queues/submit-queue.ts

+2
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ export class SubmitQueue extends ProcessingQueue<
2020
maxTries: number,
2121
private readonly incentivesContracts: Map<string, IncentivizedMessageEscrow>,
2222
relayerAddress: string,
23+
private readonly chainId: string,
2324
private readonly wallet: WalletInterface,
2425
private readonly logger: pino.Logger,
2526
) {
@@ -65,6 +66,7 @@ export class SubmitQueue extends ProcessingQueue<
6566
};
6667

6768
const txPromise = this.wallet.submitTransaction(
69+
this.chainId,
6870
txRequest,
6971
order,
7072
).then((transactionResult): SubmitOrderResult => {

src/submitter/submitter.service.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ export class SubmitterService {
179179
),
180180

181181
walletPublicKey: globalConfig.walletPublicKey,
182-
walletPort: await this.walletService.attachToWallet(chainId),
182+
walletPort: await this.walletService.attachToWallet(),
183183
loggerOptions: this.loggerService.loggerOptions,
184184
};
185185
}

src/submitter/submitter.worker.ts

+1
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,7 @@ class SubmitterWorker {
108108
maxTries,
109109
incentivesContracts,
110110
walletPublicKey,
111+
chainId,
111112
wallet,
112113
logger,
113114
);

src/wallet/transaction-helper.ts

+64-40
Original file line numberDiff line numberDiff line change
@@ -224,78 +224,102 @@ export class TransactionHelper {
224224
}
225225
}
226226

227-
getFeeDataForTransaction(priority?: boolean): GasFeeOverrides {
228-
const queriedFeeData = this.feeData;
229-
if (queriedFeeData == undefined) {
230-
return {};
227+
getCachedFeeData(): FeeData | undefined {
228+
return this.feeData;
229+
}
230+
231+
getAdjustedFeeData(
232+
priority?: boolean,
233+
): FeeData | undefined {
234+
const feeData = {...this.feeData};
235+
if (feeData == undefined) {
236+
return undefined;
231237
}
232238

233-
const queriedMaxPriorityFeePerGas = queriedFeeData.maxPriorityFeePerGas;
234-
if (queriedMaxPriorityFeePerGas != null) {
235-
// Set fee data for an EIP 1559 transactions
236-
let maxFeePerGas = this.maxFeePerGas;
239+
// Override 'maxFeePerGas' if it is specified on the config.
240+
if (this.maxFeePerGas) {
241+
feeData.maxFeePerGas = this.maxFeePerGas;
242+
}
237243

244+
// Adjust the 'maxPriorityFeePerGas' if present.
245+
if (feeData.maxPriorityFeePerGas != undefined) {
238246
// Adjust the 'maxPriorityFeePerGas' by the adjustment factor
239-
let maxPriorityFeePerGas;
240247
if (this.maxPriorityFeeAdjustmentFactor != undefined) {
241-
maxPriorityFeePerGas = queriedMaxPriorityFeePerGas
248+
feeData.maxPriorityFeePerGas = feeData.maxPriorityFeePerGas
242249
* this.maxPriorityFeeAdjustmentFactor
243250
/ DECIMAL_BASE_BIG_INT;
244251
}
245252

246253
// Apply the max allowed 'maxPriorityFeePerGas'
247254
if (
248-
maxPriorityFeePerGas != undefined &&
249255
this.maxAllowedPriorityFeePerGas != undefined &&
250-
this.maxAllowedPriorityFeePerGas < maxPriorityFeePerGas
256+
this.maxAllowedPriorityFeePerGas < feeData.maxPriorityFeePerGas
251257
) {
252-
maxPriorityFeePerGas = this.maxAllowedPriorityFeePerGas;
253-
}
254-
255-
if (priority) {
256-
if (maxFeePerGas != undefined) {
257-
maxFeePerGas = maxFeePerGas * this.priorityAdjustmentFactor / DECIMAL_BASE_BIG_INT;
258-
}
259-
260-
if (maxPriorityFeePerGas != undefined) {
261-
maxPriorityFeePerGas = maxPriorityFeePerGas * this.priorityAdjustmentFactor / DECIMAL_BASE_BIG_INT;
262-
}
258+
feeData.maxPriorityFeePerGas = this.maxAllowedPriorityFeePerGas;
263259
}
260+
}
264261

265-
return {
266-
maxFeePerGas,
267-
maxPriorityFeePerGas,
268-
};
269-
} else {
270-
// Set traditional gasPrice
271-
const queriedGasPrice = queriedFeeData.gasPrice;
272-
if (queriedGasPrice == null) return {};
273-
262+
// Adjust the 'gasPrice' if present.
263+
if (feeData.gasPrice) {
274264
// Adjust the 'gasPrice' by the adjustment factor
275-
let gasPrice;
276265
if (this.gasPriceAdjustmentFactor != undefined) {
277-
gasPrice = queriedGasPrice
266+
feeData.gasPrice = feeData.gasPrice
278267
* this.gasPriceAdjustmentFactor
279268
/ DECIMAL_BASE_BIG_INT;
280269
}
281270

282271
// Apply the max allowed 'gasPrice'
283272
if (
284-
gasPrice != undefined &&
285273
this.maxAllowedGasPrice != undefined &&
286-
this.maxAllowedGasPrice < gasPrice
274+
this.maxAllowedGasPrice < feeData.gasPrice
287275
) {
288-
gasPrice = this.maxAllowedGasPrice;
276+
feeData.gasPrice = this.maxAllowedGasPrice;
289277
}
278+
}
290279

291-
if (priority && gasPrice != undefined) {
292-
gasPrice = gasPrice
280+
// Apply the 'priority' adjustment factor
281+
if (priority) {
282+
if (feeData.maxFeePerGas != undefined) {
283+
feeData.maxFeePerGas = feeData.maxFeePerGas
284+
*this.priorityAdjustmentFactor
285+
/ DECIMAL_BASE_BIG_INT;
286+
}
287+
288+
if (feeData.maxPriorityFeePerGas != undefined) {
289+
feeData.maxPriorityFeePerGas = feeData.maxPriorityFeePerGas
290+
*this.priorityAdjustmentFactor
291+
/ DECIMAL_BASE_BIG_INT;
292+
}
293+
294+
if (feeData.gasPrice != undefined) {
295+
feeData.gasPrice = feeData.gasPrice
293296
* this.priorityAdjustmentFactor
294297
/ DECIMAL_BASE_BIG_INT;
295298
}
299+
}
300+
301+
return new FeeData(
302+
feeData.gasPrice,
303+
feeData.maxFeePerGas,
304+
feeData.maxPriorityFeePerGas
305+
);
306+
}
296307

308+
getFeeDataForTransaction(priority?: boolean): GasFeeOverrides {
309+
const adjustedFeeData = this.getAdjustedFeeData(priority);
310+
if (adjustedFeeData == undefined) {
311+
return {};
312+
}
313+
314+
if (adjustedFeeData.maxPriorityFeePerGas != undefined) {
315+
// Set fee data for EIP 1559 transactions
316+
return {
317+
maxFeePerGas: adjustedFeeData.maxFeePerGas ?? undefined,
318+
maxPriorityFeePerGas: adjustedFeeData.maxPriorityFeePerGas,
319+
};
320+
} else {
297321
return {
298-
gasPrice,
322+
gasPrice: adjustedFeeData.gasPrice ?? undefined,
299323
};
300324
}
301325
}

src/wallet/wallet.interface.ts

+68-11
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
1-
import { TransactionReceipt, TransactionRequest, TransactionResponse } from 'ethers6';
1+
import { FeeData, TransactionReceipt, TransactionRequest, TransactionResponse } from 'ethers6';
22
import { MessagePort } from 'worker_threads';
3-
import { WalletTransactionOptions, WalletTransactionRequestMessage, WalletTransactionRequestResponse } from './wallet.types';
4-
import { WALLET_WORKER_CRASHED_MESSAGE_ID } from './wallet.service';
3+
import { WALLET_WORKER_CRASHED_MESSAGE_ID, WalletFeeDataMessage, WalletGetFeeDataMessage, WalletMessageType, WalletPortData, WalletTransactionOptions, WalletTransactionRequestMessage, WalletTransactionRequestResponseMessage } from './wallet.types';
54

65
export interface TransactionResult<T = any> {
76
txRequest: TransactionRequest;
@@ -22,6 +21,7 @@ export class WalletInterface {
2221
}
2322

2423
async submitTransaction<T>(
24+
chainId: string,
2525
transaction: TransactionRequest,
2626
metadata?: T,
2727
options?: WalletTransactionOptions
@@ -30,22 +30,25 @@ export class WalletInterface {
3030
const messageId = this.getNextPortMessageId();
3131

3232
const resultPromise = new Promise<TransactionResult<T>>(resolve => {
33-
const listener = (data: any) => {
33+
const listener = (data: WalletPortData) => {
3434
if (data.messageId === messageId) {
3535
this.port.off("message", listener);
3636

37-
const walletResponse = data as WalletTransactionRequestResponse<T>;
37+
const walletResponse = data.message as WalletTransactionRequestResponseMessage<T>;
3838

3939
const result = {
4040
txRequest: walletResponse.txRequest,
4141
metadata: walletResponse.metadata,
4242
tx: walletResponse.tx,
4343
txReceipt: walletResponse.txReceipt,
44-
submissionError: data.submissionError,
45-
confirmationError: data.confirmationError
44+
submissionError: walletResponse.submissionError,
45+
confirmationError: walletResponse.confirmationError
4646
};
4747
resolve(result);
48-
} else if (data.messageId === WALLET_WORKER_CRASHED_MESSAGE_ID) {
48+
} else if (
49+
data.messageId === WALLET_WORKER_CRASHED_MESSAGE_ID
50+
&& data.chainId == chainId
51+
) {
4952
this.port.off("message", listener);
5053

5154
const result = {
@@ -59,13 +62,67 @@ export class WalletInterface {
5962
};
6063
this.port.on("message", listener);
6164

62-
const request: WalletTransactionRequestMessage = {
63-
messageId,
65+
const message: WalletTransactionRequestMessage = {
66+
type: WalletMessageType.TransactionRequest,
6467
txRequest: transaction,
6568
metadata,
6669
options
6770
};
68-
this.port.postMessage(request);
71+
72+
const portData: WalletPortData = {
73+
chainId,
74+
messageId,
75+
message,
76+
}
77+
this.port.postMessage(portData);
78+
});
79+
80+
return resultPromise;
81+
}
82+
83+
async getFeeData(
84+
chainId: string,
85+
priority?: boolean,
86+
): Promise<FeeData> {
87+
88+
const messageId = this.getNextPortMessageId();
89+
90+
const resultPromise = new Promise<FeeData>(resolve => {
91+
const listener = (data: WalletPortData) => {
92+
if (data.messageId === messageId) {
93+
this.port.off("message", listener);
94+
95+
const walletResponse = data.message as WalletFeeDataMessage;
96+
97+
const result = {
98+
gasPrice: walletResponse.gasPrice,
99+
maxFeePerGas: walletResponse.maxFeePerGas,
100+
maxPriorityFeePerGas: walletResponse.maxPriorityFeePerGas,
101+
} as FeeData;
102+
resolve(result);
103+
} else if (
104+
data.messageId === WALLET_WORKER_CRASHED_MESSAGE_ID
105+
&& data.chainId == chainId
106+
) {
107+
this.port.off("message", listener);
108+
109+
const result = {} as FeeData;
110+
resolve(result);
111+
}
112+
};
113+
this.port.on("message", listener);
114+
115+
const message: WalletGetFeeDataMessage = {
116+
type: WalletMessageType.GetFeeData,
117+
priority: priority ?? false,
118+
};
119+
120+
const portData: WalletPortData = {
121+
chainId,
122+
messageId,
123+
message,
124+
}
125+
this.port.postMessage(portData);
69126
});
70127

71128
return resultPromise;

0 commit comments

Comments
 (0)