UNPKG

@privy-io/react-auth

Version:

React client for the Privy Auth API

3 lines (2 loc) 6.42 kB
import{jsxs as e,jsx as t,Fragment as r}from"react/jsx-runtime";import n from"@heroicons/react/24/outline/CheckCircleIcon";import{useRef as o,useState as a,useMemo as i,useEffect as s}from"react";import{parseEther as c}from"viem";import{ConnectedStandardSolanaWallet as l}from"@privy-io/js-sdk-core";import{b as d,c as m,R as u}from"./Layouts-BlFm53ED.mjs";import{B as p}from"./ModalHeader-BnVmXtvG.mjs";import{C as f}from"./ScreenHeader-CHmc4-Lu.mjs";import{t as h}from"./FundWalletMethodHeader-Cb-ACySl.mjs";import{N as g}from"./index-Dq_xe9dz.mjs";import{u as v,t as w}from"./context-DLtU3JQy.mjs";import{u as I,a as y,b as C}from"./internal-context-Z-fyxadS.mjs";import{z as T}from"./useActiveWallet-CvP7iYvj.mjs";import{a as S}from"./privy-context-DrMxzgOR.mjs";import{O as j}from"./analytics-mkkvFRju.mjs";import{g as A,t as b,d as N,a as F,b as U,u as x}from"./reservoir-0wfhnc0j.mjs";import{u as E}from"./PrivyPluginContext-2QN2dVUw.mjs";import{S as k}from"./usePrivy-C_sY2Duk.mjs";import{H as W,I as R}from"./use-export-wallet-_wu5ex5t.mjs";import{T as L}from"./TransferOrBridgeLoadingScreen-DVeIRghG.mjs";import"styled-components";import"@heroicons/react/24/outline/ArrowLeftIcon";import"@heroicons/react/24/outline/ArrowRightIcon";import"@heroicons/react/24/outline/QuestionMarkCircleIcon";import"@heroicons/react/24/outline/XMarkIcon";import"tinycolor2";import"ofetch";import"zustand";import"react-device-detect";import"./events-context-CI0iqAXA.mjs";import"viem/utils";import"./useWallets-BzNCTucF.mjs";import"@privy-io/are-addresses-equal";import"eventemitter3";import"./InjectedWalletIcon-DLcYOGDj.mjs";import"@heroicons/react/24/outline/WalletIcon";import"./Row-C9vrS4Zi.mjs";import"./Value-tcJV9e0L.mjs";import"./LoadingSkeleton-U6-3yFwI.mjs";import"./Address-N-mzBgMy.mjs";import"lucide-react";import"./NetworkIcon-B48ilzF8.mjs";import"@heroicons/react/24/outline/GlobeAltIcon";const P={component:function(){let P=v(),{closePrivyModal:B,createAnalyticsEvent:D,connectors:H}=I(),{navigate:$,setModalData:M,data:q}=S(),O=v(),_=o(!1),z=E(),[G,Q]=a(!1),[V,X]=a(!1),[Y,J]=a(null),[K,Z]=a(),[ee,te]=a();if(!q?.funding||"ethereum"!==q.funding.chainType)throw Error("Invalid funding data");let{amount:re,connectedWallet:ne,chain:oe,solanaChain:ae,isUSDC:ie}=q.funding,se=q.funding.address,ce=q.funding.erc20Address,le=q.funding.isUSDC?"USDC":oe.nativeCurrency.symbol,de=i((()=>"solana"===ne?.type?ne.provider:function({connectors:e,connectedWalletAddress:t}){let r=e.find((e=>"solana"===e.chainType&&e.wallets.some((e=>e.address===t)))),n=r?.wallet.accounts.find((e=>e.address===t));if(!r||!n)throw new y("Unable to find source wallet connector");return new l({wallet:r.wallet,account:n})}({connectors:H,connectedWalletAddress:ne?.address||""})),[ne,H]),me=i((()=>{let e=z(k);if(!e)throw new y("Unable to load solana plugin");let t=P.solanaRpcs["solana:mainnet"];if(!t)throw new y("Unable to load mainnet RPC");return e.getSolanaRpcClient({rpc:t.rpc,rpcSubscriptions:t.rpcSubscriptions,chain:"solana:mainnet",blockExplorerUrl:t.blockExplorerUrl??"https://explorer.solana.com"})}),[]),ue=T(W(de?.standardWallet.name||"unknown")),pe=ue?.name||"wallet";return s((()=>{(async function(){if(!de||!oe||_.current)return;let e=z(k);if(!e)return void J(new y("Unable to solana plugin"));_.current=!0,oe?.testnet&&console.warn("Solana testnets are not supported for bridging");let t=ie?1e6*parseFloat(re):c(re),r=await A({isTestnet:!!oe.testnet,input:b({appId:O.id,amount:t.toString(),user:de.address,recipient:se,destinationChainId:oe.id,originChainId:U,originCurrency:ie?N:F,destinationCurrency:ie?ce:void 0})}).catch(console.error);if(!r)return void J(new y(`Unable to fetch quotes for bridging. Wallet ${R(de.address)} does not have enough funds.`,void 0,C.INSUFFICIENT_BALANCE));let n=await e.createTransactionFromRelayQuote({quote:r,source:de.address,solanaClient:me});if(n)try{Q(!0);let t=await e.simulateTransaction({solanaClient:me,tx:n});if(t.hasError)return t.hasFunds?(console.error("Transaction failed:",t.error),void J(new y("Something went wrong",void 0,C.TRANSACTION_FAILURE))):void J(new y(`Wallet ${R(de?.address)} does not have enough funds. ${r.details.currencyIn.amountFormatted} ${le} are needed to complete the transaction.`,void 0,C.INSUFFICIENT_BALANCE));let{signature:o}=await de.signAndSendTransaction({chain:"solana:mainnet",transaction:n}),a=e.getAddressFromBuffer(o);Z(a),te("pending")}catch(e){if(console.error(e),/user rejected the request/gi.test(e.message||""))return void J(new y("Transaction was rejected by the user",void 0,C.TRANSACTION_FAILURE));J(new y("Something went wrong",void 0,C.TRANSACTION_FAILURE))}else J(new y(`Unable to select bridge option from quotes. Wallet ${R(de.address)} does not have enough funds.`,void 0,C.INSUFFICIENT_BALANCE))})().catch(console.error)}),[]),x({transactionHash:K,isTestnet:!1,bridgingStatus:ee,setBridgingStatus:te,onSuccess({transactionHash:e}){Q(!1),X(!0),D({eventName:j,payload:{provider:"external",status:"success",txHash:e,address:de.address,chainType:"solana",clusterName:ae,token:"SOL",destinationAddress:se,destinationChainId:oe.id,destinationChainType:"ethereum",destinationValue:re,destinationToken:ie?"USDC":"ETH"}})},onFailure({error:e}){Q(!1),J(e)}}),s((()=>{if(!V)return;let e=setTimeout(B,w);return()=>clearTimeout(e)}),[V]),s((()=>{Y&&(M({funding:q?.funding,solanaFundingData:q?.solanaFundingData,sendTransaction:q?.sendTransaction,errorModalData:{error:Y,previousScreen:"TransferFromWalletScreen"}}),$("ErrorScreen",!1))}),[Y]),V?/*#__PURE__*/e(r,{children:[/*#__PURE__*/t(h,{}),/*#__PURE__*/t(d,{}),/*#__PURE__*/e(m,{children:[/*#__PURE__*/t(n,{color:"var(--privy-color-success)",width:"64px",height:"64px"}),/*#__PURE__*/t(f,{title:"Success!",description:`You’ve successfully added ${re} ${le} to your ${O.name} wallet. It may take a minute before the funds are available to use.`})]}),/*#__PURE__*/t(u,{}),/*#__PURE__*/t(p,{})]}):G&&de?/*#__PURE__*/t(L,{walletClientType:W(de?.standardWallet.name||"unknown"),displayName:pe,addressToFund:se,isBridging:G,isErc20Flow:!1,chainId:oe.id,chainName:oe.name,totalPriceInUsd:void 0,totalPriceInNativeCurrency:void 0,gasPriceInUsd:void 0,gasPriceInNativeCurrency:void 0}): /*#__PURE__*/e(r,{children:[/*#__PURE__*/t(h,{}),/*#__PURE__*/t(g,{}),/*#__PURE__*/t("div",{style:{marginTop:"1rem"}}),/*#__PURE__*/t(p,{})]})}};export{P as AwaitingSolToEvmBridgingScreen,P as default};