import{o as e}from"./rolldown-runtime.js";import{n as t}from"./jsx-runtime.js";import{_t as n,xt as r}from"./useActiveWallet-CEs8euQ3.js";import{k as i}from"./context-Cdw68BC2.js";import{t as a}from"./PrivyPluginContext-DA-yS5vO.js";import{A as o,j as s,p as c}from"./src.js";var l=e(t(),1),u=Symbol(`default-solana-rpcs-plugin`);function d(e){return new Uint8Array(c().decode(e).messageBytes)}async function f({solanaClient:e,tx:t}){let n=s().decode(d(t)),{value:r}=await e.rpc.getFeeForMessage(n).send();return r??0n}async function p({solanaClient:e,tx:t,replaceRecentBlockhash:n}){let{value:r}=await e.rpc.simulateTransaction(s().decode(t),{commitment:`confirmed`,encoding:`base64`,sigVerify:!1,replaceRecentBlockhash:n}).send();if(r.err===`BlockhashNotFound`&&n)throw Error(`Simulation failed: Blockhash not found`);return r.err===`BlockhashNotFound`?await p({solanaClient:e,tx:t,replaceRecentBlockhash:!0}):{logs:r.logs??[],error:r.err,hasError:!!r.err,hasFunds:r.logs?.every((e=>!/insufficient funds/gi.test(e)&&!/insufficient lamports/gi.test(e)))??!0}}var m=(...e)=>{if(typeof Buffer>`u`)throw new r(`Buffer is not defined.`,void 0,n.BUFFER_NOT_DEFINED);return Buffer.from(...e)};async function h({rpcSubscriptions:e,signature:t,timeout:n}){let r=new AbortController,i=await e.signatureNotifications(t,{commitment:`confirmed`}).subscribe({abortSignal:r.signal}),a=await Promise.race([new Promise((e=>{setTimeout((()=>{r.abort(),e(Error(`Transaction confirmation timed out`))}),n)})),new Promise((async e=>{for await(let t of i){if(r.abort(),t.value.err)return e(Error(`Transaction confirmation failed`));e(void 0)}}))]);if(a instanceof Error)throw a}function g({rpc:e,rpcSubscriptions:t,chain:n,blockExplorerUrl:r}){return{rpc:e,rpcSubscriptions:t,chain:n,blockExplorerUrl:r,sendAndConfirmTransaction:function({rpc:e,rpcSubscriptions:t}){return async(n,r)=>new Promise((async(i,a)=>{try{let a=await e.sendTransaction(m(n).toString(`base64`),{preflightCommitment:`confirmed`,encoding:`base64`,skipPreflight:r?.skipPreflight??!1}).send();r?.skipConfirmation||await h({rpcSubscriptions:t,signature:a,timeout:1e4}),i({signature:new Uint8Array(o().encode(a))})}catch(e){a(e)}}))}({rpc:e,rpcSubscriptions:t})}}function _(){let e=i(),t=a(),n=(0,l.useMemo)((()=>{let n=t(u)?.getDefaultRpcs({appId:e.id});return Object.fromEntries([`solana:mainnet`,`solana:devnet`,`solana:testnet`].map((t=>{let r=e.solanaRpcs[t]??n?.[t]??null;return[t,r?g({chain:t,rpc:r.rpc,rpcSubscriptions:r.rpcSubscriptions,blockExplorerUrl:r.blockExplorerUrl??`https://explorer.solana.com?cluster=${t.replace(`solana:`,``)}`}):null]})))}),[e.solanaRpcs,e.id,t]);return(0,l.useCallback)((e=>{if(!n[e])throw Error(`No RPC configuration found for chain ${e}`);return n[e]}),[n])}export{p as a,m as i,f as n,d as o,_ as r,h as t};