2

in a contract: msg.value is an amount in wei, and I can write 2 ether == 2000 finney as per the docs.

Is there a simple way to compare an amount in wei (specifically msg.value) with a concatenation of an amount (as a uint) and a "denomination" (as a string that could be wei, finney, szabo or ether ) ?

if not that would be a cool library !

edit: added pseudo-code, the unit & denomiation is clearly not correct, that's what I called a concatenation above:

function inferior(uint unit, string denomination) public returns(bool){
         if (msg.value < unit & denomination) {
             return true;
         }
inferior(100, "finney");
  • Isn't 2 ether == 2000 finney exactly the sort of thing you're talking about? Can you give an example of the sort of comparison you're trying to make that isn't supported? Or do you just want to be able to specify the unit at runtime (if so, why?) – Nick Johnson Apr 14 '16 at 12:43
  • added somne code, I hope it's clearer – euri10 Apr 14 '16 at 12:49
  • 1
    A more usual approach is to rely on your frontend DApp to convert all values into wei for you, to avoid string parsing in Solidity (and to make the interface cleaner). Can you explain your use-case in more detail? Why do you need to be able to do this? – Nick Johnson Apr 14 '16 at 12:54
  • mmm effectively that seems way simpler, I don't know why I was trying to over complicate things, typical "should have thought better about it befoore asking question" – euri10 Apr 14 '16 at 12:56
  • DApps are different in structure to other types of app; we're all still figuring out what patterns work best for them. :) – Nick Johnson Apr 14 '16 at 13:00
2

Summarizing from the comments, general guidelines are:

  • contracts should store balances in wei (to avoid division and rounding inaccuracies)
  • contracts should keep computation to a minimum (since paying a miner is much more expensive than performing the same computation locally)
  • data conversions are an example of computation that should be done by the frontend locally (instead of by a contract on the blockchain)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.