import type { RequestArguments } from 'web3-core' ; import type { JsonRpcPayload , JsonRpcResponse } from 'web3-core-helpers' ; import { createPromise , sendEvent } from './utils' ; function request ( data ) { return createPromise ( ( id ) => sendEvent ( 'solanaBridgeSendRequest' , id , data ) ) ; } function send ( payload , callback ) { createPromise ( ( id ) => sendEvent ( 'solanaBridgeSendRequest' , id , { method : payload . method , params : payload . params , } ) ) . then ( ( value ) => { callback ( null , { jsonrpc : '2.0' , id : payload . id as number , result : value , } ) ; } , ( error ) => { if ( error instanceof Error ) callback ( error ) ; } ) ; } let isConnected = false ; /** Interact with the current solana provider */ export const bridgedSolanaProvider = { connect ( ) { return createPromise ( ( id ) => sendEvent ( 'solanaBridgeExecute' , 'solana.connect' , id ) ) ; } , request , send , sendAsync : send , on ( event , callback ) { if ( ! bridgedSolana . has ( event ) ) { bridgedSolana . set ( event , new Set ( ) ) ; sendEvent ( 'solanaBridgeRequestListen' , event ) ; } const map = bridgedSolana . get ( event ) ! ; map . add ( callback ) ; return ( ) => void map . delete ( callback ) ; } , getProperty ( key ) { return createPromise ( ( id ) => sendEvent ( 'solanaBridgePrimitiveAccess' , id , key ) ) ; } , isConnected : isConnected , untilAvailable ( ) { return createPromise ( ( id ) => sendEvent ( 'untilSolanaBridgeOnline' , id ) ) ; } , } ; async function watchConnectStatus ( ) { const connected = await bridgedSolanaProvider . getProperty ( 'isConnected' ) ; if ( connected !== undefined ) { isConnected = connected ; } bridgedSolanaProvider . on ( 'connected' , ( ) => { isConnected = true ; } ) ; bridgedSolanaProvider . on ( 'disconnect' , ( ) => { isConnected = false ; } ) ; } watchConnectStatus ( ) ; export interface BridgedSolanaProvider { connect ( ) : Promise < { publicKey : { _bn : string ; } ; } > ; /** Wait for window.solana object appears. */ untilAvailable ( ) : Promise < true > ; /** Send JSON RPC to the solana provider. */ request ( data ) : Promise < unknown > ; /** Send JSON RPC  */ send ( payload , callback ) : void ; /** Async send JSON RPC  */ sendAsync ( payload , callback ) : void ; /** Add event listener */ on ( event , callback ) : ( ) => void ; /** Access primitive property on the window.solana object. */ getProperty ( key ) : Promise < boolean | undefined > ; /** Call window.solana.isConnected */ isConnected : boolean ; } const bridgedSolana = new Map < string , Set < Function > > ( ) ; internal export function onSolanaEvent ( event , data ) { for ( const f of bridgedSolana . get ( event ) || [ ] ) { try { Reflect . apply ( f , null , data ) ; } catch { } } return ; }