Basic Usage


This guide will walk you through the essential steps to get started with the OCAP Client. You'll learn how to initialize the client, connect to the ArcBlock Beta Chain, and perform a basic query to ensure everything is set up correctly.

Following this guide, you should have a working client instance ready to interact with the blockchain in just a few minutes.

1. Initialize the Client#

First, you need to import the @ocap/client package and create an instance of GraphQLClient. The constructor takes the GraphQL endpoint of the blockchain node you want to connect to. For this guide, we will use the public endpoint for the ArcBlock Beta Chain.

Initialize Client

const GraphQLClient = require('@ocap/client');

// Connect to the Beta Chain
const client = new GraphQLClient('https://beta.abtnetwork.io/api');

console.log('OCAP Client initialized!');

This creates a client instance that is pre-configured with methods for querying the chain, sending transactions, and more.

2. Query the Blockchain#

Now that the client is initialized, you can perform a simple read-only operation to verify the connection. The getChainInfo method is perfect for this, as it fetches basic information about the connected blockchain and doesn't require any authentication.

Query Chain Info

(async () => {
  try {
    const { info } = await client.getChainInfo();
    console.log('Chain Info:', info);
  } catch (error) {
    console.error('Failed to get chain info:', error);
  }
})();

If the request is successful, the info object will be logged to the console, containing details about the chain such as its ID, network, and supported transaction types. This confirms your client is successfully communicating with the blockchain node.

3. Prepare an Account#

To send transactions (write operations), you need a wallet. You can generate a new wallet locally. This wallet represents your on-chain account identity.

Create a Wallet

const { fromRandom } = require('@ocap/wallet');

// Create a new random wallet
const wallet = fromRandom();

console.log('New wallet created:');
console.log('Address:', wallet.address);
console.log('PublicKey:', wallet.publicKey);
console.log('SecretKey:', wallet.secretKey);

Important: A newly generated wallet is just a key pair. It does not exist on the blockchain yet. An account is automatically created on-chain when its address receives its first incoming transaction.

To make your new account active, you need to fund it with some test tokens. You can get free test tokens (TBA) for the Beta Chain from the official faucet:

4. Query Account State#

Once you have funded your new account using the faucet, you can query its state to verify the balance.

Query Account State

(async () => {
  try {
    // Replace with the address you funded from the faucet
    const address = wallet.address;

    const { state } = await client.getAccountState({ address });
    console.log('Account State:', state);
  } catch (error) {
    console.error('Failed to get account state:', error);
  }
})();

The state object in the response will contain your account's address, balance, nonce (number of transactions sent), and other details. Seeing a non-zero balance confirms that your account has received the tokens from the faucet and is now active on the blockchain.

Next Steps#

You have successfully set up the OCAP Client, connected to the blockchain, and verified your account. You are now ready to explore more advanced operations.

Check out the How-to Guides to learn how to perform common tasks like transferring tokens, managing assets, and more.