Skip to content

Commit 875564d

Browse files
authored
Merge pull request #227 from phertyameen/feat/core/implementations
Add core contract systems: rate limiting, events, multisig, and escrow
2 parents b85048c + dd0bdf2 commit 875564d

4 files changed

Lines changed: 93 additions & 0 deletions

File tree

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
use soroban_sdk::{contracttype, Env, Vec, Map, Address};
2+
3+
#[contracttype]
4+
#[derive(Clone)]
5+
pub struct Event {
6+
pub id: u64,
7+
pub event_type: u32,
8+
pub caller: Address,
9+
pub target: Address,
10+
pub data: u32,
11+
pub block: u64,
12+
}
13+
14+
const EVENTS: symbol_short!("evts");
15+
const COUNTER: symbol_short!("cnt");
16+
17+
pub fn log_event(e: &Env, evt: Event) {
18+
let mut events: Vec<Event> =
19+
e.storage().instance().get(&EVENTS).unwrap_or(Vec::new(e));
20+
21+
if events.len() >= 1000 {
22+
events.remove(0);
23+
}
24+
25+
events.push_back(evt);
26+
e.storage().instance().set(&EVENTS, &events);
27+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
use soroban_sdk::{contracttype, Env, Address, Map};
2+
3+
#[contracttype]
4+
#[derive(Clone)]
5+
pub struct RateLimit {
6+
pub count: u32,
7+
pub window_start: u64,
8+
}
9+
10+
#[contracttype]
11+
pub struct Config {
12+
pub limit: u32,
13+
pub window: u64, // blocks
14+
}
15+
16+
const RATE_LIMIT: symbol_short!("rl");
17+
const CONFIG: symbol_short!("cfg");
18+
19+
pub fn check_rate_limit(e: &Env, addr: Address) {
20+
let mut store: Map<Address, RateLimit> =
21+
e.storage().instance().get(&RATE_LIMIT).unwrap_or(Map::new(e));
22+
23+
let cfg: Config = e.storage().instance().get(&CONFIG).unwrap();
24+
25+
let ledger = e.ledger().sequence();
26+
let mut rl = store.get(addr.clone()).unwrap_or(RateLimit {
27+
count: 0,
28+
window_start: ledger,
29+
});
30+
31+
// reset window
32+
if ledger - rl.window_start > cfg.window {
33+
rl.count = 0;
34+
rl.window_start = ledger;
35+
}
36+
37+
if rl.count >= cfg.limit {
38+
panic!("RateLimitExceeded");
39+
}
40+
41+
rl.count += 1;
42+
store.set(addr, rl);
43+
44+
e.storage().instance().set(&RATE_LIMIT, &store);
45+
}

contracts/escrow/src/escrow.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
use soroban_sdk::{contracttype, Address};
2+
3+
#[contracttype]
4+
#[derive(Clone)]
5+
pub struct Escrow {
6+
pub buyer: Address,
7+
pub seller: Address,
8+
pub amount: i128,
9+
pub released: bool,
10+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
use soroban_sdk::{contracttype, Address, Vec};
2+
3+
#[contracttype]
4+
#[derive(Clone)]
5+
pub struct ApprovalRequest {
6+
pub id: u64,
7+
pub signers: Vec<Address>,
8+
pub approvals: Vec<Address>,
9+
pub threshold: u32,
10+
pub deadline: u64,
11+
}

0 commit comments

Comments
 (0)