Skip to content

Commit 4168df4

Browse files
committed
added class for calculating bandwidth for accounts
1 parent ef30faf commit 4168df4

File tree

1 file changed

+111
-0
lines changed

1 file changed

+111
-0
lines changed

Tools/Bandwidth.php

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
<?php
2+
3+
4+
namespace GrapheneNodeClient\Tools;
5+
6+
7+
8+
use GrapheneNodeClient\Commands\CommandQueryData;
9+
use GrapheneNodeClient\Commands\Commands;
10+
use GrapheneNodeClient\Connectors\ConnectorInterface;
11+
12+
class Bandwidth
13+
{
14+
const BANDWIDTH_PRECISION = 1000000;
15+
16+
/**
17+
* @param $trxString
18+
* @param $bandwidthPrecision
19+
*
20+
* @return float|int in bytes
21+
*/
22+
public static function getTrxBandwidth($trxString, $bandwidthPrecision)
23+
{
24+
$trxBytes = mb_strlen($trxString, '8bit');
25+
26+
return $bandwidthPrecision * $trxBytes;
27+
}
28+
29+
/**
30+
* @param string $accountAverageBandwidth
31+
*
32+
* @return string in bytes
33+
*/
34+
public static function getAccountUsedBandwidth($accountAverageBandwidth)
35+
{
36+
return gmp_strval(gmp_div_q($accountAverageBandwidth, self::BANDWIDTH_PRECISION, GMP_ROUND_MINUSINF));
37+
}
38+
39+
/**
40+
* @param string $accountVShares
41+
* @param string $totalVestingShares
42+
* @param string $maxVirtualBandwidth
43+
*
44+
* @return string in bytes
45+
*/
46+
public static function getAccountAvailableBandwidth($accountVShares, $totalVestingShares, $maxVirtualBandwidth)
47+
{
48+
return gmp_strval(gmp_div_q(
49+
gmp_div_q($maxVirtualBandwidth, self::BANDWIDTH_PRECISION, GMP_ROUND_MINUSINF),
50+
gmp_div_q($totalVestingShares, $accountVShares, GMP_ROUND_PLUSINF),
51+
GMP_ROUND_MINUSINF
52+
));
53+
}
54+
55+
/**
56+
57+
* @param string $accountName
58+
* @param string $type 'market'/'forum'
59+
* @param ConnectorInterface $connector
60+
*
61+
* @return array ['used' => int, 'available' => int] in bytes. For upvotes and comments 1:1, for transfers 10:1
62+
* @throws \Exception
63+
*/
64+
public static function getBandwidthByAccountName($accountName, $type, ConnectorInterface $connector)
65+
{
66+
$commands = new Commands($connector);
67+
$commands->get_dynamic_global_properties();
68+
$commandQueryData = new CommandQueryData();
69+
$answer = $commands->execute(
70+
$commandQueryData
71+
);
72+
if (!isset($answer['result'])) {
73+
throw new \Exception('wrong api answer for get_dynamic_global_properties');
74+
}
75+
$globalProp = $answer['result'];
76+
$maxVirtualBandwidth = $globalProp['max_virtual_bandwidth'];
77+
$totalVestingShares = str_replace(' VESTS', '', $globalProp['total_vesting_shares']);
78+
$totalVestingShares = substr($totalVestingShares, 0, strpos($totalVestingShares, '.'));
79+
80+
$commands->get_accounts();
81+
$commandQueryData->setParams(
82+
[
83+
0 => [$accountName]
84+
]
85+
);
86+
$answer = $commands->execute(
87+
$commandQueryData
88+
);
89+
if (!isset($answer['result'])) {
90+
throw new \Exception('wrong api answer for get_accounts');
91+
}
92+
$account = $answer['result'][0];
93+
$accountVShares = str_replace(' VESTS', '', $account['vesting_shares']);
94+
$accountVShares = substr($accountVShares, 0, strpos($accountVShares, '.'));
95+
96+
$paramNamePrefix = '';
97+
if ($connector->getPlatform() === ConnectorInterface::PLATFORM_GOLOS) {
98+
$paramNamePrefix = 'new_';
99+
}
100+
$paramName = $paramNamePrefix . 'average_bandwidth';
101+
if ($type === 'market') {
102+
$paramName = $paramNamePrefix . 'average_market_bandwidth';
103+
}
104+
$accountAverageBandwidth = $account[$paramName];
105+
106+
return [
107+
'used' => (int)self::getAccountUsedBandwidth($accountAverageBandwidth),
108+
'available' => (int)self::getAccountAvailableBandwidth($accountVShares, $totalVestingShares, $maxVirtualBandwidth)
109+
];
110+
}
111+
}

0 commit comments

Comments
 (0)