Skip to main content

Set up your project with Chromia FT4

In this topic, you'll learn how to set up a project to use FT4. If you want to see an example of a complete dapp using the FT4 protocol, you can explore the FT4 demo app.

  1. Install Chromia CLI and set up PostgreSQL database.

  2. Create a new directory for your project and navigate into it:

    mkdir ft4-demo && cd ft4-demo
  3. Use the Chromia CLI to create a Rell project:

    chr create-rell-dapp && cd my-rell-dapp
  4. Generate an admin key pair that will be used for registering accounts and assets:

    chr keygen --file .chromia/ft4-admin.keypair
    caution

    Ensure the keypair file is saved inside the .chromia folder, which is ignored by .gitignore. If saved elsewhere, add *.keypair to the appropriate .gitignore file.

  5. Configure the FT4 module arguments by adding the moduleArgs and libs sections to your chromia.yml file.

    blockchains:
    hello:
    module: main
    moduleArgs:
    lib.ft4.core.admin:
    admin_pubkey: 03028A31DBA82E46DE26A608249147A6A1A88C62A1A65B640C9B4369D4CAD928BE

    libs:
    ft4:
    registry: https://bitbucket.org/chromawallet/ft3-lib
    path: rell/src/lib/ft4
    tagOrBranch: v0.7.0r
    rid: x"F7C207AA595ABD25FDE5C2C2E32ECD3768B480AD03D1F2341548FF4F37D9B7AF"
    insecure: false
    iccf:
    registry: https://gitlab.com/chromaway/core/directory-chain
    path: src/iccf
    tagOrBranch: 1.32.2
    rid: x"1D567580C717B91D2F188A4D786DB1D41501086B155A68303661D25364314A4D"

    compile:
    rellVersion: 0.13.10 # Or your rell version if your is newer

    test:
    modules:
    - test.arithmetic_test
    - test.data_test
    note

    The ICCF dependency is mandatory to use cross-chain transfers in your dapp, but we suggest installing it even if you're not planning to use it. It won't be compiled in the production code, and your IDE might complain if you don't have it.

    The admin_pubkey definition is only required if you're using the admin module.

    For more information, see documentation for imports.

  6. Replace the admin_pubkey value with the generated admin public key from the ft-admin.keypair file. You can also configure the rate limiter settings as needed. Refer to the rate limiter topic for more details on the configuration.

  7. Install the FT4 Rell library to your project:

    chr install
  8. Import some of the FT4 functionality into the main app module by adding these import statements to the main.rell file:

    import lib.ft4.accounts;
    import lib.ft4.assets;
    import lib.ft4.admin;
    note

    This method of importing is the easiest to set up. For more advanced import options, refer to the documentation.

  9. Start the node to verify the configuration:

    chr node start

    Run a query to check if the FT4 module is imported correctly:

    chr query ft4.get_config

    The output should look similar to:

    {
    rate_limit={
    active=1,
    max_points=10,
    points_at_account_creation=1,
    recovery_time=5000
    }
    }

FT4 client setup with TypeScript

This section discusses how to install and initialize the FT4 client.

1. Install the Client

Use npm to install the FT4 client:

npm install @chromia/ft4

2. Initialize the client

In your JavaScript code, initialize the FT4 client:

const { createClient } = require("postchain-client");
const { createConnection } = require("@chromia/ft4");

const url = "http://localhost:7740";
const client = await createClient({
nodeUrlPool: url,
blockchainIid: 0,
});

const connection = createConnection(client);

In the code snippet above, we import the necessary modules from the postchain-client and @chromia/ft4 packages. Next, we define the url of the Postchain network we want to connect to (in this case, "http://localhost:7740"). We then create an instance of the IClient using the specified URL and the internal ID of the chain, which by default is 0 on a local network.

With the client initialized, you can now utilize its capabilities to interact with the Postchain network and perform various operations, such as sending transactions and querying the blockchain.

We then pass the client to the createConnection method from FT4, allowing us to easily use all of the FT4 features.

3. Test the connection

To test the connection, add a function to call a method on the connection object:

const { createClient } = require("postchain-client");
const { createConnection } = require("@chromia/ft4");

async function main() {
const url = "http://localhost:7740";
const client = await createClient({
nodeUrlPool: url,
blockchainIid: 0,
});

const connection = createConnection(client);
console.log(await connection.getAllAssets()); // This line is new
}

main();

Run the above example with node index.js. This will print all the assets registered on the blockchain. If no assets are registered, the response will look like this:

{ data: [], nextCursor: null }

Registering assets

Once you complete your project setup and initialize your clients, proceed to register assets and perform various blockchain operations using the FT4 protocol.

For detailed instructions on registering assets and other operations, refer to the Register assets topic.