Add tests for CameLIGO.

This commit is contained in:
Sander Spies 2020-06-05 12:51:37 +02:00
parent 54cefddd51
commit 623e6f1251
35 changed files with 1137 additions and 426 deletions

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,136 @@
type tokens = (address, nat) big_map
type allowances = (address * address, nat) big_map
type storage =
{tokens : tokens;
allowances : allowances;
total_amount : nat}
type transfer =
{address_from : address;
address_to : address;
value : nat}
type approve = {spender : address; value : nat}
type getAllowance =
{owner : address;
spender : address;
callback : nat contract}
type getBalance = {owner : address; callback : nat contract}
type getTotalSupply = {callback : nat contract}
type action =
Transfer of transfer
| Approve of approve
| GetAllowance of getAllowance
| GetBalance of getBalance
| GetTotalSupply of getTotalSupply
let transfer (p, s : transfer * storage)
: operation list * storage =
let new_allowances =
if Tezos.sender = p.address_from
then s.allowances
else
let authorized_value =
match Big_map.find_opt
(Tezos.sender, p.address_from)
s.allowances
with
Some value -> value
| None -> 0n
in if (authorized_value < p.value)
then (failwith "Not Enough Allowance" : allowances)
else
Big_map.update
(Tezos.sender, p.address_from)
(Some (abs (authorized_value - p.value)))
s.allowances
in let sender_balance =
match Big_map.find_opt p.address_from s.tokens with
Some value -> value
| None -> 0n
in if (sender_balance < p.value)
then
(failwith "Not Enough Balance"
: operation list * storage)
else
let new_tokens =
Big_map.update
p.address_from
(Some (abs (sender_balance - p.value)))
s.tokens
in let receiver_balance =
match Big_map.find_opt p.address_to s.tokens
with
Some value -> value
| None -> 0n
in let new_tokens =
Big_map.update
p.address_to
(Some (receiver_balance + p.value))
new_tokens
in ([] : operation list),
{s with
tokens = new_tokens;
allowances = new_allowances}
let approve (p, s : approve * storage)
: operation list * storage =
let previous_value =
match Big_map.find_opt
(p.spender, Tezos.sender)
s.allowances
with
Some value -> value
| None -> 0n
in if previous_value > 0n && p.value > 0n
then
(failwith "Unsafe Allowance Change"
: operation list * storage)
else
let new_allowances =
Big_map.update
(p.spender, Tezos.sender)
(Some (p.value))
s.allowances
in ([] : operation list),
{s with
allowances = new_allowances}
let getAllowance (p, s : getAllowance * storage)
: operation list * storage =
let value =
match Big_map.find_opt (p.owner, p.spender) s.allowances
with
Some value -> value
| None -> 0n
in let op = Tezos.transaction value 0mutez p.callback
in ([op], s)
let getBalance (p, s : getBalance * storage)
: operation list * storage =
let value =
match Big_map.find_opt p.owner s.tokens with
Some value -> value
| None -> 0n
in let op = Tezos.transaction value 0mutez p.callback
in ([op], s)
let getTotalSupply (p, s : getTotalSupply * storage)
: operation list * storage =
let total = s.total_amount
in let op = Tezos.transaction total 0mutez p.callback
in ([op], s)
let main (a, s : action * storage) =
match a with
Transfer p -> transfer (p, s)
| Approve p -> approve (p, s)
| GetAllowance p -> getAllowance (p, s)
| GetBalance p -> getBalance (p, s)
| GetTotalSupply p -> getTotalSupply (p, s)

View File

@ -0,0 +1,3 @@
let main (p : key_hash) =
let c : unit contract = Tezos.implicit_account p
in Tezos.address c

View File

@ -0,0 +1,2 @@
let check_ (p : unit) : int =
if Tezos.amount = 100000000mutez then 42 else 0

View File

@ -0,0 +1,10 @@
let f1 (x : unit) : unit -> tez =
let amt : tez = Current.amount
in fun (x : unit) -> amt
let f2 (x : unit) : unit -> tez =
fun (x : unit) -> Current.amount
let main (b, s : bool * (unit -> tez))
: operation list * (unit -> tez) =
(([] : operation list), (if b then f1 () else f2 ()))

View File

