@privy-io/react-auth
Version:
React client for the Privy Auth API
3 lines (2 loc) • 7.33 kB
JavaScript
"use strict";var e=require("react/jsx-runtime"),r=require("@heroicons/react/24/outline/CheckCircleIcon"),t=require("react"),n=require("viem"),a=require("@privy-io/js-sdk-core"),i=require("./Layouts-D3TOcPhm.js"),o=require("./ModalHeader-C5Wuo3-1.js"),s=require("./ScreenHeader-CgYJ5Fmg.js"),l=require("./FundWalletMethodHeader-BTGxq_KO.js"),d=require("./index-ByFhjjzH.js"),u=require("./context-Cg3IEi80.js"),c=require("./internal-context-BJv4f_bO.js"),v=require("./useActiveWallet-BfMD5hzE.js"),g=require("./privy-context-CrM9_s6G.js"),f=require("./analytics-C6C_4JmG.js"),C=require("./reservoir-Bx0HQwbb.js"),p=require("./PrivyPluginContext-DsgaS6n9.js"),S=require("./usePrivy-C-Rab7yP.js"),h=require("./use-export-wallet-_EIyxyle.js"),y=require("./TransferOrBridgeLoadingScreen-DsutKbzS.js");function A(e){return e&&"object"==typeof e&&"default"in e?e:{default:e}}require("styled-components"),require("@heroicons/react/24/outline/ArrowLeftIcon"),require("@heroicons/react/24/outline/ArrowRightIcon"),require("@heroicons/react/24/outline/QuestionMarkCircleIcon"),require("@heroicons/react/24/outline/XMarkIcon"),require("tinycolor2"),require("ofetch"),require("zustand"),require("react-device-detect"),require("./events-context-Di6--rDg.js"),require("viem/utils"),require("./useWallets-dikDynAG.js"),require("@privy-io/are-addresses-equal"),require("eventemitter3"),require("./InjectedWalletIcon-UY3nMvZo.js"),require("@heroicons/react/24/outline/WalletIcon"),require("./Row-CMqYn75Q.js"),require("./Value-mhPsfsxe.js"),require("./LoadingSkeleton-BoyADunQ.js"),require("./Address-BeuDoSGB.js"),require("lucide-react"),require("./NetworkIcon-BZ9Mj6vV.js"),require("@heroicons/react/24/outline/GlobeAltIcon");var E=/*#__PURE__*/A(r);const I={component:function(){let r=u.useAppConfig(),{closePrivyModal:A,createAnalyticsEvent:I,connectors:m}=c.usePrivyInternal(),{navigate:q,setModalData:N,data:T}=g.usePrivyModal(),j=u.useAppConfig(),w=t.useRef(!1),_=p.usePlugins(),[x,P]=t.useState(!1),[U,F]=t.useState(!1),[L,b]=t.useState(null),[R,W]=t.useState(),[D,O]=t.useState();if(!T?.funding||"ethereum"!==T.funding.chainType)throw Error("Invalid funding data");let{amount:k,connectedWallet:M,chain:B,solanaChain:G,isUSDC:H}=T.funding,$=T.funding.address,Y=T.funding.erc20Address,Q=T.funding.isUSDC?"USDC":B.nativeCurrency.symbol,V=t.useMemo((()=>"solana"===M?.type?M.provider:function({connectors:e,connectedWalletAddress:r}){let t=e.find((e=>"solana"===e.chainType&&e.wallets.some((e=>e.address===r)))),n=t?.wallet.accounts.find((e=>e.address===r));if(!t||!n)throw new c.PrivyClientError("Unable to find source wallet connector");return new a.ConnectedStandardSolanaWallet({wallet:t.wallet,account:n})}({connectors:m,connectedWalletAddress:M?.address||""})),[M,m]),X=t.useMemo((()=>{let e=_(S.SOLANA_FUNDING_PLUGIN_ID);if(!e)throw new c.PrivyClientError("Unable to load solana plugin");let t=r.solanaRpcs["solana:mainnet"];if(!t)throw new c.PrivyClientError("Unable to load mainnet RPC");return e.getSolanaRpcClient({rpc:t.rpc,rpcSubscriptions:t.rpcSubscriptions,chain:"solana:mainnet",blockExplorerUrl:t.blockExplorerUrl??"https://explorer.solana.com"})}),[]),z=v.useExternalWalletListing(h.toSolanaWalletClientType(V?.standardWallet.name||"unknown")),K=z?.name||"wallet";return t.useEffect((()=>{(async function(){if(!V||!B||w.current)return;let e=_(S.SOLANA_FUNDING_PLUGIN_ID);if(!e)return void b(new c.PrivyClientError("Unable to solana plugin"));w.current=!0,B?.testnet&&console.warn("Solana testnets are not supported for bridging");let r=H?1e6*parseFloat(k):n.parseEther(k),t=await C.getQuote({isTestnet:!!B.testnet,input:C.toGetQuoteInput({appId:j.id,amount:r.toString(),user:V.address,recipient:$,destinationChainId:B.id,originChainId:C.RELAY_SOLANA_MAINNET_CHAIN_ID,originCurrency:H?C.RELAY_SOLANA_USDC_TOKEN_ID:C.RELAY_SOLANA_NATIVE_CURRENCY_ID,destinationCurrency:H?Y:void 0})}).catch(console.error);if(!t)return void b(new c.PrivyClientError(`Unable to fetch quotes for bridging. Wallet ${h.formatSolanaWalletAddress(V.address)} does not have enough funds.`,void 0,c.PrivyErrorCode.INSUFFICIENT_BALANCE));let a=await e.createTransactionFromRelayQuote({quote:t,source:V.address,solanaClient:X});if(a)try{P(!0);let r=await e.simulateTransaction({solanaClient:X,tx:a});if(r.hasError)return r.hasFunds?(console.error("Transaction failed:",r.error),void b(new c.PrivyClientError("Something went wrong",void 0,c.PrivyErrorCode.TRANSACTION_FAILURE))):void b(new c.PrivyClientError(`Wallet ${h.formatSolanaWalletAddress(V?.address)} does not have enough funds. ${t.details.currencyIn.amountFormatted} ${Q} are needed to complete the transaction.`,void 0,c.PrivyErrorCode.INSUFFICIENT_BALANCE));let{signature:n}=await V.signAndSendTransaction({chain:"solana:mainnet",transaction:a}),i=e.getAddressFromBuffer(n);W(i),O("pending")}catch(e){if(console.error(e),/user rejected the request/gi.test(e.message||""))return void b(new c.PrivyClientError("Transaction was rejected by the user",void 0,c.PrivyErrorCode.TRANSACTION_FAILURE));b(new c.PrivyClientError("Something went wrong",void 0,c.PrivyErrorCode.TRANSACTION_FAILURE))}else b(new c.PrivyClientError(`Unable to select bridge option from quotes. Wallet ${h.formatSolanaWalletAddress(V.address)} does not have enough funds.`,void 0,c.PrivyErrorCode.INSUFFICIENT_BALANCE))})().catch(console.error)}),[]),C.useBridgingStatusPolling({transactionHash:R,isTestnet:!1,bridgingStatus:D,setBridgingStatus:O,onSuccess({transactionHash:e}){P(!1),F(!0),I({eventName:f.ON_RAMP_COMPLETE_ANALYTICS_EVENT,payload:{provider:"external",status:"success",txHash:e,address:V.address,chainType:"solana",clusterName:G,token:"SOL",destinationAddress:$,destinationChainId:B.id,destinationChainType:"ethereum",destinationValue:k,destinationToken:H?"USDC":"ETH"}})},onFailure({error:e}){P(!1),b(e)}}),t.useEffect((()=>{if(!U)return;let e=setTimeout(A,u.DEFAULT_SUCCESS_SCREEN_EXTRA_LONG_DURATION_MS);return()=>clearTimeout(e)}),[U]),t.useEffect((()=>{L&&(N({funding:T?.funding,solanaFundingData:T?.solanaFundingData,sendTransaction:T?.sendTransaction,errorModalData:{error:L,previousScreen:"TransferFromWalletScreen"}}),q("ErrorScreen",!1))}),[L]),U?/*#__PURE__*/e.jsxs(e.Fragment,{children:[/*#__PURE__*/e.jsx(l.t,{}),/*#__PURE__*/e.jsx(i.RefactorSpacerTop,{}),/*#__PURE__*/e.jsxs(i.CenteredItemWithGap,{children:[/*#__PURE__*/e.jsx(E.default,{color:"var(--privy-color-success)",width:"64px",height:"64px"}),/*#__PURE__*/e.jsx(s.CenteredScreenHeader,{title:"Success!",description:`You’ve successfully added ${k} ${Q} to your ${j.name} wallet. It may take a minute before the funds are available to use.`})]}),/*#__PURE__*/e.jsx(i.RefactorSpacerBottom,{}),/*#__PURE__*/e.jsx(o.BlobbyFooter,{})]}):x&&V?/*#__PURE__*/e.jsx(y.TransferOrBridgeLoadingScreen,{walletClientType:h.toSolanaWalletClientType(V?.standardWallet.name||"unknown"),displayName:K,addressToFund:$,isBridging:x,isErc20Flow:!1,chainId:B.id,chainName:B.name,totalPriceInUsd:void 0,totalPriceInNativeCurrency:void 0,gasPriceInUsd:void 0,gasPriceInNativeCurrency:void 0}):
/*#__PURE__*/e.jsxs(e.Fragment,{children:[/*#__PURE__*/e.jsx(l.t,{}),/*#__PURE__*/e.jsx(d.NeutralSpinner,{}),/*#__PURE__*/e.jsx("div",{style:{marginTop:"1rem"}}),/*#__PURE__*/e.jsx(o.BlobbyFooter,{})]})}};exports.AwaitingSolToEvmBridgingScreen=I,exports.default=I;