forked from trgarrett/chialisp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
p2_royalty_share_arbitrary_shares_rest_to_last.clsp
68 lines (59 loc) · 2.18 KB
/
p2_royalty_share_arbitrary_shares_rest_to_last.clsp
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
; This puzzle is designed to split its coin amount according to a predefined payout scheme.
(mod
(
PAYOUT_SCHEME ; The payout scheme is a list of recipient puzzle hashes and their share, e.g.
; ((0xcafef00d 80) (0xdeadbeef 20)) for an 80/20 split
my_amount ; The amount of the coin to be split.
total_shares ; The sum of all shares in the payout scheme. Passed in to reduce the complexity of the puzzle.
)
(include condition_codes.clib)
(include curry_and_treehash.clib)
(defun-inline get_puzhash (payout_scheme_item)
(f payout_scheme_item)
)
(defun-inline get_share (payout_scheme_item)
(f (r payout_scheme_item))
)
(defun-inline calculate_share (total_amount share total_shares)
(f (divmod (* total_amount share) total_shares))
)
(defun-inline get_amount (payout_scheme_item total_amount total_shares)
(calculate_share total_amount (get_share payout_scheme_item) total_shares)
)
; mutual recursive function to calculate the amount only once
(defun calculate_amount_and_split (PAYOUT_SCHEME total_amount total_shares shares_sum remaining_amount)
(if PAYOUT_SCHEME
(split_amount_and_create_coins PAYOUT_SCHEME (get_amount (f PAYOUT_SCHEME) total_amount total_shares) total_amount total_shares shares_sum remaining_amount)
(if (= total_shares shares_sum)
()
(x) ; raise if total shares input doesn't match the sum of all shares
)
)
)
; Loop through the royalty payout scheme and create coins
(defun split_amount_and_create_coins (PAYOUT_SCHEME this_amount total_amount total_shares shares_sum remaining_amount)
(c
(list
CREATE_COIN
(get_puzhash (f PAYOUT_SCHEME))
(if (r PAYOUT_SCHEME) this_amount remaining_amount)
(list (get_puzhash (f PAYOUT_SCHEME)))
)
(calculate_amount_and_split
(r PAYOUT_SCHEME)
total_amount
total_shares
(+ shares_sum (get_share (f PAYOUT_SCHEME)))
(- remaining_amount this_amount)
)
)
)
; main
(c
(list CREATE_COIN_ANNOUNCEMENT ())
(c
(list ASSERT_MY_AMOUNT my_amount)
(calculate_amount_and_split PAYOUT_SCHEME my_amount total_shares 0 my_amount)
)
)
)