0.0.740 • Published 9 months ago

@b3dotfun/react v0.0.740

Weekly downloads
-
License
-
Repository
-
Last release
9 months ago

@b3dotfun/react

A React component library for Anyspend & B3 Global Accounts. Includes integration with web & mobile applications, providing easy-to-use components for Web3 transactions, Web3 authentication, wallet management, and blockchain interactions & more.

Current compatibility

FeatureReact WebReact Native
AnySpend
Global Accounts (beta)

Installation

npm install @b3dotfun/react
# or
yarn add @b3dotfun/react
# or
pnpm add @b3dotfun/react

Features

  • Anyspend: A React SDK for integrating with the Anyspend protocol, providing hooks and utilities for cross-chain swaps, NFT minting, and other blockchain operations.
  • Global Accounts: B3 Global Accounts allow for seamlessly authenticating and transaction in our ecosystem, using the same account throughout the entire experience.

AnySpend

Documentation coming soon.

Global Accounts

Usage

Basic Setup

Wrap your application with the B3Provider:

import { B3Provider } from "@b3dotfun/react";
import "@b3dotfun/react/styles"; // Import default styles

function App() {
  return <B3Provider environment="production">{/* Your app content */}</B3Provider>;
}

Authentication with SignInWithB3

import { SignInWithB3, B3Provider } from "@b3dotfun/react";

function App() {
  return (
    <B3Provider environment="production">
      <SignInWithB3
        strategies={["google"]}
        partnerId="YOUR_PARTNER_ID"
        onLoginSuccess={globalAccount => {
          console.log("User authenticated with Global Account!", globalAccount);
        }}
      />
    </B3Provider>
  );
}

Using Session Keys

For enhanced security and user experience, you can use session keys with MetaMask:

import { B3Provider, SignInWithB3 } from "@b3dotfun/react";
import B3DynamicModal from "@b3dotfun/react/components/B3DynamicModal";
import { useEffect, useState } from "react";

// Add Ethereum provider type declarations
declare global {
  interface Window {
    ethereum: {
      request: (args: { method: string; params?: any[] }) => Promise<any>;
      on: (event: string, listener: (...args: any[]) => void) => void;
      removeListener: (event: string, listener: (...args: any[]) => void) => void;
    };
  }
}

// Define chain configuration
const b3Chain = {
  id: 8333,
  name: "B3",
  nativeCurrency: {
    name: "Ether",
    symbol: "ETH",
    decimals: 18
  },
  rpc: "https://mainnet-rpc.b3.fun",
  icon: {
    url: "https://cdn.b3.fun/b3-logo-white-circle.svg",
    width: 32,
    height: 32,
    format: "svg"
  },
  blockExplorers: [
    {
      name: "B3 Explorer",
      url: "https://explorer.b3.fun/"
    }
  ]
};

function App() {
  const [account, setAccount] = useState<string | null>(null);

  useEffect(() => {
    const connectToWallet = async () => {
      if (typeof window.ethereum !== "undefined") {
        try {
          // Request account access
          const accounts = await window.ethereum.request({ method: "eth_requestAccounts" });
          setAccount(accounts[0]);

          // Listen for account changes
          const handleAccountsChanged = (accounts: string[]) => {
            setAccount(accounts[0]);
          };

          window.ethereum.on("accountsChanged", handleAccountsChanged);

          return () => {
            // Clean up listeners when component unmounts
            if (window.ethereum) {
              window.ethereum.removeListener("accountsChanged", handleAccountsChanged);
            }
          };
        } catch (error) {
          console.error("Error connecting to wallet:", error);
        }
      } else {
        console.error("No Ethereum wallet detected in browser");
      }
    };

    connectToWallet();
  }, []);

  if (!account) {
    return <div>Please connect your wallet</div>;
  }

  return (
    <B3Provider environment="production">
      <B3DynamicModal />
      <div className="flex min-h-screen items-center justify-center bg-white">
        <div className="text-center">
          <h1 className="mb-6 text-3xl font-bold">B3 Authentication Example</h1>
          <SignInWithB3
            provider={{
              strategy: "google"
            }}
            chain={b3Chain}
            partnerId="YOUR_PARTNER_ID"
            sessionKeyAddress={account as `0x${string}`}
            onLoginSuccess={globalAccount => {
              console.log("User authenticated with Global Account!", globalAccount);
            }}
          />
        </div>
      </div>
    </B3Provider>
  );
}

Accessing B3 Context

import { useB3 } from "@b3dotfun/react";

function MyComponent() {
  const { account } = useB3();

  return <div>{!!account ? "User is logged in" : "User is not logged in"}</div>;
}

