Set up your FT4 project
In this section, 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.
-
Install Chromia CLI and set up PostgreSQL database.
-
Create a new directory for your project and navigate into it:
Terminalmkdir ft4-demo && cd ft4-demo -
Use the Chromia CLI to create a Rell project:
Terminalchr create-rell-dapp && cd my-rell-dapp -
Generate an admin key pair that will be used for registering accounts and assets:
Terminalchr keygen --file .chromia/ft4-admin.keypair -
Configure the FT4 module arguments by adding the
moduleArgsandlibssections to yourchromia.ymlfile.
blockchains:
hello:
module: main
moduleArgs:
lib.ft4.core.admin:
admin_pubkey: 03028A31DBA82E46DE26A608249147A6A1A88C62A1A65B640C9B4369D4CAD928BE
libs:
ft4:
registry: https://gitlab.com/chromaway/ft4-lib.git
path: rell/src/lib/ft4
tagOrBranch: v1.2.0r
rid: x"E96915FD7C9A5DD4E183D86F4A5B4BB5083A7CC1EDEB1465EDFFC926E67C93F6"
insecure: false
iccf:
registry: https://gitlab.com/chromaway/core/directory-chain
path: src/lib/iccf
tagOrBranch: 1.87.0
rid: x"9C359787B75927733034EA1CEE74EEC8829D2907E4FC94790B5E9ABE4396575D"
insecure: false
compile:
rellVersion: 0.16.7 # or newer: the Rell version your chr ships with
test:
moduleArgs:
lib.ft4.core.admin:
admin_pubkey: 03028A31DBA82E46DE26A608249147A6A1A88C62A1A65B640C9B4369D4CAD928BE
modules:
- test.arithmetic_test
- test.data_test-
Replace the
admin_pubkeyvalue with the generated admin public key from theft4-admin.keypairfile, in both places it appears (the blockchain and thetestsection). You can also configure the rate limiter settings as needed. Refer to the rate limiter topic for more details on the configuration. -
Install the FT4 Rell library to your project:
Terminalchr install -
Import some of the FT4 functionality into the main app module by adding these import statements to the
main.rellfile:Rellimport lib.ft4.accounts; import lib.ft4.assets; import lib.ft4.admin; -
Start the node to verify the configuration:
Terminalchr node startRun a query to check if the FT4 module is imported correctly:
Terminalchr query ft4.get_configThe output should look similar to:
Output[ "auth_descriptor": [ "max_number_per_account": 10, "max_rules": 8 ], "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.1.0 postchain-client@^2.1.02. 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.