123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289 |
- <?php
- /**
- * EOS相关操作
- * User: ben
- * Date: 2018/9/19
- * Time: 下午2:59
- */
- class EosBase extends Singleton {
- /**
- * 解锁钱包
- * @param string $name 钱包名 [0-9a-f]{12}
- * @param string $password 钱包名对应的密码
- */
- function unlockWallet($name = null, $password = null) {
- $name = $name ?: $GLOBALS['walletName'];
- $password = $password ?: $GLOBALS['walletPwd'];
- // 解锁钱包
- $cmd = "cleos wallet unlock -n {$name} --password {$password}";
- exec($cmd);
- }
- /**
- * 创建账号
- * @param string $name 账号名 [0-9a-f]{12}
- * @param string $ownerKey owner的公钥
- * @param string $activeKey active的公钥
- * @param string $creator 创建者账号名 [0-9a-f]{12}
- *
- * @return string json
- */
- function createAccount($name, $ownerKey, $activeKey = '', $creator = null) {
- $creator = $creator ?: $GLOBALS['codeBaccarat'];
- // $cmd = "cleos -u {$GLOBALS['eosUrl']} create account {$creator} {$name} {$ownerKey} {$activeKey} -j";
- $cmd = "cleos -u {$GLOBALS['eosUrl']} system newaccount --stake-net '0.0500 EOS' --stake-cpu '0.1500 EOS' --buy-ram-kbytes 4 {$creator} {$name} {$ownerKey} {$activeKey} -j";
- $json = Eos::execCmd($cmd);
- return $json;
- }
- /**
- * 获取链的概要信息
- * @return mixed
- */
- function getInfo() {
- $cmd = "cleos -u {$GLOBALS['eosUrl']} get info";
- $json = self::execCmd($cmd);
- return json_decode($json, true);
- }
- /**
- * eos或gt币转账给用户
- * @param $from
- * @param $to
- * @param $amount
- * @param $memo
- * @param $transaction_type
- * @param $player_transaction_id
- * @return array
- */
- public function transfer($from, $to, $amount, $memo, $transaction_type, $player_transaction_id = '') {
- $type = strpos(strtolower($amount), 'eos') === false ? 'gt' : 'eos';
- if ($type == 'gt') {
- // $from = $GLOBALS['codeGtAdmin'];
- $cmd = "cleos -u {$GLOBALS['eosUrl']} push action eosgetgtoken transfer '[ \"{$from}\", \"{$to}\", \"{$amount}\", \"{$memo}\" ]' -p {$from} -j";
- } elseif ($type == 'eos') {
- // $from = $GLOBALS['codeBaccarat'];
- $cmd = "cleos -u {$GLOBALS['eosUrl']} transfer {$from} {$to} '{$amount}' '{$memo}' -j";
- } else {
- return null;
- }
- $json = self::execCmd($cmd);
- $log = [
- 'player_transaction_id' => $player_transaction_id,
- 'transaction_type' => $transaction_type,
- 'from' => $from,
- 'to' => $to,
- 'cmd' => $cmd,
- 'json' => $json,
- ];
- // 记录交易,失败重试
- return self::logTrans($log);
- }
- /**
- * 当前交易的块id
- * @var int
- */
- static $last_json = '';
- static $last_trx_id = 0;
- /**
- * 记录交易,并定时检查是否失效了
- * @param $data [`transaction_id`, `player_transaction_id`, `transaction_type`, `from`, `to`, `cmd`, `transaction_state`, `json`, `retry_times`]
- * @return array
- */
- static function logTrans($data) {
- self::$last_json = $data['json'];
- $objTrans = new TableHelper('transaction_log', 'dw_eos');
- $ret = [];
- $flag = false;
- if (!$data['transaction_id']) {
- if (!$data['json']) {
- self::_fillId($data);
- $data['transaction_state'] = -1;
- } else {
- $ret = json_decode($data['json'], true);
- $data['transaction_id'] = $ret['transaction_id'];
- if ($data['transaction_id']) {
- $flag = true;
- $data['block_num'] = $ret['processed']['block_num'];
- } else {
- // 没有id,就需要填充
- self::_fillId($data);
- $data['transaction_state'] = -1;
- }
- }
- }
- self::$last_trx_id = $data['transaction_id'];
- $data['console'] = $data['console'] ?: $ret['processed']['action_traces'][0]['console'];
- $data['create_time'] = date('Y-m-d H:i:s');
- // $objTrans->addObject($data);
- if ($flag) {
- // return $data['transaction_id'];
- return $data;
- } else {
- return null;
- }
- }
- private static function _fillId(&$data) {
- $objTrans = new TableHelper('transaction_log', 'dw_eos');
- $count = $objTrans->getCount();
- $rand = rand(1000, 9999);
- $data['transaction_id'] = "unknown:{$count}:{$rand}";
- }
- /**
- * 获取交易详情
- * @param string $transaction_id
- * @param int $block_num_hint
- * @return string
- */
- static function getTransaction($transaction_id, $block_num_hint) {
- $api = $GLOBALS['eosUrl'] . 'v1/history/get_transaction';
- $data = ["id" => $transaction_id,];
- $block_num_hint = (int)$block_num_hint;
- if ($block_num_hint) {
- $data['block_num_hint'] = $block_num_hint;
- }
- $json = null;
- for ($i = 0; $i < 4; $i++) {
- $post_json = json_encode($data);
- $json = (new dwHttp)->post2($api, $post_json, 3, 3);
- $trx = json_decode($json, true);
- if ($trx['id']) {
- return $json;
- } else {
- $data['block_num_hint']++;
- }
- }
- return $json;
- }
- /**
- * 获取账号信息
- * @param $userName
- *
- * @return string
- */
- static function getAccount($userName) {
- $cmd = "cleos -u {$GLOBALS['eosUrl']} get account {$userName} -j";
- return self::execCmd($cmd);
- }
- /**
- * 获取余额
- * @param $userName
- *
- * @return float
- */
- public static function getBalance($userName, $code = 'eosio.token') {
- $cmd = "cleos -u {$GLOBALS['eosUrl']} get currency balance {$code} {$userName}";
- $result = self::execCmd($cmd);
- $balance = (float) $result;
- return $balance * 10000;
- }
- static function execCmd($cmd) {
- $startTime = microtime(true);
- if (strpos($cmd, ' 2>&1') === false) {
- $cmd = $cmd .' 2>&1';
- }
- // 同时输出错误信息
- $json = shell_exec($cmd);
- $msg = "{$cmd}, ret:" . substr($json, 0, 100);
- CallLog::logModuleCall("Shell:", $msg, null, $json, $startTime);
- CallLog::flushLogs();
- return $json;
- }
- static function log($msg) {
- $date = date('Y-m-d H:i:s', time());
- print_r("[{$date}], {$msg}\n");
- }
- /**
- * 重试多次获取交易id
- * @param $transaction_id
- * @param int $block_num_hint
- * @param int $maxTimes
- * @param int $times
- * @throws Exception
- * @return mixed
- */
- static function getTransaction2($transaction_id, $block_num_hint, $maxTimes = 3, $times = 0) {
- // 检验交易id是否是当前用户,当前gameid
- $json = Eos::getTransaction($transaction_id, $block_num_hint);
- $ret = json_decode($json, true);
- if ($ret['error']) {
- // 3040011 The transaction can not be found
- if ($ret['error']['code'] == 3040011) {
- if ($times < $maxTimes) {
- Tool::sleep($times);
- return self::getTransaction2($transaction_id, $block_num_hint, $maxTimes, $times + 1);
- } else {
- throw new Exception("transaction id:{$transaction_id} is not found.", CODE_PARAM_ERROR);
- }
- }
- }
- return $ret;
- }
- /**
- * 获取真正的块id
- * @param $trxId
- * @param int $block_num
- *
- * @return mixed
- */
- static function getRealBlockNum($trxId, $block_num = 0) {
- if ($block_num || ENV == ENV_DEV) {
- $ret = self::getTransaction2($trxId, $block_num);
- } else {
- $ret = Eos::getTrxBySpark($trxId);
- }
- return $ret['block_num'];
- }
- static function getTrxBySpark($transaction_id) {
- $api_key = '37400d93f3c7eb64428585f1cd355fe8';
- $url = "https://api.eospark.com/api?module=transaction&action=get_transaction_detail_info&apikey={$api_key}&trx_id={$transaction_id}";
- $objHttp = new dwHttp();
- $data = null;
- for ($i = 1; $i <= 3; $i++) {
- $json = $objHttp->get2($url);
- $data = json_decode($json, true);
- if ($data['errno'] == 0) {
- return $data['data'];
- } else {
- sleep($i / 2);
- }
- }
- return $data;
- }
- }
|