-
Notifications
You must be signed in to change notification settings - Fork 71
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Update sdk version to 0.4.2, add the
alloc
example (#193)
- Loading branch information
Showing
5 changed files
with
81 additions
and
14 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"] } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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")); | ||
} |