Skip to content

Commit bbc7211

Browse files
committed
fix: low balance warning
1 parent 3c2ff3a commit bbc7211

File tree

1 file changed

+22
-8
lines changed

1 file changed

+22
-8
lines changed

src/services/Client.ts

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { nextTick } from "node:process";
22

3-
import { chains, PERCENTAGE_FACTOR } from "@gearbox-protocol/sdk";
3+
import { chains, formatBN, PERCENTAGE_FACTOR } from "@gearbox-protocol/sdk";
44
import type {
55
AnvilClient,
66
AnvilNodeInfo,
@@ -134,17 +134,31 @@ export default class Client {
134134
if (maxPriorityFeePerGas && maxFeePerGas) {
135135
req.maxPriorityFeePerGas = 10n * maxPriorityFeePerGas;
136136
req.maxFeePerGas = 2n * maxFeePerGas + req.maxPriorityFeePerGas;
137-
this.logger.debug(
138-
{
139-
maxFeePerGas: req.maxFeePerGas,
140-
maxPriorityFeePerGas: req.maxPriorityFeePerGas,
141-
},
142-
`increase gas fees`,
143-
);
144137
}
145138
if (gas) {
146139
req.gas = (gas * (GAS_X + PERCENTAGE_FACTOR)) / PERCENTAGE_FACTOR;
147140
}
141+
const txCost = req.gas * req.maxFeePerGas + (req.value ?? 0n);
142+
this.logger.debug(
143+
{
144+
maxFeePerGas: req.maxFeePerGas,
145+
maxPriorityFeePerGas: req.maxPriorityFeePerGas,
146+
gas: req.gas,
147+
txCost,
148+
},
149+
`increase gas fees`,
150+
);
151+
152+
if (this.#balance && txCost > this.#balance.value) {
153+
this.logger.warn(
154+
{
155+
txCost,
156+
balance: this.#balance.value,
157+
},
158+
`transaction cost ${formatBN(txCost, 18)} exceeds balance (${formatBN(this.#balance.value, 18)} ETH)`,
159+
);
160+
}
161+
148162
const serializedTransaction = await this.wallet.signTransaction(req);
149163
const hash = await this.wallet.sendRawTransaction({
150164
serializedTransaction,

0 commit comments

Comments
 (0)