Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Predictive Market #90

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion betting/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@ __pycache__

src/compiled/*
deploy/*.js
deployments/*
deployments/*
deploy/node_modules
2 changes: 1 addition & 1 deletion betting/Makefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
LIGO=docker run --platform linux/amd64 --rm -v "$(PWD)":"$(PWD)" -w "$(PWD)" ligolang/ligo:0.50.0
LIGO=docker run --platform linux/amd64 --rm -v "$(PWD)":"$(PWD)" -w "$(PWD)" ligolang/ligo:0.51.0
PROTOCOL_OPT=--protocol jakarta
JSON_OPT=--michelson-format json

Expand Down
2 changes: 1 addition & 1 deletion betting/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ type bet_config_type = {
3) Add an Event using the `storage.manager` address
4) Add a Bet to the Event using an address that is not `storage.manager` nor `storage.oracle_address`
5) _(optional)_ Add more bets to the first team or second team on the Event
6) Update the Bet to specify the outcome in `is_draw`, and the winning Team in `is_team_one_win` if it is not a draw, using `storage.manager` or `storage.oracle_address`
6) Update the Bet to specify the outcome in `game_status` if it is not a draw, using `storage.manager` or `storage.oracle_address`
7) Finalize the Bet using `storage.manager`

## Initial Storage example :
Expand Down
4 changes: 1 addition & 3 deletions betting/deploy/deploy_callback_betting.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,7 @@ let store = {
end_at: 1660741034 + 3600,
modified_at: 1660741034,
opponents: { team_one: '', team_two: '' },
is_finalized : false,
is_draw: false,
is_team_one_win: false,
game_status : Ongoing,
start_bet_time: 1660741034 + 1200,
closed_bet_time: 1660741034 + 2400,
bets_team_one: (new MichelsonMap()),
Expand Down
4 changes: 1 addition & 3 deletions betting/deploy/deploy_callback_oracle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,7 @@ let store = {
end_at: 1660741034 + 3600,
modified_at: 1660741034,
opponents: { team_one: '', team_two: '' },
is_finalized: false,
is_draw: false,
is_team_one_win: false,
game_status: Ongoing,
metadata: (MichelsonMap.fromLiteral({
'': char2Bytes('tezos-storage:contents'),
contents: char2Bytes(JSON.stringify(metadataJson))
Expand Down
18 changes: 9 additions & 9 deletions betting/src/contracts/cameligo/betting/assert.mligo
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
#import "errors.mligo" "Errors"

// --------------------------------------
// CONFIG RELATED AssertIONS
// CONFIG RELATED ASSERTIONS
// --------------------------------------

let is_manager (p_sender : address)(p_manager : address) : unit =
Expand All @@ -26,7 +26,7 @@ let not_previous_oracle (p_new_oracle : address)(p_prev_oracle : address) : unit
then failwith Errors.same_previous_oracle_address

// --------------------------------------
// EVENT RELATED AssertIONS
// EVENT RELATED ASSERTIONS
// --------------------------------------

let event_creation_not_paused (p_event_creation_paused : bool) : unit =
Expand All @@ -50,20 +50,20 @@ let event_bet_ends_after_end (p_event_bet_end : timestamp) (p_event_end : timest
then failwith Errors.event_betting_end_after_end

// --------------------------------------
// BETTING RELATED AssertIONS
// BETTING RELATED ASSERTIONS
// --------------------------------------

let betting_not_paused (p_betting_paused : bool) : unit =
if (p_betting_paused)
then failwith Errors.betting_paused

let betting_not_finalized (p_betting_finalized : bool) : unit =
if (p_betting_finalized)
then failwith Errors.bet_finished
let betting_not_finalized (p_s : Types.game_status) : unit = match p_s with
| Ongoing -> ()
| _ -> failwith Errors.bet_finished

let betting_finalized (p_betting_finalized : bool) : unit =
if (not p_betting_finalized)
then failwith Errors.bet_not_finished
let betting_finalized (p_s : Types.game_status) : unit = match p_s with
| Ongoing -> failwith Errors.bet_not_finished
| _ -> ()

let no_tez (p_asserted_amount : tez) : unit =
if (p_asserted_amount = 0mutez)
Expand Down
12 changes: 3 additions & 9 deletions betting/src/contracts/cameligo/betting/callback/main.mligo
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,7 @@ type storage =
end_at : timestamp;
modified_at : timestamp;
opponents : { team_one : string; team_two : string};
is_finalized : bool;
is_draw : bool option;
is_team_one_win : bool option;
game_status : BETTING_Types.game_status;
start_bet_time : timestamp;
closed_bet_time : timestamp;
bets_team_one : (address, tez) map;
Expand All @@ -30,9 +28,7 @@ type requested_event_param = [@layout:comb] {
end_at : timestamp;
modified_at : timestamp;
opponents : { team_one : string; team_two : string};
is_finalized : bool;
is_draw : bool option;
is_team_one_win : bool option;
game_status : BETTING_Types.game_status;
start_bet_time : timestamp;
closed_bet_time : timestamp;
bets_team_one : (address, tez) map;
Expand All @@ -54,9 +50,7 @@ let saveEvent(param, store : requested_event_param * storage) : operation list *
end_at=param.end_at;
modified_at=param.modified_at;
opponents=param.opponents;
is_finalized=param.is_finalized;
is_draw=param.is_draw;
is_team_one_win=param.is_team_one_win;
game_status=param.game_status;
start_bet_time=param.start_bet_time;
closed_bet_time=param.closed_bet_time;
bets_team_one=param.bets_team_one;
Expand Down
51 changes: 17 additions & 34 deletions betting/src/contracts/cameligo/betting/main.mligo
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,11 @@ let change_oracle_address (p_new_oracle_address : address)(s : Types.storage) :

let switch_pause_event_creation (s : Types.storage) : (operation list * Types.storage) =
let _ = Assert.is_manager (Tezos.get_sender()) s.manager in
if (s.bet_config.is_event_creation_paused)
then (([] : operation list), {s with bet_config.is_event_creation_paused = false})
else (([] : operation list), {s with bet_config.is_event_creation_paused = true})
(([] : operation list), {s with bet_config.is_event_creation_paused = (not s.bet_config.is_event_creation_paused)})

let switch_pause_betting (s : Types.storage) : (operation list * Types.storage) =
let _ = Assert.is_manager (Tezos.get_sender()) s.manager in
if (s.bet_config.is_betting_paused)
then (([] : operation list), {s with bet_config.is_betting_paused = false})
else (([] : operation list), {s with bet_config.is_betting_paused = true})
(([] : operation list), {s with bet_config.is_betting_paused = (not s.bet_config.is_betting_paused)})

let update_config_type (p_new_bet_config : Types.bet_config_type)(s : Types.storage) : (operation list * Types.storage) =
let _ = Assert.is_manager (Tezos.get_sender()) s.manager in
Expand All @@ -54,9 +50,7 @@ let add_event (p_new_event : Types.add_event_parameter)(s : Types.storage) : (op
end_at = p_new_event.end_at;
modified_at = p_new_event.modified_at;
opponents = p_new_event.opponents;
is_finalized = p_new_event.is_finalized;
is_draw = p_new_event.is_draw;
is_team_one_win = p_new_event.is_team_one_win;
game_status = Ongoing;
start_bet_time = p_new_event.start_bet_time;
closed_bet_time = p_new_event.closed_bet_time;
is_claimed = False } in
Expand Down Expand Up @@ -88,9 +82,7 @@ let get_event (requested_event_id : nat)(callbackAddr : address)(s : Types.stora
end_at = cbk_event.end_at;
modified_at = cbk_event.modified_at;
opponents = { team_one = cbk_event.opponents.team_one; team_two = cbk_event.opponents.team_two};
is_finalized = cbk_event.is_finalized;
is_draw = cbk_event.is_draw;
is_team_one_win = cbk_event.is_team_one_win;
game_status = cbk_event.game_status;
start_bet_time = cbk_event.start_bet_time;
closed_bet_time = cbk_event.closed_bet_time;
bets_team_one = cbk_eventbet.bets_team_one;
Expand Down Expand Up @@ -119,7 +111,7 @@ let update_event (updated_event_id : nat)(updated_event : Types.event_type)(s :
| Some event -> event
| None -> (failwith Errors.no_event_id)
in
let _ = Assert.betting_not_finalized (requested_event.is_finalized) in
let _ = Assert.betting_not_finalized (requested_event.game_status) in
let new_events : (nat, Types.event_type) big_map = Big_map.update updated_event_id (Some(updated_event)) s.events in
(([] : operation list), {s with events = new_events})

Expand Down Expand Up @@ -177,7 +169,7 @@ let add_bet (p_requested_event_id : nat)(team_one_bet : bool)(s : Types.storage)
| Some event -> event
| None -> failwith Errors.no_event_id
in
let _ = Assert.betting_not_finalized (requested_event.is_finalized) in
let _ = Assert.betting_not_finalized (requested_event.game_status) in
let _ = Assert.betting_before_period_start (requested_event.start_bet_time) in
let _ = Assert.betting_after_period_end (requested_event.closed_bet_time) in
let requested_event_bets : Types.event_bets = match (Big_map.find_opt p_requested_event_id s.events_bets) with
Expand All @@ -191,7 +183,6 @@ let add_bet (p_requested_event_id : nat)(team_one_bet : bool)(s : Types.storage)
let new_events_map : (nat, Types.event_bets) big_map = (Big_map.update p_requested_event_id (Some(updated_bet_event)) s.events_bets) in
(([] : operation list), {s with events_bets = new_events_map;})


let make_transfer_op (addr : address ) ( value_won : tez ) (profit_quota : nat): operation =
let quota_to_send : nat = abs(100n - profit_quota) in
let value_to_send : tez = value_won * quota_to_send / 100n in
Expand All @@ -202,7 +193,7 @@ let make_transfer_op (addr : address ) ( value_won : tez ) (profit_quota : nat):
in
Tezos.transaction unit value_to_send destination

let compose_paiement ( winner_map : (address, tez) map ) ( total_value_bet : tez ) ( total_value_won : tez ) (profit_quota : nat) : operation list =
let compose_payment ( winner_map : (address, tez) map ) ( total_value_bet : tez ) ( total_value_won : tez ) (profit_quota : nat) : operation list =
let folded_op_list = fun (op_list, (address, bet_amount) : operation list * (address * tez) ) ->
let won_reward_percentage : nat = 10000000n * bet_amount / total_value_bet in
let added_reward : tez = total_value_won * won_reward_percentage / 10000000n in
Expand All @@ -228,9 +219,9 @@ let resolve_team_win (event_bets : Types.event_bets) (is_team_one_win : bool) (p
then refund_bet event_bets 0n
else if (is_team_one_win)
then
compose_paiement event_bets.bets_team_one event_bets.bets_team_one_total event_bets.bets_team_two_total profit_quota
compose_payment event_bets.bets_team_one event_bets.bets_team_one_total event_bets.bets_team_two_total profit_quota
else
compose_paiement event_bets.bets_team_two event_bets.bets_team_two_total event_bets.bets_team_one_total profit_quota
compose_payment event_bets.bets_team_two event_bets.bets_team_two_total event_bets.bets_team_one_total profit_quota

let finalize_bet (p_requested_event_id : nat)(s : Types.storage) : (operation list * Types.storage) =
let _ = Assert.is_manager (Tezos.get_sender()) s.manager in
Expand All @@ -239,29 +230,21 @@ let finalize_bet (p_requested_event_id : nat)(s : Types.storage) : (operation li
| None -> failwith Errors.no_event_id
in
let _check_is_claimed : unit = assert_with_error (requested_event.is_claimed = False) Errors.event_already_claimed in
let _ = Assert.betting_finalized (requested_event.is_finalized) in
let _ = Assert.betting_finalized (requested_event.game_status) in
let event_bets : Types.event_bets = match (Big_map.find_opt p_requested_event_id s.events_bets) with
| Some event -> event
| None -> failwith Errors.no_event_bets
in
let _ = Assert.finalizing_before_period_end (requested_event.end_at) in
let outcome_draw : bool = match requested_event.is_draw with
| Some x -> x
| None -> failwith Errors.bet_no_team_outcome
in
let profit_quota : nat = s.bet_config.retained_profit_quota in
let modified_events = Big_map.update p_requested_event_id (Some({ requested_event with is_claimed = True })) s.events in
if outcome_draw
then
let op_list = refund_bet event_bets profit_quota in
((op_list : operation list), { s with events = modified_events })
else
let is_team_one_win : bool = match requested_event.is_team_one_win with
| Some x -> x
| None -> failwith Errors.bet_no_team_outcome
in
let op_list = resolve_team_win event_bets is_team_one_win profit_quota in
((op_list : operation list), { s with events = modified_events })
let op_list : operation list = match requested_event.game_status with
| Ongoing -> failwith Errors.bet_no_team_outcome
| Team1Win -> resolve_team_win event_bets true profit_quota
| Team2Win -> resolve_team_win event_bets false profit_quota
| Draw -> refund_bet event_bets profit_quota
in
((op_list : operation list), { s with events = modified_events })

// --------------------------------------
// MAIN FUNCTION
Expand Down
10 changes: 4 additions & 6 deletions betting/src/contracts/cameligo/betting/types.mligo
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ type bet_config_type = {
retained_profit_quota : nat;
}

type game_status = Ongoing | Team1Win| Team2Win | Draw

type event_type =
[@layout:comb] {
name : string;
Expand All @@ -17,9 +19,7 @@ type event_type =
end_at : timestamp;
modified_at : timestamp;
opponents : { team_one : string; team_two : string};
is_finalized : bool;
is_draw : bool option;
is_team_one_win : bool option;
game_status : game_status;
start_bet_time : timestamp;
closed_bet_time : timestamp;
is_claimed : bool;
Expand Down Expand Up @@ -75,9 +75,7 @@ type add_event_parameter =
end_at : timestamp;
modified_at : timestamp;
opponents : { team_one : string; team_two : string};
is_finalized : bool;
is_draw : bool option;
is_team_one_win : bool option;
game_status : game_status;
start_bet_time : timestamp;
closed_bet_time : timestamp;
}
Expand Down
14 changes: 5 additions & 9 deletions betting/src/contracts/cameligo/oracle/callback/main.mligo
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
type game_status = Ongoing | Team1Win| Team2Win | Draw

type storage =
[@layout:comb] {
name : string;
Expand All @@ -6,9 +8,7 @@ type storage =
end_at : timestamp;
modified_at : timestamp;
opponents : { team_one : string; team_two : string};
is_finalized : bool;
is_draw : bool option;
is_team_one_win : bool option;
game_status : game_status;
metadata : (string, bytes) map;
}

Expand All @@ -20,9 +20,7 @@ type requested_event_param =
end_at : timestamp;
modified_at : timestamp;
opponents : { team_one : string; team_two : string};
is_finalized : bool;
is_draw : bool option;
is_team_one_win : bool option;
game_status : game_status;
}

type parameter = SaveEvent of requested_event_param | Nothing of unit
Expand All @@ -35,9 +33,7 @@ let saveEvent(param, store : requested_event_param * storage) : operation list *
end_at = param.end_at;
modified_at = param.modified_at;
opponents = param.opponents;
is_finalized = param.is_finalized;
is_draw = param.is_draw;
is_team_one_win = param.is_team_one_win;
game_status = param.game_status;
})

let main ((p, s):(parameter * storage)) : operation list * storage =
Expand Down
8 changes: 3 additions & 5 deletions betting/src/contracts/cameligo/oracle/main.mligo
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,7 @@ let change_manager (new_manager : address)( s : Types.storage) : (operation list

let switch_pause (s : Types.storage) : (operation list * Types.storage) =
let _ = Assert.is_manager (Tezos.get_sender()) s.manager in
if (s.isPaused)
then (([] : operation list), {s with isPaused = false})
else (([] : operation list), {s with isPaused = true})
(([] : operation list), {s with isPaused = (not s.isPaused)})

let change_signer (new_signer : address)( s : Types.storage) : (operation list * Types.storage) =
let _ = Assert.is_manager__or_signer (Tezos.get_sender()) s.manager s.signer in
Expand All @@ -32,7 +30,7 @@ let get_event (requested_event_id : nat)(callbackAddr : address)(s : Types.stora
in
let destination : Callback.requested_event_param contract =
match (Tezos.get_entrypoint_opt "%saveEvent" callbackAddr : Callback.requested_event_param contract option) with
| None -> failwith("Unknown contract")
None -> failwith("Unknown contract")
| Some ctr -> ctr
in
let op : operation = Tezos.transaction cbk_event 0mutez destination in
Expand Down Expand Up @@ -75,7 +73,7 @@ let getStatus (_, s : unit * Types.storage) : timestamp * bool =
[@view]
let getEvent (pRequestedEventID, s : nat * Types.storage) : timestamp * Types.event_type =
let requestedEvent : Types.event_type = match (Map.find_opt pRequestedEventID s.events) with
| Some event -> event
Some event -> event
| None -> failwith Errors.no_event_id
in
(Tezos.get_now(), requestedEvent)
6 changes: 3 additions & 3 deletions betting/src/contracts/cameligo/oracle/types.mligo
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
type game_status = Ongoing | Team1Win| Team2Win | Draw

type event_type =
[@layout:comb] {
name : string;
Expand All @@ -6,9 +8,7 @@ type event_type =
end_at : timestamp;
modified_at : timestamp;
opponents : { team_one : string; team_two : string};
is_finalized : bool;
is_draw : bool option;
is_team_one_win : bool option;
game_status : game_status;
}

type storage =
Expand Down
4 changes: 1 addition & 3 deletions betting/test/betting/helpers/assert.mligo
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,5 @@ let event (taddr : (Betting_Callback.parameter, Betting_Callback.storage) typed_
let () = assert(storage.end_at=expected_event.end_at) in
let () = assert(storage.modified_at=expected_event.modified_at) in
let () = assert(storage.opponents=expected_event.opponents) in
let () = assert(storage.is_finalized=expected_event.is_finalized) in
let () = assert(storage.is_draw=expected_event.is_draw) in
let () = assert(storage.is_team_one_win=expected_event.is_team_one_win) in
let () = assert(storage.game_status=expected_event.game_status) in
()
Loading