1import{N as e,w as o,p as n,R as t,S as a,b as r,d as s,E as i,g as c,W as d,f as u,B as l,h as T,i as k,A as m,j as g,k as p,l as v,m as w,n as A,s as S,a as P,r as b,e as f}from"./index-Dx6_pztT.js";import{i as h,a as y,x as I}from"./lit-element-DrOn3_hb.js";import{n as N}from"./class-map-eSj-i90U.js";import"./index-CXXUMzfj.js";import{c as E}from"./index-BeSlt1gv.js";import"./index-DtcqEu7Q.js";const C={getGasPriceInEther:(e,o)=>Number(o*e)/1e18,getGasPriceInUSD(o,n,t){const a=C.getGasPriceInEther(n,t);return e.bigNumber(o).times(a).toNumber()},getPriceImpact({sourceTokenAmount:o,sourceTokenPriceInUSD:n,toTokenPriceInUSD:t,toTokenAmount:a}){const r=e.bigNumber(o).times(n),s=e.bigNumber(a).times(t);return r.minus(s).div(r).times(100).toNumber()},getMaxSlippage(o,n){const t=e.bigNumber(o).div(100);return e.multiply(n,t).toNumber()},getProviderFee:(o,n=.0085)=>e.bigNumber(o).times(n).toString(),isInsufficientNetworkTokenForGas(o,n){const t=n||"0";return!!e.bigNumber(o).eq(0)||e.bigNumber(e.bigNumber(t)).gt(o)},isInsufficientSourceTokenForSwap(o,n,t){var a,r;const s=null==(r=null==(a=null==t?void 0:t.find(e=>e.address===n))?void 0:a.quantity)?void 0:r.numeric;return e.bigNumber(s||"0").lt(o)},getToTokenAmount({sourceToken:o,toToken:n,sourceTokenPrice:t,toTokenPrice:a,sourceTokenAmount:r}){if("0"===r)return"0";if(!o||!n)return"0";const s=o.decimals,i=t,c=n.decimals,d=a;if(d<=0)return"0";const u=e.bigNumber(r).times(.0085),l=e.bigNumber(r).minus(u).times(e.bigNumber(10).pow(s)),T=e.bigNumber(i).div(d),k=s-c;return l.times(T).div(e.bigNumber(10).pow(k)).div(e.bigNumber(10).pow(c)).toFixed(c).toString()}},U=15e4,D={initializing:!1,initialized:!1,loadingPrices:!1,loadingQuote:!1,loadingApprovalTransaction:!1,loadingBuildTransaction:!1,loadingTransaction:!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:v.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},B=n(D),x={state:B,subscribe:e=>P(B,()=>e(B)),subscribeKey:(e,o)=>S(B,e,o),getParams(){var o,n,t,a,r,i,c,d;const l=u.state.activeCaipAddress,k=T.getPlainAddress(l),m=w(),g=A.getConnectorId(u.state.activeChain);if(!k)throw new Error("No address found to swap the tokens from.");const p=!(null==(o=B.toToken)?void 0:o.address)||!(null==(n=B.toToken)?void 0:n.decimals),v=!(null==(t=B.sourceToken)?void 0:t.address)||!(null==(a=B.sourceToken)?void 0:a.decimals)||!e.bigNumber(B.sourceTokenAmount).gt(0),S=!B.sourceTokenAmount;return{networkAddress:m,fromAddress:k,fromCaipAddress:l,sourceTokenAddress:null==(r=B.sourceToken)?void 0:r.address,toTokenAddress:null==(i=B.toToken)?void 0:i.address,toTokenAmount:B.toTokenAmount,toTokenDecimals:null==(c=B.toToken)?void 0:c.decimals,sourceTokenAmount:B.sourceTokenAmount,sourceTokenDecimals:null==(d=B.sourceToken)?void 0:d.decimals,invalidToToken:p,invalidSourceToken:v,invalidSourceTokenAmount:S,availableToSwap:l&&!p&&!v&&!S,isAuthConnector:g===s.CONNECTOR_ID.AUTH}},setSourceToken(e){if(!e)return B.sourceToken=e,B.sourceTokenAmount="",void(B.sourceTokenPriceInUSD=0);B.sourceToken=e,M.setTokenPrice(e.address,"sourceToken")},setSourceTokenAmount(e){B.sourceTokenAmount=e},setToToken(e){if(!e)return B.toToken=e,B.toTokenAmount="",void(B.toTokenPriceInUSD=0);B.toToken=e,M.setTokenPrice(e.address,"toToken")},setToTokenAmount(o){B.toTokenAmount=o?e.formatNumberToLocalString(o,6):""},async setTokenPrice(e,o){let n=B.tokensPriceMap[e]||0;n||(B.loadingPrices=!0,n=await M.getAddressPrice(e)),"sourceToken"===o?B.sourceTokenPriceInUSD=n:"toToken"===o&&(B.toTokenPriceInUSD=n),B.loadingPrices&&(B.loadingPrices=!1),M.getParams().availableToSwap&&M.swapTokens()},switchTokens(){if(B.initializing||!B.initialized)return;const e=B.toToken?{...B.toToken}:void 0,o=B.sourceToken?{...B.sourceToken}:void 0,n=e&&""===B.toTokenAmount?"1":B.toTokenAmount;M.setSourceToken(e),M.setToToken(o),M.setSourceTokenAmount(n),M.setToTokenAmount(""),M.swapTokens()},resetState(){B.myTokensWithBalance=D.myTokensWithBalance,B.tokensPriceMap=D.tokensPriceMap,B.initialized=D.initialized,B.sourceToken=D.sourceToken,B.sourceTokenAmount=D.sourceTokenAmount,B.sourceTokenPriceInUSD=D.sourceTokenPriceInUSD,B.toToken=D.toToken,B.toTokenAmount=D.toTokenAmount,B.toTokenPriceInUSD=D.toTokenPriceInUSD,B.networkPrice=D.networkPrice,B.networkTokenSymbol=D.networkTokenSymbol,B.networkBalanceInUSD=D.networkBalanceInUSD,B.inputError=D.inputError,B.myTokensWithBalance=D.myTokensWithBalance},resetValues(){var e;const{networkAddress:o}=M.getParams(),n=null==(e=B.tokens)?void 0:e.find(e=>e.address===o);M.setSourceToken(n),M.setToToken(void 0)},getApprovalLoadingState:()=>B.loadingApprovalTransaction,clearError(){B.transactionError=void 0},async initializeState(){if(!B.initializing){if(B.initializing=!0,!B.initialized)try{await M.fetchTokens(),B.initialized=!0}catch(e){B.initialized=!1,a.showError("Failed to initialize swap"),t.goBack()}B.initializing=!1}},async fetchTokens(){var e;const{networkAddress:o}=M.getParams();await M.getTokenList(),await M.getNetworkTokenPrice(),await M.getMyTokensWithBalance();const n=null==(e=B.tokens)?void 0:e.find(e=>e.address===o);n&&(B.networkTokenSymbol=n.symbol,M.setSourceToken(n),M.setSourceTokenAmount("0"))},async getTokenList(){const e=await k.getTokenList();B.tokens=e,B.popularTokens=e.sort((e,o)=>e.symbol<o.symbol?-1:e.symbol>o.symbol?1:0),B.suggestedTokens=e.filter(e=>!!v.SWAP_SUGGESTED_TOKENS.includes(e.symbol),{})},async getAddressPrice(e){var o,n;const t=B.tokensPriceMap[e];if(t)return t;const a=await l.fetchTokenPrice({addresses:[e]}),r=(null==a?void 0:a.fungibles)||[],s=[...B.tokens||[],...B.myTokensWithBalance||[]],i=null==(o=null==s?void 0:s.find(o=>o.address===e))?void 0:o.symbol,c=(null==(n=r.find(e=>e.symbol.toLowerCase()===(null==i?void 0:i.toLowerCase())))?void 0:n.price)||0,d=parseFloat(c.toString());return B.tokensPriceMap[e]=d,d},async getNetworkTokenPrice(){var e;const{networkAddress:o}=M.getParams(),n=null==(e=(await l.fetchTokenPrice({addresses:[o]}).catch(()=>(a.showError("Failed to fetch network token price"),{fungibles:[]}))).fungibles)?void 0:e[0],t=(null==n?void 0:n.price.toString())||"0";B.tokensPriceMap[o]=parseFloat(t),B.networkTokenSymbol=(null==n?void 0:n.symbol)||"",B.networkPrice=t},async getMyTokensWithBalance(e){const o=await p.getMyTokensWithBalance(e),n=k.mapBalancesToSwapTokens(o);n&&(await M.getInitialGasPrice(),M.setBalances(n))},setBalances(o){const{networkAddress:n}=M.getParams(),t=u.state.activeCaipNetwork;if(!t)return;const a=o.find(e=>e.address===n);
1o.forEach(e=>{B.tokensPriceMap[e.address]=e.price||0}),B.myTokensWithBalance=o.filter(e=>e.address.startsWith(t.caipNetworkId)),B.networkBalanceInUSD=a?e.multiply(a.quantity.numeric,a.price).toString():"0"},async getInitialGasPrice(){var o,n;const t=await k.fetchGasPrice();if(!t)return{gasPrice:null,gasPriceInUSD:null};switch(null==(n=null==(o=u.state)?void 0:o.activeCaipNetwork)?void 0:n.chainNamespace){case s.CHAIN.SOLANA:return B.gasFee=t.standard??"0",B.gasPriceInUSD=e.multiply(t.standard,B.networkPrice).div(1e9).toNumber(),{gasPrice:BigInt(B.gasFee),gasPriceInUSD:Number(B.gasPriceInUSD)};case s.CHAIN.EVM:default:const o=t.standard??"0",n=BigInt(o),a=BigInt(U),r=C.getGasPriceInUSD(B.networkPrice,a,n);return B.gasFee=o,B.gasPriceInUSD=r,{gasPrice:n,gasPriceInUSD:r}}},async swapTokens(){var o,n;const t=m.state.address,a=B.sourceToken,r=B.toToken,s=e.bigNumber(B.sourceTokenAmount).gt(0);if(s||M.setToTokenAmount(""),!r||!a||B.loadingPrices||!s)return;B.loadingQuote=!0;const i=e.bigNumber(B.sourceTokenAmount).times(10**a.decimals).round(0);try{const s=await l.fetchSwapQuote({userAddress:t,from:a.address,to:r.address,gasPrice:B.gasFee,amount:i.toString()});B.loadingQuote=!1;const c=null==(n=null==(o=null==s?void 0:s.quotes)?void 0:o[0])?void 0:n.toAmount;if(!c)return void g.open({displayMessage:"Incorrect amount",debugMessage:"Please enter a valid amount"},"error");const d=e.bigNumber(c).div(10**r.decimals).toString();M.setToTokenAmount(d);M.hasInsufficientToken(B.sourceTokenAmount,a.address)?B.inputError="Insufficient balance":(B.inputError=void 0,M.setTransactionDetails())}catch(c){B.loadingQuote=!1,B.inputError="Insufficient balance"}},async getTransaction(){const{fromCaipAddress:e,availableToSwap:o}=M.getParams(),n=B.sourceToken,r=B.toToken;if(e&&o&&n&&r&&!B.loadingQuote)try{B.loadingBuildTransaction=!0;let o;return o=await k.fetchSwapAllowance({userAddress:e,tokenAddress:n.address,sourceTokenAmount:B.sourceTokenAmount,sourceTokenDecimals:n.decimals})?await M.createSwapTransaction():await M.createAllowanceTransaction(),B.loadingBuildTransaction=!1,B.fetchError=!1,o}catch(s){return t.goBack(),a.showError("Failed to check allowance"),B.loadingBuildTransaction=!1,B.approvalTransaction=void 0,B.swapTransaction=void 0,void(B.fetchError=!0)}},async createAllowanceTransaction(){const{fromCaipAddress:e,sourceTokenAddress:o,toTokenAddress:n}=M.getParams();if(e&&n){if(!o)throw new Error("createAllowanceTransaction - No source token address found.");try{const t=await l.generateApproveCalldata({from:o,to:n,userAddress:e}),a=T.getPlainAddress(t.tx.from);if(!a)throw new Error("SwapController:createAllowanceTransaction - address is required");const r={data:t.tx.data,to:a,gasPrice:BigInt(t.tx.eip155.gasPrice),value:BigInt(t.tx.value),toAmount:B.toTokenAmount};return B.swapTransaction=void 0,B.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(r){return t.goBack(),a.showError("Failed to create approval transaction"),B.approvalTransaction=void 0,B.swapTransaction=void 0,void(B.fetchError=!0)}}},async createSwapTransaction(){var e;const{networkAddress:o,fromCaipAddress:n,sourceTokenAmount:s}=M.getParams(),i=B.sourceToken,c=B.toToken;if(!(n&&s&&i&&c))return;const d=null==(e=r.parseUnits(s,i.decimals))?void 0:e.toString();try{const e=await l.generateSwapCalldata({userAddress:n,from:i.address,to:c.address,amount:d,disableEstimate:!0}),t=i.address===o,a=BigInt(e.tx.eip155.gas),r=BigInt(e.tx.eip155.gasPrice),s=T.getPlainAddress(e.tx.to);if(!s)throw new Error("SwapController:createSwapTransaction - address is required");const u={data:e.tx.data,to:s,gas:a,gasPrice:r,value:t?BigInt(d??"0"):BigInt("0"),toAmount:B.toTokenAmount};return B.gasPriceInUSD=C.getGasPriceInUSD(B.networkPrice,a,r),B.approvalTransaction=void 0,B.swapTransaction=u,u}catch(u){return t.goBack(),a.showError("Failed to create transaction"),B.approvalTransaction=void 0,B.swapTransaction=void 0,void(B.fetchError=!0)}},onEmbeddedWalletApprovalSuccess(){a.showLoading("Approve limit increase in your wallet"),t.replace("SwapPreview")},async sendTransactionForApproval(e){var o,n,l;const{fromAddress:T,isAuthConnector:k}=M.getParams();B.loadingApprovalTransaction=!0;k?t.pushTransactionStack({onSuccess:M.onEmbeddedWalletApprovalSuccess}):a.showLoading("Approve limit increase in your wallet");try{await r.sendTransaction({address:T,to:e.to,data:e.data,value:e.value,chainNamespace:s.CHAIN.EVM}),await M.swapTokens(),await M.getTransaction(),B.approvalTransaction=void 0,B.loadingApprovalTransaction=!1}catch(m){const e=m;B.transactionError=null==e?void 0:e.displayMessage,B.loadingApprovalTransaction=!1,a.showError((null==e?void 0:e.displayMessage)||"Transaction error"),i.sendEvent({type:"track",event:"SWAP_APPROVAL_ERROR",properties:{message:(null==e?void 0:e.displayMessage)||(null==e?void 0:e.message)||"Unknown",network:(null==(o=u.state.activeCaipNetwork)?void 0:o.caipNetworkId)||"",swapFromToken:(null==(n=M.state.sourceToken)?void 0:n.symbol)||"",swapToToken:(null==(l=M.state.toToken)?void 0:l.symbol)||"",swapFromAmount:M.state.sourceTokenAmount||"",swapToAmount:M.state.toTokenAmount||"",isSmartAccount:c(s.CHAIN.EVM)===d.ACCOUNT_TYPES.SMART_ACCOUNT}})}},async sendTransactionForSwap(o){var n,l,T,k,m,g,p,v,w,A,S,P;if(!o)return;const{fromAddress:b,toTokenAmount:f,isAuthConnector:h}=M.getParams();B.loadingTransaction=!0;const y=`Swapping ${null==(n=B.sourceToken)?void 0:n.symbol} to ${e.formatNumberToLocalString(f,3)} ${null==(l=B.toToken)?void 0:l.symbol}`,I=`Swapped ${null==(T=B.sourceToken)?void 0:T.symbol} to ${e.formatNumberToLocalString(f,3)} ${null==(k=B.toToken)?void 0:k.symbol}`;h?t.pushTransactionStack({onSuccess(){t.replace("Account"),a.showLoading(y),x.resetState()}}
1):a.showLoading("Confirm transaction in your wallet");try{const e=[null==(m=B.sourceToken)?void 0:m.address,null==(g=B.toToken)?void 0:g.address].join(","),n=await r.sendTransaction({address:b,to:o.to,data:o.data,value:o.value,chainNamespace:s.CHAIN.EVM});return B.loadingTransaction=!1,a.showSuccess(I),i.sendEvent({type:"track",event:"SWAP_SUCCESS",properties:{network:(null==(p=u.state.activeCaipNetwork)?void 0:p.caipNetworkId)||"",swapFromToken:(null==(v=M.state.sourceToken)?void 0:v.symbol)||"",swapToToken:(null==(w=M.state.toToken)?void 0:w.symbol)||"",swapFromAmount:M.state.sourceTokenAmount||"",swapToAmount:M.state.toTokenAmount||"",isSmartAccount:c(s.CHAIN.EVM)===d.ACCOUNT_TYPES.SMART_ACCOUNT}}),x.resetState(),h||t.replace("Account"),x.getMyTokensWithBalance(e),n}catch(N){const e=N;return B.transactionError=null==e?void 0:e.displayMessage,B.loadingTransaction=!1,a.showError((null==e?void 0:e.displayMessage)||"Transaction error"),void i.sendEvent({type:"track",event:"SWAP_ERROR",properties:{message:(null==e?void 0:e.displayMessage)||(null==e?void 0:e.message)||"Unknown",network:(null==(A=u.state.activeCaipNetwork)?void 0:A.caipNetworkId)||"",swapFromToken:(null==(S=M.state.sourceToken)?void 0:S.symbol)||"",swapToToken:(null==(P=M.state.toToken)?void 0:P.symbol)||"",swapFromAmount:M.state.sourceTokenAmount||"",swapToAmount:M.state.toTokenAmount||"",isSmartAccount:c(s.CHAIN.EVM)===d.ACCOUNT_TYPES.SMART_ACCOUNT}})}},hasInsufficientToken:(e,o)=>C.isInsufficientSourceTokenForSwap(e,o,B.myTokensWithBalance),setTransactionDetails(){const{toTokenAddress:e,toTokenDecimals:o}=M.getParams();e&&o&&(B.gasPriceInUSD=C.getGasPriceInUSD(B.networkPrice,BigInt(B.gasFee),BigInt(U)),B.priceImpact=C.getPriceImpact({sourceTokenAmount:B.sourceTokenAmount,sourceTokenPriceInUSD:B.sourceTokenPriceInUSD,toTokenPriceInUSD:B.toTokenPriceInUSD,toTokenAmount:B.toTokenAmount}),B.maxSlippage=C.getMaxSlippage(B.slippage,B.toTokenAmount),B.providerFee=C.getProviderFee(B.sourceTokenAmount))}},M=o(x),F=h` 2 :host { 3 display: block; 4 } 5 6 :host > button { 7 gap: var(--wui-spacing-xxs); 8 padding: var(--wui-spacing-xs); 9 padding-right: var(--wui-spacing-1xs); 10 height: 40px; 11 border-radius: var(--wui-border-radius-l); 12 background: var(--wui-color-gray-glass-002); 13 border-width: 0px; 14 box-shadow: inset 0 0 0 1px var(--wui-color-gray-glass-002); 15 } 16 17 :host > button wui-image { 18 width: 24px; 19 height: 24px; 20 border-radius: var(--wui-border-radius-s); 21 box-shadow: inset 0 0 0 1px var(--wui-color-gray-glass-010); 22 } 23`;var W=function(e,o,n,t){var a,r=arguments.length,s=r<3?o:null===t?t=Object.getOwnPropertyDescriptor(o,n):t;if("object"==typeof Reflect&&"function"==typeof Reflect.decorate)s=Reflect.decorate(e,o,n,t);else for(var i=e.length-1;i>=0;i--)(a=e[i])&&(s=(r<3?a(s):r>3?a(o,n,s):a(o,n))||s);return r>3&&s&&Object.defineProperty(o,n,s),s};let O=class extends y{constructor(){super(...arguments),this.text=""}render(){return I` 24 <button> 25 ${this.tokenTemplate()} 26 <wui-text variant="paragraph-600" color="fg-100">${this.text}</wui-text> 27 </button> 28 `}tokenTemplate(){return this.imageSrc?I`<wui-image src=${this.imageSrc}></wui-image>`:I` 29 <wui-icon-box 30 size="sm" 31 iconColor="fg-200" 32 backgroundColor="fg-300" 33 icon="networkPlaceholder" 34 ></wui-icon-box> 35 `}};O.styles=[b,f,F],W([N()],O.prototype,"imageSrc",void 0),W([N()],O.prototype,"text",void 0),O=W([E("wui-token-button")],O);export{M as S};
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.