Docs
chromia.com

Hello World Quickstart

On this page

This quickstart guide shows you how to send transactions to a blockchain node running Rell and retrieve information from it using the postchain-client library. The postchain-client allows you to connect the frontend of your dapp to the backend.

This tutorial assumes you have a Rell backend running. If you don't have one yet, see Build and run the Hello World dapp with Chromia CLI to create the backend first. You can connect the frontend at a later stage.

Prerequisites

Create the client project

Create a separate TypeScript project and initialize it.

Terminal
mkdir my-rell-client && cd my-rell-client
npm init -y
npm install postchain-client --save
npm install -D typescript @types/node tsx
npx tsc --init
mkdir src

Client setup

The code establishes the client connections, creates, signs, and posts a transaction to the blockchain node, and retrieves information through a query. For detailed explanation of the code setup, see JavaScript/TypeScript client reference.

Create my-rell-client/src/index.ts and paste in the code below, replacing <BlockchainRID> with your Blockchain RID obtained from the node output. For information on finding Blockchain RID, see Build and run the Hello World dapp with Chromia CLI.

TypeScript
import { createClient } from "postchain-client";

async function simpleClient() {
  // Key pair (DEV-ONLY, replace for anything beyond localhost)
  const adminPubkey = Buffer.from("031b84c5567b126440995d3ed5aaba0565d71e1834604819ff9c17f5e9d5dd078f", "hex");
  const adminPrivkey = Buffer.from("0101010101010101010101010101010101010101010101010101010101010101", "hex");

  // Connection setup
  const nodeUrl = "http://localhost:7740/"; // Using default postchain node REST API port
  const blockchainRid = "<BlockchainRID>"; // Dapp Blockchain RID
  const client = await createClient({
    nodeUrlPool: [nodeUrl],
    blockchainRid,
  });

  // Transaction
  /*const { status, statusCode, transactionRid } = */ await client.signAndSendUniqueTransaction(
    {
      operations: [
        {
          name: "set_name",
          args: ["Developer"],
        },
      ],
      signers: [adminPubkey],
    },
    { privKey: adminPrivkey, pubKey: adminPubkey }
  );

  // Query
  const result = await client.query("hello_world");
  console.log(result); // Hello Developer!
}

simpleClient().catch((e) => {
  console.error(e);
});

Understanding the Rell backend

The client code works with the operation and query specified in the Rell file main.rell. If you created your backend using the Hello World guide, the code in the main.rell file is as follows:

Rell
module;

object my_name {
  mutable name = "World";
}

operation set_name(name) {
  my_name.name = name;
}

query hello_world() = "Hello %s!".format(my_name.name);

The project structure for the client setup should be as follows:

Files
my-rell-client
    |--node_modules
    |--src
    |   |--index.ts
    |--package-lock.json
    |--package.json
    |--tsconfig.json

Run client code

From the project root:

Terminal
npx tsx src/index.ts

After running the code, you should see:

Output
Hello Developer!

Congratulations! With the help of the postchain-client library, you have successfully sent transactions and retrieved information from a blockchain node running Rell.

Next steps