1!function(){try{var e="u">typeof window?window:"u">typeof global?global:"u">typeof globalThis?globalThis:"u">typeof self?self:{},o=(new e.Error).stack;o&&(e._sentryDebugIds=e._sentryDebugIds||{},e._sentryDebugIds[o]="1bbb6197-39a7-4f67-8429-8de16a24c8ea",e._sentryDebugIdIdentifier="sentry-dbid-1bbb6197-39a7-4f67-8429-8de16a24c8ea")}catch(e){}}();"use strict";(self.webpackChunk_N_E=self.webpackChunk_N_E||[]).push([[3220],{43220:(e,o,t)=>{t.d(o,{GN:()=>h});var a=t(6217),n=t(39542),r=t(9897),s=t(21125),i=t(9005),c=t(66402),l=t(6874),d=t(53766),u=t(60402),T=t(60374);let k={getGasPriceInEther:(e,o)=>Number(o*e)/1e18,getGasPriceInUSD(e,o,t){let a=k.getGasPriceInEther(o,t);return r.S.bigNumber(e).times(a).toNumber()},getPriceImpact({sourceTokenAmount:e,sourceTokenPriceInUSD:o,toTokenPriceInUSD:t,toTokenAmount:a}){let n=r.S.bigNumber(e).times(o),s=r.S.bigNumber(a).times(t);return n.minus(s).div(n).times(100).toNumber()},getMaxSlippage(e,o){let t=r.S.bigNumber(e).div(100);return r.S.multiply(o,t).toNumber()},getProviderFee:(e,o=.0085)=>r.S.bigNumber(e).times(o).toString(),isInsufficientNetworkTokenForGas:(e,o)=>!!r.S.bigNumber(e).eq(0)||r.S.bigNumber(r.S.bigNumber(o||"0")).gt(e),isInsufficientSourceTokenForSwap(e,o,t){let a=t?.find(e=>e.address===o)?.quantity?.numeric;return r.S.bigNumber(a||"0").lt(e)}};var g=t(8733),p=t(30305),m=t(12961),w=t(62814),S=t(12391),A=t(66696),P=t(5552),y=t(30096),b=t(49873);let v={initializing:!1,initialized:!1,loadingPrices:!1,loadingQuote:!1,loadingApprovalTransaction:!1,loadingBuildTransaction:!1,loadingTransaction:!1,switchingTokens:!1,fetchError:!1,approvalTransaction:void 0,swapTransaction:void 0,transactionError:void 0,sourceToken:void 0,sourceTokenAmount:"",sourceTokenPriceInUSD:0,toToken:void 0,toTokenAmount:"",toTokenPriceInUSD:0,networkPrice:"0",networkBalanceInUSD:"0",networkTokenSymbol:"",inputError:void 0,slippage:d.oU.CONVERT_SLIPPAGE_TOLERANCE,tokens:void 0,popularTokens:void 0,suggestedTokens:void 0,foundTokens:void 0,myTokensWithBalance:void 0,tokensPriceMap:{},gasFee:"0",gasPriceInUSD:0,priceImpact:void 0,maxSlippage:void 0,providerFee:void 0},I=(0,a.BX)({...v}),f={state:I,subscribe:e=>(0,a.B1)(I,()=>e(I)),subscribeKey:(e,o)=>(0,n.u$)(I,e,o),getParams(){let e=w.W.state.activeChain,o=w.W.getAccountData(e)?.caipAddress??w.W.state.activeCaipAddress,t=u.w.getPlainAddress(o),a=(0,l.K1)(),n=A.a.getConnectorId(w.W.state.activeChain);if(!t)throw Error("No address found to swap the tokens from.");let i=!I.toToken?.address||!I.toToken?.decimals,c=!I.sourceToken?.address||!I.sourceToken?.decimals||!r.S.bigNumber(I.sourceTokenAmount).gt(0),d=!I.sourceTokenAmount;return{networkAddress:a,fromAddress:t,fromCaipAddress:o,sourceTokenAddress:I.sourceToken?.address,toTokenAddress:I.toToken?.address,toTokenAmount:I.toTokenAmount,toTokenDecimals:I.toToken?.decimals,sourceTokenAmount:I.sourceTokenAmount,sourceTokenDecimals:I.sourceToken?.decimals,invalidToToken:i,invalidSourceToken:c,invalidSourceTokenAmount:d,availableToSwap:o&&!i&&!c&&!
1d,isAuthConnector:n===s.o.CONNECTOR_ID.AUTH}},async setSourceToken(e){if(!e){I.sourceToken=e,I.sourceTokenAmount="",I.sourceTokenPriceInUSD=0;return}I.sourceToken=e,await h.setTokenPrice(e.address,"sourceToken")},setSourceTokenAmount(e){I.sourceTokenAmount=e},async setToToken(e){if(!e){I.toToken=e,I.toTokenAmount="",I.toTokenPriceInUSD=0;return}I.toToken=e,await h.setTokenPrice(e.address,"toToken")},setToTokenAmount(e){I.toTokenAmount=e?r.S.toFixed(e,6):""},async setTokenPrice(e,o){let t=I.tokensPriceMap[e]||0;t||(I.loadingPrices=!0,t=await h.getAddressPrice(e)),"sourceToken"===o?I.sourceTokenPriceInUSD=t:"toToken"===o&&(I.toTokenPriceInUSD=t),I.loadingPrices&&(I.loadingPrices=!1),h.getParams().availableToSwap&&!I.switchingTokens&&h.swapTokens()},async switchTokens(){if(!I.initializing&&I.initialized&&!I.switchingTokens){I.switchingTokens=!0;try{let e=I.toToken?{...I.toToken}:void 0,o=I.sourceToken?{...I.sourceToken}:void 0,t=e&&""===I.toTokenAmount?"1":I.toTokenAmount;h.setSourceTokenAmount(t),h.setToTokenAmount(""),await h.setSourceToken(e),await h.setToToken(o),I.switchingTokens=!1,h.swapTokens()}catch(e){throw I.switchingTokens=!1,e}}},resetState(){I.myTokensWithBalance=v.myTokensWithBalance,I.tokensPriceMap=v.tokensPriceMap,I.initialized=v.initialized,I.initializing=v.initializing,I.switchingTokens=v.switchingTokens,I.sourceToken=v.sourceToken,I.sourceTokenAmount=v.sourceTokenAmount,I.sourceTokenPriceInUSD=v.sourceTokenPriceInUSD,I.toToken=v.toToken,I.toTokenAmount=v.toTokenAmount,I.toTokenPriceInUSD=v.toTokenPriceInUSD,I.networkPrice=v.networkPrice,I.networkTokenSymbol=v.networkTokenSymbol,I.networkBalanceInUSD=v.networkBalanceInUSD,I.inputError=v.inputError},resetValues(){let{networkAddress:e}=h.getParams(),o=I.tokens?.find(o=>o.address===e);h.setSourceToken(o),h.setToToken(void 0)},getApprovalLoadingState:()=>I.loadingApprovalTransaction,clearError(){I.transactionError=void 0},async initializeState(){if(!I.initializing){if(I.initializing=!0,!I.initialized)try{await h.fetchTokens(),I.initialized=!0}catch(e){I.initialized=!1,b.P.showError("Failed to initialize swap"),y.I.goBack()}I.initializing=!1}},async fetchTokens(){let{networkAddress:e}=h.getParams();await h.getNetworkTokenPrice(),await h.getMyTokensWithBalance();let o=I.myTokensWithBalance?.find(o=>o.address===e);o&&(I.networkTokenSymbol=o.symbol,h.setSourceToken(o),h.setSourceTokenAmount("0"))},async getTokenList(){let e=w.W.state.activeCaipNetwork?.caipNetworkId;if(I.caipNetworkId!==e||!I.tokens)try{I.tokensLoading=!0;let o=await T.s.getTokenList(e);I.tokens=o,I.caipNetworkId=e,I.popularTokens=o.sort((e,o)=>e.symbol<o.symbol?-1:+(e.symbol>o.symbol));let t=(e&&d.oU.SUGGESTED_TOKENS_BY_CHAIN?.[e]||[]).map(e=>o.find(o=>o.symbol===e)).filter(e=>!!e),a=(d.oU.SWAP_SUGGESTED_TOKENS||[]).map(e=>o.find(o=>o.symbol===e)).filter(e=>!!e).filter(e=>!t.some(o=>o.address===e.address));I.suggestedTokens=[...t,...a]}catch(e){I.tokens=[],I.popularTokens=[],I.suggestedTokens=[]}finally{I.tokensLoading=!1}},async getAddressPrice(e){let o=I.tokensPriceMap[e];if(o)return o;let t=await m.T.fetchTokenPrice({addresses:[e]}),a=t?.fungibles||[],n=[...I.tokens||[],...I.myTokensWithBalance||[]],r=n?.find(o=>o.address===e)?.symbol,s=parseFloat((a.find(e=>e.symbol.toLowerCase()===r?.toLowerCase())?.price||0).toString());return I.tokensPriceMap[e]=s,s},async getNetworkTokenPrice(){let{networkAddress:e}=h.getParams(),o=await m.T.fetchTokenPrice({addresses:[e]}).catch(()=>(b.P.showError("Failed to fetch network token price"),{fungibles:[]})),t=o.fungibles?.[0],a=t?.price.toString()||"0";I.tokensPriceMap[e]=parseFloat(a),I.networkTokenSymbol=t?.symbol||"",I.networkPrice=a},async getMyTokensWithBalance(e){let o=await c.Z.getMyTokensWithBalance({forceUpdate:e,caipNetwork:w.W.state.activeCaipNetwork,address:w.W.getAccountData()?.address}),t=T.s.mapBalancesToSwapTokens(o);t&&(await h.getInitialGasPrice(),h.setBalances(t))},setBalances(e){let{networkAddress:o}=h.getParams(),t=w.W.state.activeCaipNetwork;if(!t)return;let a=e.find(e=>e.address===o);e.forEach(e=>{I.tokensPriceMap[e.address]=e.price||0}),I.myTokensWithBalance=e.filter(e=>e.address.startsWith(t.caipNetworkId)),I.networkBalanceInUSD=a?r.S.multiply(a.quantity.numeric,a.price).toString():"0"},async getInitialGasPrice(){let e=await T.s.fetchGasPrice();if(!e)return{gasPrice:null,gasPriceInUSD:null};switch(w.W.state?.activeCaipNetwork?.chainNamespace){case s.o.CHAIN.SOLANA:return I.gasFee=e.standard??"0",I.gasPriceInUSD=r.S.multiply(e.standard,I.networkPrice).div(1e9).toNumber(),{gasPrice:BigInt(I.gasFee),gasPriceInUSD:Number(I.gasPriceInUSD)};case s.o.CHAIN.EVM:default:let o=e.standard??"0",t=BigInt(o),a=BigInt(15e4),n=k.getGasPriceInUSD(I.networkPrice,a,t);return I.gasFee=o,I.gasPriceInUSD=n,{gasPrice:t,gasPriceInUSD:n}}},async swapTokens(){let e=w.W.getAccountData()?.address,o=I.sourceToken,t=I.toToken,a=r.S.bigNumber(I.sourceTokenAmount).gt(0);if(a||h.setToTokenAmount(""),!t||!o||I.loadingPrices||!a||!e)return;I.loadingQuote=!0;let n=r.S.bigNumber(I.sourceTokenAmount).times(10**o.decimals).round(0).toFixed(0);try{let a=await m.T.fetchSwapQuote({userAddress:e,from:o.address,to:t.address,gasPrice:I.gasFee,amount:n.toString()});I.loadingQuote=!1;let s=a?.quotes?.[0]?.toAmount;if(!s)return void p.h.open({displayMessage:"Incorrect amount",debugMessage:"Please enter a valid amount"},"error");let i=r.S.bigNumber(s).div(10**t.decimals).toString();h.setToTokenAmount(i),h.hasInsufficientToken(I.sourceTokenAmount,o.address)?I.inputError="Insufficient balance":(I.inputError=void 0,h.setTransactionDetail
1s())}catch(o){let e=await T.s.handleSwapError(o);I.loadingQuote=!1,I.inputError=e||"Insufficient balance"}},async getTransaction(){let{fromCaipAddress:e,availableToSwap:o}=h.getParams(),t=I.sourceToken,a=I.toToken;if(e&&o&&t&&a&&!I.loadingQuote)try{let o;return I.loadingBuildTransaction=!0,o=await T.s.fetchSwapAllowance({userAddress:e,tokenAddress:t.address,sourceTokenAmount:I.sourceTokenAmount,sourceTokenDecimals:t.decimals})?await h.createSwapTransaction():await h.createAllowanceTransaction(),I.loadingBuildTransaction=!1,I.fetchError=!1,o}catch(e){y.I.goBack(),b.P.showError("Failed to check allowance"),I.loadingBuildTransaction=!1,I.approvalTransaction=void 0,I.swapTransaction=void 0,I.fetchError=!0;return}},async createAllowanceTransaction(){let{fromCaipAddress:e,sourceTokenAddress:o,toTokenAddress:t}=h.getParams();if(e&&t){if(!o)throw Error("createAllowanceTransaction - No source token address found.");try{let a=await m.T.generateApproveCalldata({from:o,to:t,userAddress:e}),n=u.w.getPlainAddress(a.tx.from);if(!n)throw Error("SwapController:createAllowanceTransaction - address is required");let r={data:a.tx.data,to:n,gasPrice:BigInt(a.tx.eip155.gasPrice),value:BigInt(a.tx.value),toAmount:I.toTokenAmount};return I.swapTransaction=void 0,I.approvalTransaction={data:r.data,to:r.to,gasPrice:r.gasPrice,value:r.value,toAmount:r.toAmount},{data:r.data,to:r.to,gasPrice:r.gasPrice,value:r.value,toAmount:r.toAmount}}catch(e){y.I.goBack(),b.P.showError("Failed to create approval transaction"),I.approvalTransaction=void 0,I.swapTransaction=void 0,I.fetchError=!0;return}}},async createSwapTransaction(){let{networkAddress:e,fromCaipAddress:o,sourceTokenAmount:t}=h.getParams(),a=I.sourceToken,n=I.toToken;if(!o||!t||!a||!n)return;let r=S.x.parseUnits(t,a.decimals)?.toString();try{let t=await m.T.generateSwapCalldata({userAddress:o,from:a.address,to:n.address,amount:r,disableEstimate:!0}),s=a.address===e,i=BigInt(t.tx.eip155.gas),c=BigInt(t.tx.eip155.gasPrice),l=u.w.getPlainAddress(t.tx.to);if(!l)throw Error("SwapController:createSwapTransaction - address is required");let d={data:t.tx.data,to:l,gas:i,gasPrice:c,value:s?BigInt(r??"0"):BigInt("0"),toAmount:I.toTokenAmount};return I.gasPriceInUSD=k.getGasPriceInUSD(I.networkPrice,i,c),I.approvalTransaction=void 0,I.swapTransaction=d,d}catch(e){y.I.goBack(),b.P.showError("Failed to create transaction"),I.approvalTransaction=void 0,I.swapTransaction=void 0,I.fetchError=!0;return}},onEmbeddedWalletApprovalSuccess(){b.P.showLoading("Approve limit increase in your wallet"),y.I.replace("SwapPreview")},async sendTransactionForApproval(e){let{fromAddress:o,isAuthConnector:t}=h.getParams();I.loadingApprovalTransaction=!0,t?y.I.pushTransactionStack({onSuccess:h.onEmbeddedWalletApprovalSuccess}):b.P.showLoading("Approve limit increase in your wallet");try{await S.x.sendTransaction({address:o,to:e.to,data:e.data,value:e.value,chainNamespace:s.o.CHAIN.EVM}),await h.swapTokens(),await h.getTransaction(),I.approvalTransaction=void 0,I.loadingApprovalTransaction=!1}catch(e){I.transactionError=e?.displayMessage,I.loadingApprovalTransaction=!1,b.P.showError(e?.displayMessage||"Transaction error"),P.E.sendEvent({type:"track",event:"SWAP_APPROVAL_ERROR",properties:{message:e?.displayMessage||e?.message||"Unknown",network:w.W.state.activeCaipNetwork?.caipNetworkId||"",swapFromToken:h.state.sourceToken?.symbol||"",swapToToken:h.state.toToken?.symbol||"",swapFromAmount:h.state.sourceTokenAmount||"",swapToAmount:h.state.toTokenAmount||"",isSmartAccount:(0,l.lj)(s.o.CHAIN.EVM)===i.Vl.ACCOUNT_TYPES.SMART_ACCOUNT}})}},async sendTransactionForSwap(e){if(!e)return;let{fromAddress:o,toTokenAmount:t,isAuthConnector:a}=h.getParams();I.loadingTransaction=!0;let n=`Swapping ${I.sourceToken?.symbol} to ${r.S.formatNumberToLocalString(t,3)} ${I.toToken?.symbol}`,c=`Swapped ${I.sourceToken?.symbol} to ${r.S.formatNumberToLocalString(t,3)} ${I.toToken?.symbol}`;a?y.I.pushTransactionStack({onSuccess(){y.I.replace("Account"),b.P.showLoading(n),f.resetState()}}):b.P.showLoading("Confirm transaction in your wallet");try{let t=[I.sourceToken?.address,I.toToken?.address].join(","),n=await S.x.sendTransaction({address:o,to:e.to,data:e.data,value:e.value,chainNamespace:s.o.CHAIN.EVM});return I.loadingTransaction=!1,b.P.showSuccess(c),P.E.sendEvent({type:"track",event:"SWAP_SUCCESS",properties:{network:w.W.state.activeCaipNetwork?.caipNetworkId||"",swapFromToken:h.state.sourceToken?.symbol||"",swapToToken:h.state.toToken?.symbol||"",swapFromAmount:h.state.sourceTokenAmount||"",swapToAmount:h.state.toTokenAmount||"",isSmartAccount:(0,l.lj)(s.o.CHAIN.EVM)===i.Vl.ACCOUNT_TYPES.SMART_ACCOUNT}}),f.resetState(),a||y.I.replace("Account"),f.getMyTokensWithBalance(t),n}catch(e){I.transactionError=e?.displayMessage,I.loadingTransaction=!1,b.P.showError(e?.displayMessage||"Transaction error"),P.E.sendEvent({type:"track",event:"SWAP_ERROR",properties:{message:e?.displayMessage||e?.message||"Unknown",network:w.W.state.activeCaipNetwork?.caipNetworkId||"",swapFromToken:h.state.sourceToken?.symbol||"",swapToToken:h.state.toToken?.symbol||"",swapFromAmount:h.state.sourceTokenAmount||"",swapToAmount:h.state.toTokenAmount||"",isSmartAccount:(0,l.lj)(s.o.CHAIN.EVM)===i.Vl.ACCOUNT_TYPES.SMART_ACCOUNT}});return}},hasInsufficientToken:(e,o)=>k.isInsufficientSourceTokenForSwap(e,o,I.myTokensWithBalance),setTransactionDetail
1s(){let{toTokenAddress:e,toTokenDecimals:o}=h.getParams();e&&o&&(I.gasPriceInUSD=k.getGasPriceInUSD(I.networkPrice,BigInt(I.gasFee),BigInt(15e4)),I.priceImpact=k.getPriceImpact({sourceTokenAmount:I.sourceTokenAmount,sourceTokenPriceInUSD:I.sourceTokenPriceInUSD,toTokenPriceInUSD:I.toTokenPriceInUSD,toTokenAmount:I.toTokenAmount}),I.maxSlippage=k.getMaxSlippage(I.slippage,I.toTokenAmount),I.providerFee=k.getProviderFee(I.sourceTokenAmount))}},h=(0,g.X)(f)}}]);
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.