ligo/src/test/coase_tests.ml

237 lines
7.3 KiB
OCaml
Raw Normal View History

2019-05-13 00:56:22 +04:00
(* Copyright Coase, Inc 2019 *)
open Trace
open Test_helpers
2019-09-18 20:49:33 +04:00
let type_file = Ligo.Compile.Of_source.type_file (Syntax_name "pascaligo")
2019-05-13 00:56:22 +04:00
let get_program =
let s = ref None in
fun () -> match !s with
| Some s -> ok s
| None -> (
let%bind program = type_file "./contracts/coase.ligo" in
s := Some program ;
ok program
)
open Ast_simplified
let card owner =
ez_e_record [
2019-05-13 00:56:22 +04:00
("card_owner" , owner) ;
("card_pattern" , e_nat 0) ;
2019-05-13 00:56:22 +04:00
]
let card_ty = t_record_ez [
("card_owner" , t_address) ;
("card_pattern" , t_nat) ;
]
let card_ez owner = card (e_address owner)
2019-05-13 00:56:22 +04:00
let make_cards assoc_lst =
let card_id_ty = t_nat in
e_typed_map assoc_lst card_id_ty card_ty
2019-05-13 00:56:22 +04:00
let card_pattern (coeff , qtt) =
ez_e_record [
2019-05-13 00:56:22 +04:00
("coefficient" , coeff) ;
("quantity" , qtt) ;
]
let card_pattern_ty =
t_record_ez [
("coefficient" , t_tez) ;
("quantity" , t_nat) ;
]
let card_pattern_ez (coeff , qtt) =
2019-09-24 16:29:18 +04:00
card_pattern (e_mutez coeff , e_nat qtt)
2019-05-13 00:56:22 +04:00
let make_card_patterns lst =
let card_pattern_id_ty = t_nat in
let assoc_lst = List.mapi (fun i x -> (e_nat i , x)) lst in
e_typed_map assoc_lst card_pattern_id_ty card_pattern_ty
2019-05-13 00:56:22 +04:00
let storage cards_patterns cards next_id =
ez_e_record [
2019-05-13 00:56:22 +04:00
("cards" , cards) ;
("card_patterns" , cards_patterns) ;
("next_id" , next_id) ;
]
let storage_ez cps cs next_id =
storage (make_card_patterns cps) (make_cards cs) (e_nat next_id)
2019-05-13 00:56:22 +04:00
let cards_ez owner n =
List.mapi (fun i x -> (e_nat i , x))
2019-05-13 00:56:22 +04:00
@@ List.map card_ez
@@ List.map (Function.constant owner)
@@ List.range n
let (first_owner , first_contract) =
2019-05-15 15:44:05 +04:00
let open Proto_alpha_utils.Memory_proto_alpha in
2019-05-13 00:56:22 +04:00
let id = List.nth dummy_environment.identities 0 in
let kt = id.implicit_contract in
2019-09-06 06:02:18 +04:00
Protocol.Alpha_context.Contract.to_b58check kt , kt
2019-05-13 00:56:22 +04:00
let second_owner =
2019-05-15 15:44:05 +04:00
let open Proto_alpha_utils.Memory_proto_alpha in
2019-05-13 00:56:22 +04:00
let id = List.nth dummy_environment.identities 1 in
let kt = id.implicit_contract in
2019-09-06 06:02:18 +04:00
Protocol.Alpha_context.Contract.to_b58check kt
2019-05-13 00:56:22 +04:00
let basic a b cards next_id =
let card_patterns = List.map card_pattern_ez [
(100 , a) ;
(20 , b) ;
] in
storage_ez card_patterns cards next_id
let buy () =
let%bind program = get_program () in
let%bind () =
let make_input = fun n ->
let buy_action = ez_e_record [
("card_to_buy" , e_nat 0) ;
2019-05-13 00:56:22 +04:00
] in
let storage = basic 100 1000 (cards_ez first_owner n) (2 * n) in
e_pair buy_action storage
2019-05-13 00:56:22 +04:00
in
let make_expected = fun n ->
let ops = e_typed_list [] t_operation in
2019-05-13 00:56:22 +04:00
let storage =
let cards =
cards_ez first_owner n @
[(e_nat (2 * n) , card (e_address second_owner))]
2019-05-13 00:56:22 +04:00
in
basic 101 1000 cards ((2 * n) + 1) in
e_pair ops storage
2019-05-13 00:56:22 +04:00
in
let%bind () =
let%bind amount =
trace_option (simple_error "getting amount for run") @@
2019-09-06 06:02:18 +04:00
Memory_proto_alpha.Protocol.Alpha_context.Tez.of_mutez @@ Int64.of_int 10000000000 in
2019-05-15 15:44:05 +04:00
let options = Proto_alpha_utils.Memory_proto_alpha.make_options ~amount () in
2019-05-13 00:56:22 +04:00
expect_eq_n_pos_small ~options program "buy_single" make_input make_expected in
let%bind () =
let%bind amount =
trace_option (simple_error "getting amount for run") @@
2019-09-06 06:02:18 +04:00
Memory_proto_alpha.Protocol.Alpha_context.Tez.of_mutez @@ Int64.of_int 0 in
2019-05-15 15:44:05 +04:00
let options = Proto_alpha_utils.Memory_proto_alpha.make_options ~amount () in
2019-05-13 00:56:22 +04:00
trace_strong (simple_error "could buy without money") @@
Assert.assert_fail
@@ expect_eq_n_pos_small ~options program "buy_single" make_input make_expected in
ok ()
in
ok ()
let dispatch_buy () =
let%bind program = get_program () in
let%bind () =
let make_input = fun n ->
let buy_action = ez_e_record [
("card_to_buy" , e_nat 0) ;
2019-05-13 00:56:22 +04:00
] in
let action = e_constructor "Buy_single" buy_action in
2019-05-13 00:56:22 +04:00
let storage = basic 100 1000 (cards_ez first_owner n) (2 * n) in
e_pair action storage
2019-05-13 00:56:22 +04:00
in
let make_expected = fun n ->
let ops = e_typed_list [] t_operation in
2019-05-13 00:56:22 +04:00
let storage =
let cards =
cards_ez first_owner n @
[(e_nat (2 * n) , card (e_address second_owner))]
2019-05-13 00:56:22 +04:00
in
basic 101 1000 cards ((2 * n) + 1) in
e_pair ops storage
2019-05-13 00:56:22 +04:00
in
let%bind () =
let%bind amount =
trace_option (simple_error "getting amount for run") @@
2019-09-06 06:02:18 +04:00
Memory_proto_alpha.Protocol.Alpha_context.Tez.of_mutez @@ Int64.of_int 10000000000 in
2019-05-15 15:44:05 +04:00
let options = Proto_alpha_utils.Memory_proto_alpha.make_options ~amount () in
2019-05-13 00:56:22 +04:00
expect_eq_n_pos_small ~options program "main" make_input make_expected in
let%bind () =
let%bind amount =
trace_option (simple_error "getting amount for run") @@
2019-09-06 06:02:18 +04:00
Memory_proto_alpha.Protocol.Alpha_context.Tez.of_mutez @@ Int64.of_int 0 in
2019-05-15 15:44:05 +04:00
let options = Proto_alpha_utils.Memory_proto_alpha.make_options ~amount () in
2019-05-13 00:56:22 +04:00
trace_strong (simple_error "could buy without money") @@
Assert.assert_fail
@@ expect_eq_n_pos_small ~options program "buy_single" make_input make_expected in
ok ()
in
ok ()
let transfer () =
let%bind program = get_program () in
let%bind () =
let make_input = fun n ->
let transfer_action = ez_e_record [
("card_to_transfer" , e_nat 0) ;
("destination" , e_address second_owner) ;
2019-05-13 00:56:22 +04:00
] in
let storage = basic 100 1000 (cards_ez first_owner n) (2 * n) in
e_pair transfer_action storage
2019-05-13 00:56:22 +04:00
in
let make_expected = fun n ->
let ops = e_typed_list [] t_operation in
2019-05-13 00:56:22 +04:00
let storage =
let cards =
let new_card = card_ez second_owner in
let old_cards = cards_ez first_owner n in
(e_nat 0 , new_card) :: (List.tl old_cards)
2019-05-13 00:56:22 +04:00
in
basic 100 1000 cards (2 * n) in
e_pair ops storage
2019-05-13 00:56:22 +04:00
in
let%bind () =
2019-09-06 06:02:18 +04:00
let amount = Memory_proto_alpha.Protocol.Alpha_context.Tez.zero in
2019-05-13 00:56:22 +04:00
let payer = first_contract in
2019-05-15 15:44:05 +04:00
let options = Proto_alpha_utils.Memory_proto_alpha.make_options ~amount ~payer () in
2019-05-13 00:56:22 +04:00
expect_eq_n_strict_pos_small ~options program "transfer_single" make_input make_expected in
ok ()
in
ok ()
let sell () =
let%bind program = get_program () in
let%bind () =
let make_input = fun n ->
let sell_action = ez_e_record [
("card_to_sell" , e_nat (n - 1)) ;
2019-05-13 00:56:22 +04:00
] in
let cards = cards_ez first_owner n in
let storage = basic 100 1000 cards (2 * n) in
e_pair sell_action storage
2019-05-13 00:56:22 +04:00
in
let make_expecter : int -> expression -> unit result = fun n result ->
2019-09-10 14:42:49 +04:00
let%bind (ops , storage) = get_e_pair result.expression in
2019-05-13 00:56:22 +04:00
let%bind () =
2019-09-10 14:42:49 +04:00
let%bind lst = get_e_list ops.expression in
2019-05-13 00:56:22 +04:00
Assert.assert_list_size lst 1 in
let expected_storage =
let cards = List.hds @@ cards_ez first_owner n in
basic 99 1000 cards (2 * n) in
2019-06-11 02:06:00 +04:00
Ast_simplified.Misc.assert_value_eq (expected_storage , storage)
2019-05-13 00:56:22 +04:00
in
let%bind () =
2019-09-06 06:02:18 +04:00
let amount = Memory_proto_alpha.Protocol.Alpha_context.Tez.zero in
2019-05-13 00:56:22 +04:00
let payer = first_contract in
2019-05-15 15:44:05 +04:00
let options = Proto_alpha_utils.Memory_proto_alpha.make_options ~amount ~payer () in
2019-05-13 00:56:22 +04:00
expect_n_strict_pos_small ~options program "sell_single" make_input make_expecter in
ok ()
in
ok ()
2019-06-05 10:43:33 +04:00
let main = test_suite "Coase (End to End)" [
2019-05-13 00:56:22 +04:00
test "buy" buy ;
test "dispatch buy" dispatch_buy ;
test "transfer" transfer ;
test "sell" sell ;
]