-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathregistry.fe
95 lines (78 loc) · 2.78 KB
/
registry.fe
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
use std::context::Context
use ingot::registry::solvable::ISolvable
use ingot::registry::errors::{AlreadyLocked, InvalidClaim, OnlyAdmin, InvalidDeposit, MissingLock}
// Structure to represent a lock with a lease time
struct ClaimLock {
pub claimer: address
pub valid_until_block: u256
}
const LOCK_PERIOD_IN_BLOCKS: u256 = 1000
const LOCK_DEPOSIT_IN_WEI: u256 = 1_000_000_000_000_000_000
pub contract BountyRegistry {
lock: ClaimLock
open_challenges: Map<address, bool>
admin: address
pub fn __init__(mut self, admin: address) {
self.admin = admin
}
pub fn lock(mut self, ctx: Context) {
if self.is_locked(ctx) {
revert AlreadyLocked()
} else if ctx.msg_value() < LOCK_DEPOSIT_IN_WEI {
revert InvalidDeposit()
} else {
self.lock = ClaimLock(claimer:ctx.msg_sender(), valid_until_block: ctx.block_number() + LOCK_PERIOD_IN_BLOCKS)
}
}
pub fn validate_owns_lock(self, ctx: Context, owner: address){
if not self.is_locked(ctx) or self.lock.claimer != owner {
revert MissingLock()
}
}
pub fn is_locked(self, ctx: Context) -> bool {
return self.lock.valid_until_block >= ctx.block_number()
}
pub fn register_challenge(mut self, ctx: Context, challenge: address) {
self.validate_is_admin(ctx)
self.open_challenges[challenge] = true
}
pub fn remove_challenge(mut self, ctx: Context, challenge: address) {
self.validate_is_admin(ctx)
if self.is_locked(ctx) {
revert AlreadyLocked()
} else {
self.open_challenges[challenge] = false
}
}
pub fn is_open_challenge(self, challenge: address) -> bool {
return self.open_challenges[challenge]
}
pub fn claim(self, mut ctx: Context, challenge: address) {
self.validate_owns_lock(ctx, owner: ctx.msg_sender())
if not self.open_challenges[challenge] {
revert InvalidClaim()
} else {
let puzzle: ISolvable = ISolvable(challenge)
if puzzle.is_solved() {
ctx.send_value(to: ctx.msg_sender(), wei: ctx.self_balance())
} else {
revert InvalidClaim()
}
}
}
pub fn withdraw(self, mut ctx: Context) {
// Admin should be allowed to withdraw funds if there's no lock in progress
// This would be used to migrate to a new bounty registry
self.validate_is_admin(ctx)
if self.is_locked(ctx) {
revert AlreadyLocked()
} else {
ctx.send_value(to: ctx.msg_sender(), wei: ctx.self_balance())
}
}
fn validate_is_admin(self, ctx: Context) {
if ctx.msg_sender() != self.admin {
revert OnlyAdmin()
}
}
}