1"use strict";(self.webpackChunk_N_E=self.webpackChunk_N_E||[]).push([[615],{30615:(e,o,t)=>{t.d(o,{GN:()=>b});var a=t(91004),n=t(36442),r=t(12050),s=t(57873),i=t(47482),c=t(71016),l=t(89188),d=t(81920),u=t(33432),T=t(73688);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(17052),m=t(23655),p=t(45283),w=t(82376),S=t(36071),A=t(32590),P=t(42242),v=t(26274),I=t(82875);let y={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},f=(0,a.BX)({...y}),h={state:f,subscribe:e=>(0,a.B1)(f,()=>e(f)),subscribeKey:(e,o)=>(0,n.u$)(f,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=!f.toToken?.address||!f.toToken?.decimals,c=!f.sourceToken?.address||!f.sourceToken?.decimals||!r.S.bigNumber(f.sourceTokenAmount).gt(0),d=!f.sourceTokenAmount;return{networkAddress:a,fromAddress:t,fromCaipAddress:o,sourceTokenAddress:f.sourceToken?.address,toTokenAddress:f.toToken?.address,toTokenAmount:f.toTokenAmount,toTokenDecimals:f.toToken?.decimals,sourceTokenAmount:f.sourceTokenAmount,sourceTokenDecimals:f.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){f.sourceToken=e,f.sourceTokenAmount="",f.sourceTokenPriceInUSD=0;return}f.sourceToken=e,await b.setTokenPrice(e.address,"sourceToken")},setSourceTokenAmount(e){f.sourceTokenAmount=e},async setToToken(e){if(!e){f.toToken=e,f.toTokenAmount="",f.toTokenPriceInUSD=0;return}f.toToken=e,await b.setTokenPrice(e.address,"toToken")},setToTokenAmount(e){f.toTokenAmount=e?r.S.toFixed(e,6):""},async setTokenPrice(e,o){let t=f.tokensPriceMap[e]||0;t||(f.loadingPrices=!0,t=await b.getAddressPrice(e)),"sourceToken"===o?f.sourceTokenPriceInUSD=t:"toToken"===o&&(f.toTokenPriceInUSD=t),f.loadingPrices&&(f.loadingPrices=!1),b.getParams().availableToSwap&&!f.switchingTokens&&b.swapTokens()},async switchTokens(){if(!f.initializing&&f.initialized&&!f.switchingTokens){f.switchingTokens=!0;try{let e=f.toToken?{...f.toToken}:void 0,o=f.sourceToken?{...f.sourceToken}:void 0,t=e&&""===f.toTokenAmount?"1":f.toTokenAmount;b.setSourceTokenAmount(t),b.setToTokenAmount(""),await b.setSourceToken(e),await b.setToToken(o),f.switchingTokens=!1,b.swapTokens()}catch(e){throw f.switchingTokens=!1,e}}},resetState(){f.myTokensWithBalance=y.myTokensWithBalance,f.tokensPriceMap=y.tokensPriceMap,f.initialized=y.initialized,f.initializing=y.initializing,f.switchingTokens=y.switchingTokens,f.sourceToken=y.sourceToken,f.sourceTokenAmount=y.sourceTokenAmount,f.sourceTokenPriceInUSD=y.sourceTokenPriceInUSD,f.toToken=y.toToken,f.toTokenAmount=y.toTokenAmount,f.toTokenPriceInUSD=y.toTokenPriceInUSD,f.networkPrice=y.networkPrice,f.networkTokenSymbol=y.networkTokenSymbol,f.networkBalanceInUSD=y.networkBalanceInUSD,f.inputError=y.inputError},resetValues(){let{networkAddress:e}=b.getParams(),o=f.tokens?.find(o=>o.address===e);b.setSourceToken(o),b.setToToken(void 0)},getApprovalLoadingState:()=>f.loadingApprovalTransaction,clearError(){f.transactionError=void 0},async initializeState(){if(!f.initializing){if(f.initializing=!0,!f.initialized)try{await b.fetchTokens(),f.initialized=!0}catch(e){f.initialized=!1,I.P.showError("Failed to initialize swap"),v.I.goBack()}f.initializing=!1}},async fetchTokens(){let{networkAddress:e}=b.getParams();await b.getNetworkTokenPrice(),await b.getMyTokensWithBalance();let o=f.myTokensWithBalance?.find(o=>o.address===e);o&&(f.networkTokenSymbol=o.symbol,b.setSourceToken(o),b.setSourceTokenAmount("0"))},async getTokenList(){let e=w.W.state.activeCaipNetwork?.caipNetworkId;if(f.caipNetworkId!==e||!f.tokens)try{f.tokensLoading=!0;let o=await T.s.getTokenList(e);f.tokens=o,f.caipNetworkId=e,f.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));f.suggestedTokens=[...t,...a]}catch(e){f.tokens=[],f.popularTokens=[],f.suggestedTokens=[]}finally{f.tokensLoading=!1}},async getAddressPrice(e){let o=f.tokensPriceMap[e];if(o)return o;let t=await p.T.fetchTokenPrice({addresses:[e]}),a=t?.fungibles||[],n=[...f.tokens||[],...f.myTokensWithBalance||[]],r=n?.find(o=>o.address===e)?.symbol,s=parseFloat((a.find(e=>e.symbol.toLowerCase()===r?.toLowerCase())?.price||0).toString());return f.tokensPriceMap[e]=s,s},async getNetworkTokenPrice(){let{networkAddress:e}=b.getParams(),o=await p.T.fetchTokenPrice({addresses:[e]}).catch(()=>(I.P.showError("Failed to fetch network token price"),{fungibles:[]})),t=o.fungibles?.[0],a=t?.price.toString()||"0";f.tokensPriceMap[e]=parseFloat(a),f.networkTokenSymbol=t?.symbol||"",f.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 b.getInitialGasPrice(),b.setBalances(t))},setBalances(e){let{networkAddress:o}=b.getParams(),t=w.W.state.activeCaipNetwork;if(!t)return;let a=e.find(e=>e.address===o);e.forEach(e=>{f.tokensPriceMap[e.address]=e.price||0}),f.myTokensWithBalance=e.filter(e=>e.address.startsWith(t.caipNetworkId)),f.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 f.gasFee=e.standard??"0",f.gasPriceInUSD=r.S.multiply(e.standard,f.networkPrice).div(1e9).toNumber(),{gasPrice:BigInt(f.gasFee),gasPriceInUSD:Number(f.gasPriceInUSD)};case s.o.CHAIN.EVM:default:let o=e.standard??"0",t=BigInt(o),a=BigInt(15e4),n=k.getGasPriceInUSD(f.networkPrice,a,t);return f.gasFee=o,f.gasPriceInUSD=n,{gasPrice:t,gasPriceInUSD:n}}},async swapTokens(){let e=w.W.getAccountData()?.address,o=f.sourceToken,t=f.toToken,a=r.S.bigNumber(f.sourceTokenAmount).gt(0);if(a||b.setToTokenAmount(""),!t||!o||f.loadingPrices||!a||!e)return;f.loadingQuote=!0;let n=r.S.bigNumber(f.sourceTokenAmount).times(10**o.decimals).round(0).toFixed(0);try{let a=await p.T.fetchSwapQuote({userAddress:e,from:o.address,to:t.address,gasPrice:f.gasFee,amount:n.toString()});f.loadingQuote=!1;let s=a?.quotes?.[0]?.toAmount;if(!s)return void m.h.open({displayMessage:"Incorrect amount",debugMessage:"Please enter a valid amount"},"error");let i=r.S.bigNumber(s).div(10**t.decimals).toString();b.setToTokenAmount(i),b.hasInsufficientToken(f.sourceTokenAmount,o.address)?f.inputError="Insufficient balance":(f.inputError=void 0,b.setTransactionDetail
1s())}catch(o){let e=await T.s.handleSwapError(o);f.loadingQuote=!1,f.inputError=e||"Insufficient balance"}},async getTransaction(){let{fromCaipAddress:e,availableToSwap:o}=b.getParams(),t=f.sourceToken,a=f.toToken;if(e&&o&&t&&a&&!f.loadingQuote)try{let o;return f.loadingBuildTransaction=!0,o=await T.s.fetchSwapAllowance({userAddress:e,tokenAddress:t.address,sourceTokenAmount:f.sourceTokenAmount,sourceTokenDecimals:t.decimals})?await b.createSwapTransaction():await b.createAllowanceTransaction(),f.loadingBuildTransaction=!1,f.fetchError=!1,o}catch(e){v.I.goBack(),I.P.showError("Failed to check allowance"),f.loadingBuildTransaction=!1,f.approvalTransaction=void 0,f.swapTransaction=void 0,f.fetchError=!0;return}},async createAllowanceTransaction(){let{fromCaipAddress:e,sourceTokenAddress:o,toTokenAddress:t}=b.getParams();if(e&&t){if(!o)throw Error("createAllowanceTransaction - No source token address found.");try{let a=await p.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:f.toTokenAmount};return f.swapTransaction=void 0,f.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){v.I.goBack(),I.P.showError("Failed to create approval transaction"),f.approvalTransaction=void 0,f.swapTransaction=void 0,f.fetchError=!0;return}}},async createSwapTransaction(){let{networkAddress:e,fromCaipAddress:o,sourceTokenAmount:t}=b.getParams(),a=f.sourceToken,n=f.toToken;if(!o||!t||!a||!n)return;let r=S.x.parseUnits(t,a.decimals)?.toString();try{let t=a.address===e,s=await p.T.generateSwapCalldata({userAddress:o,from:a.address,to:n.address,amount:r,disableEstimate:!0}),i=BigInt(s.tx.eip155.gas),c=BigInt(s.tx.eip155.gasPrice);if(t&&r){let t=f.myTokensWithBalance?.find(o=>o.address===e);if(t){let e=parseInt(t.quantity.decimals,10),l=S.x.parseUnits(t.quantity.numeric,e)??0n,d=i*c;if(BigInt(r)+d>l){let e=l>d?l-d:0n;e>0n&&(s=await p.T.generateSwapCalldata({userAddress:o,from:a.address,to:n.address,amount:e.toString(),disableEstimate:!0}),r=e.toString(),i=BigInt(s.tx.eip155.gas),c=BigInt(s.tx.eip155.gasPrice))}}}let l=u.w.getPlainAddress(s.tx.to);if(!l)throw Error("SwapController:createSwapTransaction - address is required");let d={data:s.tx.data,to:l,gas:i,gasPrice:c,value:t?BigInt(r??"0"):BigInt("0"),toAmount:f.toTokenAmount};return f.gasPriceInUSD=k.getGasPriceInUSD(f.networkPrice,i,c),f.approvalTransaction=void 0,f.swapTransaction=d,d}catch(e){v.I.goBack(),I.P.showError("Failed to create transaction"),f.approvalTransaction=void 0,f.swapTransaction=void 0,f.fetchError=!0;return}},onEmbeddedWalletApprovalSuccess(){I.P.showLoading("Approve limit increase in your wallet"),v.I.replace("SwapPreview")},async sendTransactionForApproval(e){let{fromAddress:o,isAuthConnector:t}=b.getParams();f.loadingApprovalTransaction=!0,t?v.I.pushTransactionStack({onSuccess:b.onEmbeddedWalletApprovalSuccess}):I.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 b.swapTokens(),await b.getTransaction(),f.approvalTransaction=void 0,f.loadingApprovalTransaction=!1}catch(e){f.transactionError=e?.displayMessage,f.loadingApprovalTransaction=!1,I.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:b.state.sourceToken?.symbol||"",swapToToken:b.state.toToken?.symbol||"",swapFromAmount:b.state.sourceTokenAmount||"",swapToAmount:b.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}=b.getParams();f.loadingTransaction=!0;let n=`Swapping ${f.sourceToken?.symbol} to ${r.S.formatNumberToLocalString(t,3)} ${f.toToken?.symbol}`,c=`Swapped ${f.sourceToken?.symbol} to ${r.S.formatNumberToLocalString(t,3)} ${f.toToken?.symbol}`;a?v.I.pushTransactionStack({onSuccess(){v.I.replace("Account"),I.P.showLoading(n),h.resetState()}}
1):I.P.showLoading("Confirm transaction in your wallet");try{let t=[f.sourceToken?.address,f.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 f.loadingTransaction=!1,I.P.showSuccess(c),P.E.sendEvent({type:"track",event:"SWAP_SUCCESS",properties:{network:w.W.state.activeCaipNetwork?.caipNetworkId||"",swapFromToken:b.state.sourceToken?.symbol||"",swapToToken:b.state.toToken?.symbol||"",swapFromAmount:b.state.sourceTokenAmount||"",swapToAmount:b.state.toTokenAmount||"",isSmartAccount:(0,l.lj)(s.o.CHAIN.EVM)===i.Vl.ACCOUNT_TYPES.SMART_ACCOUNT}}),h.resetState(),a||v.I.replace("Account"),h.getMyTokensWithBalance(t),n}catch(e){f.transactionError=e?.displayMessage,f.loadingTransaction=!1,I.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:b.state.sourceToken?.symbol||"",swapToToken:b.state.toToken?.symbol||"",swapFromAmount:b.state.sourceTokenAmount||"",swapToAmount:b.state.toTokenAmount||"",isSmartAccount:(0,l.lj)(s.o.CHAIN.EVM)===i.Vl.ACCOUNT_TYPES.SMART_ACCOUNT}});return}},hasInsufficientToken:(e,o)=>k.isInsufficientSourceTokenForSwap(e,o,f.myTokensWithBalance),setTransactionDetails(){let{toTokenAddress:e,toTokenDecimals:o}=b.getParams();e&&o&&(f.gasPriceInUSD=k.getGasPriceInUSD(f.networkPrice,BigInt(f.gasFee),BigInt(15e4)),f.priceImpact=k.getPriceImpact({sourceTokenAmount:f.sourceTokenAmount,sourceTokenPriceInUSD:f.sourceTokenPriceInUSD,toTokenPriceInUSD:f.toTokenPriceInUSD,toTokenAmount:f.toTokenAmount}),f.maxSlippage=k.getMaxSlippage(f.slippage,f.toTokenAmount),f.providerFee=k.getProviderFee(f.sourceTokenAmount))}},b=(0,g.X)(h)}}]);
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.