-
Notifications
You must be signed in to change notification settings - Fork 395
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: support batch estimate fee through tenderly on hw OK-34493 (#6399)
* fix: hw/external account no use multiple popover Signed-off-by: 王山栋 <[email protected]> * feat: swap send confirm entry * feat: batch estimate swap fee on hw&external * fix: lint --------- Signed-off-by: 王山栋 <[email protected]> Co-authored-by: 王山栋 <[email protected]>
- Loading branch information
1 parent
7c70803
commit 736d593
Showing
11 changed files
with
243 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
160 changes: 160 additions & 0 deletions
160
packages/kit/src/views/Send/pages/SendConfirmFromSwap/SendConfirmFromSwap.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,160 @@ | ||
import { useCallback, useEffect, useRef } from 'react'; | ||
|
||
import { useRoute } from '@react-navigation/core'; | ||
import { StackActions, useNavigation } from '@react-navigation/native'; | ||
import { AppState } from 'react-native'; | ||
|
||
import { Page, Spinner, Stack } from '@onekeyhq/components'; | ||
import backgroundApiProxy from '@onekeyhq/kit/src/background/instance/backgroundApiProxy'; | ||
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 { ISendTxOnSuccessData } from '@onekeyhq/shared/types/tx'; | ||
|
||
import type { RouteProp } from '@react-navigation/core'; | ||
import type { StackActionType } from '@react-navigation/native'; | ||
|
||
function SendConfirmFromSwap() { | ||
const pendingAction = useRef<StackActionType>(); | ||
const navigation = useNavigation(); | ||
const appNavigation = useAppNavigation(); | ||
|
||
const route = | ||
useRoute< | ||
RouteProp<IModalSendParamList, EModalSendRoutes.SendConfirmFromSwap> | ||
>(); | ||
|
||
const { networkId, accountId, unsignedTxs, onSuccess, onFail, onCancel } = | ||
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[]; | ||
}[]; | ||
}) => { | ||
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, | ||
}; | ||
const isLastTx = i === len - 1; | ||
|
||
await new Promise((resolve) => { | ||
appNavigation.push(EModalSendRoutes.SendConfirm, { | ||
...route.params, | ||
popStack: false, | ||
unsignedTxs: [unsignedTx], | ||
onSuccess: (data: ISendTxOnSuccessData[]) => { | ||
if (isLastTx) { | ||
onSuccess?.(data); | ||
appNavigation.popStack(); | ||
} | ||
resolve(data); | ||
}, | ||
onFail: (error: Error) => { | ||
onFail?.(error); | ||
|
||
appNavigation.popStack(); | ||
}, | ||
onCancel: () => { | ||
onCancel?.(); | ||
appNavigation.popStack(); | ||
}, | ||
}); | ||
}); | ||
} | ||
}, | ||
[unsignedTxs, appNavigation, route.params, onSuccess, onFail, onCancel], | ||
); | ||
|
||
const navigationToSendConfirm = useCallback(async () => { | ||
let action: any; | ||
let batchEstimateButSingleConfirm = false; | ||
const isMultiTxs = unsignedTxs.length > 1; | ||
if ( | ||
isMultiTxs && | ||
(accountUtils.isHwAccount({ accountId }) || | ||
accountUtils.isExternalAccount({ accountId })) | ||
) { | ||
const vaultSettings = | ||
await backgroundApiProxy.serviceNetwork.getVaultSettings({ | ||
networkId, | ||
}); | ||
if (vaultSettings.supportBatchEstimateFee?.[networkId]) { | ||
try { | ||
const encodedTxList = unsignedTxs.map((tx) => tx.encodedTx); | ||
const multiTxsFeeResult = | ||
await backgroundApiProxy.serviceGas.batchEstimateFee({ | ||
accountId, | ||
networkId, | ||
encodedTxs: encodedTxList, | ||
}); | ||
if (multiTxsFeeResult.txFees.length === unsignedTxs.length) { | ||
await handleConfirmMultiTxsOnHwOrExternal(multiTxsFeeResult); | ||
batchEstimateButSingleConfirm = true; | ||
} else { | ||
batchEstimateButSingleConfirm = false; | ||
} | ||
} catch (e) { | ||
batchEstimateButSingleConfirm = false; | ||
} | ||
} | ||
} | ||
|
||
if (!batchEstimateButSingleConfirm) { | ||
action = StackActions.replace(EModalSendRoutes.SendConfirm, { | ||
...route.params, | ||
// @ts-ignore | ||
_disabledAnimationOfNavigate: true, | ||
}); | ||
} | ||
|
||
if (action) { | ||
if (AppState.currentState === 'active') { | ||
setTimeout(() => navigation.dispatch(action)); | ||
} else { | ||
pendingAction.current = action; | ||
} | ||
} | ||
}, [ | ||
accountId, | ||
handleConfirmMultiTxsOnHwOrExternal, | ||
navigation, | ||
networkId, | ||
route.params, | ||
unsignedTxs, | ||
]); | ||
|
||
const handleOnClose = () => { | ||
onCancel?.(); | ||
}; | ||
|
||
useEffect(() => { | ||
void navigationToSendConfirm(); | ||
}, [navigation, navigationToSendConfirm, route.params, unsignedTxs]); | ||
|
||
return ( | ||
<Page onClose={handleOnClose}> | ||
<Page.Body> | ||
<Stack h="100%" justifyContent="center" alignContent="center"> | ||
<Spinner size="large" /> | ||
</Stack> | ||
</Page.Body> | ||
</Page> | ||
); | ||
} | ||
export { SendConfirmFromSwap }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.