Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 41 additions & 0 deletions .github/workflows/rust.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
name: Contracts CI

on:
push:
branches: [main, develop]
pull_request:
branches: [main, develop]

jobs:
contracts:
runs-on: ubuntu-latest

defaults:
run:
working-directory: contracts

steps:
- name: Checkout code
uses: actions/checkout@v4

- name: Set up Rust
uses: dtolnay/rust-toolchain@stable
with:
components: rustfmt, clippy
targets: wasm32-unknown-unknown

- name: Cache Rust dependencies
uses: Swatinem/rust-cache@v2

- name: Check formatting
run: cargo fmt --all -- --check

- name: Run clippy
run: cargo clippy --workspace --all-targets --all-features -- -D warnings

- name: Run tests
run: cargo test --workspace

- name: Build WASM artifacts
run: cargo build --workspace --target wasm32-unknown-unknown --release

16 changes: 3 additions & 13 deletions contracts/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,13 +1,3 @@
[package]
name = "geev-core"
version = "0.1.0"
edition = "2021"

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

[dependencies]
soroban-sdk = "25.0.0"

[dev-dependencies]
soroban-sdk = { version = "25.0.0", features = ["testutils"] }
[workspace]
members = ["geev-core"]
resolver = "2"
3 changes: 3 additions & 0 deletions contracts/geev-core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,7 @@ version = "0.1.0"
edition = "2021"

[dependencies]
soroban-sdk = { version = "22.0.0", default-features = false }

[dev-dependencies]
soroban-sdk = { version = "22.0.0", features = ["testutils"] }
62 changes: 6 additions & 56 deletions contracts/geev-core/src/giveaway.rs
Original file line number Diff line number Diff line change
@@ -1,20 +1,8 @@
use soroban_sdk::{contracttype, Address, Env, Symbol, token};
use soroban_sdk::{contracttype, Address, Env};

#[derive(Clone, Copy, PartialEq, Eq)]
#[contracttype]
pub enum GiveawayStatus {
Active,
Ended,
}

#[derive(Clone)]
#[contracttype]
pub struct Giveaway {
pub id: u64,
pub status: GiveawayStatus,
pub creator: Address,
pub token: Address,
pub amount: i128,
pub end_time: u64,
pub participant_count: u32,
}
Expand All @@ -24,52 +12,15 @@ pub struct Giveaway {
pub enum DataKey {
Giveaway(u64),
Participant(u64, Address),
GiveawayCount,
}

pub fn create_giveaway(
env: Env,
creator: Address,
token: Address,
amount: i128,
end_time: u64,
) -> u64 {
creator.require_auth();

// Initialize the Token Client using the provided token address
let token_client = token::Client::new(&env, &token);

// Execute transfer from creator to current contract
token_client.transfer(&creator, &env.current_contract_address(), &amount);

// Generate a new Giveaway ID
let count_key = DataKey::GiveawayCount;
let mut count: u64 = env.storage().instance().get(&count_key).unwrap_or(0);
count += 1;
env.storage().instance().set(&count_key, &count);

// Create Giveaway struct
pub fn create_giveaway(env: &Env, giveaway_id: u64, end_time: u64) {
let key = DataKey::Giveaway(giveaway_id);
let giveaway = Giveaway {
id: count,
status: GiveawayStatus::Active,
creator: creator.clone(),
token: token.clone(),
amount,
end_time,
participant_count: 0,
};

// Save struct to Persistent Storage under key: Giveaway(id)
let giveaway_key = DataKey::Giveaway(count);
env.storage().persistent().set(&giveaway_key, &giveaway);

// Emit GiveawayCreated event for the NestJS indexer
env.events().publish(
(Symbol::new(&env, "GiveawayCreated"), count, creator),
(amount, token),
);

count
env.storage().persistent().set(&key, &giveaway);
}

pub fn enter_giveaway(env: Env, user: Address, giveaway_id: u64) {
Expand All @@ -88,13 +39,12 @@ pub fn enter_giveaway(env: Env, user: Address, giveaway_id: u64) {
}

let participant_key = DataKey::Participant(giveaway_id, user.clone());
if env.storage().instance().has(&participant_key) {
if env.storage().persistent().has(&participant_key) {
panic!("Double Entry");
}

env.storage().instance().set(&participant_key, &true);
env.storage().persistent().set(&participant_key, &true);

giveaway.participant_count += 1;
env.storage().persistent().set(&giveaway_key, &giveaway);
}

5 changes: 3 additions & 2 deletions contracts/geev-core/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
mod giveaway;
#![no_std]

pub use giveaway::{enter_giveaway, DataKey, Giveaway};
mod giveaway;

pub use giveaway::{create_giveaway, enter_giveaway, DataKey, Giveaway};
Loading