Plugins
Experimental
Plugins are an easy way to use local services written in JS in aqua cli
. Let's have a look at how it works with an Aqua code example:
aqua
-- struct that we want to return from a servicedata Result:left: u32right: u32-- service that returns 'Result' dataservice ResultGetter("getter"):get() -> Result-- service that can sum the resultservice Adder("adder"):sum(r: Result) -> u64-- a function that bundles the execution of servicesfunc getAndSum() -> u64:result <- ResultGetter.get()sum <- Adder.sum(result)<- sum
aqua
-- struct that we want to return from a servicedata Result:left: u32right: u32-- service that returns 'Result' dataservice ResultGetter("getter"):get() -> Result-- service that can sum the resultservice Adder("adder"):sum(r: Result) -> u64-- a function that bundles the execution of servicesfunc getAndSum() -> u64:result <- ResultGetter.get()sum <- Adder.sum(result)<- sum
We want to execute and check if the Aqua code works before we start to implement the service in Rust and deploy it to Fluence nodes. We can implement these services on JS. Files must be with .mjs
extension and with this service scheme:
javascript
// exported function name must be `plugins` without argumentsexport function plugins() {return {// not service name!service_id: {// can be asyncfunction_name: () => {// js code here}}}}
javascript
// exported function name must be `plugins` without argumentsexport function plugins() {return {// not service name!service_id: {// can be asyncfunction_name: () => {// js code here}}}}
Create two files for our example.
getter.mjs
:
javascript
export function plugins() {return {getter: {get: () => {return {left: 12,right: 32}}}}}
javascript
export function plugins() {return {getter: {get: () => {return {left: 12,right: 32}}}}}
adder.mjs
:
javascript
export function plugins() {return {adder: {sum: (result) => {return result.left + result.right}}}}
javascript
export function plugins() {return {adder: {sum: (result) => {return result.left + result.right}}}}
Run Aqua code with Aqua CLI:
sh
aqua run -f 'getAndSum()' -i path/to/aqua --addr node/addr --plugin adder.mjs --plugin getter.mjs44
sh
aqua run -f 'getAndSum()' -i path/to/aqua --addr node/addr --plugin adder.mjs --plugin getter.mjs44
Also, it is possible to move all plugin files to one directory and target this directory --plugin path/to/dir