Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
338 changes: 337 additions & 1 deletion Cargo.lock

Large diffs are not rendered by default.

26 changes: 25 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ starknet-crypto = "0.8"
starknet-types-core = { version = "0.2.0", features = ["arbitrary"] }

parking_lot = "0.12.1"
tokio = { version = "1.39.2", default-features = false, features = ["rt"] }
tokio = { version = "1.39.2", default-features = false, features = ["rt", "rt-multi-thread", "macros"] }
url = "2.5.0"
anyhow = "1.0.89"
serde = { version = "1.0.193", features = ["derive"] }
Expand Down Expand Up @@ -49,6 +49,13 @@ gloo-utils = { version = "0.2.0", features = ["serde"] }
num-bigint = "0.4.6"
num-traits = "0.2.19"
chrono = "0.4.41"
uniffi = { version = "0.30", features = ["bindgen", "cli"] }
uniffi_bindgen = "0.30"
camino = "1.1"
thiserror = "2"
hex = "0.4"
cargo_metadata = "0.19"
futures-util = "0.3"

[target.'cfg(not(target_arch = "wasm32"))'.dependencies]
axum = "0.8.1"
Expand All @@ -63,17 +70,34 @@ directories = "6.0.0"
tower-http = { version = "0.6.2", features = ["cors"] }
base64 = "0.22.1"

# UniFFI bindgen binaries using uniffi_bindgen library
[[bin]]
name = "uniffi-bindgen-swift"
path = "src/bin/uniffi-bindgen-swift.rs"

[[bin]]
name = "uniffi-bindgen-kotlin"
path = "src/bin/uniffi-bindgen-kotlin.rs"

[[bin]]
name = "uniffi-bindgen-python"
path = "src/bin/uniffi-bindgen-python.rs"

[build-dependencies]
# this addresses issue with cyclical dependencies when generating C header
# see. https://github.com/mozilla/cbindgen/issues/43
# cbindgen = { git = "https://github.com/flaviojs/cbindgen_tmp", branch = "resolve-order-dependencies-clike" }
# cbindgen = "0.29"
cbindgen = { git = "https://github.com/Larkooo/cbindgen", branch = "no-c-forward-enum" }
uniffi = { version = "0.30", features = ["build"] }


[dev-dependencies]
wasm-bindgen-test = "0.3.33"
uniffi = { version = "0.30", features = ["bindgen-tests"] }

[features]
default = []

[patch.crates-io]
crunchy = { git = "https://github.com/nmathewson/crunchy", branch = "cross-compilation-fix" }
Expand Down
94 changes: 88 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,36 @@
# dojo.c

This package provides C and low-level Wasm32 bindings for the Torii Client SDK, as well as for the starknet-rs library.
Multi-language bindings for the Dojo Torii Client SDK, providing seamless integration across multiple platforms and languages.

The approach is to generate a C client using `cbindgen` and a wasm module using `wasm-bindgen` that are interropeable in applications exporting to both native platforms and web browsers.
## Features

This package provides multiple binding strategies for maximum compatibility:

- **C/C++ Bindings** - Generated with `cbindgen` for native applications
- **WebAssembly** - Browser and Node.js support via `wasm-bindgen`
- **UniFFI Bindings** - Modern Swift, Kotlin, and Python bindings via Mozilla's UniFFI

## Project Structure

```
dojo.c/
├── src/
│ ├── c/ # C FFI implementation
│ ├── wasm/ # WebAssembly implementation
│ ├── uniffi/ # UniFFI implementation
│ │ ├── types/ # Domain types (organized by category)
│ │ ├── client.rs # ToriiClient with queries & subscriptions
│ │ └── README.md # UniFFI-specific documentation
│ ├── dojo.udl # UniFFI interface definition
│ └── lib.rs # Main library entry point
├── src/bin/ # Binding generator binaries
├── scripts/ # Build and generation scripts
├── bindings/ # Generated bindings output
│ ├── swift/
│ ├── kotlin/
│ └── python/
└── example/ # C usage examples
```

## Building

Expand All @@ -27,13 +55,67 @@ cargo build --release --target wasm32-unknown-unknown
cd pkg && bunx wasm-pack build --release
```

## Running
## Generating Bindings

### C/C++ Headers

Headers are automatically generated during `cargo build`:
- `dojo.h` - C header
- `dojo.hpp` - C++ header
- `dojo.pyx` - Cython definitions

### UniFFI Bindings (Swift, Kotlin, Python)

```bash
# Build the library first
cargo build --release

# Generate Swift bindings (iOS/macOS)
cargo run --bin uniffi-bindgen-swift --release -- \
target/release/libdojo_c.dylib bindings/swift --swift-sources

# Generate Kotlin bindings (Android)
cargo run --bin uniffi-bindgen-kotlin --release -- \
target/release/libdojo_c.dylib bindings/kotlin

# Generate Python bindings
cargo run --bin uniffi-bindgen-python --release -- \
target/release/libdojo_c.dylib bindings/python
```

See [`src/uniffi/README.md`](src/uniffi/README.md) for detailed UniFFI documentation.

## Running Examples

### C Example

```bash
# Building dojo.c
# Build dojo.c
cargo build --release
# Linking dojo.c and building example
# Compile and link C example
clang example/main.c target/release/libdojo_c.dylib
# Running example
# Run example
./a.out
```

### Python Example

```python
from dojo import ToriiClient

# Create client
client = await ToriiClient("http://localhost:8080")

# Subscribe to entity updates
def on_entity_update(entity):
print(f"Entity updated: {entity}")

def on_error(error):
print(f"Error: {error}")

sub_id = await client.subscribe_entity_updates(
None, # clause
[], # world_addresses
EntityUpdateCallback(on_entity_update, on_error)
)
```
Binary file added bindings/python/__pycache__/dojo.cpython-313.pyc
Binary file not shown.
Loading
Loading