fc icon indicating copy to clipboard operation
fc copied to clipboard

Utilize `bytes` (bytes array) as various type acceptor

Open conr2d opened this issue 5 years ago • 0 comments

conr2d/fc@5869f1abd6609b956093c3723b5a9c7cf2ee3433

I think that you don't like this idea because of ambiguity which can cause developers' mistake. This patch allows variant to serialize integer, floating-point number, boolean and hex string.

For example,

struct options {
   bool   foo;
   int    bar;
   double baz;
};

[[eosio::action]]
void setoption(std::string key, std::vector<char> rawdata) {
   options opts;
   if( key == "foo" ) {
      auto value = eosio::unpack<bool>(rawdata);
      opts.foo = value;
   } else if( key == "bar" ) {
      auto value = eosio::unpack<int64_t>(rawdata);
      opts.bar = value;
   } else if( key == "baz" ) {
      auto value = eosio::unpack<double>(rawdata);
      opts.baz = value;
   } else { // hex string
      printhex(rawdata.data(), rawdata.size());
   }
   ...
}
cleos push action test setoption '["foo", true]' -p test@active
cleos push action test setoption '["bar", 1234]' -p test@active
cleos push action test setoption '["bar", 0x1234]' -p test@active
cleos push action test setoption '["bar", "1234"]' -p test@active // wrapped by double quotes means hex string
cleos push action test setoption '["baz", 1.234]' -p test@active

Currently DEX usually handles token following standard token interface (eosio.token), but there could be various types of token. For example, non-fungible token is usually represented in 256-bit integer to support 256-bit hash of its metadata directly. If bytes array can serialize various types of raw data, the one action which handles multiple types of token will be available.

[[eosio::action]]
void transfer(name from, name to, std::vector<char> value, std::string memo);

This task can be done manually by advanced users, so I also think that this doesn't need to be supported by default. Just wanna share small experiment. You can close this at any time. :)

conr2d avatar Mar 02 '19 06:03 conr2d