Skip to content

Commit 82a46e5

Browse files
committed
refactor simulateGasWithPendingErc20Approval method
1 parent 7fe8da9 commit 82a46e5

File tree

3 files changed

+18
-29
lines changed

3 files changed

+18
-29
lines changed

apps/extension/src/pages/ibc-swap/index.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1362,6 +1362,8 @@ export const IBCSwapPage: FunctionComponent = observer(() => {
13621362
},
13631363
{
13641364
nonceMethod: "pending",
1365+
considerRequiredErc20ApprovalsForNonce:
1366+
evmTxIndex !== 0 && erc20Approval != null,
13651367
}
13661368
);
13671369

apps/extension/src/pages/main/components/ibc-history-view/index.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1450,7 +1450,7 @@ const SwapV2HistoryViewItem: FunctionComponent<{
14501450
return (
14511451
<Box
14521452
padding="1.25rem"
1453-
borderRadius="0.375rem"
1453+
borderRadius="1.25rem"
14541454
backgroundColor={
14551455
theme.mode === "light" ? ColorPalette.white : ColorPalette["gray-600"]
14561456
}
@@ -1618,7 +1618,7 @@ const SwapV2HistoryViewItem: FunctionComponent<{
16181618
);
16191619
}
16201620

1621-
// skip history의 amount에는 [sourceChain의 amount, destinationChain의 expected amount]가 들어있으므로
1621+
// swap v2 history의 amount에는 [sourceChain의 amount, destinationChain의 expected amount]가 들어있으므로
16221622
// 첫 번째 amount만 사용
16231623
const assets = (() => {
16241624
const amount = history.amount[0];

packages/stores-eth/src/account/base.ts

Lines changed: 14 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -265,29 +265,14 @@ export class EthereumAccountBase {
265265

266266
const erc20ApprovalTx = erc20ApprovalTxs[0];
267267

268-
try {
269-
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
270-
const keplr = (await this.getKeplr())!;
271-
272-
const erc20ApprovalGasEstimated = await keplr.ethereum.request<
273-
string | undefined
274-
>({
275-
method: "eth_estimateGas",
276-
params: [
277-
{
278-
from: sender,
279-
to: erc20ApprovalTx.to,
280-
value: erc20ApprovalTx.value,
281-
data: erc20ApprovalTx.data,
282-
},
283-
],
284-
chainId: this.chainId,
285-
});
286-
287-
if (!erc20ApprovalGasEstimated) {
288-
throw new Error("Failed to estimate gas for ERC20 approval");
289-
}
268+
// First, estimate gas for the ERC20 approval tx itself.
269+
// This result is reused whether the bundle simulation with state diff succeeds or falls back.
270+
const erc20ApprovalSimResult = await this.simulateGas(
271+
sender,
272+
erc20ApprovalTx
273+
);
290274

275+
try {
291276
// State diff tracing for the ERC20 approval transaction
292277
const approvalStateDiff = await traceCallWithDiff(chainInfo.rpc, {
293278
from: sender,
@@ -321,16 +306,14 @@ export class EthereumAccountBase {
321306
return {
322307
kind: EVMGasSimulateKind.TX_BUNDLE_SIMULATED,
323308
gasUsed: result.gasUsed,
324-
erc20ApprovalGasUsed: parseInt(erc20ApprovalGasEstimated),
309+
erc20ApprovalGasUsed: erc20ApprovalSimResult.gasUsed,
325310
};
326311
} catch (e) {
327312
console.error("Failed to simulate gas with pending ERC20 approval", e);
328313

329-
// fallback to simulate only the erc20 approval tx
330-
const result = await this.simulateGas(sender, erc20ApprovalTx);
331314
return {
332315
kind: EVMGasSimulateKind.APPROVAL_ONLY_SIMULATED,
333-
erc20ApprovalGasUsed: result.gasUsed,
316+
erc20ApprovalGasUsed: erc20ApprovalSimResult.gasUsed,
334317
};
335318
}
336319
}
@@ -467,6 +450,8 @@ export class EthereumAccountBase {
467450
unsignedTx: UnsignedEVMTransactionWithErc20Approvals,
468451
options?: {
469452
nonceMethod?: "pending" | "latest";
453+
// Whether to offset nonce by the number of required ERC20 approvals
454+
considerRequiredErc20ApprovalsForNonce?: boolean;
470455
}
471456
) {
472457
const chainInfo = this.chainGetter.getChain(this.chainId);
@@ -488,7 +473,9 @@ export class EthereumAccountBase {
488473
...unsignedTx,
489474
nonce:
490475
parseInt(transactionCount) +
491-
(unsignedTx.requiredErc20Approvals?.length ?? 0),
476+
(options?.considerRequiredErc20ApprovalsForNonce
477+
? unsignedTx.requiredErc20Approvals?.length ?? 0
478+
: 0),
492479
};
493480

494481
const signEthereum = keplr.signEthereum.bind(keplr);

0 commit comments

Comments
 (0)