Skip to content

Commit

Permalink
Merge pull request #1 from proximax-storage/sdk/initial
Browse files Browse the repository at this point in the history
[SC: 30] Basic Rust SDK
  • Loading branch information
mrLSD authored Sep 13, 2019
2 parents 3f2903f + 2d85ed4 commit 705a3ef
Show file tree
Hide file tree
Showing 16 changed files with 727 additions and 0 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/target
**/*.rs.bk
.idea
*.iml
205 changes: 205 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

35 changes: 35 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
[package]
name = "xpx_supercontracts_sdk"
version = "0.1.0"
authors = ["ProxymaX Core Development Team"]
description = "ProximaX Supercontracts Rust SDK"
edition = "2018"
homepage = "https://www.proximax.io/"
documentation = "https://docs.rs/xpx-supercontracts-sdk"
readme = "README.md"
keywords = ["distributed", "blockchain", "sdk", "supercontract", "xpx", "proximax"]
categories = ["cryptography", "sdk"]
license = "Apache-2.0"
repository = "https://github.com/proximax-storage/rust-xpx-supercontracts-sdk"


[dependencies]
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"
failure = "0.1.5"

[[example]]
name = "ping"
crate-type = ["cdylib"]

[[example]]
name = "debug"
crate-type = ["cdylib"]

[[example]]
name = "http"
crate-type = ["cdylib"]

[[example]]
name = "storage"
crate-type = ["cdylib"]
44 changes: 44 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
# ProximaX Supercontracts Rust SDK

Official ProximaX Supercontracts SDK Library in Rust lang.

## Getting Started
All Supercontracts stuff include in dependency:
```toml
[dependencies]
xpx_supercontracts_sdk = "0.1"
```

To start new development new Supercontrac follow this steps:
1. `cargo new sc-app`

2. Add to `Cargo.toml`:
```toml
[dependencies]
xpx_supercontracts_sdk = "0.1"
```

3. Add to `src/main.rs`:
```rust
extern xpx_supercontracts_sdk;
use xpx_supercontracts_sdk::utils::ping;

#[no_mangle]
pub extern "C" fn app_main() -> i64 {
ping(100).unwrap()
}
```

4. Build: `cargo build --target wasm32-unknown-unknown --release`
5. If build success result contains in: `target`
5. Convert to Wat/Wast format: `wasm2wat sc-app`

## SDK Documentation
See `Docs` directory.

## Exampels
* See `eamples` directory
* Build specific exmaple: `cargo build --target wasm32-unknown-unknown --example ping`

### License: Apache 2.0
Copyright (c) 2019 ProximaX Limited
44 changes: 44 additions & 0 deletions docs/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
# Rust SDK Documentation

## Functions
### utils::ping
* description: Send ping message to `WasmVM`. Successful
result should be incremented value. Useful for most
simple request/response message tests for `WasmVM`.
* params: `msg: usize`
* return: `Result<i64>`

#### Example
```rust
use xpx_supercontracts_sdk::utils::ping;

#[no_mangle]
pub extern "C" fn app_main() -> i64 {
ping(100).unwrap()
}

```

### utils::debug_message
* description: Send debug message to `WasmVM`. It's
convenient basic function for development debugging.
Message that was sent will display in `WasmVM` stdout
as information log message. It not affect basic
Supercontract execution but should be removed
from `release` version, because it will spend `Gas`
(unit ticks).
* params: `msg: &String`
* return: `()`

#### Example
```rust
use xpx_supercontracts_sdk::statuses::STATUS_SUCCESS;
use xpx_supercontracts_sdk::utils::debug_message;

#[no_mangle]
pub extern "C" fn app_main() -> i64 {
let msg = "Debug message".to_string();
debug_message(msg);
STATUS_SUCCESS
}
```
13 changes: 13 additions & 0 deletions examples/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# ProximaX Supercontracts Rust SDK examples

That directory provide basic examples for **How-To** work with SDK.

### Examples list
* `ping`git c
* `http`
* `debug`

## How to use
Run command `cargo build --target wasm32-unknown-unknown --example <example-name>`

Ex: `cargo build --target wasm32-unknown-unknown --example ping`
10 changes: 10 additions & 0 deletions examples/debug.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// Example for `debug_message` WasmVM function.
use xpx_supercontracts_sdk::statuses::STATUS_SUCCESS;
use xpx_supercontracts_sdk::utils::debug_message;

#[no_mangle]
pub extern "C" fn app_main() -> i64 {
let msg = "Debug message".to_string();
debug_message(msg);
STATUS_SUCCESS
}
20 changes: 20 additions & 0 deletions examples/http.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// Example for `http_get` WasmVM function.
use std::collections::HashMap;
use xpx_supercontracts_sdk::http::{http_get, HttpRequest};

#[no_mangle]
pub extern "C" fn app_main() -> i64 {
let mut headers: HashMap<String, String> = HashMap::new();
headers.insert("content-type".to_string(), "text/html".to_string());
let req = HttpRequest {
url: "http://google.com/".to_string(),
headers: headers,
};
let resp = http_get(&req);
if let Err(err) = resp {
// Return error status
return err as i64;
}
// Return response body length
resp.unwrap().len() as i64
}
7 changes: 7 additions & 0 deletions examples/ping.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// Example for `ping` WasmVM function.
use xpx_supercontracts_sdk::utils::ping;

#[no_mangle]
pub extern "C" fn app_main() -> i64 {
ping(100).unwrap()
}
Loading

0 comments on commit 705a3ef

Please sign in to comment.