package octez-protocol-019-PtParisB-libs

  1. Overview
  2. Docs
Legend:
Page
Library
Module
Module type
Parameter
Class
Class type
Source

Source file lqt_fa12_repr.ml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
(*****************************************************************************)
(*                                                                           *)
(* Open Source License                                                       *)
(* Copyright (c) 2021-2022 Nomadic Labs <contact@nomadic-labs.com>           *)
(*                                                                           *)
(* Permission is hereby granted, free of charge, to any person obtaining a   *)
(* copy of this software and associated documentation files (the "Software"),*)
(* to deal in the Software without restriction, including without limitation *)
(* the rights to use, copy, modify, merge, publish, distribute, sublicense,  *)
(* and/or sell copies of the Software, and to permit persons to whom the     *)
(* Software is furnished to do so, subject to the following conditions:      *)
(*                                                                           *)
(* The above copyright notice and this permission notice shall be included   *)
(* in all copies or substantial portions of the Software.                    *)
(*                                                                           *)
(* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR*)
(* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,  *)
(* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL   *)
(* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER*)
(* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING   *)
(* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER       *)
(* DEALINGS IN THE SOFTWARE.                                                 *)
(*                                                                           *)
(*****************************************************************************)

open Protocol
open Alpha_context
open Expr_common

