EOSIO / eos

An open source smart contract platform
https://developers.eos.io/manuals/eos
MIT License
11.28k stars 3.6k forks source link

<cleos get scope > returns unreadable(encoded) data & how can I decode it? #10777

Closed tea2x closed 3 years ago

tea2x commented 3 years ago

Hi all,

I'm trying to retrieve some data from the bellow command.

cleos -u https://api.canfoundation.io get scope eosio.token --table stat

And here's what it returns:

{ "rows": [{ "code": "eosio.token", "scope": "........el.o3", "table": "stat", "payer": "eosio.token", "count": 1 } ], "more": "" }

"scope": "........el.o3" seems to contain encoded data. Could someone help show me how to decode this "........el.o3"?

Thank you!

praphael commented 3 years ago

If you look in the implementation of eosio.token in eosio.contracts, https://github.com/EOSIO/eosio.contracts/blob/master/contracts/eosio.token/src/eosio.token.cpp When it uses the stat table it does it like this: stats statstable( get_self(), sym.code().raw() ); The second argument is the scope of the table. In this case "........el.o3" happens to be what sym.code().raw() returns

tea2x commented 3 years ago

Thank you @praphael for your comment!

Is there any way to decode "........el.o3" back to sym.code().raw() in coding? I hope to find a way to do that in EOSJS (probably any other technique if possible too), I'd like to automate it!

praphael commented 3 years ago

@Tung40194 You might want to take a look at https://github.com/EOSIO/eos/blob/master/libraries/chain/include/eosio/chain/symbol.hpp#L68-L80

praphael commented 3 years ago

Unfortunately what I posted was probably not helpful - the issue is the "symbol" data type, which consists of the "symbol_code" and "precision". https://github.com/EOSIO/eosio.cdt/blob/master/libraries/eosiolib/core/eosio/symbol.hpp
This is represented by a uint64_t. The upper 56 bits are used to represent the symbol code, while the lower 8 bits are used for the precision.

The chain API call get_table_by_scope, returns the symbol field as a eosio::name https://github.com/EOSIO/eosio.cdt/blob/master/libraries/eosiolib/core/eosio/symbol.hpp

So what you would have to do is first convert the string back to eosio::name, as a uin64_t, which is done here https://github.com/EOSIO/eos/blob/master/libraries/chain/include/eosio/chain/name.hpp#L18-L54.

Converting the raw uin64_t symbol code back to a string is done here https://github.com/EOSIO/eosio.cdt/blob/master/libraries/eosiolib/core/eosio/symbol.hpp#L136-L153

You can also mask out the lower 8 bits to get the precision.

When I do the above, for your string "........el.o3" I get a symbol code of "CAT" with a precision of 67.

tea2x commented 3 years ago

Hi @praphael , It works! Thank you so much for your amazing support! image

tea2x commented 3 years ago

Hi all, Since this issue is resolved. I'd like to close it. Thank you!