Skip to content

Commit 3178a2e

Browse files
committed
add geq for value
1 parent 45f4fbd commit 3178a2e

File tree

2 files changed

+29
-16
lines changed

2 files changed

+29
-16
lines changed

lib/marlowe/extensions.ak

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
use aiken/dict.{to_list}
2+
use aiken/list
3+
use aiken/transaction/value.{Value, quantity_of, to_dict}
4+
15
pub fn insert(self: List<(a, b)>, key: a, value: b) -> List<(a, b)> {
26
when self is {
37
[] ->
@@ -47,3 +51,24 @@ pub fn remove(self: List<(a, b)>, key: a) -> List<(a, b)> {
4751
}
4852
}
4953
}
54+
55+
pub fn greater_eq_value(self: Value, other: Value) -> Bool {
56+
other
57+
|> to_dict
58+
|> to_list
59+
|> list.all(
60+
fn(other_item) {
61+
let (other_policy, other_map) = other_item
62+
63+
other_map
64+
|> to_list
65+
|> list.all(
66+
fn(token) {
67+
let (token_name, token_amount) = token
68+
69+
quantity_of(self, other_policy, token_name) >= token_amount
70+
},
71+
)
72+
},
73+
)
74+
}

validators/marlowe.ak

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@ use aiken/transaction/credential.{
1212
Address, ScriptCredential, VerificationKeyCredential,
1313
}
1414
use aiken/transaction/value.{
15-
AssetName, PolicyId, Value as ContextValue, ada_policy_id, quantity_of,
15+
AssetName, PolicyId, Value as ContextValue, quantity_of,
1616
}
17-
use marlowe/extensions
17+
use marlowe/extensions.{greater_eq_value}
1818

1919
type Datum {
2020
currency: PolicyId,
@@ -431,19 +431,7 @@ validator(validator_hash: ByteArray) {
431431
},
432432
)
433433
trace val |> cbor.diagnostic()
434-
// None of these should fail unless ada is missing
435-
expect [(p_ada_policy, p_ada_balance), ..p_rest_values] =
436-
dict.to_list(value.to_dict(val))
437-
expect [(_, p_ada_amount)] = dict.to_list(p_ada_balance)
438-
expect [(o_ada_policy, o_ada_balance), ..o_rest_values] =
439-
dict.to_list(value.to_dict(output_val))
440-
expect [(_, o_ada_amount)] = dict.to_list(o_ada_balance)
441-
expect trace_if_false(and {
442-
(o_ada_amount >= p_ada_amount)?,
443-
(p_ada_policy == ada_policy_id)?,
444-
(o_ada_policy == ada_policy_id)?,
445-
}, @"p")
446-
trace_if_false(p_rest_values == o_rest_values, @"p")
434+
trace_if_false(greater_eq_value(output_val, val), @"p")
447435
}
448436
PRole(role) -> {
449437
let hsh =
@@ -468,7 +456,7 @@ validator(validator_hash: ByteArray) {
468456
let Output { address, value, datum, .. } = output
469457
when datum is {
470458
DatumHash(svh) -> and {
471-
(val == value)?,
459+
greater_eq_value(value, val)?,
472460
(hsh == Some(svh))?,
473461
(address == addr)?,
474462
}

0 commit comments

Comments
 (0)