Libraries
import declaration allows to use functions, services and data types defined in another module on the file system. While it's a very simple mechanic, together with NPM flexibility it enables full-blown package management in Aqua.

Available Aqua Libraries

How To Use Aqua Libraries

To use a library, you need to download it by adding the library to dependencies in package.json and then running npm install.
If you're not familiar with NPM, package.json is a project definition file. You can specify dependencies to be downloaded from npmjs.org and define custom commands (scripts), which can be executed from the command line, e.g. npm run compile-aqua.
To create an NPM project, run npm initin a directory of your choice and follow the instructions.
Here's an example of adding aqua-lib to package.json dependencies:
1
{
2
"name": "my-awesome-project",
3
"version": "0.0.1",
4
"dependencies": {
5
"@fluencelabs/aqua-lib": "0.1.10"
6
}
7
}
Copied!
After running npm i, you will have @fluencelabs/aqua-lib in node_modules

In Aqua

After the library is downloaded, you can import it in your .aqua script as documented in Imports And Exports:
1
import "@fluencelabs/aqua-lib/builtin.aqua"
Copied!
Check out corresponding subpages for the API of available libraries.

In TypeScript and JavaScript

To execute Aqua functions, you need to be connected to the Fluence network. The easiest way is to add JS SDK to dependencies:
1
"dependencies": {
2
"@fluencelabs/aqua-dht": "0.2.4",
3
"@fluencelabs/fluence": "0.13.0",
4
"@fluencelabs/fluence-network-environment": "1.0.10"
5
}
Copied!
After executing npm install, the Aqua API is ready to use. Now you need to export aqua-dht functions to TypeScript, that's easy. Create a file export.aqua:
1
import initTopicAndSubscribeBlocking, findSubscribers from "@fluencelabs/aqua-dht/pubsub.aqua"
2
​
3
export initTopicAndSubscribeBlocking, findSubscribers
Copied!
Now, install Aqua compiler and compile your Aqua code to TypeScript:
1
npm install --save-dev @fluencelabs/aqua
2
npx aqua -i . -o src/generated/
Copied!
That's it. Now let's call some functions on the AquaDHT service:
1
import { Fluence } from "@fluencelabs/fluence";
2
import { krasnodar } from "@fluencelabs/fluence-network-environment";
3
import { initTopicAndSubscribeBlocking, findSubscribers } from "./generated/export";
4
​
5
async function main() {
6
// connect to the Fluence network
7
await Fluence.start({ connectTo: krasnodar[1] });
8
let topic = "myTopic";
9
let value = "myValue";
10
let relay = Fluence.getStatus().relayPeerId;
11
// create topic (if not exists) and subscribe on it
12
await initTopicAndSubscribeBlocking(
13
topic, value, relay, null,
14
(s) => console.log(`node ${s} saved the record`)
15
);
16
// find other peers subscribed to that topic
17
let subscribers = await findSubscribers(topic);
18
console.log("found subscribers:", subscribers);
19
}
Copied!
Last modified 3mo ago