CommonJS.cpp 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. /*
  2. This file is part of cpp-ethereum.
  3. cpp-ethereum is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation, either version 3 of the License, or
  6. (at your option) any later version.
  7. cpp-ethereum is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with cpp-ethereum. If not, see <http://www.gnu.org/licenses/>.
  13. */
  14. /** @file CommonJS.cpp
  15. * @authors:
  16. * Gav Wood <i@gavwood.com>
  17. * Marek Kotewicz <marek@ethdev.com>
  18. * @date 2014
  19. */
  20. #include "CommonJS.h"
  21. namespace dev
  22. {
  23. std::string prettyU256(u256 _n, bool _abridged)
  24. {
  25. std::string raw;
  26. std::ostringstream s;
  27. if (!(_n >> 64))
  28. s << " " << (uint64_t)_n << " (0x" << std::hex << (uint64_t)_n << ")";
  29. else if (!~(_n >> 64))
  30. s << " " << (int64_t)_n << " (0x" << std::hex << (int64_t)_n << ")";
  31. else if ((_n >> 160) == 0)
  32. {
  33. Address a = right160(_n);
  34. std::string n;
  35. if (_abridged)
  36. n = a.abridged();
  37. else
  38. n = toHex(a.ref());
  39. if (n.empty())
  40. s << "0";
  41. else
  42. s << _n << "(0x" << n << ")";
  43. }
  44. else if (!(raw = fromRaw((h256)_n)).empty())
  45. return "\"" + raw + "\"";
  46. else
  47. s << "" << (h256)_n;
  48. return s.str();
  49. }
  50. namespace eth
  51. {
  52. BlockNumber jsToBlockNumber(std::string const& _js)
  53. {
  54. if (_js == "latest")
  55. return LatestBlock;
  56. else if (_js == "earliest")
  57. return 0;
  58. else if (_js == "pending")
  59. return PendingBlock;
  60. else
  61. return (unsigned)jsToInt(_js);
  62. }
  63. }
  64. }