25 Commits

Author SHA1 Message Date
zxq9 feae15740a WIP 2025-12-31 01:06:20 +09:00
zxq9 af1639d47b Fix missed version doodoo and patch 2025-12-23 13:55:11 +09:00
zxq9 cb36bad74b Verup patch 2025-12-23 13:50:50 +09:00
zxq9 17a2b867fe Merge pull request 'Add naked reads to read/1' (#16) from nekkid into master
Reviewed-on: #16
2025-12-23 13:48:19 +09:00
zxq9 a35118db7a Add naked reads to read/1 2025-12-23 13:48:12 +09:00
zxq9 b719c946ef Fix site link 2025-12-22 12:27:29 +09:00
zxq9 8b6085dee8 Make edoc happy 2025-12-22 11:52:29 +09:00
zxq9 60c8adb4b1 Update docs 2025-12-22 11:39:46 +09:00
zxq9 ebb84b39a1 Verup 2025-12-22 11:17:01 +09:00
zxq9 e456a96f52 Merge pull request 'formatters' (#14) from formatters into master
Reviewed-on: #14
2025-12-22 11:14:19 +09:00
zxq9 2f93c4d503 Fix docs 2025-12-22 11:14:13 +09:00
zxq9 b542205c0e Merge branch 'master' into formatters 2025-12-22 10:09:21 +09:00
zxq9 e10ef8a460 Remember to wrap final values 2025-12-20 20:38:14 +09:00
zxq9 79eb881208 Formatters work. Still need to write random testers. 2025-12-20 20:22:20 +09:00
zxq9 9ac442ffb4 WIP 2025-12-20 17:26:46 +09:00
zxq9 401f8dec7a WIP 2025-12-20 04:52:42 +09:00
zxq9 81fef99d9c WIP 2025-12-20 04:23:34 +09:00
zxq9 23137a677e WIP 2025-12-20 01:50:15 +09:00
zxq9 b219d0f784 Add "metric" formatting 2025-12-20 00:56:13 +09:00
zxq9 5a145207da WIP 2025-12-11 17:13:02 +09:00
zxq9 11730de24a WIP 2025-12-05 17:38:57 +09:00
zxq9 975325db14 WIP 2025-12-02 19:40:33 +09:00
zxq9 beed46a38b WIP: omg stuff 2025-12-02 15:22:13 +09:00
zxq9 e4e6e35bf8 WIP: Fix remaining cases 2025-12-01 18:36:50 +09:00
zxq9 7393a02de2 WIP: Moving formatters to HZ 2025-12-01 12:06:05 +09:00
11 changed files with 849 additions and 71 deletions
+59 -57
View File
@@ -1,77 +1,79 @@
@author Craig Everett <ceverett@zxq9.com> [https://gitlab.com/zxq9/zj] @author Craig Everett <craigeverett@qpq.swiss> [https://git.qpq.swiss/QPQ-AG/hakuzaru]
@version 0.3.0 @version 0.8.0
@title Vanillae: Aeternity blockchain bindings for Erlang @title Hakuzaru: Gajumaru blockchain bindings for Erlang
@doc @doc
This Erlang application provides bindings for the Erlang blockchain. This Erlang application provides bindings for the Gajumaru blockchain and basic utilities for manipulating Gajumaru-related data.
The primary goal is for usage to be easy to understand and as simple as possible to use.
The secondary goal is to enable real-world projects to more easily connect with the blockchain in an obvious way and provide a clear path for them to provide feedback regarding areas that are difficult to understand, functionality that is lacking, and explain their use cases to us so we can more easily provide needed features and usage examples to make adoption easier.
== Basic operation == == To Start or Not To Start ==
All external interfaces expected to be used by authors of programs that use Vanillae are built into the `vanillae' module. Starting the `hakuzaru' application is only required if you need to query the chain.
When Vanillae is started as an application a named process called `vanillae_man' is spawned that manages interactions with and the state of the service, as well as a simple-one-for-one supervisor that manages the lifecycle of Vanillae workers (defined in `vanillae_fetcher'). The application can be started via a call to `application', or with an explicit call to `hz:start()'.
After startup `vanillae_man' must be given the address and port of a list of Aeternity nodes that are available to service requests. Note that the service nodes will need to have the "dry run" endpoint enabled and the internal service query port made available in order to provide "dry run" and mempool TX submission functionality. Hakuzaru can also be run as a local application from the shell by invoking it with `zxh run hakuzaru' if you have `zx' installed.
The `vanillae_man' will round-robin requests to however many Aeternity nodes are provided in its configuration. Note that this congiruation is dynamic and can be changed completely at runtime. == Operation ==
All blockchain-specific operations are accessible from the main interface modulle: `hz'
== Functions == When Hakuzaru is started as an application a named process called `hz_man' is spawned that manages interactions with chain nodes, as well as a simple-one-for-one supervisor that manages the lifecycle of workers (defined in `hz_fetcher').
The `vanillae' module exposes one function per blockchain feature provided. Most of these are actually wrappers for blockchain endpoint functions, others provide functionality specific to accomplishing a local processing task related to chain data.
== Initialization == After startup `hz_man' must be given the address and port of a list of Gajumaru nodes that are available to service requests.
When Vanillae is first started the vanillae_man is started but does not yet know what Aeternity nodes to use to service queries. You will need to provide it with at least one node and port where it can make Aeternity endpoint calls. Note that the service nodes will need to have the dry-run endpoint enabled and the internal service query port made available in order to provide dry-runs and transaction submission.
Note that if you will need to make read-only calls to contracts that are deployed on chain (to queery their state or perform specific read-only operations provided by the contract) the backend nodes you configure will need to be configured with "dry-run" enabled. When configuring chain nodes a list of nodes should be provided.
To avoid sync issues in the case of fast transaction formation/submission to the chain, only one node from the list of chain nodes is used for submitting transactions and querying `next_nonce/1`.
This node is called "the sticky node".
Example of a shell session where vanillae is started and initialized manually with an AE node in the local network at 192.168.10.10:3013: The first node in the list of chain nodes provided during configuration is designated as the sticky node.
If you also want to use the sticky node as a query endpoint, include it twice in the list.
The `hz_man' will round-robin requests to however many additional Gajumaru nodes are provided in the configuration.
Note that this configuration is dynamic and can be changed at runtime, so your service can adapt to node availability on the fly if needed.
``` ```
1> vanillae:start(). ceverett@steak:~$ zxh run hakuzaru
Starting. Erlang/OTP 27 [erts-15.2] [source] [64-bit] [smp:16:16] [ds:16:16:10] [async-threads:1] [jit:ns]
Eshell V15.2 (press Ctrl+G to abort, type help(). for help)
Fetching otpr-hakuzaru-0.8.0
[100.00%]
Recompile: src/hz_sup
Recompile: src/hz_man
Recompile: src/hz_key_master
Recompile: src/hz_grids
Recompile: src/hz_format
Recompile: src/hz_fetcher
Recompile: src/hz
Recompile: src/hakuzaru
Starting otpr-hakuzaru-0.8.0.
hz_man starting.
Started [hakuzaru]
1> hz:chain_nodes([{"groot.testnet.gajumaru.io", 3013}]).
ok ok
2> vanillae:status(). 2> hz:status().
{error,no_nodes} {ok,#{"difficulty" => 2877405482,
3> vanillae:ae_nodes([{"192.168.7.7", 3013}]). "finalized" =>
ok #{"hash" =>
4> vanillae:status(). "kh_PDSn6Xru5JVdpJfdDCNpfsL8gUZvvjyhTYuzgndoy98G5oLLR",
{ok,#{"difficulty" => 59729882, "height" => 277454,"type" => "height"},
"genesis_key_block_hash" => "genesis_key_block_hash" =>
"kh_wUCideEB8aDtUaiHCtKcfywU6oHZW6gnyci8Mw6S1RSTCnCRu", "kh_Qdi5MTuuhJm7xzn5JUAbYG12cX3qoLMnXrBxPGzBkMWJ4K8vq",
"listening" => true,"network_id" => "ae_uat", "hashrate" => 864394,"listening" => true,
"network_id" => "groot.testnet",
"node_revision" => "node_revision" =>
"3a08153c635c53d92029a617f2e784731ba367c6", "7b3cc1db3bb36053023167b86f7d6f2d5dcbd01d",
"node_version" => "6.7.0", "node_version" => "0.1.0+203.7b3cc1db3",
"peer_connections" => #{"inbound" => 25,"outbound" => 10}, "peer_connections" => #{"inbound" => 1,"outbound" => 3},
"peer_count" => 50, "peer_count" => 5,
"peer_pubkey" => "peer_pubkey" =>
"pp_fCBqobeSwhdnrzC8DoSsmWbf2GzDK61CJujmsCEd3RUkmh9Ny", "pp_2nQHucGyEt5wkYruNuRkg19cbZuEeyR9BZfvtv49F3AoyNSYMT",
"pending_transactions_count" => 2, "pending_transactions_count" => 0,
"protocols" => "protocols" =>
[#{"effective_at_height" => 425900,"version" => 5}, [#{"effective_at_height" => 0,"version" => 1}],
#{"effective_at_height" => 154300,"version" => 4},
#{"effective_at_height" => 82900,"version" => 3},
#{"effective_at_height" => 40900,"version" => 2},
#{"effective_at_height" => 0,"version" => 1}],
"solutions" => 0,"sync_progress" => 100.0, "solutions" => 0,"sync_progress" => 100.0,
"syncing" => false,"top_block_height" => 802644, "syncing" => false,"top_block_height" => 277555,
"top_hash" =>
"kh_2vuNc8eG77aTmHcQDcievjKufFwR4MSSuZbEMWwW5TqUzSQy71",
"top_key_block_hash" => "top_key_block_hash" =>
"kh_28LZSvHZPCGqeWsMsqtSjxQjQHKW1pHzoBex97oMT7U2HcLPgV"}} "kh_2vuNc8eG77aTmHcQDcievjKufFwR4MSSuZbEMWwW5TqUzSQy71"}}
'''
Alternatively, here is a start function for an application using Vanillae that initializes vanillae_man with a list of nodes provided by a configuration file:
```
start(normal, _Args) ->
ok = application:ensure_started(sasl),
{ok, Started} = application:ensure_all_started(cowboy),
ok = application:ensure_started(vanillae),
Nodes = proplists:get_value(ae_nodes, read_config(), []),
ok = vanillae:ae_nodes(Nodes),
ok = log(info, "Started: ~p~n", [[vanillae | Started]]),
Routes = [{'_', [{"/", count_top, []}]}],
Dispatch = cowboy_router:compile(Routes),
Env = #{env => #{dispatch => Dispatch}},
{ok, _} = cowboy:start_clear(count_listener, [{port, 8080}], Env),
count_sup:start_link().
''' '''
+3 -3
View File
@@ -3,7 +3,7 @@
{included_applications,[]}, {included_applications,[]},
{applications,[stdlib,kernel]}, {applications,[stdlib,kernel]},
{description,"Gajumaru interoperation library"}, {description,"Gajumaru interoperation library"},
{vsn,"0.7.0"}, {vsn,"0.8.2"},
{modules,[hakuzaru,hz,hz_fetcher,hz_grids,hz_key_master,hz_man, {modules,[hakuzaru,hz,hz_fetcher,hz_format,hz_grids,
hz_sup]}, hz_key_master,hz_man,hz_sup]},
{mod,{hakuzaru,[]}}]}. {mod,{hakuzaru,[]}}]}.
+1 -1
View File
@@ -6,7 +6,7 @@
%%% @end %%% @end
-module(hakuzaru). -module(hakuzaru).
-vsn("0.7.0"). -vsn("0.8.2").
-author("Craig Everett <ceverett@tsuriai.jp>"). -author("Craig Everett <ceverett@tsuriai.jp>").
-copyright("Craig Everett <ceverett@tsuriai.jp>"). -copyright("Craig Everett <ceverett@tsuriai.jp>").
-license("GPL-3.0-or-later"). -license("GPL-3.0-or-later").
+15 -1
View File
@@ -23,7 +23,7 @@
%%% @end %%% @end
-module(hz). -module(hz).
-vsn("0.7.0"). -vsn("0.8.2").
-author("Craig Everett <ceverett@tsuriai.jp>"). -author("Craig Everett <ceverett@tsuriai.jp>").
-copyright("Craig Everett <ceverett@tsuriai.jp>"). -copyright("Craig Everett <ceverett@tsuriai.jp>").
-license("GPL-3.0-or-later"). -license("GPL-3.0-or-later").
@@ -970,6 +970,7 @@ contract_create(CreatorID, Path, InitArgs) ->
%% of course there are very good reasons why it should be set to a non-zero value %% of course there are very good reasons why it should be set to a non-zero value
%% in the case of calls related to contract-governed payment systems. %% in the case of calls related to contract-governed payment systems.
%% </li> %% </li>
%% <li>
%% <b>TTL:</b> %% <b>TTL:</b>
%% This stands for "Time-To-Live", meaning the height beyond which this element is %% This stands for "Time-To-Live", meaning the height beyond which this element is
%% considered to be eligible for garbage collection (and therefore inaccessible!). %% considered to be eligible for garbage collection (and therefore inaccessible!).
@@ -2460,6 +2461,19 @@ spend3(DSenderID,
hz:post_tx(Encoded). hz:post_tx(Encoded).
-spec sign(Scheme, Target, SecKey) -> Sig
when Scheme :: message | binary | bitcoin,
Target :: binary(),
SecKey :: binary(),
Sig :: binary().
sign(message, Target, SecKey) -> sign_message(Message, SecKey);
sign(binary, Target, SecKey) -> sign_binary(Target, SecKey);
sign(bitcoin, Target, SecKey) -> sign_bitcoin(Target, SecKey).
sign(bitcoin, Target, SecKey) -> sign_message(Target, SecKey).
-spec sign_message(Message, SecKey) -> Sig -spec sign_message(Message, SecKey) -> Sig
when Message :: binary(), when Message :: binary(),
SecKey :: binary(), SecKey :: binary(),
+1 -1
View File
@@ -1,5 +1,5 @@
-module(hz_fetcher). -module(hz_fetcher).
-vsn("0.7.0"). -vsn("0.8.2").
-author("Craig Everett <ceverett@tsuriai.jp>"). -author("Craig Everett <ceverett@tsuriai.jp>").
-copyright("Craig Everett <ceverett@tsuriai.jp>"). -copyright("Craig Everett <ceverett@tsuriai.jp>").
-license("MIT"). -license("MIT").
+728
View File
@@ -0,0 +1,728 @@
%%% @doc
%%% Formatting and reading functions for Gaju and Puck quantities
%%%
%%% The numbers involved in dealing with blockchain amounts are enormous
%%% by comparison to legacy forms of currency. It isn't so much that
%%% thousands of Gajus is hard to reason about, but rather that quadrillions
%%% of Pucks is quite hard to even lock on to visually.
%%%
%%% A normal commas and underscores method of decimal formatting is provided, as
%%% `us' formatting along with two additional approaches:
%%% - Japanese traditional myriad structure (`jp' style)
%%% - An internationalized variant inspired by the Japanese technique over periods
%%% (`metric' for SI prefixes, and `legacy' for Anglicized prefixes)
%%%
%%% These are all accessible via the `amount/N' functions.
%%%
%%% The `read/1' function can accept any of the output variants as a string and
%%% will return the number of pucks indicated by the provided string, allowing for
%%% simple copy/paste functionality as well as direct input using any of the
%%% supported notations.
%%% @end
-module(hz_format).
-vsn("0.8.2").
-author("Craig Everett <ceverett@tsuriai.jp>").
-copyright("Craig Everett <ceverett@tsuriai.jp>").
-license("GPL-3.0-or-later").
-export([amount/1, amount/2, amount/3,
approx_amount/2, approx_amount/3,
read/1,
one/1, mark/1,
price_to_string/1, string_to_price/1]).
-spec amount(Pucks) -> Formatted
when Pucks :: integer(),
Formatted :: string().
%% @doc
%% A convenience formatting function.
%% ```
%% hz_format:amount(1) ->
%% 木0.000,000,000,000,000,001
%%
%% hz_format:amount(5000) ->
%% 木0.000,000,000,000,005
%%
%% hz_format:amount(5000000000000000000) ->
%% 木5
%%
%% hz_format:amount(500000123000000000000000) ->
%% 木500,000.123
%% '''
%% @equiv amount(us, Pucks)
amount(Pucks) ->
amount(us, Pucks).
-spec amount(Style, Pucks) -> Formatted
when Style :: us | jp | metric | legacy | {Separator, Span},
Separator :: $, | $_,
Span :: 3 | 4,
Pucks :: integer(),
Formatted :: string().
%% @doc
%% A money formatting function.
%% ```
%% hz_format:amount(us, 100500040123000000000000000) ->
%% 木100,500,040.123
%%
%% hz_format:amount(jp, 100500040123000000000000000) ->
%% 1億50万40木 12京3000兆本
%%
%% hz_format:amount(metric, 100500040123000000000000000) ->
%% 木100m 500k 40 G 123p P
%%
%% hz_format:amount(legacy, 100500040123000000000000000) ->
%% 木100m 500k 40 G 123q P
%%
%% hz_format:amount({$_, 3}, 100500040123000000000000000) ->
%% 木100_500_040.123
%%
%% hz_format:amount({$_, 4}, 100500040123000000000000000) ->
%% 木1_0050_0040.123
%% '''
%% @equiv amount(gaju, Style, Pucks)
amount(Style, Pucks) ->
amount(gaju, Style, Pucks).
-spec amount(Unit, Style, Pucks) -> Formatted
when Unit :: gaju | puck,
Style :: us | jp | metric | legacy | {Separator, Span},
Separator :: $, | $_,
Span :: 3 | 4,
Pucks :: integer(),
Formatted :: string().
%% @doc
%% A simplified format function covering the most common formats desired.
%% ```
%% hz_format:amount(gaju, us, 100500040123000004500000000) ->
%% 木100,500,040.123,000,004,5
%%
%% hz_format:amount(puck, us, 100500040123000004500000000) ->
%% 本100,500,040,123,000,004,500,000,000
%%
%% hz_format:amount(gaju, jp, 100500040123000004500000000) ->
%% 1億50万40木 12京3000兆45億本
%%
%% hz_format:amount(puck, jp, 100500040123000004500000000) ->
%% 100秭5000垓4012京3000兆45億本
%%
%% hz_format:amount(gaju, metric, 100500040123000004500000000) ->
%% 木100m 500k 40 G 123p 4g 500m P
%%
%% hz_format:amount(puck, metric, 100500040123000004500000000) ->
%% 本100y 500z 40e 123p 4g 500m P
%%
%% hz_format:amount(gaju, legacy, 100500040123000004500000000) ->
%% 木100m 500k 40 G 123q 4b 500m P
%%
%% hz_format:amount(puck, legacy, 100500040123000004500000000) ->
%% 本100y 500z 40e 123q 4b 500m P
%% '''
amount(gaju, us, Pucks) ->
western($,, $., 3, all, Pucks);
amount(puck, us, Pucks) ->
western($,, 3, Pucks);
amount(Unit, jp, Pucks) ->
jp(Unit, all, Pucks);
amount(Unit, metric, Pucks) ->
bestern(Unit, ranks(metric), all, Pucks);
amount(Unit, legacy, Pucks) ->
bestern(Unit, ranks(heresy), all, Pucks);
amount(gaju, {Separator, Span}, Pucks) ->
western(Separator, $., Span, all, Pucks);
amount(puck, {Separator, Span}, Pucks) ->
western(Separator, Span, Pucks).
-spec approx_amount(Precision, Pucks) -> Serialized
when Precision :: all | 0..18,
Pucks :: integer(),
Serialized :: string().
%% A formatter for decimal notation which permits a precision
%% value to be applied to the puck side of the format.
%% ```
%% hz_format:approx_amount(3, 100500040123000004500000001) ->
%% 木100,500,040.123
%%
%% hz_format:approx_amount(13, 100500040123000004500000001) ->
%% 木100,500,040.123,000,004,5...
%%
%% hz_format:approx_amount(all, 100500040123000004500000001) ->
%% 木100,500,040.123,000,004,500,000,001
%% '''
%% @equiv approx_amount(us, Precision, Pucks)
approx_amount(Precision, Pucks) ->
approx_amount(us, Precision, Pucks).
-spec approx_amount(Style, Precision, Pucks) -> Serialized
when Style :: us | {Separator, Span},
Precision :: all | 0..18,
Separator :: $, | $_,
Span :: 3 | 4,
Pucks :: integer(),
Serialized :: string().
%% @doc
%% A formatter for decimal notation which permits a precision
%% value to be applied to the puck side of the format.
%% ```
%% hz_format:approx_amount({$_, 3}, 3, 100500040123000004500000001) ->
%% 木100_500_040.123...
%%
%% hz_format:approx_amount({$_, 4}, 12, 100500040123000004500000001) ->
%% 木1_0050_0040.1230_0000_45...
%% '''
approx_amount(us, Precision, Pucks) ->
western($,, $., 3, Precision, Pucks);
approx_amount({Separator, Span}, Precision, Pucks) ->
western(Separator, $., Span, Precision, Pucks).
western(Separator, Span, Pucks) when Pucks >= 0 ->
western2(Separator, Span, Pucks);
western(Separator, Span, Pucks) when Pucks < 0 ->
[$- | western2(Separator, Span, Pucks * -1)].
western2(Separator, Span, Pucks) ->
P = lists:reverse(integer_to_list(Pucks)),
[mark(puck) | separate(Separator, Span, P)].
western(Separator, Break, Span, Precision, Pucks) when Pucks >= 0 ->
western2(Separator, Break, Span, Precision, Pucks);
western(Separator, Break, Span, Precision, Pucks) when Pucks < 0 ->
[$- | western2(Separator, Break, Span, Precision, Pucks * -1)].
western2(Separator, _, Span, 0, Pucks) ->
G = lists:reverse(integer_to_list(Pucks div one(gaju))),
[mark(gaju) | separate(Separator, Span, G)];
western2(Separator, Break, Span, Precision, Pucks) ->
SP = integer_to_list(Pucks),
Length = length(SP),
Over18 = Length > 18,
NoPucks = (Pucks rem one(gaju)) =:= 0,
case {Over18, NoPucks} of
{true, true} ->
Gs = lists:reverse(lists:sublist(SP, Length - 18)),
[mark(gaju) | separate(Separator, Span, Gs)];
{true, false} ->
{PChars, GChars} = lists:split(18, lists:reverse(SP)),
H = [mark(gaju) | separate(Separator, Span, GChars)],
{P, E} = decimal_pucks(Precision, lists:reverse(PChars)),
T = lists:reverse(separate(Separator, Span, P)),
lists:flatten([H, Break, T, E]);
{false, true} ->
[mark(gaju), $0];
{false, false} ->
PChars = lists:flatten(string:pad(SP, 18, leading, $0)),
{P, E} = decimal_pucks(Precision, PChars),
T = lists:reverse(separate(Separator, Span, P)),
lists:flatten([mark(gaju), $0, Break, T, E])
end.
decimal_pucks(all, PChars) ->
RTrailing = lists:reverse(PChars),
{lists:reverse(lists:dropwhile(fun(C) -> C =:= $0 end, RTrailing)), ""};
decimal_pucks(Precision, PChars) ->
{Significant, Rest} = lists:split(min(Precision, 18), PChars),
RTrailing = lists:reverse(Significant),
Trailing = lists:reverse(lists:dropwhile(fun(C) -> C =:= $0 end, RTrailing)),
case lists:all(fun(C) -> C =:= $0 end, Rest) of
true -> {Trailing, ""};
false -> {Trailing, "..."}
end.
separate(_, _, "") ->
"";
separate(S, P, G) ->
separate(S, P, 1, G, []).
separate(_, _, _, [H], A) ->
[H | A];
separate(S, P, P, [H | T], A) ->
separate(S, P, 1, T, [S, H | A]);
separate(S, P, N, [H | T], A) ->
separate(S, P, N + 1, T, [H | A]).
bestern(gaju, Ranks, Precision, Pucks) when Pucks >= 0 ->
[mark(gaju), bestern2(gaju, Ranks, 3, Precision, Pucks)];
bestern(gaju, Ranks, Precision, Pucks) when Pucks < 0 ->
[$-, mark(gaju), bestern2(gaju, Ranks, 3, Precision, Pucks * -1)];
bestern(puck, Ranks, Precision, Pucks) when Pucks >= 0 ->
[mark(puck), bestern2(puck, Ranks, 3, Precision, Pucks)];
bestern(puck, Ranks, Precision, Pucks) when Pucks < 0 ->
[$-, mark(puck), bestern2(puck, Ranks, 3, Precision, Pucks * -1)].
jp(Unit, Precision, Pucks) when Pucks >= 0 ->
bestern2(Unit, ranks(jp), 4, Precision, Pucks);
jp(Unit, Precision, Pucks) when Pucks < 0 ->
[$, bestern2(Unit, ranks(jp), 4, Precision, Pucks * -1)].
bestern2(gaju, Ranks, Span, 0, Pucks) ->
G = lists:reverse(integer_to_list(Pucks div one(gaju))),
case Span of
3 -> period("G", Ranks, G);
4 -> myriad(mark(gaju), Ranks, G)
end;
bestern2(gaju, Ranks, Span, all, Pucks) ->
P = lists:flatten(string:pad(integer_to_list(Pucks rem one(gaju)), 18, leading, $0)),
Zilch = lists:all(fun(C) -> C =:= $0 end, P),
{H, T} =
case {Span, Zilch} of
{3, false} -> {bestern2(gaju, Ranks, 3, 0, Pucks), period("P", Ranks, lists:reverse(P))};
{4, false} -> {jp(gaju, 0, Pucks), myriad(mark(puck), Ranks, lists:reverse(P))};
{3, true} -> {bestern2(gaju, Ranks, 3, 0, Pucks), ""};
{4, true} -> {jp(gaju, 0, Pucks), ""}
end,
lists:flatten([H, " ", T]);
bestern2(gaju, Ranks, Span, Precision, Pucks) ->
P = lists:flatten(string:pad(integer_to_list(Pucks rem one(gaju)), 18, leading, $0)),
H =
case Span of
3 -> bestern2(gaju, Ranks, 3, 0, Pucks);
4 -> jp(gaju, 0, Pucks)
end,
Digits = min(Precision, 18),
T =
case length(P) < Digits of
false ->
ReverseP = lists:reverse(lists:sublist(P, Digits)),
PuckingString = lists:flatten(string:pad(ReverseP, 18, leading, $0)),
case lists:all(fun(C) -> C =:= $0 end, PuckingString) of
false ->
case Span of
3 -> period("P", Ranks, PuckingString);
4 -> myriad(mark(puck), Ranks, PuckingString)
end;
true ->
""
end;
true ->
[]
end,
lists:flatten([H, " ", T]);
bestern2(puck, Ranks, Span, all, Pucks) ->
P = lists:reverse(integer_to_list(Pucks)),
case lists:all(fun(C) -> C =:= $0 end, P) of
false ->
case Span of
3 -> period("P", Ranks, P);
4 -> myriad(mark(puck), Ranks, P)
end;
true ->
case Span of
3 -> [$0, " P"];
4 -> [$0, mark(puck)]
end
end;
bestern2(puck, Ranks, Span, Precision, Pucks) ->
Digits = min(Precision, 18),
P = lists:flatten(string:pad(integer_to_list(Pucks), 18, leading, $0)),
case length(P) < Digits of
true ->
case Span of
3 -> [$0, " P"];
4 -> [$0, mark(puck)]
end;
false ->
PucksToGive = lists:sublist(P, Digits),
PuckingString = lists:flatten(string:pad(lists:reverse(PucksToGive), 18, leading, $0)),
case lists:all(fun(C) -> C =:= $0 end, PuckingString) of
false ->
case Span of
3 -> period("P", Ranks, PuckingString);
4 -> myriad(mark(puck), Ranks, PuckingString)
end;
true ->
case Span of
3 -> [$0, " P"];
4 -> [$0, mark(puck)]
end
end
end.
period(Symbol, Ranks, [$0, $0, $0 | PT]) ->
rank3(Ranks, PT, [Symbol]);
period(Symbol, Ranks, [P3, $0, $0 | PT]) ->
rank3(Ranks, PT, [P3, 32, Symbol]);
period(Symbol, Ranks, [P3, P2, $0 | PT]) ->
rank3(Ranks, PT, [P2, P3, 32, Symbol]);
period(Symbol, Ranks, [P3, P2, P1 | PT]) ->
rank3(Ranks, PT, [P1, P2, P3, 32, Symbol]);
period(Symbol, _, [P3]) ->
[P3, 32, Symbol];
period(Symbol, _, [P3, P2]) ->
[P2, P3, 32, Symbol].
rank3([_ | RT], [$0, $0, $0 | PT], A) ->
rank3(RT, PT, A);
rank3([RH | RT], [P3, $0, $0 | PT], A) ->
rank3(RT, PT, [P3, RH | A]);
rank3([RH | RT], [P3, P2, $0 | PT], A) ->
rank3(RT, PT, [P2, P3, RH | A]);
rank3([RH | RT], [P3, P2, P1 | PT], A) ->
rank3(RT, PT, [P1, P2, P3, RH | A]);
rank3(_, [$0, $0, $0], A) ->
A;
rank3(_, [$0, $0], A) ->
A;
rank3(_, [$0], A) ->
A;
rank3(_, [], A) ->
A;
rank3([RH | _], [P3, $0, $0], A) ->
[P3, RH | A];
rank3([RH | _], [P3, $0], A) ->
[P3, RH | A];
rank3([RH | _], [P3], A) ->
[P3, RH | A];
rank3([RH | _], [P3, P2, $0], A) ->
[P2, P3, RH | A];
rank3([RH | _], [P3, P2], A) ->
[P2, P3, RH | A];
rank3([RH | _], [P3, P2, P1], A) ->
[P1, P2, P3, RH | A].
myriad(Symbol, Ranks, [$0, $0, $0, $0 | PT]) ->
rank4(Ranks, PT, [Symbol]);
myriad(Symbol, Ranks, [P4, $0, $0, $0 | PT]) ->
rank4(Ranks, PT, [P4, Symbol]);
myriad(Symbol, Ranks, [P4, P3, $0, $0 | PT]) ->
rank4(Ranks, PT, [P3, P4, Symbol]);
myriad(Symbol, Ranks, [P4, P3, P2, $0 | PT]) ->
rank4(Ranks, PT, [P2, P3, P4, Symbol]);
myriad(Symbol, Ranks, [P4, P3, P2, P1 | PT]) ->
rank4(Ranks, PT, [P1, P2, P3, P4, Symbol]);
myriad(Symbol, _, [P4]) ->
[P4, Symbol];
myriad(Symbol, _, [P4, P3]) ->
[P3, P4, Symbol];
myriad(Symbol, _, [P4, P3, P2]) ->
[P2, P3, P4, Symbol].
rank4([_ | RT], [$0, $0, $0, $0 | PT], A) ->
rank4(RT, PT, A);
rank4([RH | RT], [P4, $0, $0, $0 | PT], A) ->
rank4(RT, PT, [P4, RH | A]);
rank4([RH | RT], [P4, P3, $0, $0 | PT], A) ->
rank4(RT, PT, [P3, P4, RH | A]);
rank4([RH | RT], [P4, P3, P2, $0 | PT], A) ->
rank4(RT, PT, [P2, P3, P4, RH | A]);
rank4([RH | RT], [P4, P3, P2, P1 | PT], A) ->
rank4(RT, PT, [P1, P2, P3, P4, RH | A]);
rank4(_, [$0, $0, $0, $0], A) ->
A;
rank4(_, [$0, $0, $0], A) ->
A;
rank4(_, [$0, $0], A) ->
A;
rank4(_, [$0], A) ->
A;
rank4(_, [], A) ->
A;
rank4([RH | _], [P4, $0, $0, $0], A) ->
[P4, RH | A];
rank4([RH | _], [P4, $0, $0], A) ->
[P4, RH | A];
rank4([RH | _], [P4, $0], A) ->
[P4, RH | A];
rank4([RH | _], [P4], A) ->
[P4, RH | A];
rank4([RH | _], [P4, P3, $0, $0], A) ->
[P3, P4, RH | A];
rank4([RH | _], [P4, P3, $0], A) ->
[P3, P4, RH | A];
rank4([RH | _], [P4, P3], A) ->
[P3, P4, RH | A];
rank4([RH | _], [P4, P3, P2, $0], A) ->
[P2, P3, P4, RH | A];
rank4([RH | _], [P4, P3, P2], A) ->
[P2, P3, P4, RH | A];
rank4([RH | _], [P4, P3, P2, P1], A) ->
[P1, P2, P3, P4, RH | A].
ranks(jp) ->
"万億兆京垓秭穣溝澗正載極";
ranks(metric) ->
["k ", "m ", "g ", "t ", "p ", "e ", "z ", "y ", "r ", "Q "];
ranks(heresy) ->
["k ", "m ", "b ", "t ", "q ", "e ", "z ", "y ", "r ", "Q "].
mark(gaju) -> $木;
mark(puck) -> $本.
one(gaju) -> 1_000_000_000_000_000_000;
one(puck) -> 1.
-spec read(Format) -> Result
when Format :: string(),
Result :: {ok, Pucks} | error,
Pucks :: integer().
%% @doc
%% Convert any valid string formatted representation and output a value in pucks.
%% NOTE: This function does not accept approximated values.
%% ```
%% 1> hz_format:read("木100,500,040.123,000,004,5").
%% {ok,100500040123000004500000000}
%% 2> hz_format:read("本100,500,040,123,000,004,500,000,000").
%% {ok,100500040123000004500000000}
%% 3> hz_format:read("1億50万40木 12京3000兆45億本").
%% {ok,100500040123000004500000000}
%% 4> hz_format:read("100秭5000垓4012京3000兆45億本").
%% {ok,100500040123000004500000000}
%% 5> hz_format:read("木100m 500k 40 G 123p 4g 500m P").
%% {ok,100500040123000004500000000}
%% 6> hz_format:read("本100y 500z 40e 123p 4g 500m P").
%% {ok,100500040123000004500000000}
%% 7> hz_format:read("木100m 500k 40 G 123q 4b 500m P").
%% {ok,100500040123000004500000000}
%% 8> hz_format:read("本100y 500z 40e 123q 4b 500m P").
%% {ok,100500040123000004500000000}
%% '''
read([$木 | Rest]) ->
read_w_gajus(Rest, []);
read([$本 | Rest]) ->
read_w_pucks(Rest, []);
read([C | Rest])
when C =:= $- orelse
C =:= $ orelse
C =:= $ ->
case read(Rest) of
{ok, Pucks} -> {ok, Pucks * -1};
Error -> Error
end;
read([C | Rest])
when C =:= 32 orelse % ASCII space
C =:= 12288 orelse % full-width space
C =:= $\t orelse
C =:= $\r orelse
C =:= $\n ->
read(Rest);
read([C | Rest]) when $0 =< C andalso C =< $9 ->
read(Rest, [C], []);
read([C | Rest]) when $ =< C andalso C =< $ ->
NumC = C - $ + $0,
read(Rest, [NumC], []);
read(String) when is_binary(String) ->
read(binary_to_list(String));
read(_) ->
error.
read_w_gajus([C | Rest], A) when $0 =< C andalso C =< $9 ->
read_w_gajus(Rest, [C | A]);
read_w_gajus([C | Rest], A) when $ =< C andalso C =< $ ->
NumC = C - $ + $0,
read_w_gajus(Rest, [NumC | A]);
read_w_gajus([$, | Rest], A) ->
read_w_gajus(Rest, A);
read_w_gajus([$_ | Rest], A) ->
read_w_gajus(Rest, A);
read_w_gajus([$. | Rest], A) ->
case read_w_pucks(Rest, []) of
{ok, P} ->
G = list_to_integer(lists:reverse(A)) * one(gaju),
{ok, G + P};
Error ->
Error
end;
read_w_gajus([], A) ->
G = list_to_integer(lists:reverse(A)) * one(gaju),
{ok, G};
read_w_gajus([C, 32 | Rest], A) ->
read(Rest, [], [{C, A}]);
read_w_gajus([32, $G, 32 | Rest], A) ->
read(Rest, [], [{$G, A}], []);
read_w_gajus([32, $G], A) ->
calc([{$G, A}], []);
read_w_gajus([32, $P], A) ->
calc([], [{$P, A}]);
read_w_gajus(_, _) ->
error.
read_w_pucks([C | Rest], A) when $0 =< C andalso C =< $9 ->
read_w_pucks(Rest, [C | A]);
read_w_pucks([C | Rest], A) when $ =< C andalso C =< $ ->
NumC = C - $ + $0,
read_w_pucks(Rest, [NumC | A]);
read_w_pucks([$, | Rest], A) ->
read_w_pucks(Rest, A);
read_w_pucks([$_ | Rest], A) ->
read_w_pucks(Rest, A);
read_w_pucks([C, 32 | Rest], A) ->
read(Rest, [], [], [{C, A}]);
read_w_pucks([32, $P], A) ->
calc([], [{$P, A}]);
read_w_pucks([], A) ->
Padded = lists:flatten(string:pad(lists:reverse(A), 18, trailing, $0)),
{ok, list_to_integer(Padded)}.
read([C | Rest], A, G) when $0 =< C andalso C =< $9 ->
read(Rest, [C | A], G);
read([C | Rest], A, G) when $ =< C andalso C =< $ ->
NumC = C - $ + $0,
read(Rest, [NumC | A], G);
read([$木], A, G) ->
calc([{$G, A} | G], []);
read([$G], A, G) ->
calc([{$G, A} | G], []);
read([32, $G], A, G) ->
calc([{$G, A} | G], []);
read([32, $G, 32 | Rest], A, G) ->
read(Rest, [], [{$G, A} | G], []);
read([$木, 32 | Rest], A, G) ->
read(Rest, [], [{$G, A} | G], []);
read([$G, 32 | Rest], A, G) ->
read(Rest, [], [{$G, A} | G], []);
read([$本], A, P) ->
calc([], [{$P, A} | P]);
read([$P], A, P) ->
calc([], [{$P, A} | P]);
read([32, $P], A, P) ->
calc([], [{$P, A} | P]);
read([C, 32 | Rest], A, G) ->
read(Rest, [], [{C, A} | G]);
read([$, | Rest], A, []) ->
read_w_gajus(Rest, A);
read([$_ | Rest], A, []) ->
read_w_gajus(Rest, A);
read([$. | Rest], A, []) ->
case read_w_pucks(Rest, []) of
{ok, P} ->
G = list_to_integer(lists:reverse(A)) * one(gaju),
{ok, G + P};
Error ->
Error
end;
read([C | Rest], A, G) ->
read(Rest, [], [{C, A} | G]);
read([], A, []) ->
read_w_gajus([], A);
read(_, _, _) ->
error.
read([C | Rest], A, G, P) when $0 =< C andalso C =< $9 ->
read(Rest, [C | A], G, P);
read([C | Rest], A, G, P) when $ =< C andalso C =< $ ->
NumC = C - $ + $0,
read(Rest, [NumC | A], G, P);
read([$本], A, G, P) ->
calc(G, [{$P, A} | P]);
read([$P], A, G, P) ->
calc(G, [{$P, A} | P]);
read([32, $P], A, G, P) ->
calc(G, [{$P, A} | P]);
read([C, 32 | Rest], A, G, P) ->
read(Rest, [], G, [{C, A} | P]);
read([C | Rest], A, G, P) ->
read(Rest, [], G, [{C, A} | P]);
read(_, _, _, _) ->
error.
calc(G, P) ->
case calc(gaju, G, 0) of
{ok, Gajus} ->
case calc(puck, P, 0) of
{ok, Pucks} -> {ok, Gajus + Pucks};
error -> error
end;
error ->
error
end.
calc(U, [{_, []} | S], A) ->
calc(U, S, A);
calc(U, [{M, Cs} | S], A) ->
case magnitude(M) of
{ok, J} ->
N = list_to_integer(lists:reverse(Cs)) * J * one(U),
calc(U, S, A + N);
Error ->
Error
end;
calc(_, [], A) ->
{ok, A}.
magnitude($G) ->
{ok, 1};
magnitude($P) ->
{ok, 1};
magnitude(Mark) ->
case rank(Mark, ranks(jp), 1_0000, 1) of
{ok, J} ->
{ok, J};
error ->
case rank([Mark, 32], ranks(metric), 1_000, 1) of
{ok, J} -> {ok, J};
error -> rank([Mark, 32], ranks(heresy), 1_000, 1)
end
end.
rank(Mark, [Mark | _], Magnitude, Sum) ->
{ok, Sum * Magnitude};
rank(Mark, [_ | Rest], Magnitude, Sum) ->
rank(Mark, Rest, Magnitude, Sum * Magnitude);
rank(_, [], _, _) ->
error.
-spec price_to_string(Pucks) -> Gajus
when Pucks :: integer(),
Gajus :: string().
%% @doc
%% A simplified formatting function that converts an integer value in Pucks to a string representation
%% in Gajus. Useful for formatting generic output for UI elements
price_to_string(Pucks) ->
Gaju = one(gaju),
H = integer_to_list(Pucks div Gaju),
R = Pucks rem Gaju,
case string:strip(lists:flatten(io_lib:format("~18..0w", [R])), right, $0) of
[] -> H;
T -> string:join([H, T], ".")
end.
-spec string_to_price(Gajus) -> Pucks
when Gajus :: string(),
Pucks :: integer().
%% @doc
%% A simplified formatting function that converts a Gaju value represented as a string to an
%% integer value in Pucks.
string_to_price(String) ->
case string:split(String, ".") of
[H] -> join_price(H, "0");
[H, T] -> join_price(H, T);
_ -> {error, bad_price}
end.
join_price(H, T) ->
try
Parts = [H, string:pad(T, 18, trailing, $0)],
Price = list_to_integer(unicode:characters_to_list(Parts)),
case Price < 0 of
false -> {ok, Price};
true -> {error, negative_price}
end
catch
error:R -> {error, R}
end.
+38 -4
View File
@@ -37,7 +37,7 @@
%%% @end %%% @end
-module(hz_grids). -module(hz_grids).
-vsn("0.7.0"). -vsn("0.8.2").
-export([url/2, url/3, url/4, parse/1, req/2, req/3]). -export([url/2, url/3, url/4, parse/1, req/2, req/3]).
@@ -76,7 +76,7 @@ url2(Instruction, URL = #{path := Path}) ->
GRIDS :: uri_string:uri_string(). GRIDS :: uri_string:uri_string().
%% @doc %% @doc
%% Forms a GRIDS URL for spends or transfers. %% Forms a GRIDS URL for spends or transfers.
%% @equiv uri(Instruction, Recipient, Amount, ""). %% @equiv uri(Instruction, Recipient, Amount, "")
url(Instruction, Recipient, Amount) -> url(Instruction, Recipient, Amount) ->
url(Instruction, Recipient, Amount, ""). url(Instruction, Recipient, Amount, "").
@@ -190,16 +190,48 @@ l_to_i(S) ->
end. end.
-spec req(Type, Message) -> RequestMap
when Type :: {sign, message | binary | bitcoin}
| tx
| ack
| sign,
Message :: binary(),
RequestMap :: map().
%% @doc
%% GRIDS maps always contain the following keys:
%% ```
%% #{"grids" => 1,
%% "chain" => "gajumaru",
%% "network_id" => "groot.mainnet.gajumaru.io",
%% "type" => "message" | "binary" | "binary" | "tx" | "ack"
%% "public_id" => term(),
%% "payload" => string()};
%% '''
req(Type, Message) -> req(Type, Message) ->
req(Type, Message, false). req(Type, Message, false).
req(sign, Message, ID) -> req({sign, message}, Message, ID) ->
#{"grids" => 1, #{"grids" => 1,
"chain" => "gajumaru", "chain" => "gajumaru",
"network_id" => hz:network_id(), "network_id" => hz:network_id(),
"type" => "message", "type" => "message",
"public_id" => ID, "public_id" => ID,
"payload" => Message}; "payload" => Message};
req({sign, binary}, Binary, ID) ->
#{"grids" => 1,
"chain" => "gajumaru",
"network_id" => hz:network_id(),
"type" => "binary",
"public_id" => ID,
"payload" => base64:encode(Binary)};
req({sign, bitcoin}, Binary, ID) ->
#{"grids" => 1,
"chain" => "gajumaru",
"network_id" => hz:network_id(),
"type" => "bitcoin",
"public_id" => ID,
"payload" => base64:encode(Binary)};
req(tx, Data, ID) -> req(tx, Data, ID) ->
#{"grids" => 1, #{"grids" => 1,
"chain" => "gajumaru", "chain" => "gajumaru",
@@ -213,4 +245,6 @@ req(ack, Message, ID) ->
"network_id" => hz:network_id(), "network_id" => hz:network_id(),
"type" => "ack", "type" => "ack",
"public_id" => ID, "public_id" => ID,
"payload" => Message}. "payload" => Message};
req(sign, Message, ID) ->
req({sign, message}, Message, ID).
+1 -1
View File
@@ -8,7 +8,7 @@
%%% @end %%% @end
-module(hz_key_master). -module(hz_key_master).
-vsn("0.7.0"). -vsn("0.8.2").
-export([make_key/1, encode/1, decode/1]). -export([make_key/1, encode/1, decode/1]).
+1 -1
View File
@@ -9,7 +9,7 @@
%%% @end %%% @end
-module(hz_man). -module(hz_man).
-vsn("0.7.0"). -vsn("0.8.2").
-behavior(gen_server). -behavior(gen_server).
-author("Craig Everett <ceverett@tsuriai.jp>"). -author("Craig Everett <ceverett@tsuriai.jp>").
-copyright("Craig Everett <ceverett@tsuriai.jp>"). -copyright("Craig Everett <ceverett@tsuriai.jp>").
+1 -1
View File
@@ -9,7 +9,7 @@
%%% @end %%% @end
-module(hz_sup). -module(hz_sup).
-vsn("0.7.0"). -vsn("0.8.2").
-behaviour(supervisor). -behaviour(supervisor).
-author("Craig Everett <zxq9@zxq9.com>"). -author("Craig Everett <zxq9@zxq9.com>").
-copyright("Craig Everett <zxq9@zxq9.com>"). -copyright("Craig Everett <zxq9@zxq9.com>").
+1 -1
View File
@@ -4,7 +4,7 @@
{prefix,"hz"}. {prefix,"hz"}.
{desc,"Gajumaru interoperation library"}. {desc,"Gajumaru interoperation library"}.
{author,"Craig Everett"}. {author,"Craig Everett"}.
{package_id,{"otpr","hakuzaru",{0,7,0}}}. {package_id,{"otpr","hakuzaru",{0,8,2}}}.
{deps,[{"otpr","sophia",{9,0,0}}, {deps,[{"otpr","sophia",{9,0,0}},
{"otpr","gmserialization",{0,1,3}}, {"otpr","gmserialization",{0,1,3}},
{"otpr","gmbytecode",{3,4,1}}, {"otpr","gmbytecode",{3,4,1}},