From a30096df2412035e9f08963822a86ef07f87d256 Mon Sep 17 00:00:00 2001 From: weatherstar Date: Thu, 19 Dec 2024 16:11:03 +0800 Subject: [PATCH] fix: build okx bridge tx error OK-34425 (#6376) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * fix: buildOkxSwapEncodedTx add type params Signed-off-by: 王山栋 * fix: okx tron bridge build tx error * fix: lint --------- Signed-off-by: 王山栋 Co-authored-by: 王山栋 --- packages/kit-bg/src/services/ServiceGas.ts | 12 +-- packages/kit-bg/src/services/ServiceSwap.ts | 3 + .../kit-bg/src/vaults/impls/tron/Vault.ts | 77 ++++++++++++------- packages/kit-bg/src/vaults/types.ts | 2 + .../Setting/pages/FloatingIcon/index.tsx | 4 +- .../src/views/Swap/hooks/useSwapBuiltTx.ts | 4 + 6 files changed, 70 insertions(+), 32 deletions(-) diff --git a/packages/kit-bg/src/services/ServiceGas.ts b/packages/kit-bg/src/services/ServiceGas.ts index 03a8da2ac59..dd9d42bdf20 100644 --- a/packages/kit-bg/src/services/ServiceGas.ts +++ b/packages/kit-bg/src/services/ServiceGas.ts @@ -139,11 +139,13 @@ class ServiceGas extends ServiceBase { } return { ...item, - computationCostBase: item.computationCost ? new BigNumber(item.computationCost) - .dividedBy( - new BigNumber(item.gasPrice).shiftedBy(feeInfo.feeDecimals), - ) - .toFixed() : '0', + computationCostBase: item.computationCost + ? new BigNumber(item.computationCost) + .dividedBy( + new BigNumber(item.gasPrice).shiftedBy(feeInfo.feeDecimals), + ) + .toFixed() + : '0', }; }), }; diff --git a/packages/kit-bg/src/services/ServiceSwap.ts b/packages/kit-bg/src/services/ServiceSwap.ts index eecda257048..f6039454727 100644 --- a/packages/kit-bg/src/services/ServiceSwap.ts +++ b/packages/kit-bg/src/services/ServiceSwap.ts @@ -33,6 +33,7 @@ import { swapQuoteEventTimeout, } from '@onekeyhq/shared/types/swap/SwapProvider.constants'; import type { + ESwapTabSwitchType, IFetchBuildTxParams, IFetchBuildTxResponse, IFetchQuoteResult, @@ -1277,6 +1278,7 @@ export default class ServiceSwap extends ServiceBase { networkId: string; okxTx: IOKXTransactionObject; fromTokenInfo: ISwapTokenBase; + type: ESwapTabSwitchType; }) { const vault = await vaultFactory.getVault({ accountId: params.accountId, @@ -1285,6 +1287,7 @@ export default class ServiceSwap extends ServiceBase { return vault.buildOkxSwapEncodedTx({ okxTx: params.okxTx, fromTokenInfo: params.fromTokenInfo, + type: params.type, }); } } diff --git a/packages/kit-bg/src/vaults/impls/tron/Vault.ts b/packages/kit-bg/src/vaults/impls/tron/Vault.ts index 10517d86982..60e158a23c0 100644 --- a/packages/kit-bg/src/vaults/impls/tron/Vault.ts +++ b/packages/kit-bg/src/vaults/impls/tron/Vault.ts @@ -33,6 +33,7 @@ import type { IMeasureRpcStatusResult, } from '@onekeyhq/shared/types/customRpc'; import type { IOnChainHistoryTx } from '@onekeyhq/shared/types/history'; +import { ESwapTabSwitchType } from '@onekeyhq/shared/types/swap/types'; import { EDecodedTxActionType, EDecodedTxStatus, @@ -758,21 +759,53 @@ export default class Vault extends VaultBase { override async buildOkxSwapEncodedTx( params: IBuildOkxSwapEncodedTxParams, ): Promise { - const { okxTx, fromTokenInfo } = params; + const { okxTx, fromTokenInfo, type } = params; const { from, to, value, data, signatureData: _signatureData } = okxTx; const signatureData: { functionSelector: string } = JSON.parse( (_signatureData as string[])[0] ?? '{}', ); + const isSwapBridge = type === ESwapTabSwitchType.BRIDGE; + let signatureDataHex = ''; + if (signatureData) { signatureDataHex = signatureData.functionSelector ?? ''; } - const functionParams = defaultAbiCoder.decode( - ['uint256', 'uint256', 'uint256', 'bytes32[]'], - `0x${data.slice(10)}`, - ) as [{ _hex: string }, { _hex: string }, { _hex: string }, string[]]; + let buildTxParams = []; + if (isSwapBridge) { + const functionParams = defaultAbiCoder.decode( + [ + 'tuple(address,address,address,uint256,uint256,uint256,uint256,uint256,bytes,bytes,bytes)', + ], + `0x${data.slice(10)}`, + ); + buildTxParams = [ + { + type: 'tuple(address,address,address,uint256,uint256,uint256,uint256,uint256,bytes,bytes,bytes)', + value: functionParams[0], + }, + ]; + } else { + const functionParams = defaultAbiCoder.decode( + ['uint256', 'uint256', 'uint256', 'bytes32[]'], + `0x${data.slice(10)}`, + ) as [{ _hex: string }, { _hex: string }, { _hex: string }, string[]]; + + buildTxParams = [ + { type: 'uint256', value: functionParams[0]._hex }, + { + type: 'uint256', + value: functionParams[1]._hex, + }, + { type: 'uint256', value: functionParams[2]._hex }, + { + type: 'bytes32[]', + value: functionParams[3], + }, + ]; + } const [{ result, transaction }] = await this.backgroundApi.serviceAccountProfile.sendProxyRequest<{ @@ -792,42 +825,34 @@ export default class Vault extends VaultBase { feeLimit: 300_000_000, callValue: parseInt(value, 10), }, - [ - { type: 'uint256', value: functionParams[0]._hex }, - { - type: 'uint256', - value: functionParams[1]._hex, - }, - { type: 'uint256', value: functionParams[2]._hex }, - { - type: 'bytes32[]', - value: functionParams[3], - }, - ], + buildTxParams, from, ], }, }, ], }); + if (!result) { throw new OneKeyInternalError( 'Unable to build token transfer transaction', ); } - ( - transaction.raw_data.contract[0].parameter - .value as Types.TriggerSmartContract - ).data = data.slice(2); + if (!isSwapBridge) { + ( + transaction.raw_data.contract[0].parameter + .value as Types.TriggerSmartContract + ).data = data.slice(2); - const txPb = TronWeb.utils.transaction.txJsonToPb(transaction); + const txPb = TronWeb.utils.transaction.txJsonToPb(transaction); - const txRawDataHex = TronWeb.utils.transaction.txPbToRawDataHex(txPb); - const txID = TronWeb.utils.transaction.txPbToTxID(txPb); + const txRawDataHex = TronWeb.utils.transaction.txPbToRawDataHex(txPb); + const txID = TronWeb.utils.transaction.txPbToTxID(txPb); - transaction.raw_data_hex = txRawDataHex; - transaction.txID = txID.slice(2); + transaction.raw_data_hex = txRawDataHex; + transaction.txID = txID.slice(2); + } return transaction; } diff --git a/packages/kit-bg/src/vaults/types.ts b/packages/kit-bg/src/vaults/types.ts index 7fc4ab1ab59..dbbb686e443 100644 --- a/packages/kit-bg/src/vaults/types.ts +++ b/packages/kit-bg/src/vaults/types.ts @@ -37,6 +37,7 @@ import type { ILNURLPaymentInfo } from '@onekeyhq/shared/types/lightning'; import type { ENFTType } from '@onekeyhq/shared/types/nft'; import type { IStakingInfo } from '@onekeyhq/shared/types/staking'; import type { + ESwapTabSwitchType, IFetchBuildTxResult, IOKXTransactionObject, ISwapTokenBase, @@ -655,4 +656,5 @@ export type IQrWalletGetVerifyAddressChainParamsResult = { export type IBuildOkxSwapEncodedTxParams = { okxTx: IOKXTransactionObject; fromTokenInfo: ISwapTokenBase; + type: ESwapTabSwitchType; }; diff --git a/packages/kit/src/views/Setting/pages/FloatingIcon/index.tsx b/packages/kit/src/views/Setting/pages/FloatingIcon/index.tsx index 0ecd8a5dce2..95648326f49 100644 --- a/packages/kit/src/views/Setting/pages/FloatingIcon/index.tsx +++ b/packages/kit/src/views/Setting/pages/FloatingIcon/index.tsx @@ -32,7 +32,9 @@ function FloatingIconModal() { size={ESwitchSize.large} value={settings.isFloatingIconAlwaysDisplay} onChange={async (value) => { - await backgroundApiProxy.serviceSetting.setIsShowFloatingButton(value); + await backgroundApiProxy.serviceSetting.setIsShowFloatingButton( + value, + ); }} /> diff --git a/packages/kit/src/views/Swap/hooks/useSwapBuiltTx.ts b/packages/kit/src/views/Swap/hooks/useSwapBuiltTx.ts index 2c2c782b3c5..2898043646e 100644 --- a/packages/kit/src/views/Swap/hooks/useSwapBuiltTx.ts +++ b/packages/kit/src/views/Swap/hooks/useSwapBuiltTx.ts @@ -49,6 +49,7 @@ import { useSwapSelectFromTokenAtom, useSwapSelectToTokenAtom, useSwapShouldRefreshQuoteAtom, + useSwapTypeSwitchAtom, } from '../../../states/jotai/contexts/swap'; import { useSwapAddressInfo } from './useSwapAccount'; @@ -69,6 +70,7 @@ export function useSwapBuildTx() { const [settingsPersistAtom] = useSettingsPersistAtom(); const [, setSwapFromTokenAmount] = useSwapFromTokenAmountAtom(); const [, setSwapShouldRefreshQuote] = useSwapShouldRefreshQuoteAtom(); + const [swapTypeSwitch] = useSwapTypeSwitchAtom(); const swapFromAddressInfo = useSwapAddressInfo(ESwapDirectionType.FROM); const swapToAddressInfo = useSwapAddressInfo(ESwapDirectionType.TO); const [, setSwapManualSelectQuoteProviders] = @@ -446,6 +448,7 @@ export function useSwapBuildTx() { networkId: res.result.fromTokenInfo.networkId, okxTx: res.OKXTxObject, fromTokenInfo: res.result.fromTokenInfo, + type: swapTypeSwitch, }); } else if (res?.tx) { transferInfo = undefined; @@ -510,6 +513,7 @@ export function useSwapBuildTx() { fromToken, toToken, selectQuote, + swapTypeSwitch, slippageItem, swapFromAddressInfo.address, swapFromAddressInfo.networkId,