Skip to content

Commit

Permalink
Update sdk version to 0.4.2, add the alloc example (#193)
Browse files Browse the repository at this point in the history
  • Loading branch information
jayz22 authored Jan 7, 2023
1 parent 42474bc commit 76b55c9
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 14 deletions.
27 changes: 17 additions & 10 deletions Cargo.lock

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

9 changes: 5 additions & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ members = [
"logging",
"errors",
"wallet",
"alloc",
]

[profile.release-with-logs]
Expand All @@ -39,11 +40,11 @@ codegen-units = 1
lto = true

[workspace.dependencies.soroban-sdk]
version = "0.4.1"
version = "0.4.2"
git = "https://github.com/stellar/rs-soroban-sdk"
rev = "980778e"
rev = "135c3c8"

[workspace.dependencies.soroban-auth]
version = "0.4.1"
version = "0.4.2"
git = "https://github.com/stellar/rs-soroban-sdk"
rev = "980778e"
rev = "135c3c8"
17 changes: 17 additions & 0 deletions alloc/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
[package]
name = "soroban-alloc-contract"
version = "0.0.0"
authors = ["Stellar Development Foundation <[email protected]>"]
license = "Apache-2.0"
edition = "2021"
publish = false

[lib]
crate-type = ["cdylib"]
doctest = false

[dependencies]
soroban-sdk = { workspace = true, features = ["alloc"] }

[dev_dependencies]
soroban-sdk = { workspace = true, features = ["testutils", "alloc"] }
24 changes: 24 additions & 0 deletions alloc/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#![no_std]
use soroban_sdk::{contractimpl, Env};

extern crate alloc;

pub struct AllocContract;

#[contractimpl]
impl AllocContract {
/// Allocates a temporary vector holding values (0..count), then computes and returns their sum.
pub fn sum(_env: Env, count: u32) -> u32 {
let mut v1 = alloc::vec![];
(0..count).for_each(|i| v1.push(i));

let mut sum = 0;
for i in v1 {
sum += i;
}

sum
}
}

mod test;
18 changes: 18 additions & 0 deletions alloc/src/test.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#![cfg(test)]

use super::{AllocContract, AllocContractClient};
use soroban_sdk::{testutils::Logger, Env};

extern crate std;

#[test]
fn test() {
let env = Env::default();
let contract_id = env.register_contract(None, AllocContract);
let client = AllocContractClient::new(&env, &contract_id);
assert_eq!(client.sum(&1), 0);
assert_eq!(client.sum(&2), 1);
assert_eq!(client.sum(&5), 10);

std::println!("{}", env.logger().all().join("\n"));
}

0 comments on commit 76b55c9

Please sign in to comment.