diff --git a/.github/workflows/contracts.yml b/.github/workflows/contracts.yml new file mode 100644 index 00000000..2e1e6650 --- /dev/null +++ b/.github/workflows/contracts.yml @@ -0,0 +1,66 @@ +name: Build and test contracts + +on: + push: + pull_request: + branches: ["main"] + +env: + CARGO_TERM_COLOR: always + RUSTFLAGS: "-Dwarnings" + +jobs: + build: + runs-on: ubuntu-latest + + steps: + - name: Checkout repository + uses: actions/checkout@v3 + + - name: Set up cargo cache + uses: actions/cache@v3 + continue-on-error: false + with: + path: | + ~/.cargo/bin/ + ~/.cargo/registry/index/ + ~/.cargo/registry/cache/ + ~/.cargo/git/db/ + lib/target + key: 1-${{ runner.os }}-cargo-build-${{ hashFiles('**/Cargo.toml') }} + restore-keys: ${{ runner.os }}-cargo-build + + - name: Prepare target + run: rustup target add wasm32-unknown-unknown + + # This will attempt to build all contracts in the workspace. + # This command will likely change when we have more than contracts in the workspace + - name: Build All + run: cargo build --release --target wasm32-unknown-unknown --verbose + + - name: Lint + run: cargo clippy --all-targets --all-features + + test: + needs: build + runs-on: ubuntu-latest + + steps: + - name: Checkout repository + uses: actions/checkout@v3 + + - name: Set up cargo cache + uses: actions/cache@v3 + continue-on-error: false + with: + path: | + ~/.cargo/bin/ + ~/.cargo/registry/index/ + ~/.cargo/registry/cache/ + ~/.cargo/git/db/ + contracts/target + key: 1-${{ runner.os }}-cargo-build-${{ hashFiles('**/Cargo.toml') }} + restore-keys: ${{ runner.os }}-cargo-build + + - name: Test + run: cargo test diff --git a/Cargo.toml b/Cargo.toml new file mode 100644 index 00000000..829a3f74 --- /dev/null +++ b/Cargo.toml @@ -0,0 +1,26 @@ + + +[workspace] +# We want to use v2 of the cargo dependency resolver. +resolver = "2" +# Our workspace members include the packages in the contracts directory. +members = ["contracts/voting", "contracts/raffle"] + + +[profile.release] +opt-level = "z" +overflow-checks = true +debug = 0 +strip = "symbols" +debug-assertions = false +panic = "abort" +codegen-units = 1 +lto = true + +[profile.release-with-logs] +inherits = "release" +debug-assertions = true + + +[workspace.dependencies.soroban-sdk] +version = "0.9.2" \ No newline at end of file diff --git a/contracts/raffle/Cargo.toml b/contracts/raffle/Cargo.toml index c684f1b9..61a38477 100644 --- a/contracts/raffle/Cargo.toml +++ b/contracts/raffle/Cargo.toml @@ -9,22 +9,8 @@ edition = "2021" crate-type = ["cdylib"] [dependencies] -soroban-sdk = "0.9.2" +soroban-sdk = { workspace = true } rand = { version = "0.8.5", default-features = false, features = ["small_rng"] } [dev-dependencies] -soroban-sdk = { version = "0.9.2", features = ["testutils"] } - -[profile.release] -opt-level = "z" -overflow-checks = true -debug = 0 -strip = "symbols" -debug-assertions = false -panic = "abort" -codegen-units = 1 -lto = true - -[profile.release-with-logs] -inherits = "release" -debug-assertions = true +soroban-sdk = { workspace = true, features = ["testutils"] } diff --git a/contracts/raffle/src/lib.rs b/contracts/raffle/src/lib.rs index 71233b3b..356e42dd 100644 --- a/contracts/raffle/src/lib.rs +++ b/contracts/raffle/src/lib.rs @@ -122,9 +122,8 @@ impl RaffleContract { let storage = env.storage().persistent(); - if !storage - .get::<_, bool>(&DataKey::AlreadyInitialized) - .is_some() + if storage + .get::<_, bool>(&DataKey::AlreadyInitialized).is_none() { return Err(Error::NotInitialized); } @@ -157,9 +156,8 @@ impl RaffleContract { pub fn play_raffle(env: Env, random_seed: u64) -> Result<(), Error> { let storage = env.storage().persistent(); - if !storage - .get::<_, bool>(&DataKey::AlreadyInitialized) - .is_some() + if storage + .get::<_, bool>(&DataKey::AlreadyInitialized).is_none() { return Err(Error::NotInitialized); } diff --git a/contracts/raffle/src/test.rs b/contracts/raffle/src/test.rs index 30c298aa..c717b5e7 100644 --- a/contracts/raffle/src/test.rs +++ b/contracts/raffle/src/test.rs @@ -35,7 +35,7 @@ fn admin_is_identified_on_init() { } fn assert_auth( - auths: &std::vec::Vec<(Address, AuthorizedInvocation)>, + auths: &[(Address, AuthorizedInvocation)], idx: usize, call_addr: Address, auth_addr: Address, diff --git a/contracts/voting/Cargo.toml b/contracts/voting/Cargo.toml index fca6aa39..ca3c6410 100644 --- a/contracts/voting/Cargo.toml +++ b/contracts/voting/Cargo.toml @@ -7,22 +7,8 @@ edition = "2021" crate-type = ["cdylib"] [dependencies] -soroban-sdk = "0.9.2" +soroban-sdk = { workspace = true } [dev-dependencies] -soroban-sdk = { version = "0.9.2", features = ["testutils"] } -rstest = "0.17.0" - -[profile.release] -opt-level = "z" -overflow-checks = true -debug = 0 -strip = "symbols" -debug-assertions = false -panic = "abort" -codegen-units = 1 -lto = true - -[profile.release-with-logs] -inherits = "release" -debug-assertions = true \ No newline at end of file +soroban-sdk = { workspace = true, features = ["testutils"] } +rstest = "0.17.0" \ No newline at end of file diff --git a/contracts/voting/src/test.rs b/contracts/voting/src/test.rs index f043bf93..80a0f8ec 100644 --- a/contracts/voting/src/test.rs +++ b/contracts/voting/src/test.rs @@ -29,7 +29,7 @@ fn proposal_creation() { } fn assert_auth( - auths: &std::vec::Vec<(Address, AuthorizedInvocation)>, + auths: &[(Address, AuthorizedInvocation)], idx: usize, call_addr: Address, auth_addr: Address, @@ -146,14 +146,14 @@ fn cannot_vote_if_total_voters_reached() { fn advance_ledger_time_in(time: u64, env: &mut Env) { let mut ledger_info = env.ledger().get(); - ledger_info.timestamp = ledger_info.timestamp + time; + ledger_info.timestamp += time; env.ledger().set(ledger_info) } #[rstest] #[case::rate_50(2, 1, 50_00, true)] #[case::precision_is_captured_in_bps(3, 1, 33_33, false)] -#[case::rate_100(2, 2, 100_00, true)] +#[case::rate_100(2, 2, 10_000, true)] #[case::no_votes_no_rate(0, 0, 0, false)] fn proposal_calculate_approval_rate( #[case] total_voters: u32,