@ -0,0 +1,17 @@
let mod_op (n : int) : nat = n mod 42
let plus_op (n : int) : int = n + 42
let minus_op (n : int) : int = n - 42
let times_op (n : int) : int = n * 42
let div_op (n : int) : int = n / 2
let neg_op (n : int) : int = -n
let foo (n : int) : int = n + 10
let neg_op_2 (b : int) : int = -(foo b)
let ediv_op (n : int) : (int * nat) option = ediv n 2

View File

@ -0,0 +1,3 @@
let main (p, s : bool * unit) =
let u : unit = assert p
in ([] : operation list), s

View File

@ -0,0 +1,14 @@
let x = 1 [@@inline]
let foo (a : int) : int =
(let test = 2 + a [@@inline]
in test) [@@inline]
let y = 1 [@@inline][@@other]
let bar (b : int) : int =
let test = fun (z : int) -> 2 + b + z
[@@inline]
[@@foo]
[@@bar]
in test b

View File

@ -0,0 +1,8 @@
type parameter = unit
type storage = tez
type return = operation list * storage
let main (p, s : parameter * storage) : return =
([] : operation list), Tezos.balance

View File

@ -0,0 +1,3 @@
type toto = int
let foo : toto = 42 + 127

View File

@ -0,0 +1,22 @@
type foo = (int, int) big_map
let set_ (n, m : int * foo) : foo =
Big_map.update 23 (Some n) m
let add (n, m : int * foo) : foo = Big_map.add 23 n m
let rm (m : foo) : foo = Big_map.remove 42 m
let gf (m : foo) : int = Big_map.find 23 m
let get (m : foo) : int option = Big_map.find_opt 42 m
let empty_map : foo = Big_map.empty
let map1 : foo = Big_map.literal [(23, 0); (42, 0)]
let map1 : foo = Big_map.literal [(23, 0); (42, 0)]
let mutimaps (m : foo) (n : foo) : foo =
let bar : foo = Big_map.update 42 (Some 0) m
in Big_map.update 42 (get bar) n

View File

@ -0,0 +1,9 @@
let or_op (n : nat) : nat = Bitwise.or n 4n
let and_op (n : nat) : nat = Bitwise.and n 7n
let xor_op (n : nat) : nat = Bitwise.xor n 7n
let lsl_op (n : nat) : nat = Bitwise.shift_left n 7n
let lsr_op (n : nat) : nat = Bitwise.shift_right n 7n

View File

@ -0,0 +1,9 @@
let or_true (b : bool) : bool = b || true
let or_false (b : bool) : bool = b || false
let and_true (b : bool) : bool = b && true
let and_false (b : bool) : bool = b && false
let not_bool (b : bool) : bool = not b

View File

@ -0,0 +1,5 @@
let concat_op (s : bytes) : bytes = Bytes.concat s 0x7070
let slice_op (s : bytes) : bytes = Bytes.sub 1n 2n s
let hasherman (s : bytes) : bytes = Crypto.sha256 s

View File

@ -0,0 +1,11 @@
let id_string (p : string) : string option =
let packed : bytes = Bytes.pack p
in (Bytes.unpack packed : string option)
let id_int (p : int) : int option =
let packed : bytes = Bytes.pack p
in (Bytes.unpack packed : int option)
let id_address (p : address) : address option =
let packed : bytes = Bytes.pack p
in (Bytes.unpack packed : address option)

View File

@ -0,0 +1,11 @@
let check_signature
(pk, signed, msg : key * signature * bytes) : bool =
Crypto.check pk signed msg
let example : bool =
Crypto.check
("edpktz4xg6csJnJ5vcmMb2H37sWXyBDcoAp3XrBvjRaTSQ1zmZTeRQ"
: key)
("edsigtnzKd51CDomKVMFBoU8SzFZgNqRkYUaQH4DLUg8Lsimz98DFB82uiHAkdvx29DDqHxPf1noQ8noWpKMZoxTCsfprrbs4Xo"
: signature)
0x05010000000568656c6c6f

View File

@ -0,0 +1,5 @@
let test (k : int) : int =
let j : int = k + 5
in let close : int -> int = fun (i : int) -> i + j
in let j : int = 20
in close 20

View File

