import { BehaviorSubject } from 'rxjs' ; import { BlockchainData } from '@shared/models/blockchain/blockchain-data' ; import { ErrorsService } from '@core/errors/errors.service' ; import { WALLET_NAME } from '@core/wallets/components/wallets-modal/models/wallet-name' ; import CustomError from '@core/errors/models/custom-error' ; import { Connection , PublicKey } from '@solana/web3.js' ; import { PhantomWallet } from '@core/services/blockchain/wallets/wallets-adapters/solana/models/types' ; import { CommonSolanaWalletAdapter } from '@core/services/blockchain/wallets/wallets-adapters/solana/common/common-solana-wallet-adapter' ; export class PhantomWalletAdapter extends CommonSolanaWalletAdapter < PhantomWallet > { public get walletName ( ) : WALLET_NAME { return WALLET_NAME . PHANTOM ; } constructor ( onNetworkChanges$ , onAddressChanges$ , errorsService , connection ) { super ( errorsService , onAddressChanges$ , onNetworkChanges$ , connection ) ; } public async activate ( ) { const wallet = typeof window !== 'undefined' && window . solana ; await this . checkErrors ( wallet ) ; const publicKey = new PublicKey ( wallet . publicKey . toBytes ( ) ) ; this . isEnabled = true ; wallet . on ( 'disconnect' , this . deActivate ) ; this . wallet = wallet ; this . selectedAddress = publicKey . toBase58 ( ) ; this . selectedChain = 'mainnet-beta' ; this . onNetworkChanges$ . next ( this . getNetwork ( ) ) ; this . onAddressChanges$ . next ( this . selectedAddress ) ; } private async handleDisconnect ( wallet ) { const handleDisconnect = wallet . _handleDisconnect ; try { await new Promise < void > ( ( resolve , reject ) => { const connect = ( ) => { wallet . off ( 'connect' , connect ) ; resolve ( ) ; } ; wallet . _handleDisconnect = ( ... args ) => { wallet . off ( 'connect' , connect ) ; reject ( new CustomError ( 'User close modal' ) ) ; return handleDisconnect . apply ( wallet , args ) ; } ; wallet . on ( 'connect' , connect ) ; wallet . connect ( ) . catch ( ( reason ) => { wallet . off ( 'connect' , connect ) ; reject ( reason ) ; } ) ; } ) ; } catch ( error ) { if ( error instanceof CustomError ) { throw error ; } if ( error instanceof Error ) { throw new CustomError ( error ?. message ) ; } } finally { wallet . _handleDisconnect = handleDisconnect ; } } private async checkErrors ( wallet ) { if ( ! wallet ) { throw new CustomError ( 'Wallet is not instelled' ) ; } if ( ! wallet . isPhantom ) { throw new CustomError ( 'Phantom is not instelled' ) ; } if ( ! wallet . isConnected ) { await this . handleDisconnect ( wallet ) ; } if ( ! wallet . publicKey ) { throw new CustomError ( 'Connection error' ) ; } } }