Skip to content

Commit aa6f86b

Browse files
committed
Fix getting price
1 parent 8a43630 commit aa6f86b

File tree

3 files changed

+29
-16
lines changed

3 files changed

+29
-16
lines changed

app/Console/Commands/AuxCommand.php

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ class AuxCommand extends Command{
1919

2020
const bittrex = 'https://api.bittrex.com/v3/markets/LBC-BTC/ticker';
2121

22+
const coingeckoURL = 'https://api.coingecko.com/api/v3/simple/price?ids=lbry-credits&vs_currencies=btc';
23+
2224
const blockchainticker = 'https://blockchain.info/ticker';
2325

2426
const lbcpricekey = 'lbc.price';
@@ -170,11 +172,13 @@ public function pricehistory(): void{
170172
}
171173
}
172174

173-
$btrxjson = null; //TODO json_decode(self::curl_get(self::bittrex));
175+
$coingeckoJSON = Cache::remember('coingecko',60,static function(){
176+
return json_decode(self::curl_get(self::coingeckoURL));
177+
});
174178
$blckjson = json_decode(self::curl_get(self::blockchainticker));
175179

176-
if ($btrxjson) {
177-
$btc = $btrxjson->bidRate;
180+
if ($coingeckoJSON) {
181+
$btc = $coingeckoJSON->{'lbry-credits'}->btc;
178182
$usd = 0;
179183
if (isset($blckjson->USD)) {
180184
$usd = $btc * $blckjson->USD->buy;
@@ -195,7 +199,7 @@ public function pricehistory(): void{
195199
}
196200
}
197201
} else {
198-
echo "bittrex requrest returned an invalid result.\n";
202+
echo "CoinGecko requrest returned an invalid result.\n";
199203
}
200204
} catch (Exception $e) {
201205
print_r($e);

app/Http/Controllers/MainController.php

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@ class MainController extends Controller{
4747

4848
const bittrexMarketUrl = 'https://api.bittrex.com/v3/markets/LBC-BTC/ticker';
4949

50+
const coingeckoURL = 'https://api.coingecko.com/api/v3/simple/price?ids=lbry-credits&vs_currencies=btc';
51+
5052
const blockchainTickerUrl = 'https://blockchain.info/ticker';
5153

5254
const tagReceiptAddress = 'bLockNgmfvnnnZw7bM6SPz6hk5BVzhevEp';
@@ -94,11 +96,13 @@ protected function _getLatestPrice(): string{
9496
}
9597

9698
if ($shouldRefreshPrice) {
97-
$btrxjson = json_decode(self::curl_get(self::bittrexMarketUrl));
99+
$coingeckoJSON = Cache::remember('coingecko',60,static function(){
100+
return json_decode(self::curl_get(self::coingeckoURL));
101+
});
98102
$blckjson = json_decode(self::curl_get(self::blockchainTickerUrl));
99103

100-
if ($btrxjson) {
101-
$onelbc = $btrxjson->bidRate;
104+
if ($coingeckoJSON) {
105+
$onelbc = $coingeckoJSON->{'lbry-credits'}->btc;
102106
$lbcPrice = 0;
103107
if (isset($blckjson->USD)) {
104108
$lbcPrice = $onelbc * $blckjson->USD->buy;
@@ -162,7 +166,7 @@ public function claims($id = null): JsonResponse|Response|RedirectResponse|View{
162166
// $count = $stmt->fetch(\PDO::FETCH_OBJ);
163167
$numClaims = 20000000;
164168

165-
$stmt = $conn->getPdo()->query('SELECT MAX(id) AS MaxId FROM claim');
169+
$stmt = $conn->getPdo()->query('SELECT MAX(Id) AS MaxId FROM Claims');
166170
$res = $stmt->fetch(PDO::FETCH_OBJ);
167171
$maxClaimId = $res->MaxId;
168172

@@ -181,7 +185,7 @@ public function claims($id = null): JsonResponse|Response|RedirectResponse|View{
181185
}
182186

183187
$blockedList = json_decode($this->_getBlockedList());
184-
$claims = Claim::query()->addSelect(['publisher' => 'C.name', 'publisher_transaction_hash_id' => 'C.transaction_hash_id', 'publisher_vout' => 'C.vout'])->leftJoin('Claims','claim_id','=','Claims.publisher_id')->where('Claims.id','>',$startLimitId)->where( 'Claims.id','<=',$endLimitId)->orderByDesc('Claims.id')->get();
188+
$claims = [];//Claim::query()->selectRaw('Name AS Publisher')->addSelect(['publisher' => 'C.name', 'publisher_transaction_hash_id' => 'C.transaction_hash_id', 'publisher_vout' => 'C.vout'])->leftJoin('Claims','claim_id','=','Claims.publisher_id')->where('Claims.id','>',$startLimitId)->where( 'Claims.Id','<=',$endLimitId)->orderByDesc('Id')->get();
185189

186190
for ($i = 0; $i < count($claims); $i++) {
187191
if ($canConvert && $claims[$i]->fee > 0 && $claims[$i]->fee_currency == 'USD') {
@@ -344,7 +348,7 @@ public function blocks($height = null): JsonResponse|Response|RedirectResponse|V
344348
$page = intval(request()->query('page'));
345349

346350
$conn = DB::connection();
347-
$stmt = $conn->getPdo()->query('SELECT height AS Total FROM block order by id desc limit 1');
351+
$stmt = $conn->getPdo()->query('SELECT Height AS Total FROM Blocks order by Id desc limit 1');
348352
$count = $stmt->fetch(PDO::FETCH_OBJ);
349353
$numBlocks = $count->Total;
350354

@@ -357,8 +361,8 @@ public function blocks($height = null): JsonResponse|Response|RedirectResponse|V
357361
}
358362

359363
$offset = ($page - 1) * $pageLimit;
360-
$currentBlock = Block::query()->select(['height'])->orderByDesc('height')->first();
361-
$blocks = Block::query()->select(['height', 'difficulty', 'block_size', 'nonce', 'block_time','tx_count'])->offset($offset)->limit($pageLimit)->orderByDesc('height')->get();
364+
$currentBlock = Block::query()->select(['Height'])->orderByDesc('Height')->first();
365+
$blocks = Block::query()->select(['Height', 'Difficulty', 'BlockSize', 'Nonce', 'BlockTime'])->offset($offset)->limit($pageLimit)->orderByDesc('Height')->get();//'tx_count'
362366

363367
return self::generateResponse('main.blocks',[
364368
'currentBlock' => $currentBlock,
@@ -632,8 +636,9 @@ public function apiblocksize($timePeriod = '24h'): array|JsonResponse{
632636

633637
$conn = DB::connection();
634638
// get avg block sizes for the time period
635-
$stmt = $conn->getPdo()->query("SELECT AVG(block_size) AS AvgBlockSize, DATE_FORMAT(FROM_UNIXTIME(block_time), '$sqlDateFormat') AS TimePeriod " .
636-
"FROM block WHERE DATE_FORMAT(FROM_UNIXTIME(block_time), '$sqlDateFormat') >= ? GROUP BY TimePeriod ORDER BY TimePeriod ASC", [$start->format($dateFormat)]);
639+
$stmt = $conn->getPdo()->prepare("SELECT AVG(BlockSize) AS AvgBlockSize, DATE_FORMAT(FROM_UNIXTIME(BlockTime), '$sqlDateFormat') AS TimePeriod " .
640+
"FROM Blocks WHERE DATE_FORMAT(FROM_UNIXTIME(BlockTime), '$sqlDateFormat') >= ? GROUP BY TimePeriod ORDER BY TimePeriod ASC");
641+
$stmt->execute([$start->format($dateFormat)]);
637642
$avgBlockSizes = $stmt->fetchAll(PDO::FETCH_OBJ);
638643
foreach ($avgBlockSizes as $size) {
639644
if (!isset($resultSet[$size->TimePeriod])) {

resources/views/main/claims.blade.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,9 @@
174174
}
175175
@endphp
176176
@include('element.claimbox',['claim'=>$claim,'idx'=>$idx,'last_row'=>$last_row])
177-
@php($idx++)
177+
@php
178+
$idx++
179+
@endphp
178180
@endforeach
179181
<div class="clear"></div>
180182
</div>
@@ -201,7 +203,9 @@
201203
}
202204
@endphp
203205
@include('element.claimbox',['claim'=>$claim,'idx'=>$idx,'last_row'=>$last_row])
204-
@php($idx++)
206+
@php
207+
$idx++
208+
@endphp
205209
@endforeach
206210
<div class="clear"></div>
207211
</div>

0 commit comments

Comments
 (0)