Eos.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. <?php
  2. /**
  3. * EOS相关操作
  4. * User: ben
  5. * Date: 2018/9/19
  6. * Time: 下午2:59
  7. */
  8. class Eos extends EosBase {
  9. const REDIS_SERV = 'dw_chat';
  10. const PRE_ROOM_REWARD = 'globals:dw_chat:room_reward:';
  11. const LIVE_LIMIT = 20;
  12. static $objRedisMap = [];
  13. function __construct() {
  14. // 解锁钱包
  15. $this->unlockWallet();
  16. }
  17. static function pubEvent($event, $data, $cacheKey = self::REDIS_SERV) {
  18. // dwRedis::cleanInstance();
  19. // $objRedis = dwRedis::init('dw_box');
  20. if (!self::$objRedisMap[$cacheKey]) {
  21. self::$objRedisMap[$cacheKey] = dwRedis::initNewOne($cacheKey);
  22. }
  23. if (is_array($data)) {
  24. $data = json_encode($data);
  25. }
  26. return self::$objRedisMap[$cacheKey]->publish($event, $data);
  27. }
  28. /**
  29. * 块信息
  30. * @param $blockId
  31. * @return array
  32. */
  33. public static function getBlockInfo($blockId) {
  34. $cmd = "cleos -u https://api.eosnewyork.io get block '{$blockId}'";
  35. $json = self::execCmd($cmd);
  36. return json_decode($json, true);
  37. }
  38. /**
  39. * 获取块id
  40. * @param $block_num
  41. *
  42. * @return array
  43. */
  44. public static function getBlockId($block_num) {
  45. $cmd = "cleos -u {$GLOBALS['eosUrl']} get block {$block_num}";
  46. $json = self::execCmd($cmd);
  47. $json = str_replace("\n", '', $json);
  48. $info = json_decode($json, true);
  49. if ($info && $info['id']) {
  50. $block_id = $info['id'];
  51. } else {
  52. $hex = dechex($block_num);
  53. $hex = sprintf("%08s", $hex);
  54. preg_match("/\"({$hex}[0-9a-fA-F]{56})\"/", $json, $matches);
  55. $block_id = $matches[1];
  56. }
  57. return $block_id;
  58. }
  59. function _formatDateTime($str) {
  60. if (strpos($str, 'T') > 0) {
  61. return date('Y-m-d H:i:s', strtotime($str) + 8 * 3600);
  62. } else {
  63. return date('Y-m-d H:i:s', strtotime($str));
  64. }
  65. }
  66. public static function getAccountActions($account_name, $limit = 200) {
  67. // $cmd = "cleos -u {$GLOBALS['eosUrl']} get actions {$account_name} -1 -{$limit} -j";
  68. if (ENV == ENV_DEV) {
  69. $url = $GLOBALS['eosUrl'];
  70. } else {
  71. // 正式环境,要查历史数据,只能用 nodes.get-scatter.com
  72. $url = 'https://nodes.get-scatter.com';
  73. }
  74. $cmd = "cleos -u {$url} get actions {$account_name} -1 -{$limit} -j";
  75. $json = self::execCmd($cmd);
  76. return json_decode($json, true);
  77. }
  78. /**
  79. * 抢红包
  80. * @author solu
  81. * @param $redpackId
  82. * @param $trxId
  83. * @param $logId
  84. * @param $player
  85. * @param $quantity
  86. * @return string
  87. */
  88. public static function grabRedpack($redpackId, $trxId, $logId, $player, $quantity) {
  89. $cmd = "cleos -u {$GLOBALS['eosUrl']} push action {$GLOBALS['codeMee']} grabredpack '[ {$redpackId}, \"{$trxId}\", {$logId}, \"{$player}\", \"{$quantity}\" ]' -j -p {$GLOBALS['codeMee']}";
  90. $json = self::execCmd($cmd);
  91. $log = [
  92. 'player_transaction_id' => $trxId,
  93. 'transaction_type' => 'grabredpack',
  94. 'from' => $GLOBALS['codeMee'],
  95. 'to' => $player,
  96. 'cmd' => $cmd,
  97. 'json' => $json,
  98. ];
  99. // 记录交易,失败重试
  100. self::logTrans($log);
  101. return $json;
  102. }
  103. public static function toDisplayFormat($amount, $unit = 'EOS') {
  104. return sprintf("%.4f {$unit}", round($amount / 10000, 4));
  105. }
  106. public static function toDisplayFormat2($amount, $unit = 'EOS', $keep = 4) {
  107. return sprintf("%.{$keep}f {$unit}", round($amount / 10000, 4));
  108. }
  109. public static function toNumber($amount) {
  110. $arr = explode(' ', $amount);
  111. return intval(floatval($arr[0]) * 10000);
  112. }
  113. }