-
Notifications
You must be signed in to change notification settings - Fork 31
/
Copy pathyield-usda.clar
329 lines (287 loc) · 10.8 KB
/
yield-usda.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
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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
(impl-trait .trait-ownable.ownable-trait)
(impl-trait .trait-semi-fungible.semi-fungible-trait)
(define-constant ERR-NOT-AUTHORIZED (err u1000))
(define-constant ERR-TOO-MANY-POOLS (err u2004))
(define-constant ERR-INVALID-BALANCE (err u1001))
(define-fungible-token yield-usda)
(define-map token-balances {token-id: uint, owner: principal} uint)
(define-map token-supplies uint uint)
(define-map token-owned principal (list 200 uint))
(define-data-var contract-owner principal tx-sender)
(define-map approved-contracts principal bool)
;; @desc get-owner
;; @returns (response principal)
(define-read-only (get-contract-owner)
(ok (var-get contract-owner))
)
;; @desc set-owner
;; @restricted Contract-Owner
;; @params owner
;; @returns (response bool)
(define-public (set-contract-owner (owner principal))
(begin
(asserts! (is-eq tx-sender (var-get contract-owner)) ERR-NOT-AUTHORIZED)
(ok (var-set contract-owner owner))
)
)
;; @desc check-is-approved
;; @restricted Contract-Owner
;; @params sender
;; @returns (response bool)
(define-private (check-is-approved (sender principal))
(ok (asserts! (or (default-to false (map-get? approved-contracts sender)) (is-eq sender (var-get contract-owner))) ERR-NOT-AUTHORIZED))
)
(define-public (add-approved-contract (new-approved-contract principal))
(begin
(asserts! (is-eq tx-sender (var-get contract-owner)) ERR-NOT-AUTHORIZED)
(map-set approved-contracts new-approved-contract true)
(ok true)
)
)
(define-public (set-approved-contract (owner principal) (approved bool))
(begin
(asserts! (is-eq tx-sender (var-get contract-owner)) ERR-NOT-AUTHORIZED)
(ok (map-set approved-contracts owner approved))
)
)
;; @desc get-token-owned
;; @params owner
;; @returns list
(define-read-only (get-token-owned (owner principal))
(default-to (list) (map-get? token-owned owner))
)
;; @desc set-balance
;; @params token-id
;; @params balance
;; @params owner
;; @returns (response bool)
(define-private (set-balance (token-id uint) (balance uint) (owner principal))
(begin
(and
(is-none (index-of (get-token-owned owner) token-id))
(map-set token-owned owner (unwrap! (as-max-len? (append (get-token-owned owner) token-id) u200) ERR-TOO-MANY-POOLS))
)
(map-set token-balances {token-id: token-id, owner: owner} balance)
(ok true)
)
)
;; @desc get-balance-or-default
;; @params token-id
;; @params who
;; @returns (response uint)
(define-private (get-balance-or-default (token-id uint) (who principal))
(default-to u0 (map-get? token-balances {token-id: token-id, owner: who}))
)
;; @desc get-balance
;; @params token-id
;; @params who
;; @returns (response uint)
(define-read-only (get-balance (token-id uint) (who principal))
(ok (get-balance-or-default token-id who))
)
;; @desc get-overall-balance
;; @params who
;; @returns (response uint)
(define-read-only (get-overall-balance (who principal))
(ok (ft-get-balance yield-usda who))
)
;; @desc get-total-supply
;; @params token-id
;; @returns (response uint)
(define-read-only (get-total-supply (token-id uint))
(ok (default-to u0 (map-get? token-supplies token-id)))
)
;; @desc get-overall-supply
;; @returns (response uint)
(define-read-only (get-overall-supply)
(ok (ft-get-supply yield-usda))
)
;; @desc get-decimals
;; @params token-id
;; @returns (response uint)
(define-read-only (get-decimals (token-id uint))
(ok u8)
)
;; @desc get-token-uri
;; @params token-id
;; @returns (response none)
(define-read-only (get-token-uri (token-id uint))
(ok none)
)
;; @desc transfer
;; @restricted sender ; tx-sender should be sender
;; @params token-id
;; @params amount
;; @params sender
;; @params recipient
;; @returns (response bool)
(define-public (transfer (token-id uint) (amount uint) (sender principal) (recipient principal))
(let
(
(sender-balance (get-balance-or-default token-id sender))
)
(asserts! (is-eq tx-sender sender) ERR-NOT-AUTHORIZED)
(asserts! (<= amount sender-balance) ERR-INVALID-BALANCE)
(try! (ft-transfer? yield-usda amount sender recipient))
(try! (set-balance token-id (- sender-balance amount) sender))
(try! (set-balance token-id (+ (get-balance-or-default token-id recipient) amount) recipient))
(print {type: "sft_transfer", token-id: token-id, amount: amount, sender: sender, recipient: recipient})
(ok true)
)
)
;; @desc transfer-memo
;; @restricted sender ; tx-sender should be sender
;; @params token-id
;; @params amount
;; @params sender
;; @params recipient
;; @params memo; expiry
;; @returns (response bool)
(define-public (transfer-memo (token-id uint) (amount uint) (sender principal) (recipient principal) (memo (buff 34)))
(let
(
(sender-balance (get-balance-or-default token-id sender))
)
(asserts! (is-eq tx-sender sender) ERR-NOT-AUTHORIZED)
(asserts! (<= amount sender-balance) ERR-INVALID-BALANCE)
(try! (ft-transfer? yield-usda amount sender recipient))
(try! (set-balance token-id (- sender-balance amount) sender))
(try! (set-balance token-id (+ (get-balance-or-default token-id recipient) amount) recipient))
(print {type: "sft_transfer", token-id: token-id, amount: amount, sender: sender, recipient: recipient, memo: memo})
(ok true)
)
)
;; @desc mint
;; @params token-id
;; @params amount
;; @params recipient
;; @returns (response bool)
(define-public (mint (token-id uint) (amount uint) (recipient principal))
(begin
(try! (check-is-approved tx-sender))
(try! (ft-mint? yield-usda amount recipient))
(try! (set-balance token-id (+ (get-balance-or-default token-id recipient) amount) recipient))
(map-set token-supplies token-id (+ (unwrap-panic (get-total-supply token-id)) amount))
(print {type: "sft_mint", token-id: token-id, amount: amount, recipient: recipient})
(ok true)
)
)
;; @desc burn
;; @restricted Contract-Owner/Approved Contract
;; @params token-id
;; @params amount
;; @params sender
;; @returns (response bool)
(define-public (burn (token-id uint) (amount uint) (sender principal))
(begin
(try! (check-is-approved tx-sender))
(try! (ft-burn? yield-usda amount sender))
(try! (set-balance token-id (- (get-balance-or-default token-id sender) amount) sender))
(map-set token-supplies token-id (- (unwrap-panic (get-total-supply token-id)) amount))
(print {type: "sft_burn", token-id: token-id, amount: amount, sender: sender})
(ok true)
)
)
(define-constant ONE_8 u100000000)
;; @desc pow-decimals
;; @returns uint
(define-private (pow-decimals)
(pow u10 (unwrap-panic (get-decimals u0)))
)
;; @desc fixed-to-decimals
;; @params amount
;; @returns uint
(define-read-only (fixed-to-decimals (amount uint))
(/ (* amount (pow-decimals)) ONE_8)
)
;; @desc decimals-to-fixed
;; @params amount
;; @returns uint
(define-private (decimals-to-fixed (amount uint))
(/ (* amount ONE_8) (pow-decimals))
)
;; @desc get-total-supply-fixed
;; @params token-id
;; @returns (response uint)
(define-read-only (get-total-supply-fixed (token-id uint))
(ok (decimals-to-fixed (default-to u0 (map-get? token-supplies token-id))))
)
;; @desc get-balance-fixed
;; @params token-id
;; @params who
;; @returns (response uint)
(define-read-only (get-balance-fixed (token-id uint) (who principal))
(ok (decimals-to-fixed (get-balance-or-default token-id who)))
)
;; @desc get-overall-supply-fixed
;; @returns (response uint)
(define-read-only (get-overall-supply-fixed)
(ok (decimals-to-fixed (ft-get-supply yield-usda)))
)
;; @desc get-overall-balance-fixed
;; @params who
;; @returns (response uint)
(define-read-only (get-overall-balance-fixed (who principal))
(ok (decimals-to-fixed (ft-get-balance yield-usda who)))
)
;; @desc transfer-fixed
;; @params token-id
;; @params amount
;; @params sender
;; @params recipient
;; @returns (response boolean)
(define-public (transfer-fixed (token-id uint) (amount uint) (sender principal) (recipient principal))
(transfer token-id (fixed-to-decimals amount) sender recipient)
)
;; @desc transfer-memo-fixed
;; @params token-id
;; @params amount
;; @params sender
;; @params recipient
;; @params memo ; expiry
;; @returns (response boolean)
(define-public (transfer-memo-fixed (token-id uint) (amount uint) (sender principal) (recipient principal) (memo (buff 34)))
(transfer-memo token-id (fixed-to-decimals amount) sender recipient memo)
)
;; @desc mint-fixed
;; @params token-id
;; @params amount
;; @params recipient
;; @returns (response boolean)
(define-public (mint-fixed (token-id uint) (amount uint) (recipient principal))
(mint token-id (fixed-to-decimals amount) recipient)
)
;; @desc burn-fixed
;; @params token-id
;; @params amount
;; @params sender
;; @returns (response boolean)
(define-public (burn-fixed (token-id uint) (amount uint) (sender principal))
(burn token-id (fixed-to-decimals amount) sender)
)
(define-private (transfer-many-iter (item {token-id: uint, amount: uint, sender: principal, recipient: principal}) (previous-response (response bool uint)))
(match previous-response prev-ok (transfer (get token-id item) (get amount item) (get sender item) (get recipient item)) prev-err previous-response)
)
(define-public (transfer-many (transfers (list 200 {token-id: uint, amount: uint, sender: principal, recipient: principal})))
(fold transfer-many-iter transfers (ok true))
)
(define-private (transfer-many-memo-iter (item {token-id: uint, amount: uint, sender: principal, recipient: principal, memo: (buff 34)}) (previous-response (response bool uint)))
(match previous-response prev-ok (transfer-memo (get token-id item) (get amount item) (get sender item) (get recipient item) (get memo item)) prev-err previous-response)
)
(define-public (transfer-many-memo (transfers (list 200 {token-id: uint, amount: uint, sender: principal, recipient: principal, memo: (buff 34)})))
(fold transfer-many-memo-iter transfers (ok true))
)
(define-private (transfer-many-fixed-iter (item {token-id: uint, amount: uint, sender: principal, recipient: principal}) (previous-response (response bool uint)))
(match previous-response prev-ok (transfer-fixed (get token-id item) (get amount item) (get sender item) (get recipient item)) prev-err previous-response)
)
(define-public (transfer-many-fixed (transfers (list 200 {token-id: uint, amount: uint, sender: principal, recipient: principal})))
(fold transfer-many-fixed-iter transfers (ok true))
)
(define-private (transfer-many-memo-fixed-iter (item {token-id: uint, amount: uint, sender: principal, recipient: principal, memo: (buff 34)}) (previous-response (response bool uint)))
(match previous-response prev-ok (transfer-memo-fixed (get token-id item) (get amount item) (get sender item) (get recipient item) (get memo item)) prev-err previous-response)
)
(define-public (transfer-many-memo-fixed (transfers (list 200 {token-id: uint, amount: uint, sender: principal, recipient: principal, memo: (buff 34)})))
(fold transfer-many-memo-fixed-iter transfers (ok true))
)
(map-set approved-contracts .collateral-rebalancing-pool-v1 true)
(map-set approved-contracts .yield-collateral-rebalancing-pool-v1 true)