0

if I declare an asset:

asset a(10, symbol("TST", 0);

and I print it:

a.print();

I get pretty much what I'd expect:

  1. TST

the format's not quite right (10 TST would have been better), but ok. but if the symbol uses 2 decimal digits:

asset a(10, symbol("TST", 2));

the output I now get is:

0.10 TST

WTF??

to add insult to injury, if I pass it from the command line it works just fine:

void eosio::test(asset a)
{
    a.print();
}

called from the command line like this:

cleos push action t test '["10 TST"]' -p t@active

I get the expected:

  1. TST

what is going on here? how am I supposed to initialise my assets when the value I give them changes depending on the precision?

1

An asset must be expressed in terms of an integer, even if the asset itself will have decimal points.

Therefore asset a(1, symbol("EOS", 4)); refers to the smallest possible positive amount of EOS you could have (because 1 is the smallest integer larger than zero).

Given that you have 4 decimal places, this corresponds to 0.0001 EOS.

If you want to express everything how you would expect, then you could do something like this:

#define PRECISION 2
asset a(500*pow(10,PRECISION), symbol("TST", PRECISION));

This would always return 500 TST, regardless of what you set the precision to.

|improve this answer|||||
  • 1
    my gosh. I don't know who designed this asset class but it's abhorrent. so unfriendly. I would take the time to rewrite it, if I felt I had any hope of having it adopted. but thank you for clarifying that! – ekkis Jan 31 '19 at 18:04
  • it also means that cleos does a bit of magic for me, instead of passing it through to the class – ekkis Jan 31 '19 at 18:07
1

EOS use int64_t to represent the amount, so there is no decimal. In order to represent the precision, the symbol type has a precision represent the decimal.

eg:

asset a(10, symbol("TST", 0);

means amount 10, precision 0(the symbol specify the precision is 0), so it will be

  1. TST
asset a(10, symbol("TST", 2));

means amount 10, precision 2,so it will be

0.10 TST

in cleos way

cleos push action t test '["10 TST"]' -p t@active

means amount 10, precision 0, so it will be

  1. TST
cleos push action t test '["10.000 TST"]' -p t@active

means amount 10000, precision 3, so it will be

10.000 TST

Hope that helps.

|improve this answer|||||
  • 1
    thanks for the explanation. it's an absolutely retarded way to do things. if I write 10, I mean 10, not 0.1 or 0.001 or any other such thing. I swear to god I should just rewrite the asset class. it's garbage – ekkis Jan 31 '19 at 21:40

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.