0.0.740 • Published 10 months ago

@b3dotfun/react v0.0.740

Weekly downloads
-
License
-
Repository
-
Last release
10 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

10 months ago

0.0.740-alpha.4

10 months ago

0.0.740-alpha.3

10 months ago

0.0.740-alpha.2

10 months ago

0.0.740-alpha.1

10 months ago

0.0.740-test.2

10 months ago

0.0.740-test.1

10 months ago

0.0.739

10 months ago

0.0.738

10 months ago

0.0.737

10 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

11 months ago

0.0.689

11 months ago

0.0.688

11 months ago

0.0.7-react-native

11 months ago

0.0.6-react-native

11 months ago

0.0.5-react-native

11 months ago

0.0.4-react-native

11 months ago

0.0.3-react-native

11 months ago

0.0.2-react-native

11 months ago

0.0.687

11 months ago

0.0.686

11 months ago

0.0.685

11 months ago

0.0.684

11 months ago

0.0.683

11 months ago

0.0.682

11 months ago

0.0.681

11 months ago

0.0.1-react-native

11 months ago

0.0.680

11 months ago

0.0.679

11 months ago

0.0.678

11 months ago

0.0.677

11 months ago

0.0.676

11 months ago

0.0.675

11 months ago

0.0.674

11 months ago

0.0.673

11 months ago

0.0.671

11 months ago

0.0.67

11 months ago

0.0.65

11 months ago

0.0.64

11 months ago

0.0.63

11 months ago

0.0.62

11 months ago

0.0.61

11 months ago

0.0.60

11 months ago

0.0.59

11 months ago

0.0.58

11 months ago

0.0.57

11 months ago

0.0.56

11 months ago

0.0.55

11 months ago

0.0.54-alpha.540

11 months ago

0.0.54-alpha.539

11 months ago

0.0.54-alpha.538

11 months ago

0.0.54-alpha.537

11 months ago

0.0.54-alpha.536

11 months ago

0.0.54-alpha.535

11 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

12 months ago

0.0.54-alpha.519

12 months ago

0.0.54-alpha.518

12 months ago

0.0.54-alpha.517

12 months ago

0.0.54-alpha.516

12 months ago

0.0.54-alpha.515

12 months ago

0.0.54-alpha.513

12 months ago

0.0.54-alpha.512

12 months ago

0.0.54-alpha.511

12 months ago

0.0.54-alpha.510

12 months ago

0.0.54-alpha.509

12 months ago

0.0.54-alpha.508

12 months ago

0.0.54-alpha.507

12 months ago

0.0.54-alpha.506

12 months ago

0.0.54-alpha.505

12 months ago

0.0.54-alpha.504

12 months ago

0.0.54-alpha.503

12 months ago

0.0.54-alpha.502

12 months ago

0.0.54-alpha.501

12 months ago

0.0.54-alpha.500

12 months ago

0.0.54-alpha.499

12 months ago

0.0.54-alpha.498

12 months ago

0.0.54-alpha.497

12 months ago

0.0.54-alpha.496

12 months ago

0.0.54-alpha.495

12 months ago

0.0.54-alpha.494

12 months ago

0.0.54-alpha.493

12 months ago

0.0.54-alpha.492

12 months ago

0.0.54-alpha.491

12 months ago

0.0.54-alpha.49

12 months ago

0.0.54-alpha.486

12 months ago

0.0.54-alpha.484

12 months ago

0.0.54-alpha.483

12 months ago

0.0.54-alpha.482

12 months ago

0.0.54-alpha.481

12 months ago

0.0.54-alpha.48

12 months ago

0.0.54-alpha.47

12 months ago

0.0.54-alpha.46

12 months ago

0.0.54-alpha.45

12 months ago

0.0.54-alpha.44

12 months ago

0.0.54-alpha.43

1 year ago

0.0.54-alpha.41

1 year ago

