From 7e8f4f65ea6b18a5a22efbb73bacf45bff2344b7 Mon Sep 17 00:00:00 2001
From: Lucas Randazzo <lucas@nomadic-labs.com>
Date: Mon, 5 Jun 2023 18:20:14 +0200
Subject: [PATCH 1/2] Proto: expose `get_total_supply`

---
 src/proto_alpha/lib_protocol/alpha_context.mli    | 2 ++
 src/proto_alpha/lib_protocol/contract_storage.ml  | 2 ++
 src/proto_alpha/lib_protocol/contract_storage.mli | 2 ++
 3 files changed, 6 insertions(+)

diff --git a/src/proto_alpha/lib_protocol/alpha_context.mli b/src/proto_alpha/lib_protocol/alpha_context.mli
index 0e6bee8b84e0..ed61572120d7 100644
--- a/src/proto_alpha/lib_protocol/alpha_context.mli
+++ b/src/proto_alpha/lib_protocol/alpha_context.mli
@@ -1771,6 +1771,8 @@ module Contract : sig
   (** See {!Contract_delegate_storage.is_delegate}. *)
   val is_delegate : context -> public_key_hash -> bool tzresult Lwt.t
 
+  val get_total_supply : context -> Tez.t tzresult Lwt.t
+
   module Legacy_big_map_diff : sig
     type item = private
       | Update of {
diff --git a/src/proto_alpha/lib_protocol/contract_storage.ml b/src/proto_alpha/lib_protocol/contract_storage.ml
index d58ceb2bc250..bd0737753d79 100644
--- a/src/proto_alpha/lib_protocol/contract_storage.ml
+++ b/src/proto_alpha/lib_protocol/contract_storage.ml
@@ -779,3 +779,5 @@ let simulate_spending ctxt ~balance ~amount source =
       should_keep_empty_implicit_contract ctxt contract
   in
   return (new_balance, still_allocated)
+
+let get_total_supply ctxt = Storage.Contract.Total_supply.get ctxt
diff --git a/src/proto_alpha/lib_protocol/contract_storage.mli b/src/proto_alpha/lib_protocol/contract_storage.mli
index 4bbdbbf1db52..e277aae8956c 100644
--- a/src/proto_alpha/lib_protocol/contract_storage.mli
+++ b/src/proto_alpha/lib_protocol/contract_storage.mli
@@ -325,3 +325,5 @@ val simulate_spending :
   amount:Tez_repr.t ->
   Signature.public_key_hash ->
   (Tez_repr.t * bool) tzresult Lwt.t
+
+val get_total_supply : Raw_context.t -> Tez_repr.t tzresult Lwt.t
-- 
GitLab


From ee6a26c28e317e97ba55a3fdb4a91094fb7dbaea Mon Sep 17 00:00:00 2001
From: Lucas Randazzo <lucas@nomadic-labs.com>
Date: Mon, 5 Jun 2023 18:20:50 +0200
Subject: [PATCH 2/2] Proto/AI: add `total_supply` RPC

---
 src/proto_alpha/lib_protocol/TEZOS_PROTOCOL   |  1 +
 .../adaptive_inflation_services.ml            | 47 +++++++++++++++++++
 .../adaptive_inflation_services.mli           | 30 ++++++++++++
 .../lib_protocol/alpha_services.ml            |  4 +-
 src/proto_alpha/lib_protocol/dune             |  4 ++
 5 files changed, 85 insertions(+), 1 deletion(-)
 create mode 100644 src/proto_alpha/lib_protocol/adaptive_inflation_services.ml
 create mode 100644 src/proto_alpha/lib_protocol/adaptive_inflation_services.mli

diff --git a/src/proto_alpha/lib_protocol/TEZOS_PROTOCOL b/src/proto_alpha/lib_protocol/TEZOS_PROTOCOL
index d18221743251..8637a7aa5e93 100644
--- a/src/proto_alpha/lib_protocol/TEZOS_PROTOCOL
+++ b/src/proto_alpha/lib_protocol/TEZOS_PROTOCOL
@@ -249,6 +249,7 @@
         "Delegate_services",
         "Voting_services",
         "Dal_services",
+	"Adaptive_inflation_services",
         "Alpha_services",
 
         "Main"
diff --git a/src/proto_alpha/lib_protocol/adaptive_inflation_services.ml b/src/proto_alpha/lib_protocol/adaptive_inflation_services.ml
new file mode 100644
index 000000000000..5e29f3d1b1ab
--- /dev/null
+++ b/src/proto_alpha/lib_protocol/adaptive_inflation_services.ml
@@ -0,0 +1,47 @@
+(*****************************************************************************)
+(*                                                                           *)
+(* Open Source License                                                       *)
+(* Copyright (c) 2023 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 Alpha_context
+
+module S = struct
+  let context_path = RPC_path.(open_root / "context")
+
+  let _path = RPC_path.(context_path / "inflation")
+
+  let total_supply =
+    RPC_service.get_service
+      ~description:"Returns the total supply (in mutez) available on the chain"
+      ~query:RPC_query.empty
+      ~output:Tez.encoding
+      RPC_path.(context_path / "total_supply")
+end
+
+let register () =
+  let open Services_registration in
+  register0 ~chunked:false S.total_supply (fun ctxt () () ->
+      Contract.get_total_supply ctxt)
+
+let total_supply ctxt block =
+  RPC_context.make_call0 S.total_supply ctxt block () ()
diff --git a/src/proto_alpha/lib_protocol/adaptive_inflation_services.mli b/src/proto_alpha/lib_protocol/adaptive_inflation_services.mli
new file mode 100644
index 000000000000..53be0d0d0b0a
--- /dev/null
+++ b/src/proto_alpha/lib_protocol/adaptive_inflation_services.mli
@@ -0,0 +1,30 @@
+(*****************************************************************************)
+(*                                                                           *)
+(* Open Source License                                                       *)
+(* Copyright (c) 2023 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 Alpha_context
+
+val total_supply : 'a #RPC_context.simple -> 'a -> Tez.t shell_tzresult Lwt.t
+
+val register : unit -> unit
diff --git a/src/proto_alpha/lib_protocol/alpha_services.ml b/src/proto_alpha/lib_protocol/alpha_services.ml
index 5f3e5b2dd68d..871ee8c15d40 100644
--- a/src/proto_alpha/lib_protocol/alpha_services.ml
+++ b/src/proto_alpha/lib_protocol/alpha_services.ml
@@ -220,6 +220,7 @@ module Constants = Constants_services
 module Delegate = Delegate_services
 module Voting = Voting_services
 module Sapling = Sapling_services
+module Adaptive_inflation = Adaptive_inflation_services
 
 module Liquidity_baking = struct
   module S = struct
@@ -308,4 +309,5 @@ let register () =
   Voting.register () ;
   Sapling.register () ;
   Liquidity_baking.register () ;
-  Cache.register ()
+  Cache.register () ;
+  Adaptive_inflation.register ()
diff --git a/src/proto_alpha/lib_protocol/dune b/src/proto_alpha/lib_protocol/dune
index 38a190b09180..e9af55b479c9 100644
--- a/src/proto_alpha/lib_protocol/dune
+++ b/src/proto_alpha/lib_protocol/dune
@@ -253,6 +253,7 @@
   Delegate_services
   Voting_services
   Dal_services
+  Adaptive_inflation_services
   Alpha_services
   Main))
 
@@ -516,6 +517,7 @@
   delegate_services.ml delegate_services.mli
   voting_services.ml voting_services.mli
   dal_services.ml dal_services.mli
+  adaptive_inflation_services.ml adaptive_inflation_services.mli
   alpha_services.ml alpha_services.mli
   main.ml main.mli
   (:src_dir TEZOS_PROTOCOL))
@@ -780,6 +782,7 @@
   delegate_services.ml delegate_services.mli
   voting_services.ml voting_services.mli
   dal_services.ml dal_services.mli
+  adaptive_inflation_services.ml adaptive_inflation_services.mli
   alpha_services.ml alpha_services.mli
   main.ml main.mli (:src_dir TEZOS_PROTOCOL))
  (action
@@ -1028,6 +1031,7 @@
   delegate_services.ml delegate_services.mli
   voting_services.ml voting_services.mli
   dal_services.ml dal_services.mli
+  adaptive_inflation_services.ml adaptive_inflation_services.mli
   alpha_services.ml alpha_services.mli
   main.ml main.mli (:src_dir TEZOS_PROTOCOL))
  (action
-- 
GitLab