Commit e07894de authored by Eugene Mishura's avatar Eugene Mishura

policy contract impl guarded hook invocation

parent 2d089c0e
Pipeline #117711263 passed with stage
in 2 minutes and 36 seconds
......@@ -17,12 +17,20 @@ type entry_points =
| Tokens_transferred_hook of hook_param
| Register_with_fa2 of fa2_with_hook_entry_points contract
(* registered FA2 contracts which can call this policy contract *)
type fa2_registry = address set
(**
This will not work with babylon/LIGO since `allowance_key` is a composite
non-comparable record which cannot be used as a key in the big_map.
*)
type allowances = (allowance_id, nat) big_map
type storage = {
fa2_registry : fa2_registry;
allowances : allowances;
}
let get_current_allowance (id : allowance_id) (a : allowances) : nat =
let a = Big_map.find_opt id a in
match a with
......@@ -87,18 +95,31 @@ let config_allowances (param : fa2_allowances_config_entry_points) (s : allowanc
let op = Operation.transaction resp 0mutez p.view in
[op], s
let main (param, s : entry_points * allowances) : (operation list) * allowances =
let main (param, s : entry_points * storage) : (operation list) * storage =
match param with
| Allowances p -> config_allowances p s
| Allowances p ->
let ops, new_a = config_allowances p s.allowances in
let new_s = { s with allowances = new_a; } in
ops, new_s
| Tokens_transferred_hook p ->
let new_s = List.fold (track_allowances p.operator) p.batch s in
([] : operation list), new_s
if Set.mem Current.sender s.fa2_registry
then
let new_a = List.fold (track_allowances p.operator) p.batch s.allowances in
let new_s = { s with allowances = new_a } in
([] : operation list), new_s
else
(failwith "unknown FA2 caller" : (operation list) * storage)
| Register_with_fa2 fa2 ->
let c : fa2_allowances_config_entry_points contract =
Operation.get_entrypoint "%allowances" Current.self_address in
let config_address = Current.address c in
let op = create_register_hook_op fa2 [Allowances_config config_address] in
[op], s
let fa2_address = Current.address fa2 in
let new_fa2s = Set.add fa2_address s.fa2_registry in
let new_s = { s with fa2_registry = new_fa2s; } in
[op], new_s
......@@ -8,23 +8,36 @@ Any address can be a recipient of the token transfer
#include "hook_lib.mligo"
(* registered FA2 contracts which can call this policy contract *)
type fa2_registry = address set
let validate_operator (p : hook_param) : unit =
List.iter ( fun (tx : hook_transfer) ->
match tx.from_ with
| None -> unit
| Some from_ ->
if from_ = p.operator
then unit
else failwith "cannot transfer tokens on behalf of other owner"
) p.batch
type entry_points =
| Tokens_transferred_hook of hook_param
| Register_with_fa2 of fa2_with_hook_entry_points contract
let main (param, s : entry_points * unit) : (operation list) * unit =
let main (param, s : entry_points * fa2_registry)
: (operation list) * fa2_registry =
match param with
| Tokens_transferred_hook p ->
let u = List.iter ( fun (tx : hook_transfer) ->
match tx.from_ with
| None -> unit
| Some from_ ->
if from_ = p.operator
then unit
else failwith "cannot transfer tokens on behalf of other owner"
) p.batch in
([] : operation list), unit
if Set.mem Current.sender s
then
let u = validate_operator p in
([] : operation list), s
else
(failwith "unknown FA2 caller" : (operation list) * fa2_registry)
| Register_with_fa2 fa2 ->
let op = create_register_hook_op fa2 ([] : permission_policy_config list) in
[op], s
\ No newline at end of file
let fa2_address = Current.address fa2 in
let new_s = Set.add fa2_address s in
[op], new_s
\ No newline at end of file
......@@ -11,6 +11,16 @@
#include "hook_lib.mligo"
(* registered FA2 contracts which can call this policy contract *)
type fa2_registry = address set
(* owner -> operator set*)
type operators = (address, (address set)) big_map
type storage = {
fa2_registry : fa2_registry;
operators : operators;
}
type entry_points =
| Operators of fa2_operators_config_entry_points
......@@ -18,9 +28,6 @@
| Register_with_fa2 of fa2_with_hook_entry_points contract
(* owner -> operator set*)
type operators = (address, (address set)) big_map
let is_allowed (owner : address) (operator : address) (operators : operators) : bool =
if owner = operator
then true
......@@ -85,25 +92,37 @@ let config_operators (param : fa2_operators_config_entry_points) (s : operators)
[view_op], s
(* ([] : operation list), s *)
let main (param, s : entry_points * operators) : (operation list) * operators =
let main (param, s : entry_points * storage) : (operation list) * storage =
match param with
| Operators oc -> config_operators oc s
| Operators oc ->
let ops, new_operators = config_operators oc s.operators in
let new_s = { s with operators = new_operators; } in
ops, new_s
| Tokens_transferred_hook p ->
let u = List.iter (fun (tx : hook_transfer) ->
let allowed = match tx.from_ with
| None -> true
| Some from_ -> is_allowed from_ p.operator s
in
if allowed
then unit
else failwith "operator is not allowed"
) p.batch in
([] : operation list), s
if Set.mem Current.sender s.fa2_registry
then
let u = List.iter (fun (tx : hook_transfer) ->
let allowed = match tx.from_ with
| None -> true
| Some from_ -> is_allowed from_ p.operator s.operators
in
if allowed
then unit
else failwith "operator is not allowed"
) p.batch in
([] : operation list), s
else
(failwith "unknown FA2 caller" : (operation list) * storage)
| Register_with_fa2 fa2 ->
let c : fa2_operators_config_entry_points contract =
Operation.get_entrypoint "%operators" Current.self_address in
let config_address = Current.address c in
let op = create_register_hook_op fa2 [Operators_config config_address] in
[op], s
let fa2_address = Current.address fa2 in
let new_fa2s = Set.add fa2_address s.fa2_registry in
let new_s = { s with fa2_registry = new_fa2s; } in
[op], new_s
......@@ -17,9 +17,17 @@ MUST fail.
| Register_with_fa2 of fa2_with_hook_entry_points contract
(* registered FA2 contracts which can call this policy contract *)
type fa2_registry = address set
type whitelist = address set
let config_whitelist (param : fa2_whitelist_config_entry_points) (s : whitelist)
type storage = {
fa2_registry : fa2_registry;
whitelist : whitelist;
}
let config_whitelist (param, s : fa2_whitelist_config_entry_points * whitelist)
: (operation list) * whitelist =
match param with
| Add_to_white_list owners ->
......@@ -41,25 +49,37 @@ let config_whitelist (param : fa2_whitelist_config_entry_points) (s : whitelist)
let op = Operation.transaction responses 0mutez p.whitelist_view in
[op], s
let main (param, s : entry_points * whitelist) : (operation list) * whitelist =
let main (param, s : entry_points * storage) : (operation list) * storage =
match param with
| Whitelist p -> config_whitelist p s
| Whitelist p ->
let ops, new_wl = config_whitelist (p, s.whitelist) in
let new_s = { s with whitelist = new_wl; } in
ops, new_s
| Tokens_transferred_hook p ->
let u = List.iter (fun (tx : hook_transfer) ->
let allowed = match tx.to_ with
| None -> true
| Some to_ -> Set.mem to_ s
in
if allowed
then unit
else failwith "receiver is not whitelisted"
) p.batch in
([] : operation list), s
if Set.mem Current.sender s.fa2_registry
then
let u = List.iter (fun (tx : hook_transfer) ->
let allowed = match tx.to_ with
| None -> true
| Some to_ -> Set.mem to_ s.whitelist
in
if allowed
then unit
else failwith "receiver is not whitelisted"
) p.batch in
([] : operation list), s
else
(failwith "unknown FA2 caller" : (operation list) * storage)
| Register_with_fa2 fa2 ->
let c : fa2_whitelist_config_entry_points contract =
Operation.get_entrypoint "%whitelist" Current.self_address in
let config_address = Current.address c in
let op = create_register_hook_op fa2 [Whitelist_config config_address] in
[op], s
let fa2_address = Current.address fa2 in
let new_fa2s = Set.add fa2_address s.fa2_registry in
let new_s = { s with fa2_registry = new_fa2s; } in
[op], new_s
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment