-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathtransfer.clar
More file actions
172 lines (151 loc) · 5.57 KB
/
transfer.clar
File metadata and controls
172 lines (151 loc) · 5.57 KB
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
;; STX Transfer Smart Contract
;; A simple contract to transfer STX from one wallet to another
;; Error codes
(define-constant ERR-INVALID-AMOUNT (err u100))
(define-constant ERR-INVALID-RECIPIENT (err u101))
(define-constant ERR-TRANSFER-FAILED (err u102))
(define-constant ERR-INSUFFICIENT-BALANCE (err u103))
;; Events are emitted via print
(define-data-var transfer-count uint u0)
;; Read-only function to get current transfer count
(define-read-only (get-transfer-count)
(var-get transfer-count)
)
;; Read-only function to check STX balance
(define-read-only (get-balance (account principal))
(stx-get-balance account)
)
;; Main transfer function
;; Transfers STX from sender to recipient
(define-public (transfer-stx (amount uint) (recipient principal))
(begin
;; Validate amount
(asserts! (> amount u0) ERR-INVALID-AMOUNT)
;; Validate recipient is not sender
(asserts! (not (is-eq tx-sender recipient)) ERR-INVALID-RECIPIENT)
;; Check sender has sufficient balance
(asserts! (>= (stx-get-balance tx-sender) amount) ERR-INSUFFICIENT-BALANCE)
;; Transfer STX
(match (stx-transfer? amount tx-sender recipient)
success
(begin
;; Increment transfer count
(var-set transfer-count (+ (var-get transfer-count) u1))
;; Print event
(print {
event: "stx-transfer",
sender: tx-sender,
recipient: recipient,
amount: amount,
transfer-id: (var-get transfer-count)
})
(ok true)
)
error ERR-TRANSFER-FAILED
)
)
)
;; Batch transfer function
;; Transfer STX to multiple recipients
(define-public (batch-transfer (recipients (list 20 {recipient: principal, amount: uint})))
(begin
(asserts! (> (len recipients) u0) ERR-INVALID-AMOUNT)
;; Process all transfers
(ok (map process-single-transfer recipients))
)
)
;; Helper function for batch transfers
(define-private (process-single-transfer (transfer-data {recipient: principal, amount: uint}))
(let
(
(recipient (get recipient transfer-data))
(amount (get amount transfer-data))
)
(match (stx-transfer? amount tx-sender recipient)
success
(begin
(var-set transfer-count (+ (var-get transfer-count) u1))
(print {
event: "stx-batch-transfer",
sender: tx-sender,
recipient: recipient,
amount: amount,
transfer-id: (var-get transfer-count)
})
true
)
error false
)
)
)
;; Transfer with memo
;; Allows adding a message to the transfer
(define-public (transfer-stx-memo (amount uint) (recipient principal) (memo (string-utf8 256)))
(begin
;; Validate inputs
(asserts! (> amount u0) ERR-INVALID-AMOUNT)
(asserts! (not (is-eq tx-sender recipient)) ERR-INVALID-RECIPIENT)
(asserts! (>= (stx-get-balance tx-sender) amount) ERR-INSUFFICIENT-BALANCE)
;; Transfer STX
(match (stx-transfer? amount tx-sender recipient)
success
(begin
(var-set transfer-count (+ (var-get transfer-count) u1))
;; Print event with memo
(print {
event: "stx-transfer-memo",
sender: tx-sender,
recipient: recipient,
amount: amount,
memo: memo,
transfer-id: (var-get transfer-count)
})
(ok true)
)
error ERR-TRANSFER-FAILED
)
)
)
;; Split transfer
;; Split an amount equally between multiple recipients
(define-public (split-transfer (total-amount uint) (recipients (list 10 principal)))
(let
(
(recipient-count (len recipients))
(amount-per-recipient (/ total-amount recipient-count))
)
(begin
;; Validate inputs
(asserts! (> total-amount u0) ERR-INVALID-AMOUNT)
(asserts! (> recipient-count u0) ERR-INVALID-RECIPIENT)
(asserts! (>= (stx-get-balance tx-sender) total-amount) ERR-INSUFFICIENT-BALANCE)
;; Process transfers
(ok (map split-transfer-single amount-per-recipient recipients))
)
)
)
;; Helper function for split transfers
(define-private (split-transfer-single (amount uint) (recipient principal))
(match (stx-transfer? amount tx-sender recipient)
success
(begin
(var-set transfer-count (+ (var-get transfer-count) u1))
(print {
event: "stx-split-transfer",
sender: tx-sender,
recipient: recipient,
amount: amount,
transfer-id: (var-get transfer-count)
})
true
)
error false
)
)
;; Read-only function to check if transfer would succeed
(define-read-only (can-transfer (sender principal) (amount uint))
(and
(> amount u0)
(>= (stx-get-balance sender) amount)
)
)