-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #13 from Rigidity/royalty-split
Royalty split puzzle
- Loading branch information
Showing
10 changed files
with
285 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
// This puzzle has not been audited or tested, and is for example purposes only. | ||
|
||
struct Payout { | ||
puzzle_hash: Bytes32, | ||
share: Int, | ||
} | ||
|
||
fun main(payouts: Payout[], total_shares: Int, my_amount: Int) -> Condition[] { | ||
let announcement = Condition::CreateCoinAnnouncement { message: '$' }; | ||
let assert_amount = Condition::AssertMyAmount { amount: my_amount }; | ||
|
||
let conditions = calculate_amount_and_split(payouts, my_amount, total_shares, 0, my_amount); | ||
[announcement, assert_amount, ...conditions] | ||
} | ||
|
||
fun calculate_amount_and_split( | ||
payouts: Payout[], | ||
total_amount: Int, | ||
total_shares: Int, | ||
shares_sum: Int, | ||
remaining_amount: Int, | ||
) -> Condition[] { | ||
if payouts is (Payout, Payout[]) { | ||
let amount = get_amount(payouts.first, total_amount, total_shares); | ||
return split_amount_and_create_coins(payouts, amount, total_amount, total_shares, shares_sum, remaining_amount); | ||
} | ||
assert total_shares == shares_sum; | ||
[] | ||
} | ||
|
||
fun split_amount_and_create_coins( | ||
payouts: (Payout, Payout[]), | ||
this_amount: Int, | ||
total_amount: Int, | ||
total_shares: Int, | ||
shares_sum: Int, | ||
remaining_amount: Int, | ||
) -> Condition[] { | ||
let payout = payouts.first; | ||
let create_coin = Condition::CreateCoin { | ||
puzzle_hash: payout.puzzle_hash, | ||
amount: if payout.share > 0 { this_amount } else { remaining_amount }, | ||
memos: [payout.puzzle_hash], | ||
}; | ||
let rest = calculate_amount_and_split(payouts.rest, total_amount, total_shares, shares_sum + payout.share, remaining_amount - this_amount); | ||
[create_coin, ...rest] | ||
} | ||
|
||
fun get_amount(payout: Payout, total_amount: Int, total_shares: Int) -> Int { | ||
(total_amount * payout.share) / total_shares | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
fun main() -> Int { | ||
if 1000 > 100 { | ||
42 | ||
} | ||
|
||
100 | ||
} | ||
|
||
fun another() -> Int { | ||
if 100 > 1000 { | ||
assert true; | ||
} | ||
} | ||
|
||
fun yet_another() -> Int { | ||
if 100 > 1000 { | ||
assert true; | ||
assert false; | ||
assert false; | ||
} | ||
100 | ||
} | ||
|
||
fun raise_ok() -> Int { | ||
raise; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
type A = B; | ||
type B = C; | ||
type C = D; | ||
type D = A; | ||
|
||
type E = F; | ||
type F = E; | ||
|
||
fun main() -> A { | ||
true | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
fun main() -> Int { | ||
a(5) | ||
} | ||
|
||
fun a(num: Int) -> Int { | ||
if num > 0 { | ||
b(num - 1) | ||
} else { | ||
c() | ||
} | ||
} | ||
|
||
fun b(num: Int) -> Int { | ||
if num > 0 { | ||
a(num - 1) | ||
} else { | ||
0 | ||
} | ||
} | ||
|
||
fun c() -> Int { | ||
52 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
fun main(value: Int) -> Int { | ||
fun inner_1(num: Int) -> Int { | ||
outer_1() * value + num | ||
} | ||
|
||
fun inner_2(num: Int) -> Int { | ||
outer_2(num, inner_1) | ||
} | ||
|
||
outer_1() + inner_1(inner_2(value)) * outer_2(value, inner_2) | ||
} | ||
|
||
fun outer_1() -> Int { | ||
96 | ||
} | ||
|
||
fun outer_2(num: Int, closure: fun(num: Int) -> Int) -> Int { | ||
num * closure(num) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
fun main() -> Int { | ||
let a = [1, 2, 3]; | ||
let b = [4, 5, 6]; | ||
|
||
assume !(a is Nil) && !(b is Nil); | ||
|
||
a.first + b.first | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,57 @@ | ||
fun main() -> Int { | ||
let a = [1, 2, 3]; | ||
let b = [4, 5, 6]; | ||
let items = [1, 2, 3]; | ||
let bytes = "Hello, world!"; | ||
sum(...items) + (bytes is Bytes32) as Int | ||
} | ||
|
||
fun sum(...nums: Int[]) -> Int { | ||
if nums is (Int, Int[]) { | ||
nums.first + sum(...nums.rest) | ||
} else { | ||
0 | ||
} | ||
} | ||
|
||
fun bytes_guard(value: Any) -> Bool { | ||
if value is Bytes { | ||
value.length > 1 | ||
} else { | ||
false | ||
} | ||
} | ||
|
||
assume !(a is Nil) && !(b is Nil); | ||
fun bytes32_guard(value: Any) -> Bool { | ||
assert value is Bytes; | ||
|
||
if value is Bytes32 { | ||
value.length > 1 | ||
} else { | ||
false | ||
} | ||
} | ||
|
||
fun public_key_guard(value: Any) -> Bool { | ||
assert value is Bytes; | ||
|
||
if value is PublicKey { | ||
value.length > 1 | ||
} else { | ||
false | ||
} | ||
} | ||
|
||
fun inverted_guard(value: Any) -> Bool { | ||
if !(value is (Any, Any)) { | ||
value.length > 1 | ||
} else { | ||
false | ||
} | ||
} | ||
|
||
a.first + b.first | ||
fun pair_guard(value: Any) -> Int { | ||
if value is (Any, Any) { | ||
pair_guard(value.first) + pair_guard(value.rest) | ||
} else { | ||
value as Int | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
fun main() -> Int { | ||
let empty = []; | ||
let empty_hinted: Int[] = empty; | ||
|
||
let items = [1, 2, 3]; | ||
let items_hinted: Int[] = items; | ||
|
||
let spread = [...items]; | ||
let spread_hinted: Int[] = [...items]; | ||
|
||
let prepend = [0, ...items]; | ||
let prepend_hinted: Int[] = [0, ...items]; | ||
|
||
items[0] + items_hinted[1] + spread[2] + spread_hinted[0] + prepend[1] + prepend_hinted[2] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
fun main() -> Int { | ||
let pair = (20, 24); | ||
let pair_hinted: (Int, Int) = pair; | ||
|
||
pair.first + pair.rest + pair_hinted.first + pair_hinted.rest | ||
} |