@ -0,0 +1,28 @@
let int_ (a : int) = a < a
let nat_ (a : nat) = a < a
let bool_ (a : bool) = a < a
let mutez_ (a : tez) = a < a
let string_ (a : string) = a < a
let bytes_ (a : bytes) = a < a
let address_ (a : address) = a < a
let timestamp_ (a : timestamp) = a < a
let key_hash_ (a : key_hash) = a < a
type comp_pair = int * int
let comp_pair (a : comp_pair) = a < a
type inner_record = (int, "one", nat, "two") michelson_pair
type comb_record =
(int, "three", inner_record, "four") michelson_pair
let comb_record (a : comb_record) = a < a

View File

@ -0,0 +1,4 @@
type integer = int
let main (i : int) =
if (i = 2 : bool) then (42 : int) else (0 : integer)

View File

@ -0,0 +1,9 @@
let main (i : int) =
let result = 0
in if i = 2
then
let result = 42
in result
else
let result = 0
in result

View File

@ -0,0 +1 @@
let main (i : int) = if i = 2 then 42 else 0

View File

@ -0,0 +1,4 @@
type storage = int
let main (p, s : int * storage) =
([] : operation list), p + s

View File

@ -0,0 +1,11 @@
type return = operation list * string
let main (action, store : string * string) : return =
let toto : operation * address =
Tezos.create_contract
(fun (p, s : nat * string) ->
(([] : operation list), "one"))
(None : key_hash option)
300000000mutez
"un"
in ([toto.0], store)

View File

@ -0,0 +1,3 @@
let hasherman512 (s : bytes) : bytes = Crypto.sha512 s
let hasherman_blake (s : bytes) : bytes = Crypto.blake2b s

View File

@ -0,0 +1,9 @@
let conv_test (j : int) (k : int) = j + k
let main (i : int) : int = conv_test i 10
let partial (a : int) (b : int) : int = a + b
let mk_partial (j : int) : int -> int = partial j
let partial_apply (i : int) : int = mk_partial 10 i

View File

@ -0,0 +1,10 @@
type storage = (int, "foo", string, "bar") michelson_or
type foobar = (int, "baz", int, "fooo") michelson_or
type return = operation list * storage
let main (action, store : unit * storage) : return =
let foo = (M_right ("one") : storage)
in let bar = (M_right 1 : foobar)
in (([] : operation list), (foo : storage))

View File

@ -0,0 +1,6 @@
type foo = Bar of int | Baz
let main (f : foo) : int =
match f with
Bar i -> i
| Baz -> -1

View File

@ -0,0 +1 @@
let main (a, b : bool * bool) = if a = b then 999 else 1

View File

@ -0,0 +1,6 @@
type storage = unit
let main (p, store : unit * storage)
: operation list * storage =
(failwith "This contract always fails"
: operation list * storage)

View File

@ -0,0 +1,13 @@
type storage = unit
let main (p, store : unit * storage)
: operation list * storage =
let n =
(fun (f : int * int -> int)
(x : int)
(y : int) ->
f (y, x))
(fun (x : int) (y : int) -> x + y)
0
1
in ([] : operation list), store

View File

@ -0,0 +1,10 @@
type storage = unit
let main (p, store : unit * storage)
: operation list * storage =
let n =
(fun (f : int -> int) (z : int) (y : int) -> f y)
(fun (x : int) -> x)
0
1
in ([] : operation list), store

View File

@ -0,0 +1,12 @@
type storage = unit
let main (p, s : unit * storage) : operation list * storage =
let n =
(fun (f : int -> int -> int)
(x : int)
(y : int) ->
f y (x + y))
(fun (x : int) (y : int) -> x + y)
0
1
in ([] : operation list), store

View File

@ -0,0 +1,6 @@
type storage = unit
let main (p, s : unit * storage) =
(fun (f : int -> int) (x : int) -> f x)
(fun (x : int) -> x)
1

View File

@ -0,0 +1,5 @@
let foo (i : int) : int = i + 20
let bar (i : int) : int = i + 50
let foobar (i : int) : int = foo i + bar i

View File

@ -0,0 +1,17 @@
type storage = {challenge : string}
type param = {new_challenge : string; attempt : string}
type return = operation list * storage
let attempt (p, store : param * storage) : return =
let contract : unit contract =
match (Tezos.get_contract_opt Tezos.sender
: unit contract option)
with
Some contract -> contract
| None -> (failwith "No contract" : unit contract)
in let transfer : operation =
Tezos.transaction (unit, contract, 10000000mutez)
in let store : storage = {challenge = p.new_challenge}
in ([] : operation list), store