@privy-io/react-auth
Version:
React client for the Privy Auth API
3 lines (2 loc) • 9.53 kB
JavaScript
import{jsxs as e,Fragment as t,jsx as i}from"react/jsx-runtime";import a from"@heroicons/react/24/outline/CheckCircleIcon";import{useState as r,useEffect as n}from"react";import{createWalletClient as o,custom as s,publicActions as d,createPublicClient as c,http as m,formatUnits as l}from"viem";import{addToDefaultChains as u}from"@privy-io/js-sdk-core";import{b as p,c as h,R as g}from"./Layouts-BlFm53ED.mjs";import{B as f}from"./ModalHeader-D8-mhjp4.mjs";import{C as v}from"./ScreenHeader-CHmc4-Lu.mjs";import{t as j}from"./FundWalletMethodHeader-Cfci4-83.mjs";import{N as y}from"./index-Dq_xe9dz.mjs";import{u as w,t as I}from"./context-WTldtXS8.mjs";import{u as T,a as C,b}from"./internal-context-Z-fyxadS.mjs";import{Q as S,D as x}from"./useActiveWallet-3RHPIBxJ.mjs";import{a as N}from"./get-is-unified-wallet-Boa8DYrQ.mjs";import{u as A}from"./useGetTokenPrice-BPBpM07F.mjs";import{u as k}from"./useWallets-CR0z5IIt.mjs";import{O as E}from"./analytics-mkkvFRju.mjs";import{g as F,t as L,a as P,b as U,c as B,u as M}from"./reservoir-0wfhnc0j.mjs";import{x as W}from"./index-BBRkihRn.mjs";import{g as q}from"./getChainName-DjpPdUSc.mjs";import{g as H,a as O}from"./transaction-CnfuREWo.mjs";import{g as R,B as D}from"./BridgeNetworkSelectionView-CaDh-Zck.mjs";import{a as $}from"./getPublicClient-B4uPLKn-.mjs";import{T as _}from"./TransferOrBridgeLoadingScreen-DcCdHB-g.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"./use-export-wallet-CV4p7L0b.mjs";import"eventemitter3";import"viem/utils";import"./events-context-CI0iqAXA.mjs";import"@privy-io/are-addresses-equal";import"./useGetSolPrice-DwwjjGbd.mjs";import"uuid";import"jose";import"@coinbase/wallet-sdk";import"@privy-io/ethereum";import"mipd";import"@privy-io/popup";import"./paths-3HW55qZg.mjs";import"./usePrivy-6nkpbErN.mjs";import"@scure/base";import"@headlessui/react";import"@walletconnect/ethereum-provider";import"@privy-io/urls";import"./PrivyPluginContext-2QN2dVUw.mjs";import"./getEmbeddedConnectedWallet-JzK4iD-L.mjs";import"js-cookie";import"./frame-uzTmvtww.mjs";import"@privy-io/routes";import"x402/client";import"@privy-io/api-base";import"viem/accounts";import"./use-sign-with-user-signer-Do5Oi_rb.mjs";import"./getFormattedUsdFromLamports-B6EqSEho.mjs";import"./getErc20Balance-DA4dGV9O.mjs";import"./Row-C9vrS4Zi.mjs";import"./ErrorMessage-D8VaAP5m.mjs";import"./Value-tcJV9e0L.mjs";import"./LoadingSkeleton-U6-3yFwI.mjs";import"./Subtitle-CV-2yKE4.mjs";import"./Title-BnzYV3Is.mjs";import"@heroicons/react/24/outline/WalletIcon";import"./Chip-D2-wZOHJ.mjs";import"./NetworkIcon-B48ilzF8.mjs";import"@heroicons/react/24/outline/GlobeAltIcon";import"./shared-FM0rljBt.mjs";import"@heroicons/react/24/outline/ChevronDownIcon";import"./formatErc20TokenAmount-BuPk9xcy.mjs";import"./ethers-D1WT71Ay.mjs";import"./styles-BmZHuqSX.mjs";import"./LinkPasskeyScreen-OxaghvUl.mjs";import"lucide-react";import"./TodoList-CgrU7uwu.mjs";import"./ScreenLayout-DTmQLGPf.mjs";import"./Screen-Bp-TN9gb.mjs";import"./InjectedWalletIcon-DLcYOGDj.mjs";import"./Address-BjZb-TIL.mjs";const G={component:()=>{let G=w(),{rpcConfig:V,appId:Q,closePrivyModal:z,createAnalyticsEvent:X}=T(),{navigate:Y,setModalData:J,data:K}=N(),Z=w(),{wallets:ee}=k(),[te,ie]=r(null),[ae,re]=r(null),[ne,oe]=r([]),[se,de]=r(0),[ce,me]=r(!1),[le,ue]=r(!1),[pe,he]=r(!1),[ge,fe]=r(!1),[ve,je]=r(),[ye,we]=r();if(!K?.funding||"solana"!==K.funding.chainType)throw Error("Invalid funding data");let{address:Ie,chain:Te,connectedWallet:Ce}=K.funding,[be,Se]=r(K.funding.amount),xe=("ethereum"===Ce?.type?Ce:void 0)??ee[0],Ne=S(xe?.walletClientType||"unknown"),Ae=Ne?.name||"wallet",[ke,Ee]=r(null);n((()=>{(async()=>{if(!xe)return;let e=await xe.getEthereumProvider();Ee(o({account:xe.address,transport:s(e)}).extend(d))})().catch(console.error)}),[xe]);let[Fe,Le]=r(0n),Pe=H(Fe);n((()=>{let e=G.solanaRpcs[Te];e?W({rpc:e.rpc,address:Ie}).then((e=>Le(BigInt(e)))).catch(console.error):console.warn("Unable to load solana rpc, skipping balance")}),[]);let[Ue,Be]=r(),{tokenPrice:Me}=A("solana"),{fundingAmountInBaseUnit:We,fundingAmountInUsd:qe}=O({amount:be,fee:0n,tokenPrice:Me,isUsdc:K.funding.isUSDC});if(n((()=>{(async()=>{if(!ke||!xe)return;let e=["solana:testnet","solana:devnet"].includes(Te);e&&console.warn("Solana testnets are not supported for bridging");let t=u(Z.chains).filter((({testnet:t})=>!!t===e)),i=(await R({chains:t,address:xe.address,appId:Q,rpcConfig:V})).filter((e=>e.balance>0n));if(i.length<1)return void ie(new C(`Wallet ${x(xe.address)} does not have enough funds.`,void 0,b.INSUFFICIENT_BALANCE));i.sort(((e,t)=>Number(t.balance-e.balance)));let a=(await Promise.allSettled(i.map((async e=>({...e,quote:await F({isTestnet:!1,input:L({appId:Q,amount:We.toString(),user:xe.address,recipient:Ie,destinationChainId:U,destinationCurrency:P,originChainId:e.chain.id})})}))))).filter((e=>"fulfilled"===e.status)).map((e=>e.value));if(a.length<1)return void ie(new C(`Unable to fetch quotes for bridging. Wallet ${x(xe.address)} does not have enough funds.`,void 0,b.INSUFFICIENT_BALANCE));let r=a.map((({quote:e,balance:t,chain:i})=>({bridgeTx:B(e),balance:t,chain:i,isErc20Quote:!1}))).filter((({bridgeTx:e})=>!!e));if(r.length>1)return void oe(r);let n=r.at(0);n?(ue(!0),Be({data:n.bridgeTx.data,to:n.bridgeTx.to,value:n.bridgeTx.value,chain:n.chain})):ie(new C(`Unable to select bridge option from quotes. Wallet ${x(xe.address)} does not have enough funds.`,void 0,b.INSUFFICIENT_BALANCE))})().catch(console.error)}),[ke]),n((()=>{(async()=>{let e,t;if(!ke||!xe||ce||pe||!Ue)return;me(!0);let i=c({chain:Ue.chain,transport:m($(Ue.chain,V,Q))});try{e=await i.prepareTransactionRequest({account:xe.address,to:Ue.to,chain:Ue.chain,data:Ue.data,value:BigInt(Ue.value??0)})}catch(e){console.error(e),ne.length>1&&re(e.shortMessage??"Something went wrong")}if(e){me(!1),he(!0);try{await ke.switchChain({id:Ue.chain.id})}catch(e){await ke.addChain({chain:Ue.chain}),await ke.switchChain({id:Ue.chain.id})}try{t=await ke.sendTransaction(e)}catch(e){console.error(e),"TransactionExecutionError"===e.name&&(ne.length<1?ie(new C(e.shortMessage,void 0,b.TRANSACTION_FAILURE)):re(e.shortMessage??"Something went wrong"))}if(t)return await ke.waitForTransactionReceipt({hash:t}),le?(we("pending"),void je(t)):(he(!1),fe(!0),void X({eventName:E,payload:{provider:"external",status:"success",txHash:t,address:xe.address,chainId:Ue.chain.id,chainType:"ethereum",value:Ue.value?l(BigInt(Ue.value),18):void 0,token:"ETH",destination:Ie,destinationClusterName:"mainnet-beta",destinationChainType:"solana",destinationValue:l(We,9),destinationToken:"SOL"}}));he(!1)}else me(!1)})().catch(console.error)}),[ke,Ue]),M({transactionHash:ve,isTestnet:!1,bridgingStatus:ye,setBridgingStatus:we,onSuccess({transactionHash:e}){ue(!1),fe(!0),X({eventName:E,payload:{provider:"external",status:"success",txHash:e,address:xe?.address,chainId:Ue?.chain.id,chainType:"ethereum",value:Ue?.value?l(BigInt(Ue.value),18):void 0,token:"ETH",destination:Ie,destinationClusterName:"mainnet-beta",destinationChainType:"solana",destinationValue:l(We,9),destinationToken:"SOL"}})},onFailure({error:e}){ue(!1),ie(e)}}),n((()=>{te&&(J({funding:K?.funding,solanaFundingData:K?.solanaFundingData,sendTransaction:K?.sendTransaction,errorModalData:{error:te,previousScreen:"TransferFromWalletScreen"}}),Y("ErrorScreen",!1))}),[te]),n((()=>{if(!ge)return;let e=setTimeout(z,I);return()=>clearTimeout(e)}),[ge]),ge/*#__PURE__*/)return e(t,{children:[/*#__PURE__*/i(j,{}),/*#__PURE__*/i(p,{}),/*#__PURE__*/e(h,{children:[/*#__PURE__*/i(a,{color:"var(--privy-color-success)",width:"64px",height:"64px"}),/*#__PURE__*/i(v,{title:"Success!",description:`You’ve successfully added ${be} SOL to your ${Z.name} wallet. It may take a minute before the funds are available to use.`})]}),/*#__PURE__*/i(g,{}),/*#__PURE__*/i(f,{})]});let He=ne[se];return ne.length>1&&He?/*#__PURE__*/i(D,{displayName:Ae,configuredFundingChain:Te,formattedBalance:Pe,fundingAmount:be,fundingCurrency:"SOL",fundingAmountInUsd:qe,options:ne,selectedOption:He,isPreparing:ce,isSubmitting:pe,addressToFund:Ie,fundingWalletAddress:xe?.address||"",errorMessage:ae,onSubmit:()=>{K.funding?.amount!==be?async function(){if(xe&&He)try{let e=await F({isTestnet:!1,input:L({appId:Q,amount:We.toString(),user:xe.address,recipient:Ie,destinationChainId:U,destinationCurrency:P,originChainId:He.chain.id})}),t=B(e);if(!t)throw Error("Invalid transaction request");ue(!0),Be({data:t.data,to:t.to,value:t.value,chain:He.chain})}catch(e){console.error(e),ie(new C("Unable to fetch quotes for bridging",e,b.INSUFFICIENT_BALANCE))}}().catch(console.error):Be({to:He.bridgeTx.to,data:He.bridgeTx.data,value:He.bridgeTx.value,chain:He.chain})},onSelect:e=>{e!==se&&(re(null),de(e))},onAmountChange:Se}):pe&&xe?/*#__PURE__*/i(_,{walletClientType:xe?.walletClientType||"unknown",displayName:Ae,addressToFund:Ie,isBridging:le,isErc20Flow:!1,chainId:"solana",chainName:q(Te),totalPriceInUsd:void 0,totalPriceInNativeCurrency:void 0,gasPriceInUsd:void 0,gasPriceInNativeCurrency:void 0}):
/*#__PURE__*/e(t,{children:[/*#__PURE__*/i(j,{}),/*#__PURE__*/i(y,{}),/*#__PURE__*/i("div",{style:{marginTop:"1rem"}}),/*#__PURE__*/i(f,{})]})}};export{G as AwaitingEvmToSolBridgingScreen,G as default};