Skip to content

Commit

Permalink
fix: build okx bridge tx error OK-34425 (#6376)
Browse files Browse the repository at this point in the history
* fix: buildOkxSwapEncodedTx  add type params

Signed-off-by: 王山栋 <[email protected]>

* fix: okx tron bridge build tx error

* fix: lint

---------

Signed-off-by: 王山栋 <[email protected]>
Co-authored-by: 王山栋 <[email protected]>
  • Loading branch information
weatherstar and ezailWang authored Dec 19, 2024
1 parent 9976b56 commit a30096d
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 32 deletions.
12 changes: 7 additions & 5 deletions packages/kit-bg/src/services/ServiceGas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
};
}),
};
Expand Down
3 changes: 3 additions & 0 deletions packages/kit-bg/src/services/ServiceSwap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import {
swapQuoteEventTimeout,
} from '@onekeyhq/shared/types/swap/SwapProvider.constants';
import type {
ESwapTabSwitchType,
IFetchBuildTxParams,
IFetchBuildTxResponse,
IFetchQuoteResult,
Expand Down Expand Up @@ -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,
Expand All @@ -1285,6 +1287,7 @@ export default class ServiceSwap extends ServiceBase {
return vault.buildOkxSwapEncodedTx({
okxTx: params.okxTx,
fromTokenInfo: params.fromTokenInfo,
type: params.type,
});
}
}
77 changes: 51 additions & 26 deletions packages/kit-bg/src/vaults/impls/tron/Vault.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -758,21 +759,53 @@ export default class Vault extends VaultBase {
override async buildOkxSwapEncodedTx(
params: IBuildOkxSwapEncodedTxParams,
): Promise<IEncodedTxTron> {
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<{
Expand All @@ -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;
}
Expand Down
2 changes: 2 additions & 0 deletions packages/kit-bg/src/vaults/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -655,4 +656,5 @@ export type IQrWalletGetVerifyAddressChainParamsResult = {
export type IBuildOkxSwapEncodedTxParams = {
okxTx: IOKXTransactionObject;
fromTokenInfo: ISwapTokenBase;
type: ESwapTabSwitchType;
};
4 changes: 3 additions & 1 deletion packages/kit/src/views/Setting/pages/FloatingIcon/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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,
);
}}
/>
</XStack>
Expand Down
4 changes: 4 additions & 0 deletions packages/kit/src/views/Swap/hooks/useSwapBuiltTx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ import {
useSwapSelectFromTokenAtom,
useSwapSelectToTokenAtom,
useSwapShouldRefreshQuoteAtom,
useSwapTypeSwitchAtom,
} from '../../../states/jotai/contexts/swap';

import { useSwapAddressInfo } from './useSwapAccount';
Expand All @@ -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] =
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -510,6 +513,7 @@ export function useSwapBuildTx() {
fromToken,
toToken,
selectQuote,
swapTypeSwitch,
slippageItem,
swapFromAddressInfo.address,
swapFromAddressInfo.networkId,
Expand Down

0 comments on commit a30096d

Please sign in to comment.