UNPKG

@privy-io/react-auth

Version:

React client for the Privy Auth API

2 lines (1 loc) 2.71 kB
"use strict";var e=require("react"),n=require("./privy-provider-BivyFPW2.js"),t=require("@solana/spl-token"),a=require("@solana/web3.js"),r=require("@privy-io/js-sdk-core"),s=require("./internal-context-GxD_jR-e.js"),o={id:n.SOLANA_FUNDING_PLUGIN_ID,getBalance:({address:e,cluster:n})=>new a.Connection(r.getSolanaRpcEndpointForCluster(n)).getBalance(new a.PublicKey(e)),getTokenBalance:async({address:e,cluster:n,mintAddress:t})=>await new r.SolanaClient({...n,rpcUrl:r.getSolanaRpcEndpointForCluster(n)}).getTokenAccountsByOwner(e,t)||{amount:BigInt(0),decimals:9},createTransferTransaction:async({from:e,to:n,amount:t,cluster:s})=>{let o=new a.PublicKey(e),i=new a.Transaction;i.add(a.SystemProgram.transfer({fromPubkey:o,toPubkey:new a.PublicKey(n),lamports:t}));let c=new a.Connection(r.getSolanaRpcEndpointForCluster(s)),l=await c.getLatestBlockhash();return i.recentBlockhash=l.blockhash,i.feePayer=o,{transaction:i,connection:c}},createSplTransferTransaction:async({from:e,to:n,amount:s,cluster:o,mintAddress:i})=>{let c=new a.PublicKey(i),l=new a.PublicKey(e),u=new a.PublicKey(n),d=new a.Transaction,p=t.getAssociatedTokenAddressSync(c,l),y=t.getAssociatedTokenAddressSync(c,u);d.add(t.createAssociatedTokenAccountIdempotentInstruction(l,y,u,c)),d.add(t.createTransferInstruction(p,y,l,s));let g=new a.Connection(r.getSolanaRpcEndpointForCluster(o)),w=await g.getLatestBlockhash();return d.recentBlockhash=w.blockhash,d.feePayer=l,{transaction:d,connection:g}},createBridgeTransactionFromRelayQuote:async({quote:e,from:t,cluster:s})=>{let o=e.steps[0]?.items?.[0];if(!o)return;let i=o.data.instructions.map((({keys:e,programId:t,data:r})=>new a.TransactionInstruction({keys:e.map((({pubkey:e,isSigner:n,isWritable:t})=>({pubkey:new a.PublicKey(e),isSigner:n,isWritable:t}))),programId:new a.PublicKey(t),data:n.bufferFrom(r,"hex")}))),c=o.data.addressLookupTableAddresses?.map((e=>new a.PublicKey(e))),l=new a.Connection(r.getSolanaRpcEndpointForCluster(s)),u=await Promise.all((c??[]).map((async e=>l.getAddressLookupTable(e).then((e=>e.value))))),{blockhash:d}=await l.getLatestBlockhash();return{transaction:new a.VersionedTransaction(new a.TransactionMessage({payerKey:new a.PublicKey(t),instructions:i,recentBlockhash:d}).compileToV0Message(u)),connection:l}}};exports.t=o,exports.useFundWallet=t=>{n.usePrivyEventSubscription("fundSolanaWallet",t);let a=n.useAppConfig(),{setModalData:r}=n.usePrivyModal(),{openModal:i}=s.usePrivyInternal();return n.useRegisterPlugin(o),{fundWallet:e.useCallback((async(e,t)=>{r({funding:n.prepareSolanaFundingModalData({address:e,appConfig:a,fundWalletConfig:t,methodScreen:n.FundingMethodSelectionScreen})}),i(n.FundingMethodSelectionScreen)}),[a,r,i])}};