Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

connect phantom wallet react typescript

// create phantomProvider.ts file
import {
    PublicKey,
    Transaction,
  } from "@solana/web3.js";
  
  
  
  type DisplayEncoding = "utf8" | "hex";
  type PhantomEvent = "disconnect" | "connect" | "accountChanged";
  type PhantomRequestMethod =
    | "connect"
    | "disconnect"
    | "signTransaction"
    | "signAllTransactions"
    | "signMessage";
  
   interface ConnectOpts {
    onlyIfTrusted: boolean;
  }
  
  export interface PhantomProvider {
    publicKey: PublicKey | null;
    isConnected: boolean | null;
    signTransaction: (transaction: Transaction) => Promise<Transaction>;
    signAllTransactions: (transactions: Transaction[]) => Promise<Transaction[]>;
    signMessage: (
      message: Uint8Array | string,
      display?: DisplayEncoding
    ) => Promise<any>;
    connect: (opts?: Partial<ConnectOpts>) => Promise<{ publicKey: PublicKey }>;
    disconnect: () => Promise<void>;
    on: (event: PhantomEvent, handler: (args: any) => void) => void;
    request: (method: PhantomRequestMethod, params: any) => Promise<unknown>;
  }

//create getProvider.ts file
import { PhantomProvider } from "./PhontomProvider";

export const getProvider = (): PhantomProvider | undefined => {
    if ("solana" in window) {
      // @ts-ignore
      const provider = window.solana as any;
      if (provider.isPhantom) return provider as PhantomProvider;
    }
    window.open("https://phantom.app/", "_blank");
  };

//App.tsx
import React, { useState } from "react";
import { PhantomProvider } from "../phantom/PhontomProvider";
import {
  connectToPhantomWallet,
  handleWalletDisconnect,
} from "../services/handleWallet";

const App: React.FC = () => {
  const [provider, setProvider] = useState<PhantomProvider | undefined>(
    undefined
  );
  const [walletKey, setWalletKey] = useState<PhantomProvider | undefined>(
    undefined
  );

  return (
    <div>
      {!provider && !walletKey ? (
        <button
          onClick={() => {
            connectToPhantomWallet(setProvider, setWalletKey);
          }}
        >
          Connect Wallet
        </button>
      ) : (
        <button
          onClick={() => {
            handleWalletDisconnect(setProvider, setWalletKey);
          }}
        >
          Disconnect Wallet
        </button>
      )}
    </div>
  );
};

export default App;
Comment

connect phantom wallet react typescript

// create phantomProvider.ts file
import {
    PublicKey,
    Transaction,
  } from "@solana/web3.js";
  
  
  
  type DisplayEncoding = "utf8" | "hex";
  type PhantomEvent = "disconnect" | "connect" | "accountChanged";
  type PhantomRequestMethod =
    | "connect"
    | "disconnect"
    | "signTransaction"
    | "signAllTransactions"
    | "signMessage";
  
   interface ConnectOpts {
    onlyIfTrusted: boolean;
  }
  
  export interface PhantomProvider {
    publicKey: PublicKey | null;
    isConnected: boolean | null;
    signTransaction: (transaction: Transaction) => Promise<Transaction>;
    signAllTransactions: (transactions: Transaction[]) => Promise<Transaction[]>;
    signMessage: (
      message: Uint8Array | string,
      display?: DisplayEncoding
    ) => Promise<any>;
    connect: (opts?: Partial<ConnectOpts>) => Promise<{ publicKey: PublicKey }>;
    disconnect: () => Promise<void>;
    on: (event: PhantomEvent, handler: (args: any) => void) => void;
    request: (method: PhantomRequestMethod, params: any) => Promise<unknown>;
  }

//create getProvider.ts file
import { PhantomProvider } from "./PhontomProvider";

export const getProvider = (): PhantomProvider | undefined => {
    if ("solana" in window) {
      // @ts-ignore
      const provider = window.solana as any;
      if (provider.isPhantom) return provider as PhantomProvider;
    }
    window.open("https://phantom.app/", "_blank");
  };

//App.tsx
import React, { useState } from "react";
import { PhantomProvider } from "../phantom/PhontomProvider";
import {
  connectToPhantomWallet,
  handleWalletDisconnect,
} from "../services/handleWallet";

const App: React.FC = () => {
  const [provider, setProvider] = useState<PhantomProvider | undefined>(
    undefined
  );
  const [walletKey, setWalletKey] = useState<PhantomProvider | undefined>(
    undefined
  );

  return (
    <div>
      {!provider && !walletKey ? (
        <button
          onClick={() => {
            connectToPhantomWallet(setProvider, setWalletKey);
          }}
        >
          Connect Wallet
        </button>
      ) : (
        <button
          onClick={() => {
            handleWalletDisconnect(setProvider, setWalletKey);
          }}
        >
          Disconnect Wallet
        </button>
      )}
    </div>
  );
};

export default App;
Comment

PREVIOUS NEXT
Code Example
Javascript :: how to generate random 6 digit charecter in js for coupon 
Javascript :: phaser place on circles 
Javascript :: phaser random circle 
Javascript :: phaser set alpha 
Javascript :: phaser animation repeat event 
Javascript :: phaser mixed animation 
Javascript :: phaser reverse animation 
Javascript :: add multiple phone using js 
Javascript :: swr vs axios 
Javascript :: remove text and keep div inside a div jquery 2 
Javascript :: file size to string js 
Javascript :: string variable 
Javascript :: store reference of event listener inside a element 
Javascript :: white space below image next image 
Javascript :: javascript show alert if browser is not google chrome 
Javascript :: what is computed property in vue js 
Javascript :: js foreach syntax 
Javascript :: loop on each character js 
Javascript :: localstorage in next js 
Javascript :: anonymous function 
Javascript :: vue js laravel tutorial 
Javascript :: in js pass infinite argument in function 
Javascript :: get the last item in an array 
Javascript :: Auto increment in firebase realtime database 
Javascript :: preview file before upload in react 
Javascript :: working of a recursive function 
Javascript :: converter rgba to hex without opacity 
Javascript :: create a node 
Javascript :: how to clear nodejs terminal in vs code 
Javascript :: how to add function in javascript 
ADD CONTENT
Topic
Content
Source link
Name
6+1 =