Using the Request Permissions Button

After authenticating with a session key, you can request specific permissions for smart contract interactions:

import { RequestPermissionsButton } from "@b3dotfun/react";

// Define your chain configuration
const b3Chain = {
  id: 8333,
  name: "B3",
  rpc: "https://mainnet-rpc.b3.fun"
  // other chain properties
};

function RequestPermissionsExample({ sessionKeyAddress }) {
  return (
    <RequestPermissionsButton
      chain={b3Chain}
      sessionKeyAddress={sessionKeyAddress}
      onSuccess={() => {
        console.log("Successfully requested permissions!");
      }}
      onError={error => {
        console.error("Error requesting permissions:", error);
      }}
      permissions={{
        // Specify contract addresses that the session key can interact with
        approvedTargets: ["0x06012c8cf97BEaD5deAe237070F9587f8E7A266d", "0xa8e42121e318e3D3BeD7f5969AF6D360045317DD"],
        // Set validity period for the permissions
        startDate: new Date(),
        endDate: new Date(Date.now() + 1000 * 60 * 60 * 24 * 30), // 30 days
        nativeTokenLimitPerTransaction: 0.0001
      }}
    />
  );
}

Using the Modal System

import { B3Provider } from "@b3dotfun/react";
import B3DynamicModal from "@b3dotfun/react/components/B3DynamicModal";

function App() {
  return (
    <B3Provider environment="production">
      <YourApp />
      <B3DynamicModal />
    </B3Provider>
  );
}

Cross-chain swap, minting NFTs, and more with AnySpend

AnySpend components simplify cross-chain swaps, NFT minting, and other blockchain operations.

Cross-chain swap example

Page mode

import { AnySpend } from "@b3dotfun/react";

function CrossChainSwapExample() {
  return <AnySpend isMainnet={true} mode="page" />;
}

Modal mode

import { AnySpend } from "@b3dotfun/react";

function CrossChainSwapExample() {
  const { setB3ModalOpen, setB3ModalContentType } = useModalStore();

  const handleClick = () => {
    setB3ModalOpen(true);
    setB3ModalContentType({
      type: "anySpend",
      isMainnet: true
    });
  };

  return <button onClick={handleClick}>Cross-chain swap</button>;
}

NFT minting example

import { AnySpendNFTButton } from "@b3dotfun/react/components/AnySpend/AnySpendNFTButton";
import { useB3 } from "@b3dotfun/react";

function NFTMintingExample() {
  const { account } = useB3();

  // Define your NFT contract details
  const nftContract = {
    address: "0x9c275ff1634519E9B5449ec79cd939B5F900564d", // NFT contract address
    chainId: 8333, // B3 chain ID
    name: "Example NFT Collection",
    imageUrl: "https://example.com/nft-image.jpg",
    description: "A beautiful NFT from our collection",
    price: "1000000000000000000", // Price in wei (1 ETH)
    currency: {
      symbol: "ETH",
      decimals: 18
    }
  };

  return (
    <div className="nft-card">
      <img src={nftContract.imageUrl} alt={nftContract.name} />
      <h3>{nftContract.name}</h3>
      <p>{nftContract.description}</p>
      <p>
        Price: {nftContract.price} {nftContract.currency.symbol}
      </p>

      <AnySpendNFTButton nftContract={nftContract} recipientAddress={account?.address} />
    </div>
  );
}

The AnySpendNFTButton component handles the entire minting process, including:

  • Checking wallet connection
  • Requesting necessary permissions
  • Processing the transaction
  • Handling success and error states

You can customize the appearance and behavior of the button using standard React props.

Components

  • SignInWithB3: Authentication component with multiple provider options
  • B3Provider: Context provider for B3 services
  • B3DynamicModal: Dynamic modal system for B3 interactions
  • RequestPermissionsButton: Component to request permissions for session keys
  • AnySpend: Components for cryptocurrency transactions
  • AnySpendNFT: NFT-specific transaction component

Advanced Configuration

The B3Provider accepts several configuration options:

<B3Provider
  environment="production" // or "development"
  // Additional config options as needed
>
  {children}
</B3Provider>
0.0.740

9 months ago

0.0.740-alpha.4

9 months ago

0.0.740-alpha.3

9 months ago

0.0.740-alpha.2

9 months ago

0.0.740-alpha.1

9 months ago

0.0.740-test.2

9 months ago

0.0.740-test.1

9 months ago

0.0.739

9 months ago

0.0.738

9 months ago

0.0.737

9 months ago

0.0.736

10 months ago

0.0.735

10 months ago

