Set up a project
In this section, you set up a project to use FT4.
Rell setup
To set up a project using Rell, perform the following steps:
-
Install Chromia CLI and set up PostgreSQL database.
-
Create a project directory by executing the following command:
mkdir ft4-demo && cd ft4-demo
-
Create a Rell project using the
chr
command:chr create-rell-dapp && cd create-rell-dapp
-
Generate an admin key pair that'll be used for registering accounts and assets:
chr keygen --save .chromia/ft4-admin.keypair
Note that we are saving the keypair file inside the .chromia
folder, which is by default ignored by .gitignore
.
If you save the file somewhere else, add *.keypair
to the correct .gitignore
file!
-
Configure the FT4 module arguments by adding the
moduleArgs
andlibs
sections to yourchromia.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
compile:
rellVersion: 0.13.10 # Or your rell version if your is newer
test:
modules:
- test.arithmetic_test
- test.data_test -
Replace the
admin_pubkey
value with the generated admin public key in theft-admin.keypair
file. Additionally, you can configure the rate limiter settings. Refer to the rate limiter topic for more details on the configuration. -
Install the FT4 Rell library
chr install
-
Import the FT4 module into the main app module by adding the import statement
import ^.lib.ft4.ft4_basic_dev.*;
to themain.rell
file. -
Start the node to verify if the configuration file is set up correctly by running the command
chr node start
. You can call a query to check if the FT4 module is imported correctly, such aschr query ft4.get_config
. The output should look like this if you did not configure the rate limiter:
{
rate_limit={
active=1,
max_points=10,
points_at_account_creation=1,
recovery_time=5000
}
}
Client setup with TypeScript
This section discusses how to install and initialize clients.
Install the clients
To install the FT4 client, you can use npm (Node Package Manager) to download and install the required package into your existing npm project. Follow the steps below. To get started with npm, see the official guide.
- Open your terminal or command prompt.
- Run the following command:
npm install @chromia/ft4
This command downloads and installs the @chromia/ft4
and postchain-client
packages from the npm registry.
Initialize the clients
After installing the clients, you can initialize them in your JavaScript code to start interacting with the Postchain network. Here's an example of how to initialize the clients:
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 use all of the FT4 features easily.
To test that everything works, we can add a line that calls a function on the connection object that will trigger a backend call. For example:
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();
We can run the above example with node index.js
; the line will print all the assets registered from the blockchain. If you followed along in this example, the list of assets would be empty as we have yet to register any assets. Since the query is paginated (see the section about paginated entities to learn more), the response will look like this:
{ data: [], nextCursor: null }
In the next section, we will cover how to register assets.