Skip to content

Commit a11482d

Browse files
authored
Merge branch 'master' into 2025-01-17-mem-leak-debug
2 parents b32a96c + 2af8ca4 commit a11482d

File tree

9 files changed

+319
-8
lines changed

9 files changed

+319
-8
lines changed

src/modes/interOrderbook.ts

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,23 @@ export async function dryrun({
125125
const estimation = await estimateGasCost(rawtx, signer, config, l1GasPrice);
126126
l1Cost = estimation.l1Cost;
127127
gasLimit = ethers.BigNumber.from(estimation.gas).mul(config.gasLimitMultiplier).div(100);
128+
129+
// include dryrun headroom gas estimation in otel logs
130+
extendSpanAttributes(
131+
spanAttributes,
132+
{
133+
gasLimit: estimation.gas.toString(),
134+
totalCost: estimation.totalGasCost.toString(),
135+
gasPrice: estimation.gasPrice.toString(),
136+
...(config.isSpecialL2
137+
? {
138+
l1Cost: estimation.l1Cost.toString(),
139+
l1GasPrice: estimation.l1GasPrice.toString(),
140+
}
141+
: {}),
142+
},
143+
"gasEst.headroom",
144+
);
128145
} catch (e) {
129146
const isNodeError = containsNodeError(e as BaseError);
130147
const errMsg = errorSnapshot("", e);
@@ -153,6 +170,10 @@ export async function dryrun({
153170
// sender output which is already called above
154171
if (config.gasCoveragePercentage !== "0") {
155172
const headroom = (Number(config.gasCoveragePercentage) * 1.03).toFixed();
173+
spanAttributes["gasEst.headroom.minBountyExpected"] = gasCost
174+
.mul(headroom)
175+
.div("100")
176+
.toString();
156177
task.evaluable.bytecode = await parseRainlang(
157178
await getBountyEnsureRainlang(
158179
ethers.utils.parseUnits(inputToEthPrice),
@@ -177,12 +198,34 @@ export async function dryrun({
177198
.div(100);
178199
if (gasLimit.isZero()) {
179200
throw new ExecutionRevertedError({
201+
cause: new BaseError("RPC returned 0 for eth_estimateGas", {
202+
cause: new Error(
203+
"Failed to estimated gas, RPC returned 0 for eth_estimateGas call without rejection",
204+
),
205+
}),
180206
message:
181-
"Failed to estimated gas, rpc returned 0 for gasEstimate call without rejection",
207+
"Failed to estimated gas, RPC returned 0 for eth_estimateGas call without rejection",
182208
});
183209
}
184210
rawtx.gas = gasLimit.toBigInt();
185211
gasCost = gasLimit.mul(gasPrice).add(estimation.l1Cost);
212+
213+
// include dryrun final gas estimation in otel logs
214+
extendSpanAttributes(
215+
spanAttributes,
216+
{
217+
gasLimit: estimation.gas.toString(),
218+
totalCost: estimation.totalGasCost.toString(),
219+
gasPrice: estimation.gasPrice.toString(),
220+
...(config.isSpecialL2
221+
? {
222+
l1Cost: estimation.l1Cost.toString(),
223+
l1GasPrice: estimation.l1GasPrice.toString(),
224+
}
225+
: {}),
226+
},
227+
"gasEst.final",
228+
);
186229
task.evaluable.bytecode = await parseRainlang(
187230
await getBountyEnsureRainlang(
188231
ethers.utils.parseUnits(inputToEthPrice),
@@ -198,6 +241,10 @@ export async function dryrun({
198241
takeOrdersConfigStruct,
199242
task,
200243
]);
244+
spanAttributes["gasEst.final.minBountyExpected"] = gasCost
245+
.mul(config.gasCoveragePercentage)
246+
.div("100")
247+
.toString();
201248
} catch (e) {
202249
const isNodeError = containsNodeError(e as BaseError);
203250
const errMsg = errorSnapshot("", e);

src/modes/intraOrderbook.ts

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,23 @@ export async function dryrun({
117117
const estimation = await estimateGasCost(rawtx, signer, config, l1GasPrice);
118118
l1Cost = estimation.l1Cost;
119119
gasLimit = ethers.BigNumber.from(estimation.gas).mul(config.gasLimitMultiplier).div(100);
120+
121+
// include dryrun headroom gas estimation in otel logs
122+
extendSpanAttributes(
123+
spanAttributes,
124+
{
125+
gasLimit: estimation.gas.toString(),
126+
totalCost: estimation.totalGasCost.toString(),
127+
gasPrice: estimation.gasPrice.toString(),
128+
...(config.isSpecialL2
129+
? {
130+
l1Cost: estimation.l1Cost.toString(),
131+
l1GasPrice: estimation.l1GasPrice.toString(),
132+
}
133+
: {}),
134+
},
135+
"gasEst.headroom",
136+
);
120137
} catch (e) {
121138
// reason, code, method, transaction, error, stack, message
122139
const isNodeError = containsNodeError(e as BaseError);
@@ -146,6 +163,10 @@ export async function dryrun({
146163
// sender output which is already called above
147164
if (config.gasCoveragePercentage !== "0") {
148165
const headroom = (Number(config.gasCoveragePercentage) * 1.03).toFixed();
166+
spanAttributes["gasEst.headroom.minBountyExpected"] = gasCost
167+
.mul(headroom)
168+
.div("100")
169+
.toString();
149170
task.evaluable.bytecode = await parseRainlang(
150171
await getWithdrawEnsureRainlang(
151172
signer.account.address,
@@ -179,12 +200,34 @@ export async function dryrun({
179200
.div(100);
180201
if (gasLimit.isZero()) {
181202
throw new ExecutionRevertedError({
203+
cause: new BaseError("RPC returned 0 for eth_estimateGas", {
204+
cause: new Error(
205+
"Failed to estimated gas, RPC returned 0 for eth_estimateGas call without rejection",
206+
),
207+
}),
182208
message:
183-
"Failed to estimated gas, rpc returned 0 for gasEstimate call without rejection",
209+
"Failed to estimated gas, RPC returned 0 for eth_estimateGas call without rejection",
184210
});
185211
}
186212
rawtx.gas = gasLimit.toBigInt();
187213
gasCost = gasLimit.mul(gasPrice).add(estimation.l1Cost);
214+
215+
// include dryrun final gas estimation in otel logs
216+
extendSpanAttributes(
217+
spanAttributes,
218+
{
219+
gasLimit: estimation.gas.toString(),
220+
totalCost: estimation.totalGasCost.toString(),
221+
gasPrice: estimation.gasPrice.toString(),
222+
...(config.isSpecialL2
223+
? {
224+
l1Cost: estimation.l1Cost.toString(),
225+
l1GasPrice: estimation.l1GasPrice.toString(),
226+
}
227+
: {}),
228+
},
229+
"gasEst.final",
230+
);
188231
task.evaluable.bytecode = await parseRainlang(
189232
await getWithdrawEnsureRainlang(
190233
signer.account.address,
@@ -209,6 +252,10 @@ export async function dryrun({
209252
rawtx.data = obInterface.encodeFunctionData("multicall", [
210253
[clear2Calldata, withdrawInputCalldata, withdrawOutputCalldata],
211254
]);
255+
spanAttributes["gasEst.final.minBountyExpected"] = gasCost
256+
.mul(config.gasCoveragePercentage)
257+
.div("100")
258+
.toString();
212259
} catch (e) {
213260
const isNodeError = containsNodeError(e as BaseError);
214261
const errMsg = errorSnapshot("", e);

src/modes/routeProcessor.ts

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,23 @@ export async function dryrun({
189189
gasLimit = ethers.BigNumber.from(estimation.gas)
190190
.mul(config.gasLimitMultiplier)
191191
.div(100);
192+
193+
// include dryrun headroom gas estimation in otel logs
194+
extendSpanAttributes(
195+
spanAttributes,
196+
{
197+
gasLimit: estimation.gas.toString(),
198+
totalCost: estimation.totalGasCost.toString(),
199+
gasPrice: estimation.gasPrice.toString(),
200+
...(config.isSpecialL2
201+
? {
202+
l1Cost: estimation.l1Cost.toString(),
203+
l1GasPrice: estimation.l1GasPrice.toString(),
204+
}
205+
: {}),
206+
},
207+
"gasEst.headroom",
208+
);
192209
} catch (e) {
193210
// reason, code, method, transaction, error, stack, message
194211
const isNodeError = containsNodeError(e as BaseError);
@@ -219,6 +236,10 @@ export async function dryrun({
219236
// sender output which is already called above
220237
if (config.gasCoveragePercentage !== "0") {
221238
const headroom = (Number(config.gasCoveragePercentage) * 1.03).toFixed();
239+
spanAttributes["gasEst.headroom.minBountyExpected"] = gasCost
240+
.mul(headroom)
241+
.div("100")
242+
.toString();
222243
task.evaluable.bytecode = await parseRainlang(
223244
await getBountyEnsureRainlang(
224245
ethers.utils.parseUnits(ethPrice),
@@ -243,12 +264,34 @@ export async function dryrun({
243264
.div(100);
244265
if (gasLimit.isZero()) {
245266
throw new ExecutionRevertedError({
267+
cause: new BaseError("RPC returned 0 for eth_estimateGas", {
268+
cause: new Error(
269+
"Failed to estimated gas, RPC returned 0 for eth_estimateGas call without rejection",
270+
),
271+
}),
246272
message:
247-
"Failed to estimated gas, rpc returned 0 for gasEstimate call without rejection",
273+
"Failed to estimated gas, RPC returned 0 for eth_estimateGas call without rejection",
248274
});
249275
}
250276
rawtx.gas = gasLimit.toBigInt();
251277
gasCost = gasLimit.mul(gasPrice).add(estimation.l1Cost);
278+
279+
// include dryrun final gas estimation in otel logs
280+
extendSpanAttributes(
281+
spanAttributes,
282+
{
283+
gasLimit: estimation.gas.toString(),
284+
totalCost: estimation.totalGasCost.toString(),
285+
gasPrice: estimation.gasPrice.toString(),
286+
...(config.isSpecialL2
287+
? {
288+
l1Cost: estimation.l1Cost.toString(),
289+
l1GasPrice: estimation.l1GasPrice.toString(),
290+
}
291+
: {}),
292+
},
293+
"gasEst.final",
294+
);
252295
task.evaluable.bytecode = await parseRainlang(
253296
await getBountyEnsureRainlang(
254297
ethers.utils.parseUnits(ethPrice),
@@ -264,6 +307,10 @@ export async function dryrun({
264307
takeOrdersConfigStruct,
265308
task,
266309
]);
310+
spanAttributes["gasEst.final.minBountyExpected"] = gasCost
311+
.mul(config.gasCoveragePercentage)
312+
.div("100")
313+
.toString();
267314
} catch (e) {
268315
const isNodeError = containsNodeError(e as BaseError);
269316
const errMsg = errorSnapshot("", e);

test/findOpp.test.js

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,18 @@ describe("Test find opp", async function () {
141141
marketPrice: formatUnits(getCurrentPrice(vaultBalance)),
142142
route: expectedRouteVisual,
143143
clearModePick: "rp4",
144+
"gasEst.final.gasLimit": gasLimitEstimation.toString(),
145+
"gasEst.final.totalCost": gasLimitEstimation.mul(gasPrice).toString(),
146+
"gasEst.final.gasPrice": gasPrice.toString(),
147+
"gasEst.final.minBountyExpected": gasLimitEstimation.mul(gasPrice).toString(),
148+
"gasEst.headroom.gasLimit": gasLimitEstimation.toString(),
149+
"gasEst.headroom.totalCost": gasLimitEstimation.mul(gasPrice).toString(),
150+
"gasEst.headroom.gasPrice": gasPrice.toString(),
151+
"gasEst.headroom.minBountyExpected": gasLimitEstimation
152+
.mul(gasPrice)
153+
.mul(103)
154+
.div(100)
155+
.toString(),
144156
},
145157
};
146158
assert.deepEqual(result, expected);
@@ -237,6 +249,18 @@ describe("Test find opp", async function () {
237249
foundOpp: true,
238250
maxInput: vaultBalance.toString(),
239251
clearModePick: "inter",
252+
"gasEst.final.gasLimit": gasLimitEstimation.toString(),
253+
"gasEst.final.totalCost": gasLimitEstimation.mul(gasPrice).toString(),
254+
"gasEst.final.gasPrice": gasPrice.toString(),
255+
"gasEst.final.minBountyExpected": gasLimitEstimation.mul(gasPrice).toString(),
256+
"gasEst.headroom.gasLimit": gasLimitEstimation.toString(),
257+
"gasEst.headroom.totalCost": gasLimitEstimation.mul(gasPrice).toString(),
258+
"gasEst.headroom.gasPrice": gasPrice.toString(),
259+
"gasEst.headroom.minBountyExpected": gasLimitEstimation
260+
.mul(gasPrice)
261+
.mul(103)
262+
.div(100)
263+
.toString(),
240264
},
241265
};
242266
assert.deepEqual(result, expected);
@@ -338,6 +362,18 @@ describe("Test find opp", async function () {
338362
oppBlockNumber,
339363
foundOpp: true,
340364
clearModePick: "intra",
365+
"gasEst.final.gasLimit": gasLimitEstimation.toString(),
366+
"gasEst.final.totalCost": gasLimitEstimation.mul(gasPrice).toString(),
367+
"gasEst.final.gasPrice": gasPrice.toString(),
368+
"gasEst.final.minBountyExpected": gasLimitEstimation.mul(gasPrice).toString(),
369+
"gasEst.headroom.gasLimit": gasLimitEstimation.toString(),
370+
"gasEst.headroom.totalCost": gasLimitEstimation.mul(gasPrice).toString(),
371+
"gasEst.headroom.gasPrice": gasPrice.toString(),
372+
"gasEst.headroom.minBountyExpected": gasLimitEstimation
373+
.mul(gasPrice)
374+
.mul(103)
375+
.div(100)
376+
.toString(),
341377
},
342378
};
343379
assert.deepEqual(result, expected);

test/gas.test.ts

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ describe("Test gas", async function () {
1414
// mock L1 signer for L2 client
1515
const l1Signer = {
1616
getL1BaseFee: async () => 20n,
17-
estimateL1Gas: async () => 5n,
17+
estimateL1Fee: async () => 5n,
1818
};
1919
// mock normal L1 signer
2020
const signer = {
@@ -32,10 +32,9 @@ describe("Test gas", async function () {
3232
const expected1 = {
3333
gas: 55n,
3434
gasPrice: 2n,
35-
l1Gas: 5n,
3635
l1GasPrice: 20n,
37-
l1Cost: 20n * 5n,
38-
totalGasCost: 2n * 55n + 20n * 5n,
36+
l1Cost: 5n,
37+
totalGasCost: 2n * 55n + 5n,
3938
};
4039
assert.deepEqual(result1, expected1);
4140

@@ -45,7 +44,6 @@ describe("Test gas", async function () {
4544
const expected2 = {
4645
gas: 55n,
4746
gasPrice: 2n,
48-
l1Gas: 0n,
4947
l1GasPrice: 0n,
5048
l1Cost: 0n,
5149
totalGasCost: 2n * 55n,

test/mode-interOrderbook.test.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,18 @@ describe("Test inter-orderbook dryrun", async function () {
124124
oppBlockNumber,
125125
foundOpp: true,
126126
maxInput: vaultBalance.toString(),
127+
"gasEst.final.gasLimit": gasLimitEstimation.toString(),
128+
"gasEst.final.totalCost": gasLimitEstimation.mul(gasPrice).toString(),
129+
"gasEst.final.gasPrice": gasPrice.toString(),
130+
"gasEst.final.minBountyExpected": gasLimitEstimation.mul(gasPrice).toString(),
131+
"gasEst.headroom.gasLimit": gasLimitEstimation.toString(),
132+
"gasEst.headroom.totalCost": gasLimitEstimation.mul(gasPrice).toString(),
133+
"gasEst.headroom.gasPrice": gasPrice.toString(),
134+
"gasEst.headroom.minBountyExpected": gasLimitEstimation
135+
.mul(gasPrice)
136+
.mul(103)
137+
.div(100)
138+
.toString(),
127139
},
128140
};
129141
assert.deepEqual(result, expected);
@@ -263,6 +275,18 @@ describe("Test inter-orderbook find opp", async function () {
263275
oppBlockNumber,
264276
foundOpp: true,
265277
maxInput: vaultBalance.toString(),
278+
"gasEst.final.gasLimit": gasLimitEstimation.toString(),
279+
"gasEst.final.totalCost": gasLimitEstimation.mul(gasPrice).toString(),
280+
"gasEst.final.gasPrice": gasPrice.toString(),
281+
"gasEst.final.minBountyExpected": gasLimitEstimation.mul(gasPrice).toString(),
282+
"gasEst.headroom.gasLimit": gasLimitEstimation.toString(),
283+
"gasEst.headroom.totalCost": gasLimitEstimation.mul(gasPrice).toString(),
284+
"gasEst.headroom.gasPrice": gasPrice.toString(),
285+
"gasEst.headroom.minBountyExpected": gasLimitEstimation
286+
.mul(gasPrice)
287+
.mul(103)
288+
.div(100)
289+
.toString(),
266290
},
267291
};
268292
assert.deepEqual(result, expected);

0 commit comments

Comments
 (0)