Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: wallet balance use current currency rate #677

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 9 additions & 4 deletions app/Services/ExchangeRate.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,20 @@

final class ExchangeRate
{
public static function convert(float $amount, int $timestamp, bool $showSmallAmounts = false): string
public static function convert(float $amount, ?int $timestamp = null, bool $showSmallAmounts = false): string
{
return NumberFormatter::currency(self::convertNumerical($amount, $timestamp), Settings::currency(), $showSmallAmounts);
}

public static function convertNumerical(float $amount, int $timestamp): float
public static function convertNumerical(float $amount, ?int $timestamp = null): float
{
$prices = (new CryptoDataCache())->getPrices(Settings::currency().'.week');
$exchangeRate = Arr::get($prices, Carbon::parse(static::timestamp($timestamp))->format('Y-m-d'), 0);
$exchangeRate = 0;
if ($timestamp !== null) {
$prices = (new CryptoDataCache())->getPrices(Settings::currency().'.week');
$exchangeRate = Arr::get($prices, Carbon::parse(static::timestamp($timestamp))->format('Y-m-d'), 0);
} else {
$exchangeRate = static::currentRate();
}

return $amount * $exchangeRate;
}
Expand Down
3 changes: 1 addition & 2 deletions app/ViewModels/WalletViewModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
use App\Models\Wallet;
use App\Services\ArkVaultUrlBuilder;
use App\Services\ExchangeRate;
use App\Services\Timestamp;
use App\ViewModels\Concerns\Wallet\CanBeCold;
use App\ViewModels\Concerns\Wallet\CanBeDelegate;
use App\ViewModels\Concerns\Wallet\CanForge;
Expand Down Expand Up @@ -63,7 +62,7 @@ public function balance(): float

public function balanceFiat(): string
{
return ExchangeRate::convert($this->balance(), Timestamp::now()->unix());
return ExchangeRate::convert($this->balance());
}

public function balancePercentage(): float
Expand Down
15 changes: 4 additions & 11 deletions tests/Feature/Http/Livewire/WalletBalanceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,12 @@
use App\Facades\Settings;
use App\Http\Livewire\WalletBalance;
use App\Models\Wallet;
use App\Services\Cache\CryptoDataCache;
use App\Services\Cache\NetworkStatusBlockCache;
use App\Services\NumberFormatter;
use Carbon\Carbon;
use Livewire\Livewire;

it('should show the balance of the wallet', function () {
(new CryptoDataCache())->setPrices('USD.week', collect([
Carbon::now()->format('Y-m-d') => 10,
]));
(new NetworkStatusBlockCache())->setPrice('DARK', 'USD', 10);

$wallet = Wallet::factory()->create(['balance' => 125456]);

Expand All @@ -22,13 +19,9 @@
});

it('updates the balance when currency changes', function () {
(new CryptoDataCache())->setPrices('USD.week', collect([
Carbon::now()->format('Y-m-d') => 10,
]));
(new NetworkStatusBlockCache())->setPrice('DARK', 'USD', 10);

(new CryptoDataCache())->setPrices('BTC.week', collect([
Carbon::now()->format('Y-m-d') => 0.1234567,
]));
(new NetworkStatusBlockCache())->setPrice('DARK', 'BTC', 0.1234567);

$wallet = Wallet::factory()->create(['balance' => 125456]);

Expand Down
28 changes: 28 additions & 0 deletions tests/Unit/Services/ExchangeRateTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,20 @@
->toBe(NumberFormatter::currency(30, 'USD.week'));
});

it('should convert with current rate if no timestamp', function () {
(new CryptoDataCache())->setPrices('USD.week', collect([
Carbon::now()->subDays(3)->format('Y-m-d') => 1,
Carbon::now()->subDays(2)->format('Y-m-d') => 2,
Carbon::now()->subDays(1)->format('Y-m-d') => 3,
Carbon::now()->format('Y-m-d') => 10,
]));

(new NetworkStatusBlockCache())->setPrice('DARK', 'USD', 24);

expect(ExchangeRate::convert(10))
->toBe('$240.00');
});

it('should convert with a historical rate and return numerical value', function () {
(new CryptoDataCache())->setPrices('USD.week', collect([
Carbon::now()->subDays(3)->format('Y-m-d') => 1,
Expand All @@ -34,6 +48,20 @@
->toBe(30.0);
});

it('should convert with current rate and return numerical value', function () {
(new CryptoDataCache())->setPrices('USD.week', collect([
Carbon::now()->subDays(3)->format('Y-m-d') => 1,
Carbon::now()->subDays(2)->format('Y-m-d') => 2,
Carbon::now()->subDays(1)->format('Y-m-d') => 3,
Carbon::now()->format('Y-m-d') => 10,
]));

(new NetworkStatusBlockCache())->setPrice('DARK', 'USD', 24);

expect(ExchangeRate::convertNumerical(10))
->toBe(240.0);
});

it('should convert with the current rate', function () {
(new CryptoDataCache())->setPrices('USD.day', collect([
Carbon::parse('-3 hours')->format('Y-m-d H:i:s') => 1,
Expand Down