package octez-injector

  1. Overview
  2. Docs

Source file injector_server_operation.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
(*****************************************************************************)
(*                                                                           *)
(* SPDX-License-Identifier: MIT                                              *)
(* Copyright (c) 2023 Nomadic Labs <contact@nomadic-labs.com>                *)
(*                                                                           *)
(*****************************************************************************)

type parameters = {entrypoint : string; value : string}

type t =
  | Transaction of {
      amount : int64;
      destination : string;
      parameters : parameters option;
    }

(* adapted from Operation_repr.Encoding.Manager_operations *)
let encoding : t Data_encoding.t =
  let open Data_encoding in
  let case tag kind encoding proj inj =
    case
      ~title:kind
      (Tag tag)
      (merge_objs (obj1 (req "kind" (constant kind))) encoding)
      (fun o -> Option.map (fun p -> ((), p)) (proj o))
      (fun ((), p) -> inj p)
  in
  def "injector_operation"
  @@ union
       [
         case
           0
           "transaction"
           (obj3
              (req "amount" string)
              (req "destination" string)
              (opt
                 "parameters"
                 (obj2 (req "entrypoint" string) (req "value" string))))
           (function
             | Transaction {amount; destination; parameters} ->
                 Some
                   ( Int64.to_string amount,
                     destination,
                     Option.map
                       (fun {entrypoint; value} -> (entrypoint, value))
                       parameters ))
           (fun (amount, destination, parameters) ->
             Transaction
               {
                 amount = Int64.of_string amount;
                 destination;
                 parameters =
                   Option.map
                     (fun (entrypoint, value) -> {entrypoint; value})
                     parameters;
               });
       ]

let pp ppf = function
  | Transaction {amount; destination = _; parameters = _} ->
      Format.fprintf ppf "Transaction of %Ld tez" amount

let unique = function Transaction _ -> true