11import {
22 AcrossClient ,
33 createAcrossClient ,
4+ getIntegratorDataSuffix ,
45 parseDepositLogs ,
56 parseFillLogs ,
67 waitForDepositTx ,
@@ -12,6 +13,7 @@ import { Connection } from '@solana/web3.js'
1213import {
1314 type Address ,
1415 type Hash ,
16+ type Hex ,
1517 type TransactionReceipt ,
1618 type Chain as ViemChain ,
1719 WalletClient ,
@@ -34,6 +36,9 @@ import {
3436 SwapStatus ,
3537} from './BaseSwapAdapter'
3638
39+ // Integrator ID for Across tracking
40+ const KYBERSWAP_INTEGRATOR_ID : Hex = '0x008a'
41+
3742// Chain ID to viem Chain mapping
3843const chainIdToViemChain : Record < number , ViemChain > = {
3944 [ ChainId . MAINNET ] : mainnet ,
@@ -208,10 +213,22 @@ export type SwapAndBridgeProgress =
208213 txReceipt : TransactionReceipt
209214 meta : ApproveMeta
210215 }
216+ | {
217+ step : 'swapAndBridge'
218+ status : 'simulationPending'
219+ meta : SwapAndBridgeMeta
220+ }
221+ | {
222+ step : 'swapAndBridge'
223+ status : 'simulationSuccess'
224+ txRequest : any
225+ meta : SwapAndBridgeMeta
226+ }
211227 | {
212228 step : 'swapAndBridge'
213229 status : 'txPending'
214230 txHash : Hash
231+ txRequest ?: any
215232 meta : SwapAndBridgeMeta
216233 }
217234 | {
@@ -312,7 +329,7 @@ export class KyberAcrossAdapter extends BaseSwapAdapter {
312329 constructor ( ) {
313330 super ( )
314331 this . acrossClient = createAcrossClient ( {
315- integratorId : `0x008a` ,
332+ integratorId : KYBERSWAP_INTEGRATOR_ID ,
316333 chains : [ mainnet , arbitrum , bsc , optimism , linea , polygon , zksync , base , scroll , blast , unichain , plasma , monad ] ,
317334 rpcUrls : [
318335 ChainId . MAINNET ,
@@ -592,53 +609,53 @@ export class KyberAcrossAdapter extends BaseSwapAdapter {
592609 }
593610
594611 // Step 2: Execute swapAndBridge
612+ // 1. Simulate the swapAndBridge transaction
613+ // 2. If successful, execute the swapAndBridge transaction
614+ // 3. Wait for the transaction to be mined
595615 currentProgressMeta = {
596616 swapAndDepositData,
597617 }
598618
599- // Encode the swapAndBridge call
600- const swapAndBridgeCalldata = encodeFunctionData ( {
601- abi : spokePoolPeripheryAbi ,
602- functionName : 'swapAndBridge' ,
603- args : [
604- {
605- submissionFees : swapAndDepositData . submissionFees ,
606- depositData : swapAndDepositData . depositData ,
607- swapToken : swapAndDepositData . swapToken ,
608- exchange : swapAndDepositData . exchange ,
609- transferType : swapAndDepositData . transferType ,
610- swapTokenAmount : swapAndDepositData . swapTokenAmount ,
611- minExpectedInputTokenAmount : swapAndDepositData . minExpectedInputTokenAmount ,
612- routerCalldata : swapAndDepositData . routerCalldata ,
613- enableProportionalAdjustment : swapAndDepositData . enableProportionalAdjustment ,
614- spokePool : swapAndDepositData . spokePool ,
615- nonce : BigInt ( nonce ) ,
616- } ,
617- ] ,
618- } )
619+ // Report simulation pending status
620+ currentProgress = {
621+ step : 'swapAndBridge' ,
622+ status : 'simulationPending' ,
623+ meta : currentProgressMeta ,
624+ }
625+ onProgressHandler ( currentProgress )
626+
627+ // Prepare the swapAndBridge args with updated nonce
628+ const swapAndBridgeArgs = { ...swapAndDepositData , nonce : BigInt ( nonce ) }
619629
620- // First simulate the transaction to catch revert errors with proper decoding
621- await originClient . simulateContract ( {
630+ // Simulate the transaction to catch revert errors with proper decoding
631+ // and get the request object for execution
632+ const { request : txRequest } = await originClient . simulateContract ( {
622633 address : spokePoolPeripheryAddress ,
623634 abi : spokePoolPeripheryAbi ,
624635 functionName : 'swapAndBridge' ,
625- args : [ { ...swapAndDepositData } ] as any ,
636+ args : [ { ...swapAndBridgeArgs } ] as any ,
626637 account : userAddress ,
627638 value : isNative ? swapAndDepositData . swapTokenAmount : undefined ,
639+ dataSuffix : getIntegratorDataSuffix ( KYBERSWAP_INTEGRATOR_ID ) ,
628640 } )
629641
630- const swapAndBridgeTxHash = await walletClient . sendTransaction ( {
631- account : walletClient . account ! ,
632- chain : originChain ,
633- to : spokePoolPeripheryAddress ,
634- data : swapAndBridgeCalldata ,
635- value : isNative ? swapAndDepositData . swapTokenAmount : undefined ,
636- } )
642+ // Report simulation success status
643+ currentProgress = {
644+ step : 'swapAndBridge' ,
645+ status : 'simulationSuccess' ,
646+ txRequest,
647+ meta : currentProgressMeta ,
648+ }
649+ onProgressHandler ( currentProgress )
650+
651+ // Execute the transaction using writeContract with the simulated request
652+ const swapAndBridgeTxHash = await walletClient . writeContract ( txRequest )
637653
638654 currentProgress = {
639655 step : 'swapAndBridge' ,
640656 status : 'txPending' ,
641657 txHash : swapAndBridgeTxHash ,
658+ txRequest,
642659 meta : currentProgressMeta ,
643660 }
644661 onProgressHandler ( currentProgress )
0 commit comments