K3 Wasm Rust SDK
This document is for developers new to the framework who would like to make applications using our Rust SDK.
Getting started
The recommended way to start is by forking one of the many Rust examples we have on our GitHub (the simplest one is https://github.com/k3-labs/k3-template-hello-world-rust) and then deploy that on our platform through your GitHub login. If you have a Rust project already or want to learn how to set up a K3 project yourself here are the steps:
Add K3 dependencies:
(optional) switch to nightly and add the nightly feature for the
k3-wasm-macros
dependencySwitching to nightly gives you access to file-based routing for the macros (i.e. you can have a
get
function exported in/api/users/mod.rs
and it would work as an HTTP GET handler for/api/users
. Without file routing you have to specify the HTTP route you want to catch in the macro (this will be shown later)Add the init macro call to your
main.rs
file:Annotate any handlers you want to export with the
http_handler
decorator:Or without file-based routing:
Writing handlers
Handlers are similar to serverless functions if you’ve ever implemented those in Node.js or Go. Their only parameter is an HTTP request object and it expects you to return an HTTP response object (we re-export the types from the popular Rust http
crate for both). For example:
You can have any logic you like there but you must remember that this will be compiled to WebAssembly which is a very sandboxed environment and doesn’t have direct access to the file-system or the internet for example.
To access the outside world you can use functions from our SDK and for dependencies you will have to check in their documentation if they support the WebAssembly environment (libraries that work in no_std
will usually directly support WebAssembly as well).
Using the SDK
The K3 execution environment gives you many tools to access from the outside world (some of which you might have to enable in the dashboard for your team). We’ll go through all of them with a reference for each export and then examples on how to use them.
HTTP
This is currently the most minimal SDK module and will likely have more functions added soon.
fn get(url: &str) -> Option<Vec<u8>>
fn get(url: &str) -> Option<Vec<u8>>
Makes an HTTP GET request to the provided URL and returns the response body as a buffer.
fn get_with_auth(url: &str, auth: &str) -> Option<Vec<u8>>
Makes an HTTP GET request with AUTHORIZATION header to the provided URL and returns the response body as a buffer.
IPFS
The IPFS module allows you to store and read files from IPFS.
fn upload(contents: Vec<u8>) -> String
fn upload(contents: Vec<u8>) -> String
Uploads some data to IPFS and returns a content ID that can be used to fetch it later.
fn read(cid: &str) -> Vec<u8>
fn read(cid: &str) -> Vec<u8>
Reads a file from IPFS given its content ID.
Key Value Store
The kv
module gives you access to a fast, in-memory, string-based key value store (that unreliably commits to IPFS). The module only exports the Db
struct, the methods in which give you access to the functionality.
fn Db::open_default() -> Db
fn Db::open_default() -> Db
Opens the default store for this module.
fn Db::get(&self, key: &str) -> Option<String>
fn Db::get(&self, key: &str) -> Option<String>
Gets and returns a value from this store.
fn Db::set(&mut self, key: &str, value: String)
fn Db::set(&mut self, key: &str, value: String)
Inserts or modifies a value (depending on whether it exists or not) in this store.
fn Db::delete(&mut self, key: &str)
fn Db::delete(&mut self, key: &str)
Deletes the value for a given key in this store.
Smart contracts
The sc
module let’s you call Ethereum smart contracts and query data from them. Functions in this module expect you to provide the smart contracts address and also its ABI as a JSON string.
fn call(address: &str, abi: &str, fn_name: &str, params: impl Tokenize) -> String
fn call(address: &str, abi: &str, fn_name: &str, params: impl Tokenize) -> String
Calls a function in the smart contract with the provided parameters and returns the transaction hash as a hex string. Panics if params are in an invalid format for the function.
fn query<R: Detokenize>(address: &str, abi: &str, fn_name: &str, params: impl Tokenize) -> R
fn query<R: Detokenize>(address: &str, abi: &str, fn_name: &str, params: impl Tokenize) -> R
Queries a function in the smart contract and returns the data. Panics if params or expected response type are invalid for the function.
Space & Time
The sxt
module gives you access to your team’s Space & Time databases letting you run SQL on existing tables and make new ones.
fn execute_sql(sql: &str) -> String
fn execute_sql(sql: &str) -> String
Executes some provided SQL on your databases. Returns a JSON string with either info about how many rows were updated or the queried data. To learn more about the JSON format check out Space & Time’s HTTP API reference.
fn execute_create_table(name: &str, types: &str) -> String
fn execute_create_table(name: &str, types: &str) -> String
Creates a table in your selected database. This function exists because CREATE
table instructions are special in Space & Time and require a biscuit generated using a unique public/private key pair. This function will handle that logic for you, but you can also manually do that using their API as reference and directly call execute_sql
instead.
Last updated