我是Web3开发人员,试图使用Typescript
为window.solana
添加类型
尚未找到任何合适的解决方案
通常在solana typescript中,我们导入适配器:
import {
PhantomWalletAdapter,
SlopeWalletAdapter,
SolflareWalletAdapter,
SolletWalletAdapter,
} from "@solana/wallet-adapter-wallets";
import type { WalletProviderProps } from "@solana/wallet-adapter-react";
然后为钱包提供商编写一个函数:
export function ClientWalletProvider(
props: Omit<WalletProviderProps, "wallets">
): JSX.Element {
const wallets = useMemo(
() => [
new PhantomWalletAdapter(),
new SlopeWalletAdapter(),
new SolflareWalletAdapter(),
new SolletWalletAdapter(),
],
[YOURNETWORK]
);
如果您进入PhantomWalletAdapter
的源代码。
export declare class PhantomWalletAdapter extends BaseMessageSignerWalletAdapter {
name: WalletName<"Phantom">;
url: string;
icon: string;
private _connecting;
private _wallet;
private _publicKey;
private _readyState;
constructor(config?: PhantomWalletAdapterConfig);
get publicKey(): PublicKey | null;
get connecting(): boolean;
get connected(): boolean;
get readyState(): WalletReadyState;
connect(): Promise<void>;
disconnect(): Promise<void>;
sendTransaction(transaction: Transaction, connection: Connection, options?: SendTransactionOptions): Promise<TransactionSignature>;
signTransaction(transaction: Transaction): Promise<Transaction>;
signAllTransactions(transactions: Transaction[]): Promise<Transaction[]>;
signMessage(message: Uint8Array): Promise<Uint8Array>;
private _disconnected;
}
如果你的浏览器上安装了任何钱包,它都会注入solana,所以如果你在控制台上键入solana
,你可以为你的项目编写自己的类型
connect: ƒ (e)
disconnect: ƒ ()
isPhantom: true
openBridge: ƒ ()
postMessage: ƒ (e)
request: ƒ (e)
signAllTransactions: ƒ ()
signAndSendTransaction: ƒ (e)
signMessage: ƒ (e)
signTransaction: ƒ (e)
// _ is indicating that those methods should not be called publicly
// so you dont need to add this to your type
_checkIfValidRequestOrThrow: ƒ (e)
_createHandleIncomingRequestMiddleWare: ƒ ()
_events: i {}
_eventsCount: 0
_handleDisconnect: ƒ ()
_handleMessage: ƒ (e)
_handleStreamDisconnect: ƒ (e)
_incomingJsonRpc: o {_events: {…}, _eventsCount: 1, _maxListeners: undefined, _middleware: Array(2)}
_nextRequestId: 1
_outgoingJsonRpc: o {_events: {…}, _eventsCount: 0, _maxListeners: undefined, _middleware: Array(3)}
_publicKey: null
_responseCallbacks: Map(0) {size: 0}