From 2526c66094e78344b1d4844f784941402f08bb13 Mon Sep 17 00:00:00 2001 From: Guten Ye Date: Fri, 22 Mar 2019 14:27:12 +0800 Subject: [PATCH 1/4] Add estimate_fee --- monero_utils/MyMoneroCoreBridgeClass.js | 10 ++++++++++ src/index.cpp | 20 ++++++++++++++++++++ tests/MyMoneroCoreCpp.node.js | 7 +++++++ 3 files changed, 37 insertions(+) diff --git a/monero_utils/MyMoneroCoreBridgeClass.js b/monero_utils/MyMoneroCoreBridgeClass.js index b09d1b4..870ca91 100644 --- a/monero_utils/MyMoneroCoreBridgeClass.js +++ b/monero_utils/MyMoneroCoreBridgeClass.js @@ -217,6 +217,16 @@ class MyMoneroCoreBridgeClass extends MyMoneroCoreBridgeEssentialsClass mask: ret.mask, }; } + estimate_fee(args) + { + const args_str = JSON.stringify(args); + const ret_string = this.Module.estimate_fee(args_str); + const ret = JSON.parse(ret_string); + if (typeof ret.err_msg !== 'undefined' && ret.err_msg) { + throw ret.err_msg; + } + return ret.retVal; + } estimate_rct_tx_size(n_inputs, mixin, n_outputs, extra_size, bulletproof) { const args = diff --git a/src/index.cpp b/src/index.cpp index 4b0f487..b695c5b 100644 --- a/src/index.cpp +++ b/src/index.cpp @@ -175,6 +175,25 @@ string estimated_tx_network_fee(const string &args_string) return serial_bridge_utils::error_ret_json_from_message(e.what()); } } +// +string estimate_fee(const string &args_string) +{ + try { + return serial_bridge::estimate_fee(args_string); + } catch (std::exception &e) { + return serial_bridge_utils::error_ret_json_from_message(e.what()); + } +} +// +string estimate_tx_weight(const string &args_string) +{ + try { + return serial_bridge::estimate_tx_weight(args_string); + } catch (std::exception &e) { + return serial_bridge_utils::error_ret_json_from_message(e.what()); + } +} +// string estimate_rct_tx_size(const string &args_string) { try { @@ -272,6 +291,7 @@ EMSCRIPTEN_BINDINGS(my_module) emscripten::function("address_and_keys_from_seed", &address_and_keys_from_seed); // emscripten::function("estimated_tx_network_fee", &estimated_tx_network_fee); + emscripten::function("estimate_fee", &estimate_fee); emscripten::function("estimate_rct_tx_size", &estimate_rct_tx_size); // emscripten::function("generate_key_image", &generate_key_image); diff --git a/tests/MyMoneroCoreCpp.node.js b/tests/MyMoneroCoreCpp.node.js index 6a4daa8..7a6b3f9 100644 --- a/tests/MyMoneroCoreCpp.node.js +++ b/tests/MyMoneroCoreCpp.node.js @@ -133,6 +133,13 @@ function tests(Module) console.timeEnd("estimated_tx_network_fee") console.log("estimated_tx_network_fee ret", ret_string) } + { + console.time("estimate_fee") + const args = {"use_per_byte_fee": "true", "use_rct": "true", "n_inputs": "2", "mixin": "10", "n_outputs": "2","extra_size": "0", "bulletproof": "true", "base_fee": "24658", "fee_quantization_mask": "10000", "priority": "2", "fork_version": "10"} + const ret_string = Module.estimate_fee(JSON.stringify(args)) + console.timeEnd("estimate_fee") + console.log("estimate_fee ret", ret_string) + } { console.time("estimate_rct_tx_size") const args_str = '{"n_inputs":1,"mixin":10,"n_outputs":2,"extra_size":0,"bulletproof":true}' From 0f0c5a3050b959052ab3de3ebb864d20203551d4 Mon Sep 17 00:00:00 2001 From: Guten Ye Date: Sat, 23 Mar 2019 21:58:13 +0800 Subject: [PATCH 2/4] Add estimate_tx_weight --- monero_utils/MyMoneroCoreBridgeClass.js | 10 ++++++++++ src/index.cpp | 1 + tests/MyMoneroCoreCpp.node.js | 7 +++++++ 3 files changed, 18 insertions(+) diff --git a/monero_utils/MyMoneroCoreBridgeClass.js b/monero_utils/MyMoneroCoreBridgeClass.js index 870ca91..7b33827 100644 --- a/monero_utils/MyMoneroCoreBridgeClass.js +++ b/monero_utils/MyMoneroCoreBridgeClass.js @@ -227,6 +227,16 @@ class MyMoneroCoreBridgeClass extends MyMoneroCoreBridgeEssentialsClass } return ret.retVal; } + estimate_tx_weight(args) + { + const args_str = JSON.stringify(args); + const ret_string = this.Module.estimate_tx_weight(args_str); + const ret = JSON.parse(ret_string); + if (typeof ret.err_msg !== 'undefined' && ret.err_msg) { + throw ret.err_msg; + } + return parseInt(ret.retVal, 10); + } estimate_rct_tx_size(n_inputs, mixin, n_outputs, extra_size, bulletproof) { const args = diff --git a/src/index.cpp b/src/index.cpp index b695c5b..71009e5 100644 --- a/src/index.cpp +++ b/src/index.cpp @@ -292,6 +292,7 @@ EMSCRIPTEN_BINDINGS(my_module) // emscripten::function("estimated_tx_network_fee", &estimated_tx_network_fee); emscripten::function("estimate_fee", &estimate_fee); + emscripten::function("estimate_tx_weight", &estimate_tx_weight); emscripten::function("estimate_rct_tx_size", &estimate_rct_tx_size); // emscripten::function("generate_key_image", &generate_key_image); diff --git a/tests/MyMoneroCoreCpp.node.js b/tests/MyMoneroCoreCpp.node.js index 7a6b3f9..6f480ff 100644 --- a/tests/MyMoneroCoreCpp.node.js +++ b/tests/MyMoneroCoreCpp.node.js @@ -140,6 +140,13 @@ function tests(Module) console.timeEnd("estimate_fee") console.log("estimate_fee ret", ret_string) } + { + console.time("estimate_tx_weight") + const args = {"use_rct": "true", "n_inputs": "2", "mixin": "10", "n_outputs": "2","extra_size": "0", "bulletproof": "true"} + const ret_string = Module.estimate_tx_weight(JSON.stringify(args)) + console.timeEnd("estimate_tx_weight") + console.log("estimate_tx_weight ret", ret_string) + } { console.time("estimate_rct_tx_size") const args_str = '{"n_inputs":1,"mixin":10,"n_outputs":2,"extra_size":0,"bulletproof":true}' From c1c562a0ef47ad62d34bbf704661ee77817e206d Mon Sep 17 00:00:00 2001 From: Guten Ye Date: Sat, 23 Mar 2019 22:01:40 +0800 Subject: [PATCH 3/4] Update README --- README.md | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/README.md b/README.md index 18d3492..eeae663 100644 --- a/README.md +++ b/README.md @@ -246,10 +246,16 @@ decodeRct decodeRctSimple ``` +``` +estimate_fee +``` ``` estimated_tx_network_fee ``` ``` +estimate_tx_weight +``` +``` estimate_rct_tx_size ``` From d94a4f3c04bf380b545264b9cb4fcbdf0000f242 Mon Sep 17 00:00:00 2001 From: Guten Ye Date: Tue, 26 Mar 2019 13:20:05 +0800 Subject: [PATCH 4/4] Move tests from MyMOneroCoreCpp.node.js to monero_utils.spec.js --- tests/MyMoneroCoreCpp.node.js | 14 -------------- tests/monero_utils.spec.js | 36 ++++++++++++++++++++++++++++++++++- 2 files changed, 35 insertions(+), 15 deletions(-) diff --git a/tests/MyMoneroCoreCpp.node.js b/tests/MyMoneroCoreCpp.node.js index 6f480ff..6a4daa8 100644 --- a/tests/MyMoneroCoreCpp.node.js +++ b/tests/MyMoneroCoreCpp.node.js @@ -133,20 +133,6 @@ function tests(Module) console.timeEnd("estimated_tx_network_fee") console.log("estimated_tx_network_fee ret", ret_string) } - { - console.time("estimate_fee") - const args = {"use_per_byte_fee": "true", "use_rct": "true", "n_inputs": "2", "mixin": "10", "n_outputs": "2","extra_size": "0", "bulletproof": "true", "base_fee": "24658", "fee_quantization_mask": "10000", "priority": "2", "fork_version": "10"} - const ret_string = Module.estimate_fee(JSON.stringify(args)) - console.timeEnd("estimate_fee") - console.log("estimate_fee ret", ret_string) - } - { - console.time("estimate_tx_weight") - const args = {"use_rct": "true", "n_inputs": "2", "mixin": "10", "n_outputs": "2","extra_size": "0", "bulletproof": "true"} - const ret_string = Module.estimate_tx_weight(JSON.stringify(args)) - console.timeEnd("estimate_tx_weight") - console.log("estimate_tx_weight ret", ret_string) - } { console.time("estimate_rct_tx_size") const args_str = '{"n_inputs":1,"mixin":10,"n_outputs":2,"extra_size":0,"bulletproof":true}' diff --git a/tests/monero_utils.spec.js b/tests/monero_utils.spec.js index ad3580b..bc46fa9 100644 --- a/tests/monero_utils.spec.js +++ b/tests/monero_utils.spec.js @@ -181,5 +181,39 @@ describe("cryptonote_utils tests", function() { "4501", // TODO: is this correct? ); }); - + it("estimate_fee", async function() { + const monero_utils = await require("../monero_utils/MyMoneroCoreBridge")({}) + var fee = monero_utils.estimate_fee({ + use_per_byte_fee: true, + use_rct: true, + n_inputs: 2, + mixin: 10, + n_outputs: 2, + extra_size: 0, + bulletproof: true, + base_fee: 24658, + fee_quantization_mask: 10000, + priority: 2, + fork_version: 10 + }); + assert.equal( + fee, + 330050000 + ); + }); + it("estimate_tx_weight", async function() { + const monero_utils = await require("../monero_utils/MyMoneroCoreBridge")({}) + var weight = monero_utils.estimate_tx_weight({ + use_rct: true, + n_inputs: 2, + mixin: 10, + n_outputs: 2, + extra_size: 0, + bulletproof: true, + }); + assert.equal( + weight, + 2677 + ); + }); });