Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow RPC 'nano_to_raw' to accept decimals. #3777

Open
nano2dev opened this issue Apr 3, 2022 · 5 comments · May be fixed by #3781
Open

Allow RPC 'nano_to_raw' to accept decimals. #3777

nano2dev opened this issue Apr 3, 2022 · 5 comments · May be fixed by #3781
Labels
rpc Changes related to Remote Procedure Calls
Milestone

Comments

@nano2dev
Copy link

nano2dev commented Apr 3, 2022

Summary

RPC:

{
  "action": "nano_to_raw",
  "amount": "10.50"
}
{ error: 'Invalid amount number' }

What problem would be solved by this feature?

Would be nice if the Node handle decimals.

Why make an rpc call for rather basic arithmetic?

When creating new blocks, it's expected that you provide the 'new balance'... a lot of math that can be solved by the Node.

I'd also like to propose a new RPC call: 'calculate_new_balance' -- or something like that.

The use case is to avoid having to code my own arithmetic.

I'm not lazy, I just don't trust my own code, and prefer to depend on Node when working with such small decimals.

Are there any previous requests for this feature?

Could not find any.

Do you have a suggested solution?

My apologies. Don't speak C++. Only JS.

If this feature is approved, would you be willing to submit a pull request with the solution?

I am willing to collaborate

Possible solution

Accept decimals.

Additionally, (and I can make a separate Issue) add a RPC call(s) to handle 'subtraction' & 'addition' to be used in the 'block_create' RPC and anywhere a 'new balance' is expected.

Supporting files

I was pointe to this file in the source code:

nano::amount nano::json_handler::amount_impl ()
{
nano::amount result (0);
if (!ec)
{
std::string amount_text (request.get<std::string> ("amount"));
if (result.decode_dec (amount_text))
{
ec = nano::error_common::invalid_amount;
}
}
return result;
}

@clemahieu
Copy link
Contributor

We force the API caller to do the conversion because of the general problem of using floating point math in finance.

@clemahieu
Copy link
Contributor

I do wonder if we could interpret it as a fixed-fraction so it can be assured precision won't be lost.

@shryder
Copy link
Contributor

shryder commented Apr 3, 2022

Btw you don't have to write your own code, just use one of the existing Nano libraries to do the conversion. This is one of them if you're writing code in JS:
https://github.com/marvinroger/nanocurrency-js/tree/master/packages/nanocurrency

I agree it would be nice to have a create_send_block RPC where you pass the amount you want to send instead of the updated balance though.

@nano2dev
Copy link
Author

nano2dev commented Apr 3, 2022

Thank you, gentlemen.

Wish I could respond to Colin, intelligently. Math is not my strong suit.

Indeed, a single RPC call that returns the calculated new balance of a "new block" would be lovely.

I feel bad not being able to provide a PR, so for future readers:

I'd like to personally offer a bounty Ӿ50 (~$50) to anyone that provides a PR on my behalf with the proposed changes: 'add decimal support' to the existing RPC 'nano_to_raw'. And core team gives the thumbs up at least.

@aleycryto
Copy link

Ai sẽ giải đáp bài toán này

@zhyatt zhyatt added the rpc Changes related to Remote Procedure Calls label Apr 4, 2022
@qwahzi qwahzi modified the milestones: Backlog, Ongoing Jan 5, 2023
@qwahzi qwahzi modified the milestones: Ongoing, Backlog Sep 26, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
rpc Changes related to Remote Procedure Calls
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants