Skip to content

Commit

Permalink
fix: batch estimate fee error fallback (#6401)
Browse files Browse the repository at this point in the history
  • Loading branch information
weatherstar authored Dec 24, 2024
1 parent 736d593 commit 03a3218
Showing 1 changed file with 47 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,11 @@ import useAppNavigation from '@onekeyhq/kit/src/hooks/useAppNavigation';
import type { IModalSendParamList } from '@onekeyhq/shared/src/routes';
import { EModalSendRoutes } from '@onekeyhq/shared/src/routes';
import accountUtils from '@onekeyhq/shared/src/utils/accountUtils';
import type { IGasEIP1559, IGasLegacy } from '@onekeyhq/shared/types/fee';
import type {
IFeeInfoUnit,
IGasEIP1559,
IGasLegacy,
} from '@onekeyhq/shared/types/fee';
import type { ISendTxOnSuccessData } from '@onekeyhq/shared/types/tx';

import type { RouteProp } from '@react-navigation/core';
Expand All @@ -30,52 +34,66 @@ function SendConfirmFromSwap() {
route.params;

const handleConfirmMultiTxsOnHwOrExternal = useCallback(
async (multiTxsFeeResult: {
common: {
baseFee: string | undefined;
feeDecimals: number;
feeSymbol: string;
nativeDecimals: number;
nativeSymbol: string;
nativeTokenPrice: number | undefined;
};
txFees: {
gas: IGasLegacy[];
gasEIP1559: IGasEIP1559[];
}[];
}) => {
async (
multiTxsFeeResult:
| {
common: {
baseFee: string | undefined;
feeDecimals: number;
feeSymbol: string;
nativeDecimals: number;
nativeSymbol: string;
nativeTokenPrice: number | undefined;
};
txFees: {
gas: IGasLegacy[];
gasEIP1559: IGasEIP1559[];
}[];
}
| undefined,
) => {
let prevTxFeeInfo: IFeeInfoUnit | undefined;

for (let i = 0, len = unsignedTxs.length; i < len; i += 1) {
const unsignedTx = unsignedTxs[i];
unsignedTx.feesInfo = {
common: multiTxsFeeResult.common,
gas: multiTxsFeeResult.txFees[i].gas,
gasEIP1559: multiTxsFeeResult.txFees[i].gasEIP1559,
};
if (multiTxsFeeResult) {
unsignedTx.feesInfo = {
common: multiTxsFeeResult.common,
gas: multiTxsFeeResult.txFees[i].gas,
gasEIP1559: multiTxsFeeResult.txFees[i].gasEIP1559,
};
} else if (prevTxFeeInfo) {
unsignedTx.feeInfo = prevTxFeeInfo;
}
const isLastTx = i === len - 1;

await new Promise((resolve) => {
const result: ISendTxOnSuccessData[] = await new Promise((resolve) => {
appNavigation.push(EModalSendRoutes.SendConfirm, {
...route.params,
popStack: false,
unsignedTxs: [unsignedTx],
onSuccess: (data: ISendTxOnSuccessData[]) => {
if (isLastTx) {
onSuccess?.(data);
appNavigation.popStack();
appNavigation.pop();
}
resolve(data);
},
onFail: (error: Error) => {
onFail?.(error);

appNavigation.popStack();
appNavigation.pop();
},
onCancel: () => {
onCancel?.();
appNavigation.popStack();
appNavigation.pop();
},
});
});

if (result && result.length > 0 && result[0].feeInfo) {
prevTxFeeInfo = result[0].feeInfo;
}
}
},
[unsignedTxs, appNavigation, route.params, onSuccess, onFail, onCancel],
Expand All @@ -90,6 +108,7 @@ function SendConfirmFromSwap() {
(accountUtils.isHwAccount({ accountId }) ||
accountUtils.isExternalAccount({ accountId }))
) {
batchEstimateButSingleConfirm = true;
const vaultSettings =
await backgroundApiProxy.serviceNetwork.getVaultSettings({
networkId,
Expand All @@ -105,13 +124,14 @@ function SendConfirmFromSwap() {
});
if (multiTxsFeeResult.txFees.length === unsignedTxs.length) {
await handleConfirmMultiTxsOnHwOrExternal(multiTxsFeeResult);
batchEstimateButSingleConfirm = true;
} else {
batchEstimateButSingleConfirm = false;
await handleConfirmMultiTxsOnHwOrExternal(undefined);
}
} catch (e) {
batchEstimateButSingleConfirm = false;
await handleConfirmMultiTxsOnHwOrExternal(undefined);
}
} else {
await handleConfirmMultiTxsOnHwOrExternal(undefined);
}
}

Expand Down

0 comments on commit 03a3218

Please sign in to comment.