-
Notifications
You must be signed in to change notification settings - Fork 0
/
bot.php
143 lines (96 loc) · 5.03 KB
/
bot.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
<?php
declare(strict_types=1);
require_once __DIR__ . '/vendor/autoload.php';
if (!isset($_SERVER['SPAMMY_MEMPOOL_WS'])) {
die('SPAMMY_MEMPOOL_WS env var is not defined' . PHP_EOL);
}
if (!isset($_SERVER['ORDISRESPECTOR_MEMPOOL_WS'])) {
die('ORDISRESPECTOR_MEMPOOL_WS env var is not defined' . PHP_EOL);
}
final class MempoolData
{
public readonly int $unconfTxs;
public readonly float $memoryUsageMBs;
public readonly int $minimumFee;
public readonly int $noPrioFee;
public readonly int $lowPrioFee;
public readonly int $mediumPrioFee;
public readonly int $highPrioFee;
public readonly int $nMempoolBlocks;
public function __construct(string $serverEndpoint)
{
$client = new WebSocket\Client("$serverEndpoint/api/v1/ws");
$client->text('{"action":"init"}');
$client->text('{"action":"want","data":["blocks","stats","mempool-blocks","live-2h-chart","watch-mempool"]}');
$raw = json_decode($client->receive());
$this->unconfTxs = $raw->mempoolInfo->size;
$this->memoryUsageMBs = round((float)($raw->mempoolInfo->usage/1000000));
$this->minimumFee = $raw->fees->minimumFee;
$this->noPrioFee = $raw->fees->economyFee;
$this->lowPrioFee = $raw->fees->hourFee;
$this->mediumPrioFee = $raw->fees->halfHourFee;
$this->highPrioFee = $raw->fees->fastestFee;
$mempoolBlocks = $raw->{'mempool-blocks'};
$lastBlock = $mempoolBlocks[array_key_last($mempoolBlocks)];
$this->nMempoolBlocks = $lastBlock->blockVSize <= 1000000 ?
count($mempoolBlocks) : count($mempoolBlocks) + (int)($lastBlock->blockVSize/1000000);
}
public static function formatDelta(int|float $a, int|float $b, float $epsilon = 1.0): string
{
$c = round((($a - $b) / $b) * 100);
return abs($c) < $epsilon ? '=' : "$c%";
}
public function purgingText(MempoolData $spammy = null): string
{
$text = $this->minimumFee > 1 ?
"Purging TXs below (s/vB): **$this->minimumFee**" : 'Not purging TXs (**1** s/vB default)';
if (null !== $spammy) {
$minimumFeeDiff = $this->minimumFee - $spammy->minimumFee;
$minimumFeeDelta = self::formatDelta($this->minimumFee, $spammy->minimumFee);
$text .= " *($minimumFeeDiff, $minimumFeeDelta)*";
}
return $text;
}
}
$now = new DateTimeImmutable('now', new DateTimeZone('UTC'));
$date = $now->format(DateTimeInterface::RSS);
$spammyMempool = new MempoolData($_SERVER['SPAMMY_MEMPOOL_WS']);
$ordisrespector = new MempoolData($_SERVER['ORDISRESPECTOR_MEMPOOL_WS']);
$txsDiff = $ordisrespector->unconfTxs - $spammyMempool->unconfTxs;
$txsDelta = MempoolData::formatDelta($ordisrespector->unconfTxs, $spammyMempool->unconfTxs);
$nBlocksDiff = $ordisrespector->nMempoolBlocks - $spammyMempool->nMempoolBlocks;
$nBlocksDelta = MempoolData::formatDelta($ordisrespector->nMempoolBlocks, $spammyMempool->nMempoolBlocks);
$nodeMemDiff = $ordisrespector->memoryUsageMBs - $spammyMempool->memoryUsageMBs;
$nodeMemDelta = MempoolData::formatDelta($ordisrespector->memoryUsageMBs, $spammyMempool->memoryUsageMBs);
$noPrioFeeDiff = $ordisrespector->noPrioFee - $spammyMempool->noPrioFee;
$noPrioFeeDelta = MempoolData::formatDelta($ordisrespector->noPrioFee, $spammyMempool->noPrioFee);
$lowPrioFeeDiff = $ordisrespector->lowPrioFee - $spammyMempool->lowPrioFee;
$lowPrioFeeDelta = MempoolData::formatDelta($ordisrespector->lowPrioFee, $spammyMempool->lowPrioFee);
$mediumPrioFeeDiff = $ordisrespector->mediumPrioFee - $spammyMempool->mediumPrioFee;
$mediumPrioFeeDelta = MempoolData::formatDelta($ordisrespector->mediumPrioFee, $spammyMempool->mediumPrioFee);
$highPrioFeeDiff = $ordisrespector->highPrioFee - $spammyMempool->highPrioFee;
$highPrioFeeDelta = MempoolData::formatDelta($ordisrespector->highPrioFee, $spammyMempool->highPrioFee);
$note = <<<TEXT
Date: $date
---
#### Spammy Mempool (mempool.space)
Unconfirmed TXs: **$spammyMempool->unconfTxs**
Unconfirmed Blocks: **$spammyMempool->nMempoolBlocks**
Memory Usage (MB): **$spammyMempool->memoryUsageMBs**
No Prio Fee (s/vB): **$spammyMempool->noPrioFee**
Low Prio Fee (s/vB): **$spammyMempool->lowPrioFee**
Medium Prio Fee (s/vB): **$spammyMempool->mediumPrioFee**
Max Prio Fee (s/vB): **$spammyMempool->highPrioFee**
{$spammyMempool->purgingText()}
---
#### Tidy Mempool (Ordisrespector node)
Unconfirmed TXs: **$ordisrespector->unconfTxs** *($txsDiff, $txsDelta)*
Unconfirmed Blocks: **$ordisrespector->nMempoolBlocks** *($nBlocksDiff, $nBlocksDelta)*
Memory Usage (MB): **$ordisrespector->memoryUsageMBs** *($nodeMemDiff, $nodeMemDelta)*
No Prio Fee (s/vB): **$ordisrespector->noPrioFee** *($noPrioFeeDiff, $noPrioFeeDelta)*
Low Prio Fee (s/vB): **$ordisrespector->lowPrioFee** *($lowPrioFeeDiff, $lowPrioFeeDelta)*
Medium Prio Fee (s/vB): **$ordisrespector->mediumPrioFee** *($mediumPrioFeeDiff, $mediumPrioFeeDelta)*
Max Prio Fee (s/vB): **$ordisrespector->highPrioFee** *($highPrioFeeDiff, $highPrioFeeDelta)*
{$ordisrespector->purgingText($spammyMempool)}
TEXT;
echo $note;