unich-web3-sdk
Version:
Web3 SDK for DEX applications with multi-chain support
295 lines (288 loc) • 7.96 kB
text/typescript
import { b as ChainType, C as ChainId } from './chain-BlOpMgJb.mjs';
import EventEmitter from 'eventemitter3';
/**
* Interface for wallet info
*/
interface WalletInfo {
id: string;
name: string;
icon: string;
description?: string;
downloadUrls?: {
browserExtension?: string;
mobileApp?: string;
website?: string;
};
supportedChains: ChainType[];
}
/**
* Interface for connector status
*/
declare enum ConnectorStatus {
DISCONNECTED = "disconnected",
CONNECTING = "connecting",
CONNECTED = "connected",
ERROR = "error"
}
/**
* Interface for connector error
*/
interface ConnectorError {
code: string;
message: string;
details?: any;
}
/**
* Interface for connector
*/
interface Connector {
id: string;
name: string;
type: string;
icon: string;
supportedChains: ChainType[];
connect: (chainId?: ChainId) => Promise<string>;
disconnect: () => Promise<void>;
switchChain: (chainId: ChainId) => Promise<void>;
isConnected: () => boolean;
getStatus: () => ConnectorStatus;
getError: () => ConnectorError | null;
getAccount: () => string | null;
getChainId: () => ChainId | null;
on: (event: ConnectorEvent, listener: (...args: any[]) => void) => void;
off: (event: ConnectorEvent, listener: (...args: any[]) => void) => void;
removeAllListeners: (event?: ConnectorEvent) => void;
}
/**
* Enum for connector events
*/
declare enum ConnectorEvent {
CONNECT = "connect",
DISCONNECT = "disconnect",
CHAIN_CHANGED = "chainChanged",
ACCOUNT_CHANGED = "accountChanged",
ERROR = "error"
}
/**
* Base connector class that implements common functionality
* All specific wallet connectors should extend this class
*/
declare abstract class BaseConnector implements Connector {
id: string;
name: string;
type: string;
icon: string;
supportedChains: ChainType[];
protected _status: ConnectorStatus;
protected _error: ConnectorError | null;
protected _account: string | null;
protected _chainId: ChainId | null;
protected _eventEmitter: EventEmitter;
constructor(id: string, name: string, type: string, icon: string, supportedChains: ChainType[]);
/**
* Connect to the wallet
* @param chainId Optional chain ID to connect to
*/
abstract connect(chainId?: ChainId): Promise<string>;
/**
* Disconnect from the wallet
*/
abstract disconnect(): Promise<void>;
/**
* Switch to a different chain
* @param chainId Chain ID to switch to
*/
abstract switchChain(chainId: ChainId): Promise<void>;
/**
* Check if the wallet is connected
*/
isConnected(): boolean;
/**
* Get the current connection status
*/
getStatus(): ConnectorStatus;
/**
* Get the current error (if any)
*/
getError(): ConnectorError | null;
/**
* Get the connected account address
*/
getAccount(): string | null;
/**
* Get the current chain ID
*/
getChainId(): ChainId | null;
/**
* Register an event listener
* @param event Event to listen for
* @param listener Listener function
*/
on(event: ConnectorEvent, listener: (...args: any[]) => void): void;
/**
* Remove an event listener
* @param event Event to stop listening for
* @param listener Listener function to remove
*/
off(event: ConnectorEvent, listener: (...args: any[]) => void): void;
/**
* Remove all event listeners for a specific event
* @param event Event to stop listening for
*/
removeAllListeners(event?: ConnectorEvent): void;
/**
* Update the connection status
* @param status New status
*/
protected updateStatus(status: ConnectorStatus): void;
/**
* Update the current error
* @param error New error
*/
protected updateError(error: ConnectorError | null): void;
/**
* Update the connected account
* @param account New account address
*/
protected updateAccount(account: string | null): void;
/**
* Update the current chain ID
* @param chainId New chain ID
*/
protected updateChainId(chainId: ChainId | null): void;
}
declare global {
interface Window {
ethereum?: any;
}
}
/**
* MetaMask connector
* Implements wallet connection for MetaMask and other EVM-compatible wallets
*/
declare class MetaMaskConnector extends BaseConnector {
private boundAccountsChanged;
private boundChainChanged;
private boundDisconnect;
constructor();
/**
* Check if MetaMask is available
*/
isAvailable(): boolean;
/**
* Connect to MetaMask
* @param chainId Optional chain ID to connect to
*/
connect(chainId?: ChainId): Promise<string>;
/**
* Disconnect from MetaMask
*/
disconnect(): Promise<void>;
/**
* Switch to a different chain
* @param chainId Chain ID to switch to
*/
switchChain(chainId: ChainId): Promise<void>;
/**
* Add a chain to MetaMask
* @param chain Chain configuration
*/
private addChain;
/**
* Set up event listeners for MetaMask events
*/
private setupEventListeners;
/**
* Remove event listeners
*/
private removeEventListeners;
/**
* Handle accounts changed event
* @param accounts New accounts
*/
private handleAccountsChanged;
/**
* Handle chain changed event
* @param chainId New chain ID
*/
private handleChainChanged;
/**
* Handle disconnect event
*/
private handleDisconnect;
}
declare global {
interface Window {
phantom?: {
solana?: {
isPhantom?: boolean;
connect: () => Promise<{
publicKey: {
toString: () => string;
};
}>;
disconnect: () => Promise<void>;
on: (event: string, handler: (...args: any[]) => void) => void;
off: (event: string, handler: (...args: any[]) => void) => void;
request: (request: {
method: string;
params?: any;
}) => Promise<any>;
};
};
}
}
/**
* Phantom connector
* Implements wallet connection for Phantom (Solana)
*/
declare class PhantomConnector extends BaseConnector {
constructor();
/**
* Check if Phantom is available
*/
isAvailable(): boolean;
/**
* Connect to Phantom
* @param chainId Optional chain ID to connect to
*/
connect(chainId?: ChainId): Promise<string>;
/**
* Disconnect from Phantom
*/
disconnect(): Promise<void>;
/**
* Switch to a different Solana network
* @param chainId Chain ID to switch to (e.g., 'mainnet-beta', 'testnet', 'devnet')
*/
switchChain(chainId: ChainId): Promise<void>;
/**
* Set up event listeners for Phantom events
*/
private setupEventListeners;
/**
* Remove event listeners
*/
private removeEventListeners;
/**
* Handle account changed event
* @param publicKey New public key
*/
private handleAccountChanged;
/**
* Handle disconnect event
*/
private handleDisconnect;
}
/**
* Get all available connectors
* @returns Array of available connectors
*/
declare function getAvailableConnectors(): Connector[];
/**
* Get a connector by ID
* @param id Connector ID
* @returns Connector or undefined if not found
*/
declare function getConnectorById(id: string): Connector | undefined;
export { BaseConnector as B, ConnectorStatus as C, MetaMaskConnector as M, PhantomConnector as P, type WalletInfo as W, type Connector as a, type ConnectorError as b, ConnectorEvent as c, getConnectorById as d, getAvailableConnectors as g };