Defining APIs
Your application deployed on Klave is a collections of query
and transaction
functions. Klave SDK provides a simple way to declare query and transaction functions.
Transaction Function
Transaction on Klave are strongly consistent, deterministic requests that modify app state and execute across all TEE cluster nodes, but their deterministic requirements limit which SDK features can be used within transaction scope compared to queries.
Check for warning signs in the Klave SDK documentation to see if a feature is compatible with transaction contexts.
Declaring a Transaction Function
The transaction template function in Klave can take up to one parameters and the return type is always void
.
To declare a transaction function in your app with the SDK you just have to decorate the method signature as follow:
/**
* @transaction
*/
export function myTransaction(): void {
//develop your transactional logic here
}
/**
* @transaction
*/
export function myTransactionWithInput(input: InputType): void {
//develop your transactional logic here
}
In Rust, The transaction template function in Klave take exactly one string
argument and the return type is always void
.
App in Rust are built as a wasm component. Therefore transaction function have to be declared within the Guest implementation of the Component.
In addition, as opposed to AssemblyScript that can use annotations to register transaction, for Rust an extra step of registering is needed by calling add_user_transaction
within the register_routes
function.
mod bindings;
use bindings::Guest;
use klave;
struct Component;
impl Guest for Component {
// Function to declare to register Transaction and Query
fn register_routes(){
//When registering routes by convention it is recommended to use the `wit` function name.
//Make sure, when calling your query that you use the name you used to register it.
//In that case you can register with either `my-transaction` (the `wit` name) or `my_transaction`.
klave::router::add_user_transaction("my-transaction");
}
fn my_transaction(cmd: String){
//develop your transactional logic here
}
}
Rust application are built as a wasm component and need to expose a wit
interface to export the registered transaction
.
package component:klave-rust-app-example;
/// An example world for the component to target.
world klave-rust-app-example {
//The exported route name need to use `-` instead of `_` by convention in the `.wit` file
export register-routes: func();
export my-transaction: func(cmd: string);
}
Query Function
Query on Klave are non-state-modifying, eventually consistent requests that can be non-deterministic and run on a single TEE node, making them less expensive and more scalable than transactions.
Declaring a Query Function
In a similar fashion the query template function in Klave can take up to one parameters and the return type is always void
.
To declare a query function in your app with the SDK you just have to decorate the method signature as follow:
/**
* @query
*/
export function myQuery(): void {
//develop your query logic here
}
/**
* @query
*/
export function myQueryWithInput(input: InputType): void {
//develop your query logic here
}
In a similar fashion the query template function in Klave take one string
parameter and the return type is always void
.
App in Rust are built as a wasm component. Therefore query function have to be declared within the Guest implementation of the Component.
In addition, as opposed to AssemblyScript that can use annotations to register query, for Rust an extra step of registering is needed by calling add_user_query
within the register_routes
function.
mod bindings;
use bindings::Guest;
use klave;
struct Component;
impl Guest for Component {
// Function to declare to register Transaction and Query
fn register_routes(){
//When registering routes by convention it is recommended to use the `wit` function name.
//Make sure, when calling your query that you use the name you used to register it.
//In that case you can register with either `my-query` (the `wit` name) or `my_query`.
klave::router::add_user_query("my-query");
}
fn my_query(cmd: String){
//develop your query logic here
}
fn my_transaction(cmd: String){
//develop your query logic here
}
}
Rust application are built as a wasm component and need to expose a wit
interface to export the registered query
.
package component:klave-rust-app-example;
/// An example world for the component to target.
world klave-rust-app-example {
//The exported route name need to use `-` instead of `_` by convention in the `.wit` file
export register-routes: func();
export my-query: func(cmd: string);
}
Input Parameters Management
if your unique input parameter is not a native type then you have to declare and decorate it accordingly.
@serializable
export class InputType {
success!: boolean;
message!: string;
}
The serialisation and deserialisation of the input are automatically managed by the Klave SDK.
In Rust, user defined function declared can only accept a single string
parameters.
You can pass json
formatted string
and then use json
library such as serde_json
to deserialize within your function.
Last updated on