module Parameter = struct
  (* // =============================================================================
   * // Entrypoints
   * // ============================================================================= *)

  (* Note: in the lqt_fa12 contract, [value] is a nat. Hence, it
     should always be positive *)
  type approve = {spender : Contract.t; value : Z.t}

  type mintOrBurn = {quantity : Z.t; target : Contract.t}

  (* Note: this wrapper does not implement a reprensentation for the
     entrypoints transfer, getAllowance, getBalance, getTotalSupply,
     as they are not used as of yet. *)
  type t = Approve of approve | MintOrBurn of mintOrBurn

  let approve p =
    assert (Z.lt Z.zero p.value || Z.equal Z.zero p.value) ;
    Approve p

  let mintOrBurn p = MintOrBurn p

  let approve_to_string {spender; value} =
    Format.asprintf
      "{ spender: %a; value: %a }"
      Contract.pp
      spender
      Z.pp_print
      value

  let mint_or_burn_to_string {quantity; target} =
    Format.asprintf
      "{ quantity: %a; target: %a }"
      Z.pp_print
      quantity
      Contract.pp
      target

  let to_string : t -> string = function
    | Approve p -> Format.asprintf "Approve %s" (approve_to_string p)
    | MintOrBurn p -> Format.asprintf "MintOrBurn %s" (mint_or_burn_to_string p)

  let entrypoint_of_parameter : t -> Entrypoint.t = function
    | Approve _ -> Entrypoint.of_string_strict_exn "approve"
    | MintOrBurn _ -> Entrypoint.of_string_strict_exn "mintOrBurn"

  let pp fmt s = Format.fprintf fmt "%s" (to_string s)

  let eq s s' = s = s'

  let to_expr_rooted :
      loc:'a ->
      t ->
      ('a, Michelson_v1_primitives.prim) Tezos_micheline.Micheline.node =
   fun ~loc -> function
    | MintOrBurn {quantity; target} ->
        comb ~loc [int ~loc quantity; address_string ~loc target]
    | Approve {spender; value} ->
        comb ~loc [address_string ~loc spender; int ~loc value]

  let to_expr :
      loc:'a ->
      t ->
      ('a, Michelson_v1_primitives.prim) Tezos_micheline.Micheline.node =
   fun ~loc p ->
    let rooted = to_expr_rooted ~loc p in
    match p with
    | MintOrBurn _ -> right ~loc @@ left ~loc rooted
    | Approve _ -> left ~loc @@ left ~loc @@ left ~loc rooted

  let to_michelson_string e =
    let e = to_expr ~loc:0 e in
    Format.asprintf
      "%a"
      Michelson_v1_printer.print_expr
      (Micheline.strip_locations e)
end

(* // =============================================================================
 * // Storage
 * // ============================================================================= *)

module Storage = struct
  let pp_big_map_id fmt v = Z.pp_print fmt (Big_map.Id.unparse_to_z v)

  type t = {
    tokens : Big_map.Id.t;
    allowances : Big_map.Id.t;
    admin : Contract.t;
    totalSupply : Z.t;
  }

  let pp {tokens; allowances; admin; totalSupply} =
    Format.asprintf
      "{ tokens: %a; allowances: %a; admin: %a; totalSupply: %a}"
      Z.pp_print
      (Big_map.Id.unparse_to_z tokens)
      Z.pp_print
      (Big_map.Id.unparse_to_z allowances)
      Contract.pp
      admin
      Z.pp_print
      totalSupply

  let null : t =
    {
      tokens = Big_map.Id.parse_z Z.zero;
      allowances = Big_map.Id.parse_z Z.one;
      admin = Contract.Implicit Signature.Public_key_hash.zero;
      totalSupply = Z.zero;
    }

  let eq s s' = s = s'

  let to_expr :
      loc:'a ->
      t ->
      ('a, Michelson_v1_primitives.prim) Tezos_micheline.Micheline.node =
   fun ~loc {tokens; allowances; admin; totalSupply} ->
    comb
      ~loc
      [
        big_map_id ~loc tokens;
        big_map_id ~loc allowances;
        address_string ~loc admin;
        int ~loc totalSupply;
      ]

  let to_michelson_string e =
    let e = to_expr ~loc:0 e in
    Format.asprintf
      "%a"
      Michelson_v1_printer.print_expr
      (Micheline.strip_locations e)

  type exn += Invalid_storage_expr of string

  (** Note: parses a storage unparsed in readable mode (as
     e.g. returned by [Alpha_services.Contract.storage]), so that
     contracts are represented by strings.  *)
  let of_expr_exn :
      ('a, Michelson_v1_primitives.prim) Tezos_micheline.Micheline.node -> t =
    function
    | Tezos_micheline.Micheline.Prim
        ( _,
          Script.D_Pair,
          [
            Tezos_micheline.Micheline.Int (_, tokens);
            Tezos_micheline.Micheline.Int (_, allowances);
            Tezos_micheline.Micheline.String (_, admin);
            Tezos_micheline.Micheline.Int (_, totalSupply);
          ],
          [] ) ->
        let tokens = Big_map.Id.parse_z tokens in
        let allowances = Big_map.Id.parse_z allowances in
        let admin = address_of_string_exn admin in
        {tokens; allowances; admin; totalSupply}
    | e ->
        let canonical = Micheline.strip_locations e in
        let msg =
          Format.asprintf
            "Not a valid LQT_FA1.2 storage: %s /// %a"
            (try
               Michelson_v1_printer.micheline_string_of_expression
                 ~zero_loc:true
                 canonical
             with Z.Overflow ->
               "Cannot represent as micheline due to overflowing Z -> int")
            Michelson_v1_printer.print_expr
            canonical
        in
        raise (Invalid_storage_expr msg)

  let get (ctxt : Context.t) ~(contract : Contract.t) : t tzresult Lwt.t =
    let open Lwt_result_syntax in
    match contract with
    | Implicit _ ->
        invalid_arg "Lqt_fa12_repr.Storage.get called on implicit account"
    | Originated c ->
        let+ expr = Context.Contract.storage ctxt c in
        Micheline.root expr |> of_expr_exn

  let get_alpha_context (ctxt : Context.t) : Alpha_context.t tzresult Lwt.t =
    let open Lwt_result_syntax in
    let+ result =
      match ctxt with
      | B b ->
          (* can perhaps be retrieved through Raw_context.prepare ? *)
          Incremental.begin_construction b
      | I i -> return i
    in
    Incremental.alpha_ctxt result

  let getBalance_opt (ctxt : Context.t) ~(contract : Contract.t)
      (owner : Script_typed_ir.address) =
    let open Lwt_result_wrap_syntax in
    let* storage = get ctxt ~contract in
    let tokens = storage.tokens in
    let* ctxt = get_alpha_context ctxt in
    let*@ address_hash, ctxt =
      Script_ir_translator.hash_data ctxt Script_typed_ir.address_t owner
    in
    let*@ _, result = Big_map.get_opt ctxt tokens address_hash in
    match result with
    | Some canonical -> (
        match Tezos_micheline.Micheline.root canonical with
        | Tezos_micheline.Micheline.Int (_, amount) -> return_some amount
        | _ -> assert false)
    | None -> return_none

  let getBalance (ctxt : Context.t) ~(contract : Contract.t)
      (owner : Script_typed_ir.address) =
    let open Lwt_result_syntax in
    let+ t = getBalance_opt ctxt ~contract owner in
    Option.value ~default:Z.zero t
end

let transaction (ctxt : Context.t) ~(contract : Contract.t) ~(src : Contract.t)
    ?(amount = Tez.zero) (parameters : Parameter.t) =
  let entrypoint = Parameter.entrypoint_of_parameter parameters in
  let rooted_param_lazy =
    parameters
    |> Parameter.to_expr_rooted ~loc:0
    |> Micheline.strip_locations |> Alpha_context.Script.lazy_expr
  in
  Op.transaction
    ctxt
    src
    contract
    amount
    ~entrypoint
    ~parameters:rooted_param_lazy
OCaml

Innovation. Community. Security.