Commit f46c5dae authored by Max's avatar Max

ocaml binding for inpage.js

from liqedit to synchronize
parent bbf404ea
OCaml.library("metal-lib",
ocaml + {
files = [
"metal_js.ml", pp_js ;
"metal.ml", pp_js ;
];
requires = [
"js_of_ocaml"
];
});
OCaml.library("metal-lib.lwt",
ocaml + {
files = [
"metal_lwt.ml"
];
requires = [
"lwt";
"metal-lib"
];
});
open Js_of_ocaml
open Metal_js
let installed = has_metal
let is_enabled f =
metal##isEnabled(fun js_b -> f (Js.to_bool js_b))
let is_unlocked f =
metal##isUnlocked(fun js_b -> f (Js.to_bool js_b))
let is_approved f =
metal##isApproved(fun js_b -> f (Js.to_bool js_b))
let get_account f =
metal##getAccount(fun js_str -> f (Js.to_string js_str))
let get_network f =
metal##getNetwork(fun res ->
f (Js.to_string res##.name, Js.to_string res##.url))
let on_state_changed f =
metal##onStateChanged(fun () -> f ())
type 'a result =
| Ok of 'a
| Canceled
type code =
| Nocode
| Code of string * string
| CodeHash of string * string
let map_opt f = function
| None -> Js.Optdef.empty
| Some o -> Js.Optdef.return (f o)
let send ~destination ~amount
?fee ?parameter ?gas_limit ?storage_limit callback =
let destination = Js.string destination in
let amount = Js.string amount in
let fee = map_opt Js.string fee in
let parameter = map_opt Js.string parameter in
let gas_limit = map_opt (fun x -> Js.string (string_of_int x)) gas_limit in
let storage_limit =
map_opt (fun x -> Js.string (string_of_int x)) storage_limit in
let callback = fun res ->
match Js.to_bool res##.ok with
| false -> callback Canceled
| true ->
callback
(Ok (Js.to_string (Js.Optdef.get (res##.msg) (fun () -> assert false))))
in
metal##send(object%js
val mutable dst = destination
val mutable amount = amount
val mutable fee = fee
val mutable parameter = parameter
val mutable gas_limit_ = gas_limit
val mutable storage_limit_ = storage_limit
method cb res = callback res
end)
let originate ~balance ?manager ?(code=Nocode)
?delegate ?delegatable ?spendable
?fee ?gas_limit ?storage_limit callback =
let manager = map_opt Js.string manager in
let balance = Js.string balance in
let fee = map_opt Js.string fee in
let gas_limit = map_opt (fun x -> Js.string (string_of_int x)) gas_limit in
let storage_limit =
map_opt (fun x -> Js.string (string_of_int x)) storage_limit in
let delegate = map_opt Js.string delegate in
let delegatable = map_opt Js.bool delegatable in
let spendable = map_opt Js.bool spendable in
let sc_storage, sc_code, sc_code_hash = match code with
| Nocode -> Js.Optdef.empty, Js.Optdef.empty, Js.Optdef.empty
| Code (code, storage) ->
Js.Optdef.return (Js.string storage),
Js.Optdef.return (Js.string code),
Js.Optdef.empty
| CodeHash (code_hash, storage) ->
Js.Optdef.return (Js.string storage),
Js.Optdef.empty,
Js.Optdef.return (Js.string code_hash)
in
let callback = fun res ->
match Js.to_bool res##.ok with
| false -> callback Canceled
| true ->
let msg = Js.Optdef.get (res##.msg) (fun () -> assert false) in
let op_hash = Js.to_string msg##.op_hash in
let contract = Js.to_string msg##.contract in
callback (Ok (op_hash, contract))
in
metal##originate(object%js
val mutable balance = balance
val mutable manager = manager
val mutable fee = fee
val mutable gas_limit_ = gas_limit
val mutable storage_limit_ = storage_limit
val mutable delegate = delegate
val mutable delegatable = delegatable
val mutable spendable = spendable
val mutable sc_storage_ = sc_storage
val mutable sc_code_ = sc_code
val mutable sc_code_hash_ = sc_code_hash
method cb res = callback res
end)
open Js_of_ocaml
class type network = object
method name : Js.js_string Js.t Js.prop
method url : Js.js_string Js.t Js.prop
end
class type send_result = object
method ok : bool Js.t Js.prop
method msg : Js.js_string Js.t Js.Optdef.t Js.prop
end
class type originate_msg = object
method op_hash : Js.js_string Js.t Js.prop
method contract : Js.js_string Js.t Js.prop
end
class type originate_result = object
method ok : bool Js.t Js.prop
method msg : originate_msg Js.t Js.Optdef.t Js.prop
end
class type send_param = object
method dst : Js.js_string Js.t Js.prop
method amount : Js.js_string Js.t Js.prop (* in mudun *)
method fee : Js.js_string Js.t Js.Optdef.t Js.prop (* in mudun *)
method parameter : Js.js_string Js.t Js.Optdef.t Js.prop (* in json *)
method gas_limit_ : Js.js_string Js.t Js.Optdef.t Js.prop
method storage_limit_ : Js.js_string Js.t Js.Optdef.t Js.prop
method cb : send_result Js.t -> unit Js.meth
end
class type originate_param = object
method balance : Js.js_string Js.t Js.prop (* in mudun *)
method fee : Js.js_string Js.t Js.Optdef.t Js.prop (* in mudun *)
method gas_limit_ : Js.js_string Js.t Js.Optdef.t Js.prop
method storage_limit_ : Js.js_string Js.t Js.Optdef.t Js.prop
method spendable : bool Js.t Js.Optdef.t Js.prop
method delegatable : bool Js.t Js.Optdef.t Js.prop
method delegate : Js.js_string Js.t Js.Optdef.t Js.prop
method manager : Js.js_string Js.t Js.Optdef.t Js.prop
method sc_code_hash_ : Js.js_string Js.t Js.Optdef.t Js.prop (* in json *)
method sc_storage_ : Js.js_string Js.t Js.Optdef.t Js.prop (* in json *)
method sc_code_ : Js.js_string Js.t Js.Optdef.t Js.prop (* in json *)
method cb : originate_result Js.t -> unit Js.meth
end
class type metal = object
method isEnabled : (bool Js.t -> unit) -> unit Js.meth
method isUnlocked : (bool Js.t -> unit) -> unit Js.meth
method isApproved : (bool Js.t -> unit) -> unit Js.meth
method getAccount : (Js.js_string Js.t -> unit) -> unit Js.meth
method getNetwork : (network Js.t -> unit) -> unit Js.meth
method onStateChanged : (unit -> unit) -> unit Js.meth
method send : send_param Js.t -> unit Js.meth
method originate : originate_param Js.t -> unit Js.meth
end
let has_metal = Js.Optdef.test (Js.Unsafe.global##.metal)
let metal : metal Js.t = Js.Unsafe.global##.metal
include Metal
let get_account () =
let (promise, resolver) = Lwt.task () in
get_account (Lwt.wakeup resolver);
promise
let send ~destination
?fee ?parameter ?gas_limit ?storage_limit ~amount =
let (promise, resolver) = Lwt.task () in
send ~destination ~amount
?fee ?parameter ?gas_limit ?storage_limit (Lwt.wakeup resolver);
promise
let originate ?manager ?code
?delegate ?delegatable ?spendable
?fee ?gas_limit ?storage_limit ~balance =
let (promise, resolver) = Lwt.task () in
originate ~balance ?manager ?code
?delegate ?delegatable ?spendable
?fee ?gas_limit ?storage_limit (Lwt.wakeup resolver);
promise
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