Giter Club home page Giter Club logo

zig-soroban-sdk's Introduction

zig-soroban-sdk

Zig SDK for writing Soroban contracts.

Why a Zig SDK for Soroban?

Introducing a Zig SDK offers distinct advantages: zig provides an exceptional level of control over their code (for example carefully choosing performance or safety for every operation depending on the needs). Moreover, Zig's first-class WebAssembly compilation compatibility aligns perfectly with the Soroban virtual machine. Lastly, this SDK expands the language options for Soroban developers contributing to a more adaptable ecosystem.

Progress

Ultimately I aim to make the sdk complete (all conversions, support for host functions) and make it ergonomic. This is the current progress:

  • conversions

    • bool
    • void
    • u32, i32
    • u64Small, i64small
    • TimepointSmall
    • DurationSmall
    • u128small, i128small
    • u256, i256 (TODO: switch to env functions for operations).
    • small symbols
  • host functions

    • none

Also, there is no spec generation for the contracts currently (although it's a P1 feature), so you'll either have to first write the contract's interface in rust and then manually inject WASM bytecode from the rust contract's wasm custom section to Zig's wasm, or you can use the RPC (or js soroban client) instead of the CLI and only inject the contract env meta (so you don't have to write the rust contract).

About The Types

Given rust's ability to generate conversions thorugh macros, contracts built with the rust sdk can directly interact with the host using the already Val-decoded types: decoding and encoding from and to Val is already taken care of.

Zig promotes a clarity and predictability approach to metaprogramming: comptime. This means that it might be possible to have a similar style to the rust sdk, but I haven't explored a solution yet also since it seems that comptime parameters are not allowed on exported wasm fns due to C-ABI compatibility.

This means that I'm currently writing the SDK around wrapper types that implement Val conversions. For example, a multiplier contract currently looks like:

const soroban_sdk = @import("soroban_sdk");
const Val = soroban_sdk.val.Val;
const U32 = soroban_sdk.val.U32;

export fn multiply(a_: Val, b_: Val, c_: Val, d_: Val) Val {
    const a = U32.from_val(a_) catch unreachable;
    const b = U32.from_val(b_) catch unreachable;
    const c = U32.from_val(c_) catch unreachable;
    const d = U32.from_val(d_) catch unreachable;

    return a.mul(b).mul(c).mul(d).to_val();
}

zig-soroban-sdk's People

Contributors

heytdep avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.