WalletController   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 96
Duplicated Lines 0 %

Importance

Changes 6
Bugs 0 Features 1
Metric Value
eloc 47
c 6
b 0
f 1
dl 0
loc 96
rs 10
wmc 8

2 Methods

Rating   Name   Duplication   Size   Complexity  
B viewWallet() 0 70 7
A __construct() 0 9 1
1
<?php
2
3
/**
4
 *  * Wallet controller
5
 *  *
6
 * The file contains all the functions use  to display and
7
 * manage the user wallet in admin panel
8
 *
9
 *  * @category   Controllers
10
 *  * @package    SuperHive
11
 *  * @author     Florent Kosmala <[email protected]>
12
 *  * @license    https://www.gnu.org/licenses/gpl-3.0.txt GPL-3.0
13
 *  */
14
15
declare(strict_types=1);
16
17
namespace App\Controllers;
18
19
use Hive\PhpLib\Hive\Condenser as HiveCondenser;
20
use Hive\PhpLib\HiveEngine\Account as HeAccount;
21
use Psr\Container\ContainerInterface;
22
use Psr\Http\Message\ResponseInterface as Response;
23
24
final class WalletController
25
{
26
    private ContainerInterface $app;
27
28
    public function __construct(ContainerInterface $app)
29
    {
30
        $this->app = $app;
31
32
        $session = $this->app->get('session');
33
34
        $this->app->get('view')->getEnvironment()->addGlobal("user", [
35
            'author' => $session['sh_author'],
36
            'signature' => $session['sh_sign'],
37
        ]);
38
    }
39
40
    /**
41
     *  * View wallet function
42
     *  *
43
     * This function display the wallet with all information of account.
44
     * Account must be specified in config file or admin index.
45
     *
46
     * @since available since Realease 0.4.0
47
     *
48
     * @param Response $response
49
     */
50
    public function viewWallet(Response $response): Response
51
    {
52
        $settings = $this->app->get('settings');
53
        $accountFile = $this->app->get('accountfile');
54
        $bcFile = $this->app->get('datadir') . 'bcVars.json';
55
        $heFile = $this->app->get('datadir') . 'heTokens.json';
56
57
        $cache_interval = $settings['delay'];
58
        $current_time = time();
59
60
        /*
61
         *  Get Hive engine tokens from account
62
         */
63
        if ((!file_exists($heFile)) || ($current_time - filemtime($heFile) > $cache_interval)) {
64
            $config = [
65
                'debug' => false,
66
                'heNode' => 'api.hive-engine.com/rpc',
67
                'hiveNode' => 'anyx.io',
68
            ];
69
70
            $heApi = new HeAccount($config);
71
72
            $heResponse = $heApi->getAccountBalance($settings['author']);
73
            $heResult = json_encode($heResponse, JSON_PRETTY_PRINT);
74
            file_put_contents($heFile, $heResult);
75
        }
76
77
        $heTokens = json_decode(file_get_contents($heFile), true);
78
79
        /*
80
         * Get HIVE/ HBD & Savings from account
81
         */
82
        $apiConfig = [
83
            'webservice_url' => $settings['api'],
84
            'debug' => false,
85
        ];
86
        $api = new HiveCondenser($apiConfig);
87
88
        if ((!file_exists($accountFile)) || ($current_time - filemtime($accountFile) > $cache_interval)) {
89
            $result = json_encode($api->getAccounts($settings['author']), JSON_PRETTY_PRINT);
90
            file_put_contents($accountFile, $result);
91
        }
92
93
        if ((!file_exists($bcFile)) || ($current_time - filemtime($bcFile) > ($cache_interval * 2))) {
94
            $result = json_encode($api->getDynamicGlobalProperties(), JSON_PRETTY_PRINT);
95
            file_put_contents($bcFile, $result);
96
        }
97
98
        $account = json_decode(file_get_contents($accountFile), true);
99
100
        /*
101
         *  Convert VESTS to HP
102
         */
103
        $bcVars = json_decode(file_get_contents($bcFile), true);
104
        $vests = [];
105
        $vests['tvfh'] = (float)$bcVars['total_vesting_fund_hive'];
106
        $vests['tvs'] = (float)$bcVars['total_vesting_shares'];
107
        $vests['totalVests'] = $vests['tvfh'] / $vests['tvs'];
108
        $vests['userHP'] = round((float)$account[0]['vesting_shares'] * $vests['totalVests'], 3);
109
        $vests['delegHP'] = round((float)$account[0]['delegated_vesting_shares'] * $vests['totalVests'], 3);
110
111
        /*
112
         * Just render the view with vars
113
         */
114
        return $this->app->get('view')->render($response, '/admin/admin-wallet.html', [
115
            'settings' => $settings,
116
            'vests' => $vests,
117
            'blockchain' => $bcVars,
118
            'hetokens' => $heTokens,
119
            'account' => $account[0],
120
        ]);
121
    }
122
}
123