0.0.734

10 months ago

0.0.733

10 months ago

0.0.732

10 months ago

0.0.731

10 months ago

0.0.730

10 months ago

0.0.729

10 months ago

0.0.728

10 months ago

0.0.727

10 months ago

0.0.726

10 months ago

0.0.725

10 months ago

0.0.724

10 months ago

0.0.723

10 months ago

0.0.721

10 months ago

0.0.720

10 months ago

0.0.719

10 months ago

0.0.718

10 months ago

0.0.717

10 months ago

0.0.716

10 months ago

0.0.715

10 months ago

0.0.714

10 months ago

0.0.713

10 months ago

0.0.712

10 months ago

0.0.711

10 months ago

0.0.710

10 months ago

0.0.709

10 months ago

0.0.708

10 months ago

0.0.707

10 months ago

0.0.706

10 months ago

0.0.705

10 months ago

0.0.704

10 months ago

0.0.703

10 months ago

0.0.702

10 months ago

0.0.701

10 months ago

0.0.700

10 months ago

0.0.699

10 months ago

0.0.698

10 months ago

0.0.697

10 months ago

0.0.696

10 months ago

0.0.695

10 months ago

0.0.694

10 months ago

0.0.693

10 months ago

0.0.692

10 months ago

0.0.691

10 months ago

0.0.690

10 months ago

0.0.689

10 months ago

0.0.688

10 months ago

0.0.7-react-native

10 months ago

0.0.6-react-native

10 months ago

0.0.5-react-native

10 months ago

0.0.4-react-native

10 months ago

0.0.3-react-native

10 months ago

0.0.2-react-native

10 months ago

0.0.687

10 months ago

0.0.686

10 months ago

0.0.685

10 months ago

0.0.684

10 months ago

0.0.683

10 months ago

0.0.682

10 months ago

0.0.681

10 months ago

0.0.1-react-native

10 months ago

0.0.680

10 months ago

0.0.679

10 months ago

0.0.678

10 months ago

0.0.677

10 months ago

0.0.676

10 months ago

0.0.675

10 months ago

0.0.674

10 months ago

0.0.673

10 months ago

0.0.671

10 months ago

0.0.67

10 months ago

0.0.65

10 months ago

0.0.64

10 months ago

0.0.63

10 months ago

0.0.62

10 months ago

0.0.61

10 months ago

0.0.60

10 months ago

0.0.59

10 months ago

0.0.58

10 months ago

0.0.57

10 months ago

0.0.56

10 months ago

0.0.55

10 months ago

0.0.54-alpha.540

10 months ago

0.0.54-alpha.539

10 months ago

0.0.54-alpha.538

10 months ago

0.0.54-alpha.537

10 months ago

0.0.54-alpha.536

10 months ago

0.0.54-alpha.535

10 months ago

0.0.54-alpha.534

11 months ago

0.0.54-alpha.533

11 months ago

0.0.54-alpha.532

11 months ago

0.0.54-alpha.531

11 months ago

0.0.54-alpha.530

11 months ago

0.0.54-alpha.529

11 months ago

0.0.54-alpha.528

11 months ago

0.0.54-alpha.527

11 months ago

0.0.54-alpha.526

11 months ago

0.0.54-alpha.525

11 months ago

0.0.54-alpha.524

11 months ago

0.0.54-alpha.523

11 months ago

0.0.54-alpha.522

11 months ago

0.0.54-alpha.521

11 months ago

0.0.54-alpha.520

11 months ago

0.0.54-alpha.519

11 months ago

0.0.54-alpha.518

11 months ago

0.0.54-alpha.517

11 months ago

0.0.54-alpha.516

11 months ago

0.0.54-alpha.515

11 months ago

0.0.54-alpha.513

11 months ago

0.0.54-alpha.512

11 months ago

0.0.54-alpha.511

11 months ago

0.0.54-alpha.510

11 months ago

0.0.54-alpha.509

11 months ago

0.0.54-alpha.508

11 months ago

0.0.54-alpha.507

11 months ago

0.0.54-alpha.506

11 months ago

0.0.54-alpha.505

11 months ago

0.0.54-alpha.504

11 months ago

0.0.54-alpha.503

11 months ago

0.0.54-alpha.502

11 months ago

0.0.54-alpha.501

11 months ago

0.0.54-alpha.500

11 months ago

0.0.54-alpha.499

11 months ago

0.0.54-alpha.498

11 months ago

0.0.54-alpha.497

11 months ago

0.0.54-alpha.496

11 months ago

0.0.54-alpha.495

11 months ago

0.0.54-alpha.494

11 months ago

0.0.54-alpha.493

11 months ago

0.0.54-alpha.492

11 months ago

0.0.54-alpha.491

11 months ago

0.0.54-alpha.49

11 months ago

0.0.54-alpha.486

11 months ago

0.0.54-alpha.484

11 months ago

0.0.54-alpha.483

11 months ago

0.0.54-alpha.482

11 months ago

0.0.54-alpha.481

11 months ago

0.0.54-alpha.48

11 months ago

0.0.54-alpha.47

11 months ago

0.0.54-alpha.46

11 months ago

0.0.54-alpha.45

11 months ago

0.0.54-alpha.44

11 months ago

0.0.54-alpha.43

12 months ago

0.0.54-alpha.41

12 months ago

0.0.54-alpha.38

12 months ago

0.0.54-alpha.37

12 months ago

0.0.54-alpha.36

12 months ago

0.0.54-alpha.35

12 months ago

0.0.54-alpha.34

12 months ago

0.0.54-alpha.33

12 months ago

0.0.54-alpha.32

12 months ago

0.0.54-alpha.31

12 months ago

0.0.54-alpha.30

12 months ago

0.0.54-alpha.29

12 months ago

0.0.54-alpha.28

12 months ago

0.0.54-alpha.27

12 months ago

0.0.54-alpha.26

12 months ago

0.0.54-alpha.25

12 months ago

0.0.54-alpha.24

12 months ago

0.0.54-alpha.23

12 months ago

0.0.54-alpha.22

12 months ago

0.0.54-alpha.21

12 months ago

0.0.54-alpha.20

12 months ago

0.0.54-alpha.19

12 months ago

0.0.54-alpha.18

12 months ago

0.0.54-alpha.17

12 months ago

0.0.54-alpha.16

12 months ago

0.0.54-alpha.15

12 months ago

0.0.54-alpha.14

12 months ago

0.0.54-alpha.13

12 months ago

0.0.54-alpha.12

12 months ago

0.0.54-alpha.11

12 months ago

0.0.54-alpha.10

12 months ago

0.0.54-alpha.9

12 months ago

0.0.54-alpha.8

12 months ago

0.0.54-alpha.7

12 months ago

0.0.54-alpha.6

12 months ago

0.0.54-alpha.5

12 months ago

0.0.54-alpha.4

12 months ago

0.0.54-alpha.3

12 months ago

0.0.54-alpha.2

12 months ago

0.0.54-alpha.1

12 months ago

0.0.52-alpha.11

12 months ago

0.0.52-alpha.10

12 months ago

0.0.52-alpha.9

12 months ago

0.0.52-alpha.8

12 months ago

0.0.52-alpha.7

12 months ago

0.0.52-alpha.6

12 months ago

0.0.52-alpha.5

12 months ago

0.0.52-alpha.4

12 months ago

0.0.52-alpha.3

12 months ago

0.0.52-alpha.2

12 months ago

0.0.52-alpha.1

12 months ago

0.0.51

12 months ago

0.0.51-alpha.1

12 months ago

0.0.50

12 months ago

0.0.49-alpha.3

12 months ago

0.0.49-alpha.2

12 months ago

0.0.49-alpha.1

12 months ago

0.0.48

12 months ago

0.0.47

12 months ago

0.0.46

12 months ago

0.0.45

12 months ago

0.0.44

12 months ago

0.0.43

12 months ago

0.0.42

12 months ago

0.0.41

12 months ago

0.0.40

12 months ago

0.0.39

12 months ago

0.0.38

12 months ago

0.0.37

12 months ago

0.0.36

12 months ago

0.0.35

12 months ago

0.0.34

12 months ago

0.0.33

12 months ago

0.0.31

12 months ago

0.0.30

12 months ago

0.0.29

12 months ago

0.0.28

12 months ago

0.0.27

12 months ago

0.0.26

12 months ago

0.0.25

12 months ago

0.0.24

12 months ago

0.0.23

12 months ago

0.0.22

12 months ago

0.0.21

12 months ago

0.0.20

12 months ago

0.0.19

12 months ago

0.0.18

12 months ago

0.0.17

12 months ago

0.0.16

12 months ago

0.0.15

12 months ago

0.0.14

12 months ago

0.0.13

12 months ago

0.0.12

12 months ago

0.0.11

12 months ago

0.0.10

12 months ago

0.0.9

12 months ago

0.0.8

12 months ago

0.0.7

12 months ago

0.0.6

12 months ago

0.0.5

12 months ago

0.0.4

12 months ago

0.0.3

12 months ago

0.0.2

1 year ago

0.0.1

1 year ago