import { ChainIdCallback , ConnectCallback , Connector , DefaultConnectionPayload , DisconnectCallback , SubscribedObject , } from '@web3-wallets-kit/types' ; import { getAccount , getChainId , SendingInterface } from '@web3-wallets-kit/utils' ; export abstract class AbstractConnector < P extends DefaultConnectionPayload > implements Connector < P > { protected payload : P | null = null ; private sendingInterface : SendingInterface = 'EIP 1193' ; public abstract connect ( ) ; public async disconnect ( ) { this . payload = null ; } public async getAccount ( ) { if ( ! this . payload ) { return null ; } const { account , sendingInterface } = await getAccount ( this . payload . provider , this . sendingInterface ) ; this . sendingInterface = sendingInterface ; return account ; } public async getChainId ( ) { if ( ! this . payload ) { return null ; } const { chainId , sendingInterface } = await getChainId ( this . payload . provider , this . sendingInterface ) ; this . sendingInterface = sendingInterface ; return chainId ; } public getConnectionPayload ( ) { return this . payload ; } public subscribeConnectAccount ( callback ) { const convertedCallback = ( accounts ) => callback ( accounts [ 0 ] ) ; this . payload ?. provider . on && this . payload . provider . on ( 'accountsChanged' , convertedCallback ) ; return { unsubscribe : ( ) => { this . payload ?. provider . removeListener && this . payload . provider . removeListener ( 'accountsChanged' , convertedCallback ) ; } , } ; } public subscribeChainId ( callback ) { const convertedCallback = ( chainId ) => { const convertedChainId = typeof chainId === 'string' ? parseInt ( chainId , 16 ) : chainId ; if ( Number . isNaN ( convertedChainId ) ) { throw new Error ( 'ChainId is incorrect' ) ; } else { callback ( convertedChainId ) ; } } ; this . payload ?. provider . on && this . payload . provider . on ( 'chainChanged' , convertedCallback ) ; return { unsubscribe : ( ) => { this . payload ?. provider . removeListener && this . payload . provider . removeListener ( 'chainChanged' , convertedCallback ) ; } , } ; } public subscribeDisconnect ( callback ) { this . payload ?. provider . on && this . payload . provider . on ( 'disconnect' , ( event ) => { const isRecoverableMetamaskDisconnection = this . payload ?. provider ?. isMetaMask && event ?. code === 1013 ; ! isRecoverableMetamaskDisconnection && callback ( ) ; } ) ; return { unsubscribe : ( ) => { this . payload ?. provider . removeListener && this . payload . provider . removeListener ( 'disconnect' , callback ) ; } , } ; } }