import{getTransactionDecoder as r,getBase64Decoder as n,getBase58Encoder as e}from"@solana/kit";import{useMemo as t,useCallback as o}from"react";import{u as a}from"./context-BKnOUECq.mjs";import{u as i}from"./PrivyPluginContext-DA-yS5vO.mjs";import{P as s,a as c}from"./errors-Dd6zfYtp.mjs";const l=Symbol("default-solana-rpcs-plugin");function u(n){return new Uint8Array(r().decode(n).messageBytes)}async function f({solanaClient:r,tx:e}){let t=n().decode(u(e)),{value:o}=await r.rpc.getFeeForMessage(t).send();return o??0n}async function p({solanaClient:r,tx:e,replaceRecentBlockhash:t}){let{value:o}=await r.rpc.simulateTransaction(n().decode(e),{commitment:"confirmed",encoding:"base64",sigVerify:!1,replaceRecentBlockhash:t}).send();if("BlockhashNotFound"===o.err&&t)throw Error("Simulation failed: Blockhash not found");return"BlockhashNotFound"===o.err?await p({solanaClient:r,tx:e,replaceRecentBlockhash:!0}):{logs:o.logs??[],error:o.err,hasError:!!o.err,hasFunds:o.logs?.every((r=>!/insufficient funds/gi.test(r)&&!/insufficient lamports/gi.test(r)))??!0}}const m=(...r)=>{if("undefined"==typeof Buffer)throw new s("Buffer is not defined.",void 0,c.BUFFER_NOT_DEFINED);return Buffer.from(...r)};async function d({rpcSubscriptions:r,signature:n,timeout:e}){let t=new AbortController,o=await r.signatureNotifications(n,{commitment:"confirmed"}).subscribe({abortSignal:t.signal}),a=await Promise.race([new Promise((r=>{setTimeout((()=>{t.abort(),r(Error("Transaction confirmation timed out"))}),e)})),new Promise((async r=>{for await(let n of o){if(t.abort(),n.value.err)return r(Error("Transaction confirmation failed"));r(void 0)}}))]);if(a instanceof Error)throw a}function h({rpc:r,rpcSubscriptions:n,chain:t,blockExplorerUrl:o}){let a=function({rpc:r,rpcSubscriptions:n}){return async t=>new Promise((async(o,a)=>{try{let a=await r.sendTransaction(m(t).toString("base64"),{preflightCommitment:"confirmed",encoding:"base64"}).send();await d({rpcSubscriptions:n,signature:a,timeout:1e4}),o({signature:new Uint8Array(e().encode(a))})}catch(r){a(r)}}))}({rpc:r,rpcSubscriptions:n});return{rpc:r,rpcSubscriptions:n,chain:t,blockExplorerUrl:o,sendAndConfirmTransaction:a}}function b(){let r=a(),n=i(),e=t((()=>{let e=n(l),t=e?.getDefaultRpcs({appId:r.id});return Object.fromEntries(["solana:mainnet","solana:devnet","solana:testnet"].map((n=>{let e=r.solanaRpcs[n]??t?.[n]??null;return[n,e?h({chain:n,rpc:e.rpc,rpcSubscriptions:e.rpcSubscriptions,blockExplorerUrl:e.blockExplorerUrl??`https://explorer.solana.com?cluster=${n.replace("solana:","")}`}):null]})))}),[r.solanaRpcs,r.id,n]);return o((r=>{if(!e[r])throw Error(`No RPC configuration found for chain ${r}`);return e[r]}),[e])}export{l as D,u as a,m as b,f,h as g,p as s,b as u,d as w};
//# sourceMappingURL=useSolanaRpcClient-VCEpZd2k.mjs.map
