1.3.3 • Published 10 months ago

@fractalwagmi/react-sdk v1.3.3

Weekly downloads
-
License
-
Repository
github
Last release
10 months ago

Fractal React SDK

Before Integrating

You will need to provide us with a list of allowed origins (e.g. http://localhost, https://your.game.com). Please reach out to us at developers@fractal.is and let us know the origins you would like to allow when you are ready to start integrating.

Installation

npm install @fractalwagmi/react-sdk

Example

SDK Demo Preview

Setup and Authentication

1. Import the global stylesheet

If using the provided default button rather than your own UI, import our global stylesheet to ensure that the component renders correctly:

import '@fractalwagmi/react-sdk/styles.css';

2. Set up the provider

Render the provider above any components that need access to data from the SDK.

import { FractalProvider } from '@fractalwagmi/react-sdk';

const App = () => {
  return <FractalProvider clientId="YOUR_CLIENT_ID">...</FractalProvider>;
};

3A. Render the SignInWithFractal component

This will display a button for logging in.

import {
  Scope,
  SignInWithFractal,
  User,
  FractalSDKError,
} from '@fractalwagmi/react-sdk';

export function YourSignInComponent() {
  return (
    <SignInWithFractal
      // `scopes` defaults to [Scope.IDENTIFY].
      scopes={[Scope.IDENTIFY, Scope.ITEMS_READ, Scope.COINS_READ]}
      onError={(err: FractalSDKError) => {
        console.log(err.getUserFacingErrorMessage());
      }}
      onSuccess={(user: User) => {
        console.log('user = ', user);
      }}
    />
  );
}

SignInWithFractal Props

PropType / DescriptionDefault
buttonPropsHTMLAttributes<HTMLButtonElement>Any additional props for <button> that should be passed to the default sign-in button.{}
onError(e: FractalSDKError) => voidA callback function to call when an error occurs.undefined
onSuccess(user: User) => voidA callback function to call when a user successfully signs in.undefined
onSignOut() => voidA callback function to call when a sign out occurs.undefined
scopesScope[]The scope to assign to the access token. See src/types/scope.ts for a list of available scopes.[Scope.IDENTIFY]
variant"light" \| "dark"The button style variant to use."light"

Customizations

By default, there are 2 button variants that we support:

const YourComponent = () => {
  // There is a "light" (default) and "dark" variant:
  return <SignInWithFractal variant="dark">;
}

You can make minor adjustments with your own class name to override any styles:

const YourComponent = () => {
  // Using your own class name to override any default styles:
  return <SignInWithFractal className="foobar">;
};

3B: Render Your Own Button

You can go headless and render your own button with the help of the useAuthButtonProps hook.

This option offers full control over your mark up and styles:

import { useAuthButtonProps, Scope } from '@fractalwagmi/react-sdk';

const YourButtonComponent = () => {
  const { loading, signedIn, onClick } = useAuthButtonProps({
    scopes: [Scope.IDENTIFY, Scope.ITEMS_READ, Scope.COINS_READ],
  });

  if (loading) {
    return '...';
  }

  return <button onClick={onClick}>{signedIn ? 'Sign Out' : 'Sign In'}</button>;
};

useAuthButtonProps supports the same props as SignInWithFractal except for the variant and buttonProps.

Be sure to add support for both signed in and signed out states (like in the example above with the alternating button text,) because the onClick prop will invoke different logic based on the signedIn boolean.

General Error Handling

All exported error classes extends FractalSDKError which extends the native JS Error class.

All exported error classes have a getUserFacingErrorMessage method that returns a UI-friendly fallback message, but we encourage you to handle each error case individually and render UI text that is appropriate for your application.

You may handle different error cases using instanceof checks to infer the meaning of the error state. Example:

import {
  useSignTransaction,
  FractalSDKSignTransactionDeniedError,
  FractalSDKApprovalOccurringError,
} from '@fractalwagmi/react-sdk';

const MyComponent = () => {
  const { signTransaction } = useSignTransaction();

  const doSignTransaction = async () => {
    try {
      await signTransaction('some base58 transaction string');
    } catch (err: unknown) {
      if (error instanceof FractalSDKApprovalOccurringError) {
        console.log('an approval is already occurring');
      }
      if (error instanceof FractalSDKSignTransactionDeniedError) {
        console.log('transaction denied');
      }
    }
  };

  return <button onClick={() => doSignTransaction()}>...</button>;
};

Data Hooks

There are a wide variety of hooks that wrap our API functions to give you access to user data.

import {
  useCoins,
  useItems,
  useUser,
  useUserWallet,
} from '@fractalwagmi/react-sdk';

export function YourWalletComponent() {
  // Returns user information like email, username, and id.
  const { data: user } = useUser();

  // Returns the user's wallet information like solana public keys.
  const { data: userWallet } = useUserWallet();

  // Returns the items in the user's wallet.
  const { data: items } = useItems();

  // Returns the coins in the user's wallet.
  const { data: coins } = useCoins();

  return <div>...</div>;
}

Functional Hooks

Signing Out

If you need to programmatically sign the user out, you can use the useSignOut hook:

import { useSignOut } from '@fractalwagmi/react-sdk';

export function YourWalletComponent() {
  const { signOut } = useSignOut();

  return <button onClick={signOut}>Your Sign Out Button Text</button>;
}

Approving a Generic Transaction

If you need the user to approve a generic transaction, you can create an unsigned transaction and initialize an approval popup flow for the user to approve the transaction:

import { useSignTransaction } from '@fractalwagmi/react-sdk';

interface YourComponentProps {
  someTransactionB58: string | undefined;
}

export function YourComponent({ someTransactionB58 }: YourComponentProps) {
  const {
    // An async function to run which request's user approval to sign a
    // transaction.
    signTransaction,
  } = useSignTransaction();

  return (
    <div>
      <button
        onClick={async () => {
          try {
            const { signature } = await signTransaction(someTransactionB58);
            // This is the transaction signature for the signed transaction.
            console.log('signature = ', signature);
          } catch (err: unknown) {
            // See memo below on error handling.
            console.log('err = ', err);
          }
        }}
      >
        Request user approval for transaction
      </button>
    </div>
  );
}

Keep in mind that a signed transaction does not mean that it has been posted to the chain yet. As of now, this hook only returns a signed transaction signature.

If you need to know when a transaction completes, use the returned transaction signature and Solana's JSON RPC API to accomplish this.

Error handling for useSignTransaction

The signTransaction function returned by useSignTransaction will potentially throw the following error classes:

Error classMeaning
FractalSDKAuthenticationErrorAn authentication error occurred. This typically means that the user is not properly authenticated.
FractalSDKApprovalOccurringErrorAn approval flow popup is already open for this hook instance. This error can occur if the unsignedTransactionB58 input changes while approving=true.
FractalSDKInvalidTransactionErrorThe transaction input was invalid.
FractalSDKSignTransactionDeniedErrorThe transaction was denied.
FractalSDKSignTransactionUnknownErrorAn unknown error occurred (catch-all).
1.3.3

10 months ago

1.3.1-alpha05

12 months ago

1.3.2

12 months ago

1.3.1

12 months ago

1.3.1-alpha

1 year ago

1.3.1-alpha03

1 year ago

1.3.1-alpha02

1 year ago

1.3.1-alpha04

1 year ago

1.3.1-alpha01

1 year ago

1.2.3-test.0

1 year ago

1.2.3-test.2

1 year ago

1.2.3-test.1

1 year ago

1.2.2-1

1 year ago

1.2.3

1 year ago

1.2.2

1 year ago

1.2.0

1 year ago

1.2.1

1 year ago

1.1.6-alpha.18

1 year ago

1.1.6-alpha.19

1 year ago

1.1.6-alpha.12

1 year ago

1.1.6-alpha.13

1 year ago

1.1.6-alpha.10

1 year ago

1.1.6-alpha.11

1 year ago

1.1.6-alpha.16

1 year ago

1.1.6-alpha.17

1 year ago

1.1.6-alpha.14

1 year ago

1.1.7-alpha.4

1 year ago

1.1.6-alpha.15

1 year ago

1.1.6-alpha.20

1 year ago

1.1.8-alpha.4

1 year ago

1.1.8-alpha.3

1 year ago

1.1.8-alpha.2

1 year ago

1.1.8-alpha.1

1 year ago

1.1.8-alpha.0

1 year ago

1.1.8

1 year ago

1.1.7

1 year ago

1.1.6

1 year ago

1.1.6-alpha.23n

1 year ago

1.1.6-alpha.24

1 year ago

1.1.6-alpha.21

1 year ago

1.1.6-alpha.22

1 year ago

1.1.6-alpha.25

1 year ago

1.1.6-alpha.26

1 year ago

1.1.6-alpha.0

1 year ago

1.1.6-alpha.1

1 year ago

1.1.6-alpha.2

1 year ago

1.1.6-alpha.3

1 year ago

1.1.6-alpha.4

1 year ago

1.1.6-alpha.5

1 year ago

1.1.6-alpha.6

1 year ago

1.1.6-alpha.7

1 year ago

1.1.6-alpha.8

1 year ago

1.1.6-alpha.9

1 year ago

1.1.5

1 year ago

1.1.4

1 year ago

1.1.3

2 years ago

1.1.2

2 years ago

1.1.4-1

1 year ago

1.1.1

2 years ago

1.1.0

2 years ago

1.0.5

2 years ago

1.0.4

2 years ago

1.0.5-3

2 years ago

1.0.3-17

2 years ago

1.0.5-2

2 years ago

1.0.5-1

2 years ago

1.1.0-2

2 years ago

1.1.0-1

2 years ago

1.0.3-16

2 years ago

1.0.3-15

2 years ago

1.0.3-14

2 years ago

1.0.3-13

2 years ago

1.0.3-12

2 years ago

1.0.3-11

2 years ago

1.0.3-10

2 years ago

1.0.3-9

2 years ago

1.0.3-8

2 years ago

1.0.3-7

2 years ago

1.0.3-6

2 years ago

1.0.3-5

2 years ago

1.0.3-4

2 years ago

1.0.3-3

2 years ago

1.0.3-2

2 years ago

1.0.3-1

2 years ago

1.0.3

2 years ago