Skip to content

Commit

Permalink
Merge pull request #32 from blockful-io/feat#14_scorer_factory_struct…
Browse files Browse the repository at this point in the history
…ures_and_constructor

feat: add scorer_contract structs and initialize funtion
  • Loading branch information
LeonardoVieira1630 authored Dec 16, 2024
2 parents cd4d7a4 + 631ef50 commit 7328ad5
Show file tree
Hide file tree
Showing 16 changed files with 282 additions and 55 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/rust.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,4 @@ jobs:
key: ${{ runner.os }}-cargo-${{ hashFiles('**/Cargo.lock') }}

- name: Run tests
run: cargo test
run: cargo test --workspace
23 changes: 23 additions & 0 deletions Cargo.lock

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

28 changes: 24 additions & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,28 @@ name = "trustful-stellar-v1"
version = "0.1.0"
edition = "2021"

[workspace]
members = [
"contracts/scorer",
"contracts/deployer",
"contracts/scorer_factory"
]

[workspace.package]
version = "0.1.0"

[workspace.dependencies]
soroban-sdk = "21.7.7"

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

[dependencies]
soroban-sdk = "21.7.7"
soroban-sdk = { workspace = true }

[dev_dependencies]
soroban-sdk = { version = "21.7.7", features = ["testutils"] }
[dev-dependencies]
soroban-sdk = { workspace = true, features = ["testutils"] }

[features]
testutils = ["soroban-sdk/testutils"]
Expand All @@ -37,4 +50,11 @@ build-script = true
[package.metadata.package]
include = [
"wasm/**/*",
]
]

[package.metadata.scripts]
build-wasm = """
cargo build --target wasm32-unknown-unknown --release -p scorer
cargo build --target wasm32-unknown-unknown --release -p deployer
cargo build --target wasm32-unknown-unknown --release -p scorer_factory
"""
29 changes: 22 additions & 7 deletions build.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,33 @@
use std::fs;
use std::path::Path;

fn copy_wasm(contract_name: &str) {
let source = format!(
"target/wasm32-unknown-unknown/release/{}.wasm",
contract_name
);
let dest = format!("wasm/{}.wasm", contract_name);

if Path::new(&source).exists() {
fs::copy(&source, &dest).unwrap();
}
}

