Eos.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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. const NET_EOS = 1;
  13. const NET_MEETONE = 2;
  14. const NET_ETH = 3;
  15. const NET_TRON = 4;
  16. private static $netToAccount = [
  17. self::NET_EOS => Account::TYPE_EOS,
  18. self::NET_MEETONE => Account::TYPE_MEETONE,
  19. self::NET_ETH => Account::TYPE_ETH,
  20. self::NET_TRON => Account::TYPE_TRON,
  21. ];
  22. public static function getNetAccount($net) {
  23. return self::$netToAccount[$net];
  24. }
  25. public static function getAllNet() {
  26. return array_keys(self::$netToAccount);
  27. }
  28. static $objRedisMap = [];
  29. function __construct() {
  30. // 解锁钱包
  31. $this->unlockWallet();
  32. }
  33. static function pubEvent($event, $data, $cacheKey = self::REDIS_SERV) {
  34. // dwRedis::cleanInstance();
  35. // $objRedis = dwRedis::init('dw_box');
  36. if (!self::$objRedisMap[$cacheKey]) {
  37. self::$objRedisMap[$cacheKey] = dwRedis::initNewOne($cacheKey);
  38. }
  39. if (is_array($data)) {
  40. $data = json_encode($data);
  41. }
  42. return self::$objRedisMap[$cacheKey]->publish($event, $data);
  43. }
  44. /**
  45. * 块信息
  46. * @param $blockId
  47. * @return array
  48. */
  49. public static function getBlockInfo($blockId) {
  50. $cmd = "cleos -u https://api.eosnewyork.io get block '{$blockId}'";
  51. $json = self::execCmd($cmd);
  52. return json_decode($json, true);
  53. }
  54. /**
  55. * 获取块id
  56. * @param $block_num
  57. *
  58. * @return array
  59. */
  60. public static function getBlockId($block_num) {
  61. $cmd = "cleos -u {$GLOBALS['eosUrl']} get block {$block_num}";
  62. $json = self::execCmd($cmd);
  63. $json = str_replace("\n", '', $json);
  64. $info = json_decode($json, true);
  65. if ($info && $info['id']) {
  66. $block_id = $info['id'];
  67. } else {
  68. $hex = dechex($block_num);
  69. $hex = sprintf("%08s", $hex);
  70. preg_match("/\"({$hex}[0-9a-fA-F]{56})\"/", $json, $matches);
  71. $block_id = $matches[1];
  72. }
  73. return $block_id;
  74. }
  75. function _formatDateTime($str) {
  76. if (strpos($str, 'T') > 0) {
  77. return date('Y-m-d H:i:s', strtotime($str) + 8 * 3600);
  78. } else {
  79. return date('Y-m-d H:i:s', strtotime($str));
  80. }
  81. }
  82. public static function getAccountActions($account_name, $limit = 200) {
  83. // $cmd = "cleos -u {$GLOBALS['eosUrl']} get actions {$account_name} -1 -{$limit} -j";
  84. if (ENV == ENV_DEV) {
  85. $url = $GLOBALS['eosUrl'];
  86. } else {
  87. // 正式环境,要查历史数据,只能用 nodes.get-scatter.com
  88. $url = 'https://nodes.get-scatter.com';
  89. }
  90. $cmd = "cleos -u {$url} get actions {$account_name} -1 -{$limit} -j";
  91. $json = self::execCmd($cmd);
  92. return json_decode($json, true);
  93. }
  94. /**
  95. * 抢红包
  96. * @author solu
  97. * @param $redpackId
  98. * @param $trxId
  99. * @param $logId
  100. * @param $player
  101. * @param $quantity
  102. * @param $netId
  103. * @return string
  104. */
  105. public static function grabRedpack($redpackId, $trxId, $logId, $player, $quantity, $netId = Eos::NET_EOS) {
  106. $url = $GLOBALS['eosUrl'];
  107. $account = $GLOBALS['codeMee'];
  108. if ($netId == Eos::NET_MEETONE) { // MeetOne侧链红包
  109. $url = $GLOBALS['eosxURL'];
  110. $account = $GLOBALS['codeMeeMO'];
  111. }
  112. $cmd = "cleos -u {$url} push action {$account} grabredpack '[ {$redpackId}, \"{$trxId}\", {$logId}, \"{$player}\", \"{$quantity}\" ]' -j -p {$account}";
  113. $json = self::execCmd($cmd);
  114. $log = [
  115. 'player_transaction_id' => $trxId,
  116. 'transaction_type' => 'grabredpack',
  117. 'from' => $account,
  118. 'to' => $player,
  119. 'cmd' => $cmd,
  120. 'json' => $json,
  121. ];
  122. // 记录交易,失败重试
  123. self::logTrans($log);
  124. return $json;
  125. }
  126. /**
  127. * 退回红包
  128. * @author solu
  129. * @param $redpackId
  130. * @param $trxId
  131. * @param $player
  132. * @return string
  133. */
  134. public static function returnRedpack($redpackId, $trxId, $player, $netId = Eos::NET_EOS) {
  135. $url = $GLOBALS['eosUrl'];
  136. $account = $GLOBALS['codeMee'];
  137. if ($netId == Eos::NET_MEETONE) { // MeetOne侧链红包
  138. $url = $GLOBALS['eosxURL'];
  139. $account = $GLOBALS['codeMeeMO'];
  140. }
  141. $cmd = "cleos -u {$url} push action {$account} returnpack '[ {$redpackId} ]' -j -p {$account}";
  142. $json = self::execCmd($cmd);
  143. $log = [
  144. 'player_transaction_id' => $trxId,
  145. 'transaction_type' => 'returnpack',
  146. 'from' => $account,
  147. 'to' => $player,
  148. 'cmd' => $cmd,
  149. 'json' => $json,
  150. ];
  151. // 记录交易,失败重试
  152. self::logTrans($log);
  153. return $json;
  154. }
  155. public static function toDisplayFormat($amount, $unit = 'EOS') {
  156. return sprintf("%.4f {$unit}", round($amount / 10000, 4));
  157. }
  158. public static function toDisplayFormat2($amount, $unit = 'EOS', $keep = 4) {
  159. return sprintf("%.{$keep}f {$unit}", round($amount / 10000, 4));
  160. }
  161. public static function toNumber($amount) {
  162. $arr = explode(' ', $amount);
  163. return intval(floatval($arr[0]) * 10000);
  164. }
  165. }