0.0.740 • Published 12 months ago

@b3dotfun/react v0.0.740

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

12 months ago

0.0.740-alpha.4

12 months ago

0.0.740-alpha.3

12 months ago

0.0.740-alpha.2

12 months ago

0.0.740-alpha.1

12 months ago

0.0.740-test.2

12 months ago

0.0.740-test.1

12 months ago

0.0.739

12 months ago

0.0.738

12 months ago

0.0.737

12 months ago

0.0.736

12 months ago

0.0.735

12 months ago

0.0.734

12 months ago

0.0.733

12 months ago

0.0.732

12 months ago

0.0.731

12 months ago

0.0.730

12 months ago

0.0.729

12 months ago

0.0.728

12 months ago

0.0.727

12 months ago

0.0.726

12 months ago

0.0.725

12 months ago

0.0.724

12 months ago

0.0.723

12 months ago

0.0.721

12 months ago

0.0.720

12 months ago

0.0.719

12 months ago

0.0.718

12 months ago

0.0.717

12 months ago

0.0.716

12 months ago

0.0.715

12 months ago

0.0.714

12 months ago

0.0.713

12 months ago

0.0.712

12 months ago

0.0.711

12 months ago

0.0.710

12 months ago

0.0.709

12 months ago

0.0.708

12 months ago

0.0.707

12 months ago

0.0.706

12 months ago

0.0.705

12 months ago

0.0.704

12 months ago

0.0.703

12 months ago

0.0.702

12 months ago

0.0.701

12 months ago

0.0.700

1 year ago

0.0.699

1 year ago

0.0.698

1 year ago

0.0.697

1 year ago

0.0.696

1 year ago

0.0.695

1 year ago

0.0.694

1 year ago

0.0.693

1 year ago

0.0.692

1 year ago

0.0.691

1 year ago

0.0.690

1 year ago

0.0.689

1 year ago

0.0.688

1 year ago

0.0.687

1 year ago

0.0.686

1 year ago

0.0.685

1 year ago

0.0.684

1 year ago

0.0.683

1 year ago

0.0.682

1 year ago

0.0.681

1 year ago

0.0.680

1 year ago

0.0.679

1 year ago

0.0.678

1 year ago

0.0.677

1 year ago

0.0.676

1 year ago

0.0.675

1 year ago

0.0.674

1 year ago

0.0.673

1 year ago

0.0.671

1 year ago

0.0.67

1 year ago

0.0.65

1 year ago

0.0.64

1 year ago

0.0.63

1 year ago

0.0.62

1 year ago

0.0.61

1 year ago

0.0.60

1 year ago

0.0.59

1 year ago

0.0.58

1 year ago

0.0.57

1 year ago

0.0.56

1 year ago

0.0.55

1 year ago

0.0.54-alpha.49

1 year ago

0.0.54-alpha.48

1 year ago

0.0.54-alpha.47

1 year ago

0.0.54-alpha.46

1 year ago

0.0.54-alpha.45

1 year ago

0.0.54-alpha.44

1 year 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