UNPKG

@privy-io/react-auth

Version:

React client for the Privy Auth API

3 lines (2 loc) 7.33 kB
"use strict";var e=require("react/jsx-runtime"),r=require("@heroicons/react/24/outline/CheckCircleIcon"),n=require("react"),t=require("viem"),a=require("@privy-io/js-sdk-core"),i=require("./Layouts-dRL0OSpp.js"),o=require("./ModalHeader-DB7Tbjwz.js"),s=require("./ScreenHeader-CyURV6Ti.js"),l=require("./FundWalletMethodHeader-BKgYjMau.js"),d=require("./index-CRFXS9pP.js"),u=require("./context-DFq1obBO.js"),c=require("./internal-context-NWsAL807.js"),g=require("./useActiveWallet-BOX8xCn9.js"),v=require("./get-is-unified-wallet-D4ouXJpi.js"),f=require("./analytics-C6C_4JmG.js"),C=require("./reservoir-CQGyEBb7.js"),p=require("./usePrivy-CcfSRv1R.js"),S=require("./TransferOrBridgeLoadingScreen-B3zo8l43.js"),h=require("./prepareFundingModalData-Cnd6rhhJ.js");function y(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("./getPublicClient-CGlodIp_.js"),require("./useWallets-BLFNjTwC.js"),require("./InjectedWalletIcon-UY3nMvZo.js"),require("@heroicons/react/24/outline/WalletIcon"),require("./Row-CxDF-VMy.js"),require("./Value-C-XtZyFa.js"),require("./LoadingSkeleton-DoCrfGnl.js"),require("./Address-5znFAhQw.js"),require("lucide-react"),require("./Button-Bm_Ft0Dw.js"),require("./NetworkIcon-BZ9Mj6vV.js"),require("@heroicons/react/24/outline/GlobeAltIcon"),require("eventemitter3");var A=/*#__PURE__*/y(r);const E={component:function(){let r=u.useAppConfig(),{closePrivyModal:y,createAnalyticsEvent:E,connectors:I}=c.usePrivyInternal(),{navigate:m,setModalData:q,data:N}=v.usePrivyModal(),T=u.useAppConfig(),j=n.useRef(!1),w=p.usePlugins(),[_,P]=n.useState(!1),[x,F]=n.useState(!1),[U,L]=n.useState(null),[b,R]=n.useState(),[W,D]=n.useState();if(!N?.funding||"ethereum"!==N.funding.chainType)throw Error("Invalid funding data");let{amount:O,connectedWallet:M,chain:k,solanaChain:B,isUSDC:H}=N.funding,G=N.funding.address,$=N.funding.erc20Address,Y=N.funding.isUSDC?"USDC":k.nativeCurrency.symbol,Q=n.useMemo((()=>"solana"===M?.type?M.provider:function({connectors:e,connectedWalletAddress:r}){let n=e.find((e=>"solana"===e.chainType&&e.wallets.some((e=>e.address===r)))),t=n?.wallet.accounts.find((e=>e.address===r));if(!n||!t)throw new c.PrivyClientError("Unable to find source wallet connector");return new a.ConnectedStandardSolanaWallet({wallet:n.wallet,account:t})}({connectors:I,connectedWalletAddress:M?.address||""})),[M,I]),V=n.useMemo((()=>{let e=w(p.SOLANA_FUNDING_PLUGIN_ID);if(!e)throw new c.PrivyClientError("Unable to load solana plugin");let n=r.solanaRpcs["solana:mainnet"];if(!n)throw new c.PrivyClientError("Unable to load mainnet RPC");return e.getSolanaRpcClient({rpc:n.rpc,rpcSubscriptions:n.rpcSubscriptions,chain:"solana:mainnet",blockExplorerUrl:n.blockExplorerUrl??"https://explorer.solana.com"})}),[]),X=g.useExternalWalletListing(h.toSolanaWalletClientType(Q?.standardWallet.name||"unknown")),z=X?.name||"wallet";return n.useEffect((()=>{(async function(){if(!Q||!k||j.current)return;let e=w(p.SOLANA_FUNDING_PLUGIN_ID);if(!e)return void L(new c.PrivyClientError("Unable to solana plugin"));j.current=!0,k?.testnet&&console.warn("Solana testnets are not supported for bridging");let r=H?1e6*parseFloat(O):t.parseEther(O),n=await C.getQuote({isTestnet:!!k.testnet,input:C.toGetQuoteInput({appId:T.id,amount:r.toString(),user:Q.address,recipient:G,destinationChainId:k.id,originChainId:C.RELAY_SOLANA_MAINNET_CHAIN_ID,originCurrency:H?C.RELAY_SOLANA_USDC_TOKEN_ID:C.RELAY_SOLANA_NATIVE_CURRENCY_ID,destinationCurrency:H?$:void 0})}).catch(console.error);if(!n)return void L(new c.PrivyClientError(`Unable to fetch quotes for bridging. Wallet ${g.formatSolanaWalletAddress(Q.address)} does not have enough funds.`,void 0,c.PrivyErrorCode.INSUFFICIENT_BALANCE));let a=await e.createTransactionFromRelayQuote({quote:n,source:Q.address,solanaClient:V});if(a)try{P(!0);let r=await e.simulateTransaction({solanaClient:V,tx:a});if(r.hasError)return r.hasFunds?(console.error("Transaction failed:",r.error),void L(new c.PrivyClientError("Something went wrong",void 0,c.PrivyErrorCode.TRANSACTION_FAILURE))):void L(new c.PrivyClientError(`Wallet ${g.formatSolanaWalletAddress(Q?.address)} does not have enough funds. ${n.details.currencyIn.amountFormatted} ${Y} are needed to complete the transaction.`,void 0,c.PrivyErrorCode.INSUFFICIENT_BALANCE));let{signature:t}=await Q.signAndSendTransaction({chain:"solana:mainnet",transaction:a}),i=e.getAddressFromBuffer(t);R(i),D("pending")}catch(e){if(console.error(e),/user rejected the request/gi.test(e.message||""))return void L(new c.PrivyClientError("Transaction was rejected by the user",void 0,c.PrivyErrorCode.TRANSACTION_FAILURE));L(new c.PrivyClientError("Something went wrong",void 0,c.PrivyErrorCode.TRANSACTION_FAILURE))}else L(new c.PrivyClientError(`Unable to select bridge option from quotes. Wallet ${g.formatSolanaWalletAddress(Q.address)} does not have enough funds.`,void 0,c.PrivyErrorCode.INSUFFICIENT_BALANCE))})().catch(console.error)}),[]),C.useBridgingStatusPolling({transactionHash:b,isTestnet:!1,bridgingStatus:W,setBridgingStatus:D,onSuccess({transactionHash:e}){P(!1),F(!0),E({eventName:f.ON_RAMP_COMPLETE_ANALYTICS_EVENT,payload:{provider:"external",status:"success",txHash:e,address:Q.address,chainType:"solana",clusterName:B,token:"SOL",destinationAddress:G,destinationChainId:k.id,destinationChainType:"ethereum",destinationValue:O,destinationToken:H?"USDC":"ETH"}})},onFailure({error:e}){P(!1),L(e)}}),n.useEffect((()=>{if(!x)return;let e=setTimeout(y,u.DEFAULT_SUCCESS_SCREEN_EXTRA_LONG_DURATION_MS);return()=>clearTimeout(e)}),[x]),n.useEffect((()=>{U&&(q({funding:N?.funding,solanaFundingData:N?.solanaFundingData,sendTransaction:N?.sendTransaction,errorModalData:{error:U,previousScreen:"TransferFromWalletScreen"}}),m("ErrorScreen",!1))}),[U]),x?/*#__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(A.default,{color:"var(--privy-color-success)",width:"64px",height:"64px"}),/*#__PURE__*/e.jsx(s.CenteredScreenHeader,{title:"Success!",description:`You’ve successfully added ${O} ${Y} to your ${T.name} wallet. It may take a minute before the funds are available to use.`})]}),/*#__PURE__*/e.jsx(i.RefactorSpacerBottom,{}),/*#__PURE__*/e.jsx(o.BlobbyFooter,{})]}):_&&Q?/*#__PURE__*/e.jsx(S.TransferOrBridgeLoadingScreen,{walletClientType:h.toSolanaWalletClientType(Q?.standardWallet.name||"unknown"),displayName:z,addressToFund:G,isBridging:_,isErc20Flow:!1,chainId:k.id,chainName:k.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=E,exports.default=E;