Installation and Setup

Installing the RACE JS SDK

You can install the RACE JS SDK using either npm or yarn:

Using npm:

npm install @race-foundation/sdk-core

Using yarn:

yarn add @race-foundation/sdk-core

This will install the core SDK package, which provides the essential functionalities for interacting with Race Protocol games. Depending on the blockchain network you are targeting, you may also need to install the corresponding transport implementation package (e.g., @race-foundation/sdk-solana for Solana).

Importing and Initializing the SDK

Once installed, you can import the necessary classes and functions from the SDK into your client application:

// TypeScript code
import { AppClient } from '@race-foundation/sdk-core';

To initialize the SDK, you need to create an AppClient instance. This requires providing the following information:

  • gameAddr: The address of the Race game you want to connect to.

  • transport: An instance of the appropriate transport implementation (e.g., SolanaTransport).

  • wallet: An instance of a wallet adapter that will be used to sign transactions (e.g., PhantomWalletAdapter).

  • onEvent: A callback function that will be invoked whenever a game event occurs. This function receives the game context snapshot, the current handler state, and the event object as arguments.

Here's an example of initializing the SDK with a Solana transport and a Phantom wallet:

// TypeScript code
import { SolanaTransport } from '@race-foundation/sdk-solana';
import { PhantomWalletAdapter } from '@race-foundation/sdk-solana';

const gameAddr = '...'; // Replace with your game address

const transport = new SolanaTransport('https://api.devnet.solana.com'); // Replace with your Solana endpoint
const wallet = new PhantomWalletAdapter({ skipPreflight: true }); // Configure wallet options

const appClient = await AppClient.initialize({
  gameAddr,
  transport,
  wallet,
  onEvent: (context, state, event) => {
    // Handle game events here
  },
});

Configuration Options

When initializing the RACE JS SDK, you may need to provide additional configuration options depending on your specific setup and requirements. Some common options include:

  • chain: This specifies the blockchain network you are targeting (e.g., "solana", "facade"). This option is typically used by the transport implementation to determine how to interact with the blockchain.

  • endpoint: This defines the endpoint URL for the Transactor server. This is necessary for the SDK to establish a connection and communicate with the game server.

  • storage: Optionally, you can provide an instance of a storage adapter (e.g., local storage) that will be used by the SDK to cache data like game bundles and player profiles.

Refer to the API reference and documentation for the specific transport implementation you are using for details on additional configuration options that may be available.

By following these steps and providing the necessary configuration options, you can successfully install and set up the RACE JS SDK in your client application, enabling you to interact with Race Protocol games and leverage its functionalities.

Last updated