Skip to content

Commit

Permalink
feat: implement geq for values
Browse files Browse the repository at this point in the history
  • Loading branch information
euonymos committed Sep 18, 2024
1 parent 1d748b7 commit 8e315ee
Showing 1 changed file with 69 additions and 11 deletions.
80 changes: 69 additions & 11 deletions validators/bet_ref.ak
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
use aiken/collection/list.{filter, find, push}
use aiken/collection/dict.{Dict, foldl, map, union_with, values}
use aiken/collection/list.{all, concat, filter, find, push}
use aiken/interval.{
Finite, Interval, IntervalBound, NegativeInfinity, PositiveInfinity, before,
contains,
}
use cardano/address.{Address}
use cardano/assets.{Value, merge, negate}
use cardano/transaction.{
Datum, InlineDatum, Output, OutputReference, Transaction,
}
use cardano/assets.{AssetName, PolicyId, Value, merge, negate, to_dict}
use cardano/transaction.{InlineDatum, Output, OutputReference, Transaction}
use types.{
Bet, BetRefAction, BetRefDatum, BetRefParams, OracleAnswerDatum, POSIXTime,
Take,
Expand All @@ -21,7 +19,7 @@ validator bet_ref(params: BetRefParams) {
utxo: OutputReference,
self: Transaction,
) {
let BetRefParams(oracle_pkh, bet_until, bet_reveal, bet_step) = params
let BetRefParams(_oracle_pkh, bet_until, _bet_reveal, bet_step) = params
when brAction is {
Bet(guess) -> {
trace @"Previous guess should exist"
Expand Down Expand Up @@ -67,7 +65,7 @@ fn handle_bet(
trace @"The current bet must be more than the previous bet by at least `bet_step` amount"
//FIXME: this is not very clear to me
let min_out_value = in_value |> merge(prev_bet_value) |> merge(bet_step)
expect out_value |> value_geq(min_out_value)
expect out_value |> geq(min_out_value)

trace @"Out bet value is wrong"
let expected_in_value = out_value |> merge(negate(bet_value))
Expand Down Expand Up @@ -136,7 +134,7 @@ fn interval_contains(container: Interval<Int>, containee: Interval<Int>) -> Bool
let IntervalBound(ee_lower_bound_type, _is_inclusive) = ee_lower_bound
let IntervalBound(er_upper_bound_type, er_upper_bound_is_inclusive) =
er_upper_bound
let IntervalBound(ee_upper_bound_type, _is_inclusive) = er_upper_bound
let IntervalBound(ee_upper_bound_type, _is_inclusive) = ee_upper_bound
// Lower bound
let lower_check =
when (er_lower_bound_type, ee_lower_bound_type) is {
Expand Down Expand Up @@ -170,6 +168,66 @@ fn interval_contains(container: Interval<Int>, containee: Interval<Int>) -> Bool
lower_check && upper_check
}

fn value_geq(a: Value, b: Value) -> Bool {
todo
// Some partial order operations over Value

type These<a, b> {
This(a)
That(b)
These(a, b)
}

fn combine(l: These<a, b>, r: These<a, b>) -> These<a, b> {
when (l, r) is {
(This(a), That(b)) -> These(a, b)
(That(b), This(a)) -> These(a, b)
_ -> fail
}
}

fn check_bin_rel(f: fn(Int, Int) -> Bool, l: Value, r: Value) {
let un_these =
fn(these: These<Int, Int>) -> Bool {
when these is {
This(a) -> f(a, 0)
That(b) -> f(0, b)
These(a, b) -> f(a, b)
}
}
check_pred(un_these, l, r)
}

fn check_pred(pred: fn(These<Int, Int>) -> Bool, l: Value, r: Value) -> Bool {
// Helper function to map values into combinable format
let mk_these =
fn(con: fn(Int) -> These<Int, Int>) -> fn(ByteArray, Dict<AssetName, Int>) ->
Dict<AssetName, These<Int, Int>> {
fn(_, d) { d |> map(fn(_, i) { con(i) }) }
}
// Map over dictiorinized values
let ld: Dict<PolicyId, Dict<AssetName, These<Int, Int>>> =
l |> to_dict |> map(mk_these(This))
let rd: Dict<PolicyId, Dict<AssetName, These<Int, Int>>> =
r |> to_dict |> map(mk_these(That))

// Build union
let u: Dict<PolicyId, Dict<AssetName, These<Int, Int>>> =
union_with(
ld,
rd,
fn(_, li, ri) {
Some(
union_with(li, ri, fn(_, this, that) { Some(combine(this, that)) }),
)
},
)
// Make a list of all values
let l: List<These<Int, Int>> =
u |> foldl([], fn(_, inner, res) { res |> concat(values(inner)) })

// Check the predicate
l |> all(pred)
}

fn geq(a: Value, b: Value) -> Bool {
check_bin_rel(>=, a, b)
}

0 comments on commit 8e315ee

Please sign in to comment.