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
56 changes: 50 additions & 6 deletions crates/contracts/core/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ use soroban_sdk::{
contract, contractimpl, contracttype, symbol_short, token, Address, BytesN, Env, IntoVal, Val,
contract, contracterror, contractimpl, contracttype, panic_with_error, symbol_short, token,
Address, Bytes, BytesN, Env, IntoVal, Symbol, Val,
contract, contractimpl, contracttype, symbol_short, token, Address, Env, IntoVal, Val,
};

#[derive(Clone)]
Expand Down Expand Up @@ -58,15 +57,35 @@ enum DataKey {
LockedSession(BytesN<32>),
}

#[derive(Clone)]
#[contracttype]
pub struct InitializedEvent {
pub admin: Address,
pub treasury: Address,
pub dispute_window: u32,
}

#[derive(Clone)]
#[contracttype]
pub struct FundsLockedEvent {
pub session_id: BytesN<32>,
pub buyer: Address,
pub seller: Address,
pub amount: i128,
pub timestamp: u64,
}

#[derive(Clone)]
#[contracttype]
pub struct SessionApprovedEvent {
pub session_id: BytesN<32>,
pub buyer: Address,
pub seller: Address,
pub token: Address,
pub amount: i128,
pub payout: i128,
pub fee: i128,
pub timestamp: u64,
}

#[derive(Clone)]
Expand Down Expand Up @@ -142,9 +161,20 @@ impl CoreContract {
.instance()
.set(&DataKey::NextSessionId, &1_u64);
// Default dispute window: 7 days (604800 seconds)
let dispute_window_secs: u64 = 604800;
env.storage()
.instance()
.set(&DataKey::DisputeWindowSecs, &604800_u64);
.set(&DataKey::DisputeWindowSecs, &dispute_window_secs);

// Emit Initialized event
let topics = (Symbol::new(&env, "Initialized"),);
let data: Val = InitializedEvent {
admin: admin.clone(),
treasury: treasury.clone(),
dispute_window: dispute_window_secs as u32,
}
.into_val(&env);
env.events().publish(topics, data);
}

pub fn lock_funds(
Expand Down Expand Up @@ -266,6 +296,9 @@ impl CoreContract {
session.status = SessionStatus::Approved;
Self::save_session(&env, &session_id, &session);

// Emit SessionApproved event with gross amount, fee, and net payout
let timestamp = env.ledger().timestamp();
let topics = (Symbol::new(&env, "SessionApproved"), session_id);
let topics = (symbol_short!("approved"), session_id.clone());
let topics = (Symbol::new(&env, "fee_deducted"), session_id);
let fee_data = FeeDeductedEvent {
Expand All @@ -282,8 +315,10 @@ impl CoreContract {
buyer: session.buyer,
seller: session.seller,
token: session.token,
amount: session.amount,
payout,
fee,
timestamp,
}
.into_val(&env);
env.events().publish(topics, data);
Expand Down Expand Up @@ -543,10 +578,19 @@ impl CoreContract {
};

env.storage().persistent().set(&key, &session);
env.events().publish(
(Symbol::new(&env, "FundsLocked"), session_id),
(buyer, seller, amount),
);

// Emit FundsLocked event with all relevant session metadata
let timestamp = env.ledger().timestamp();
let topics = (Symbol::new(&env, "FundsLocked"), session_id.clone());
let data: Val = FundsLockedEvent {
session_id,
buyer,
seller,
amount,
timestamp,
}
.into_val(&env);
env.events().publish(topics, data);
}

pub fn get_session(env: Env, session_id: u64) -> Session {
Expand Down
135 changes: 135 additions & 0 deletions crates/contracts/core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -680,3 +680,138 @@ fn validate_note(note: &Option<Bytes>) -> Result<(), Error> {
}
Ok(())
}
use soroban_sdk::{contract, contractimpl, contracttype, symbol_short, Address, BytesN, Env, Symbol, Vec};

#[contracttype]
#[derive(Clone)]
pub enum DataKey {
Admin,
WasmHash,
}
mod contract;

pub use contract::{
AutoRefundExecutedEvent, CoreContract, CoreContractClient, FundsLockedEvent, InitializedEvent,
Session, SessionApprovedEvent, SessionCompletedEvent, SessionStatus,

ContractError, CoreContract, CoreContractClient, LockedSession, Session,
SessionApprovedEvent, SessionCompletedEvent, SessionStatus,

CoreContract, CoreContractClient, Session, SessionApprovedEvent, SessionCompletedEvent,
SessionStatus, RefundRequestedEvent, RefundedEvent, DisputeInitiatedEvent,
DisputeResolvedEvent,
SessionRefundedEvent, SessionStatus,
};

#[contractimpl]
impl CoreContract {
pub fn init(env: Env, admin: Address) {
admin.require_auth();
env.storage().instance().set(&DataKey::Admin, &admin);
}

pub fn upgrade(env: Env, new_wasm_hash: BytesN<32>) {
let admin: Address = env.storage().instance().get(&DataKey::Admin).unwrap();
admin.require_auth();

let old_wasm_hash: BytesN<32> = env
.storage()
.instance()
.get(&DataKey::WasmHash)
.unwrap_or(BytesN::from_array(&env, &[0; 32]));

env.deployer().update_current_contract_wasm(new_wasm_hash.clone());
env.storage().instance().set(&DataKey::WasmHash, &new_wasm_hash);

env.events().publish(
(Symbol::new(&env, "ContractUpgraded"),),
(old_wasm_hash, new_wasm_hash),
);
}

pub fn hello(env: Env, to: Symbol) -> Vec<Symbol> {
let mut vec = Vec::new(&env);
vec.push_back(symbol_short!("Hello"));
vec.push_back(to);
vec
}
}
#[cfg(test)]
mod test;

#![no_std]

mod contract;

pub use contract::{
CoreContract, CoreContractClient, DisputeResolvedEvent, FeeDeductedEvent, InitializedEvent,
RefundEvent, Session, SessionApprovedEvent, SessionCompletedEvent, SessionStatus,
CoreContract, CoreContractClient, Session, SessionApprovedEvent, SessionCompletedEvent,
SessionRefundedEvent, SessionStatus,
};

#[cfg(test)]
mod test;


fn refund() {
let env = Env::default();
let contract_id = env.register_contract(None, CoreContract);
let result: Vec<Symbol> = env.invoke_contract(
&contract_id,
&symbol_short!("hello"),
vec![&env, symbol_short!("World")],
);
assert_eq!(result, vec![&env, symbol_short!("Hello"), symbol_short!("World")]);


#[contractimpl]
impl CoreContract {
pub fn hello(env: Env, to: Symbol) -> Vec<Symbol> {
vec![&env, symbol_short!("Refund"), to]
}
}
}



fn setup() -> (
Env,
CoreContractClient<'static>,
TokenClient<'static>,
StellarAssetClient<'static>,
Address,
Address,
Address,
Address,
) {
let env = Env::default();
env.mock_all_auths();

let buyer = Address::generate(&env);
let seller = Address::generate(&env);
let treasury = Address::generate(&env);
let token_admin = Address::generate(&env);

let token_address = env.register_stellar_asset_contract(token_admin.clone());
let token_client = TokenClient::new(&env, &token_address);
let asset_client = StellarAssetClient::new(&env, &token_address);

asset_client.mint(&buyer, &1_000);

let contract_id = env.register_contract(None, CoreContract);
let contract = CoreContractClient::new(&env, &contract_id);
contract.initialize(&treasury, &500);

(
env,
contract,
token_client,
asset_client,
buyer,
seller,
treasury,
contract_id,
)
}