nanocurrency / nano-node

Nano is digital currency. Its ticker is: XNO and its currency symbol is: Ӿ
https://nano.org
BSD 3-Clause "New" or "Revised" License
3.48k stars 786 forks source link

Allow RPC 'nano_to_raw' to accept decimals. #3777

Open nano2dev opened 2 years ago

nano2dev commented 2 years ago

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:

https://github.com/nanocurrency/nano-node/blob/4752c66c80a6543ae0c1840e9036377561b85e43/nano/node/json_handler.cpp#L272-L284

clemahieu commented 2 years ago

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

clemahieu commented 2 years ago

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

shryder commented 2 years ago

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 commented 2 years ago

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 commented 2 years ago

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