0.0.54-alpha.38

1 year ago

0.0.54-alpha.37

1 year ago

0.0.54-alpha.36

1 year ago

0.0.54-alpha.35

1 year ago

0.0.54-alpha.34

1 year ago

0.0.54-alpha.33

1 year ago

0.0.54-alpha.32

1 year ago

0.0.54-alpha.31

1 year ago

0.0.54-alpha.30

1 year ago

0.0.54-alpha.29

1 year ago

0.0.54-alpha.28

1 year ago

0.0.54-alpha.27

1 year ago

0.0.54-alpha.26

1 year ago

0.0.54-alpha.25

1 year ago

0.0.54-alpha.24

1 year ago

0.0.54-alpha.23

1 year ago

0.0.54-alpha.22

1 year ago

0.0.54-alpha.21

1 year ago

0.0.54-alpha.20

1 year ago

0.0.54-alpha.19

1 year ago

0.0.54-alpha.18

1 year ago

0.0.54-alpha.17

1 year ago

0.0.54-alpha.16

1 year ago

0.0.54-alpha.15

1 year ago

0.0.54-alpha.14

1 year ago

0.0.54-alpha.13

1 year ago

0.0.54-alpha.12

1 year ago

0.0.54-alpha.11

1 year ago

0.0.54-alpha.10

1 year ago

0.0.54-alpha.9

1 year ago

0.0.54-alpha.8

1 year ago

0.0.54-alpha.7

1 year ago

0.0.54-alpha.6

1 year ago

0.0.54-alpha.5

1 year ago

0.0.54-alpha.4

1 year ago

0.0.54-alpha.3

1 year ago

0.0.54-alpha.2

1 year ago

0.0.54-alpha.1

1 year ago

0.0.52-alpha.11

1 year ago

0.0.52-alpha.10

1 year ago

0.0.52-alpha.9

1 year ago

0.0.52-alpha.8

1 year ago

0.0.52-alpha.7

1 year ago

0.0.52-alpha.6

1 year ago

0.0.52-alpha.5

1 year ago

0.0.52-alpha.4

1 year ago

0.0.52-alpha.3

1 year ago

0.0.52-alpha.2

1 year ago

0.0.52-alpha.1

1 year ago

0.0.51

1 year ago

0.0.51-alpha.1

1 year ago

0.0.50

1 year ago

0.0.49-alpha.3

1 year ago

0.0.49-alpha.2

1 year ago

0.0.49-alpha.1

1 year ago

0.0.48

1 year ago

0.0.47

1 year ago

0.0.46

1 year ago

0.0.45

1 year ago

0.0.44

1 year ago

0.0.43

1 year ago

0.0.42

1 year ago

0.0.41

1 year ago

0.0.40

1 year ago

0.0.39

1 year ago

0.0.38

1 year ago

0.0.37

1 year ago

0.0.36

1 year ago

0.0.35

1 year ago

0.0.34

1 year ago

0.0.33

1 year ago

0.0.31

1 year ago

0.0.30

1 year ago

0.0.29

1 year ago

0.0.28

1 year ago

0.0.27

1 year ago

0.0.26

1 year ago

0.0.25

1 year ago

0.0.24

1 year ago

0.0.23

1 year ago

0.0.22

1 year ago

0.0.21

1 year ago

0.0.20

1 year ago

0.0.19

1 year ago

0.0.18

1 year ago

0.0.17

1 year ago

0.0.16

1 year ago

0.0.15

1 year ago

0.0.14

1 year ago

0.0.13

1 year ago

0.0.12

1 year ago

0.0.11

1 year ago

0.0.10

1 year ago

0.0.9

1 year ago

0.0.8

1 year ago

0.0.7

1 year ago

0.0.6

1 year ago

0.0.5

1 year ago

0.0.4

1 year ago

0.0.3

1 year ago

0.0.2

1 year ago

0.0.1

1 year ago