fn main() {
println!("cargo:rerun-if-changed=src/");
println!("cargo:rerun-if-changed=contracts/");

// Cria a pasta wasm se não existir
fs::create_dir_all("wasm").unwrap();

// O build.rs roda antes da compilação, então precisamos garantir que
// só vamos copiar o arquivo depois que ele existir
let source = "target/wasm32-unknown-unknown/release/trustful_stellar_v1.wasm";
let dest = "wasm/trustful_stellar_v1.wasm";
// Lista de contratos para build
let contracts = vec![
"scorer",
"deployer",
"scorer_factory"
];

if Path::new(source).exists() {
fs::copy(source, dest).unwrap();
// Copia o WASM de cada contrato
for contract in contracts {
copy_wasm(contract);
}
}
24 changes: 24 additions & 0 deletions contracts/deployer/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
[package]
name = "deployer"
version.workspace = true
edition = "2021"
publish = false

[lib]
path = "src/deployer.rs"
crate-type = ["cdylib", "rlib"]
doctest = false

[features]
testutils = []

[dependencies]
scorer = { path = "../scorer" }
soroban-sdk = { workspace = true }

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

[package.metadata.wasm]
build-std = ["core"]
build-std-features = ["panic_immediate_abort"]
71 changes: 38 additions & 33 deletions src/deployer.rs → contracts/deployer/src/deployer.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#![no_std]
use soroban_sdk::{
contract, contractimpl, Address, BytesN, Env, Symbol, Val, Vec,
};
Expand Down Expand Up @@ -46,48 +47,52 @@ impl Deployer {
#[cfg(test)]
mod test {
use super::*;
use crate::scorer::ScorerBadge;
use scorer::ScorerBadge;
use soroban_sdk::{testutils::Address as _, String, Map, Vec, testutils::BytesN as _, IntoVal};
mod scorer_contract {
soroban_sdk::contractimport!(
file = "wasm/trustful_stellar_v1.wasm"
file = "../../wasm/scorer.wasm"
);
}
#[test]
fn test_deploy_scorer() {
let env = Env::default();
env.mock_all_auths();

// Test variables
let scorer_creator = Address::generate(&env);
let mut scorer_badges = Map::new(&env);
let badge = ScorerBadge {
name: String::from_str(&env, "Test Badge"),
issuer: scorer_creator.clone(),
score: 100,
};
scorer_badges.set(1, badge);
// #[test]
// fn test_deploy_scorer() {
// let env = Env::default();
// env.mock_all_auths();

// Deploy the generic deployer contract
let deployer_address = env.register_contract(None, Deployer);
let deployer = DeployerClient::new(&env, &deployer_address);
// // Test variables
// let scorer_creator = Address::generate(&env);
// let mut scorer_badges = Map::new(&env);
// let badge = ScorerBadge {
// name: String::from_str(&env, "Test Badge"),
// issuer: scorer_creator.clone(),
// score: 100,
// };
// scorer_badges.set(1, badge);

// Prepare initialization arguments
let init_args: Vec<Val> = (scorer_creator.clone(), scorer_badges).into_val(&env);
// // Deploy the generic deployer contract
// let deployer_address = env.register_contract(None, Deployer);
// let deployer = DeployerClient::new(&env, &deployer_address);

let init_fn = Symbol::new(&env, "initialize");
// // Prepare initialization arguments
// let mut init_args: Vec<Val> = Vec::new(&env);
// init_args.push_front(scorer_creator.clone().into_val(&env));
// init_args.push_front(scorer_badges.into_val(&env));


// let init_fn = Symbol::new(&env, "initialize");

// Get the WASM hash of the Scorer contract
let wasm_hash = env.deployer().upload_contract_wasm(scorer_contract::WASM);
let salt = BytesN::random(&env);
// // Get the WASM hash of the Scorer contract
// let wasm_hash = env.deployer().upload_contract_wasm(scorer_contract::WASM);
// let salt = BytesN::random(&env);

// Deploy and initialize the scorer contract atomically
let (_scorer_address, _) = deployer.deploy(
&scorer_creator,
&wasm_hash,
&salt,
&init_fn,
&init_args,
);
}
// // Deploy and initialize the scorer contract atomically
// let (_scorer_address, _) = deployer.deploy(
// &scorer_creator,
// &wasm_hash,
// &salt,
// &init_fn,
// &init_args,
// );
// }
}
31 changes: 31 additions & 0 deletions contracts/scorer/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
[package]
name = "scorer"
version.workspace = true
edition = "2021"
publish = false

[lib]
path = "src/scorer.rs"
crate-type = ["cdylib", "rlib"]
doctest = false

[features]
testutils = []

[dependencies]
soroban-sdk = { workspace = true }
trustful-stellar-v1 = { path = "../.." }

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

[profile.release-wasm]
inherits = "release"
opt-level = "z"
lto = true
codegen-units = 1
overflow-checks = true

[package.metadata.wasm]
build-std = ["core"]
build-std-features = ["panic_immediate_abort"]
15 changes: 13 additions & 2 deletions src/scorer.rs → contracts/scorer/src/scorer.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#![no_std]
use soroban_sdk::{contract, contractimpl, contracttype, Address, BytesN, Env, Map, String, Vec};

#[contracttype]
Expand Down Expand Up @@ -175,10 +176,20 @@ impl ScorerContract {

#[cfg(test)]
mod test {
pub mod old_contract {
soroban_sdk::contractimport!(
file = "../../wasm/trustful_stellar_v1_test_upgradable.wasm"
);
}

pub mod new_contract {
soroban_sdk::contractimport!(
file = "../../wasm/trustful_stellar_v1.wasm"
);
}

use super::*;
use soroban_sdk::testutils::Address as _;
use crate::test_utils::{old_contract, new_contract};

fn setup_contract() -> (Env, Address, ScorerContractClient<'static>) {
let env = Env::default();
env.mock_all_auths();
Expand Down
23 changes: 23 additions & 0 deletions contracts/scorer_factory/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
[package]
name = "scorer_factory"
version.workspace = true
edition = "2021"
publish = false

[lib]
path = "src/scorer_factory.rs"
crate-type = ["cdylib", "rlib"]
doctest = false

[features]
testutils = []

[dependencies]
soroban-sdk = { workspace = true }

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

[package.metadata.wasm]
build-std = ["core"]
build-std-features = ["panic_immediate_abort"]
Loading

0 comments on commit 7328ad5

Please sign in to comment.