-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchannels.clar
49 lines (47 loc) · 1.32 KB
/
channels.clar
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
(define-constant ERR_CHANNEL_ALREADY_EXISTS u2001)
(define-constant ERR_CHANNEL_NOT_FOUND u2002)
(define-map channels
{ channel-id: uint }
{
sender: principal,
recipient: principal,
balance-sender: uint,
balance-recipient: uint,
token: (optional principal)
}
)
(define-public (fund-channel (channel-id uint) (recipient principal) (amount uint) (token (optional principal)))
(asserts! (is-none (map-get? channels { channel-id: channel-id })) (err ERR_CHANNEL_ALREADY_EXISTS))
(asserts! (> amount u0) (err ERR_INVALID_AMOUNT))
(match token
(some token-contract)
(begin
(asserts! (ft-transfer? token-contract amount tx-sender recipient) (err ERR_INSUFFICIENT_BALANCE))
(map-set channels
{ channel-id: channel-id }
{
sender: tx-sender,
recipient: recipient,
balance-sender: amount,
balance-recipient: u0,
token: token
}
)
(ok channel-id)
)
(begin
(asserts! (stx-transfer? amount tx-sender recipient) (err ERR_INSUFFICIENT_BALANCE))
(map-set channels
{ channel-id: channel-id }
{
sender: tx-sender,
recipient: recipient,
balance-sender: amount,
balance-recipient: u0,
token: none
}
)
(ok channel-id)
)
)
)