"use strict";var e=require("@solana/kit"),r=require("react"),t=require("./context-rs-ygOK2.js"),n=require("./PrivyPluginContext-DsLtVhZA.js"),o=require("./errors-CBapWvRs.js");const i=Symbol("default-solana-rpcs-plugin");function a(r){return new Uint8Array(e.getTransactionDecoder().decode(r).messageBytes)}const s=(...e)=>{if("undefined"==typeof Buffer)throw new o.PrivyClientError("Buffer is not defined.",void 0,o.PrivyErrorCode.BUFFER_NOT_DEFINED);return Buffer.from(...e)};async function c({rpcSubscriptions:e,signature:r,timeout:t}){let n=new AbortController,o=await e.signatureNotifications(r,{commitment:"confirmed"}).subscribe({abortSignal:n.signal}),i=await Promise.race([new Promise((e=>{setTimeout((()=>{n.abort(),e(Error("Transaction confirmation timed out"))}),t)})),new Promise((async e=>{for await(let r of o){if(n.abort(),r.value.err)return e(Error("Transaction confirmation failed"));e(void 0)}}))]);if(i instanceof Error)throw i}function l({rpc:r,rpcSubscriptions:t,chain:n,blockExplorerUrl:o}){let i=function({rpc:r,rpcSubscriptions:t}){return async n=>new Promise((async(o,i)=>{try{let i=await r.sendTransaction(s(n).toString("base64"),{preflightCommitment:"confirmed",encoding:"base64"}).send();await c({rpcSubscriptions:t,signature:i,timeout:1e4}),o({signature:new Uint8Array(e.getBase58Encoder().encode(i))})}catch(e){i(e)}}))}({rpc:r,rpcSubscriptions:t});return{rpc:r,rpcSubscriptions:t,chain:n,blockExplorerUrl:o,sendAndConfirmTransaction:i}}exports.DEFAULT_SOLANA_RPCS_PLUGIN_ID=i,exports.bufferFrom=s,exports.fetchTransactionEstimatedFees=async function({solanaClient:r,tx:t}){let n=e.getBase64Decoder().decode(a(t)),{value:o}=await r.rpc.getFeeForMessage(n).send();return o??0n},exports.getSolanaRpcClient=l,exports.getTransactionMessageBytes=a,exports.simulateTransaction=async function r({solanaClient:t,tx:n,replaceRecentBlockhash:o}){let{value:i}=await t.rpc.simulateTransaction(e.getBase64Decoder().decode(n),{commitment:"confirmed",encoding:"base64",sigVerify:!1,replaceRecentBlockhash:o}).send();if("BlockhashNotFound"===i.err&&o)throw Error("Simulation failed: Blockhash not found");return"BlockhashNotFound"===i.err?await r({solanaClient:t,tx:n,replaceRecentBlockhash:!0}):{logs:i.logs??[],error:i.err,hasError:!!i.err,hasFunds:i.logs?.every((e=>!/insufficient funds/gi.test(e)&&!/insufficient lamports/gi.test(e)))??!0}},exports.useSolanaRpcClient=function(){let e=t.useAppConfig(),o=n.usePlugins(),a=r.useMemo((()=>{let r=o(i),t=r?.getDefaultRpcs({appId:e.id});return Object.fromEntries(["solana:mainnet","solana:devnet","solana:testnet"].map((r=>{let n=e.solanaRpcs[r]??t?.[r]??null;return[r,n?l({chain:r,rpc:n.rpc,rpcSubscriptions:n.rpcSubscriptions,blockExplorerUrl:n.blockExplorerUrl??`https://explorer.solana.com?cluster=${r.replace("solana:","")}`}):null]})))}),[e.solanaRpcs,e.id,o]);return r.useCallback((e=>{if(!a[e])throw Error(`No RPC configuration found for chain ${e}`);return a[e]}),[a])},exports.waitForSignatureConfirmation=c;
//# sourceMappingURL=useSolanaRpcClient-COqLztBk.js.map
