diff --git a/etherlink/bin_node/lib_dev/tezlink/tezlink_backend_sig.ml b/etherlink/bin_node/lib_dev/tezlink/tezlink_backend_sig.ml index 3d25fd3bf5f274534e5268fc8ce61245ac3bb619..e5c0190dc2d297a637ab74acf34472e4662901fb 100644 --- a/etherlink/bin_node/lib_dev/tezlink/tezlink_backend_sig.ml +++ b/etherlink/bin_node/lib_dev/tezlink/tezlink_backend_sig.ml @@ -5,6 +5,8 @@ (* *) (*****************************************************************************) +open Tezos_types + module type S = sig val current_level : [> `Main] -> @@ -14,4 +16,7 @@ module type S = sig val constants : [> `Main] -> [> `Head of 'a] -> Tezlink_constants.t tzresult Lwt.t + + val balance : + [> `Main] -> [> `Head of 'a] -> Contract.t -> Tez.t tzresult Lwt.t end diff --git a/etherlink/bin_node/lib_dev/tezlink/tezos_services.ml b/etherlink/bin_node/lib_dev/tezlink/tezos_services.ml index 7f8ad776c9980e1c216a172b2d0bed633bd16a60..cea7c83ad0b5e5719b044ecf6c5bf0c400913c9e 100644 --- a/etherlink/bin_node/lib_dev/tezlink/tezos_services.ml +++ b/etherlink/bin_node/lib_dev/tezlink/tezos_services.ml @@ -65,15 +65,6 @@ module Protocol_types = struct ~dst:encoding ~src:conversion_encoding end - - module Tez = struct - include Alpha_context.Tez - - let convert q = - q |> Ethereum_types.Qty.to_z |> Z.to_int64 |> Alpha_context.Tez.of_mutez - |> Option.value ~default:Alpha_context.Tez.zero - |> Result_syntax.return - end end (** [wrap conversion service_implementation] changes the output type @@ -99,8 +90,6 @@ type tezlink_rpc_context = { chain : Tezos_shell_services.Chain_services.chain; } -type contract = Imported_protocol.Alpha_context.Contract.t - (** Builds a [tezlink_rpc_context] from paths parameters. *) let make_env (chain : Tezos_shell_services.Chain_services.chain) (block : Tezos_shell_services.Block_services.block) : @@ -203,10 +192,10 @@ module Imported_services = struct let balance : ( [`GET], tezlink_rpc_context, - tezlink_rpc_context * contract, + tezlink_rpc_context * Tezos_types.Contract.t, unit, unit, - Protocol_types.Tez.t ) + Tezos_types.Tez.t ) Tezos_rpc.Service.t = Tezos_rpc.Service.subst1 (* TODO: #7876 should be imported *) @@ -217,7 +206,7 @@ module Imported_services = struct neither staked, nor in unstaked requests, nor in frozen bonds. \ Identical to the 'spendable' RPC." ~query:Tezos_rpc.Query.empty - ~output:Protocol_types.Tez.encoding + ~output:Tezos_types.Tez.encoding (contract_arg_path "balance") let constants : @@ -239,9 +228,6 @@ let block_directory_path = let protocols () = Lwt_result_syntax.return Tezlink_protocols.current -let balance _ _ _ = - Lwt_result_syntax.return @@ Ethereum_types.quantity_of_z Z.one - let version () = (* TODO: #7857 need proper implementation *) Lwt_result_syntax.return Tezlink_version.mock @@ -259,11 +245,10 @@ let build_block_dir (module Backend : Tezlink_backend_sig.S) = ~convert_output:Protocol_types.Level.convert |> register ~service:Imported_services.protocols ~impl:(fun _ _ () -> protocols ()) - |> register_with_conversion + |> register ~service:Imported_services.balance - ~impl:(fun ({block; chain}, contract) _ _ -> - balance chain block contract) - ~convert_output:Protocol_types.Tez.convert + ~impl:(fun ({chain; block}, contract) _ _ -> + Backend.balance chain block contract) |> register_with_conversion ~service:Imported_services.constants ~impl:(fun {block; chain} () () -> Backend.constants chain block) diff --git a/etherlink/bin_node/lib_dev/tezlink/tezos_types.ml b/etherlink/bin_node/lib_dev/tezlink/tezos_types.ml index 2834d2252a47fc1cd7927fee90aa8627f4d805e5..9fa6eecde15c986d90b4ff913ca165287f901736 100644 --- a/etherlink/bin_node/lib_dev/tezlink/tezos_types.ml +++ b/etherlink/bin_node/lib_dev/tezlink/tezos_types.ml @@ -2,6 +2,7 @@ (* *) (* SPDX-License-Identifier: MIT *) (* Copyright (c) 2025 Functori *) +(* Copyright (c) 2025 Nomadic Labs *) (* *) (*****************************************************************************) @@ -45,3 +46,11 @@ let convert_using_serialization ~name ~dst ~src value = tzfail @@ Serialization_for_conversion (name, Format.asprintf "%a" Data_encoding.Binary.pp_read_error e)) + +module Contract = struct + type t = Tezlink_imports.Alpha_context.Contract.t + + let encoding = Tezlink_imports.Alpha_context.Contract.encoding +end + +module Tez = Tezlink_imports.Alpha_context.Tez diff --git a/etherlink/bin_node/lib_dev/tezlink/tezos_types.mli b/etherlink/bin_node/lib_dev/tezlink/tezos_types.mli index 243009e432d1395b259960746fbfc6929cb2c864..227364d0b808c8f1594330dfb98d9ee5b4960b20 100644 --- a/etherlink/bin_node/lib_dev/tezlink/tezos_types.mli +++ b/etherlink/bin_node/lib_dev/tezlink/tezos_types.mli @@ -2,6 +2,7 @@ (* *) (* SPDX-License-Identifier: MIT *) (* Copyright (c) 2025 Functori *) +(* Copyright (c) 2025 Nomadic Labs *) (* *) (*****************************************************************************) @@ -28,3 +29,15 @@ val convert_using_serialization : src:'b Data_encoding.t -> 'b -> 'a tzresult + +(** Imports the type Contract.t from Alpha_context. Not everything is imported + from Alpha_context.Contract as most of it require a context, which we + can't provide. + *) +module Contract : sig + type t = Tezlink_imports.Alpha_context.Contract.t + + val encoding : t Data_encoding.t +end + +module Tez = Tezlink_imports.Alpha_context.Tez diff --git a/etherlink/bin_node/lib_dev/tezlink_services_impl.ml b/etherlink/bin_node/lib_dev/tezlink_services_impl.ml index bd205cb45077450790a6317da9ac1103a50dd1b9..77b9b98a6d00e5abbc6e2cf5c95c6668edbdfcdb 100644 --- a/etherlink/bin_node/lib_dev/tezlink_services_impl.ml +++ b/etherlink/bin_node/lib_dev/tezlink_services_impl.ml @@ -5,6 +5,34 @@ (* *) (*****************************************************************************) +open Tezos_types + +module Path = struct + (** [to_path encoding value] uses [encoding] to encode [value] in + hexadecimal *) + let to_path encoding value = + let raw_key = Data_encoding.Binary.to_bytes_exn encoding value in + let (`Hex s) = Hex.of_bytes raw_key in + s + + let account contract = + "/tezlink/context/contracts/index/" ^ to_path Contract.encoding contract + + let balance contract = account contract ^ "/balance" +end + +let balance read chain block c = + (* TODO: #7831 !17664 + Support non-default chain and block parameters. *) + ignore chain ; + ignore block ; + + Durable_storage.inspect_durable_and_decode_default + ~default:Tezos_types.Tez.zero + read + (Path.balance c) + (Data_encoding.Binary.of_bytes_exn Tez.encoding) + module type Backend = sig include Durable_storage.READER @@ -108,4 +136,13 @@ module Make (Backend : Backend) : Tezlink_backend_sig.S = struct err in return Tezlink_constants.{fixed; parametric = Tezlink_constants.mainnet} + + let read p = + let open Lwt_result_syntax in + let* state = Backend.get_state () in + Backend.read state p + + (* TODO: #7831 !17664 + we type [chain], even though we don't use it, to satisfy the compiler. *) + let balance (chain : [> `Main]) = balance read chain end diff --git a/etherlink/tezt/tests/evm_sequencer.ml b/etherlink/tezt/tests/evm_sequencer.ml index 80725bb1b9d644ca36466e1cc94e921864a9a6e2..e028badfd7db89bd627d84f1c69bd195caa65952 100644 --- a/etherlink/tezt/tests/evm_sequencer.ml +++ b/etherlink/tezt/tests/evm_sequencer.ml @@ -571,7 +571,7 @@ let test_tezlink_balance = let* res = Client.get_balance_for ~endpoint ~account:Constant.bootstrap1.alias client in - Check.((Tez.to_mutez res = 1) int ~error_msg:"Expected %R but got %L") ; + Check.((Tez.to_mutez res = 0) int ~error_msg:"Expected %R but got %L") ; unit let test_tezlink_version =