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

[9.x] add exchange unit. decimal_places=8 #670

Merged
merged 2 commits into from
Mar 18, 2023
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
27 changes: 27 additions & 0 deletions tests/Infra/Services/ExchangeUsdToBtcService.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

declare(strict_types=1);

namespace Bavix\Wallet\Test\Infra\Services;

use Bavix\Wallet\Internal\Service\MathServiceInterface;
use Bavix\Wallet\Services\ExchangeServiceInterface;

final class ExchangeUsdToBtcService implements ExchangeServiceInterface
{
private array $rates = [
'USD' => [
'BTC' => 0.004636,
],
];

public function __construct(
private MathServiceInterface $mathService
) {
}

public function convertTo(string $fromCurrency, string $toCurrency, float|int|string $amount): string
{
return $this->mathService->mul($amount, $this->rates[$fromCurrency][$toCurrency] ?? 1.);
}
}
56 changes: 56 additions & 0 deletions tests/Units/Domain/ExchangeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,14 @@

namespace Bavix\Wallet\Test\Units\Domain;

use Bavix\Wallet\External\Dto\Extra;
use Bavix\Wallet\Models\Transfer;
use Bavix\Wallet\Services\ExchangeService;
use Bavix\Wallet\Services\ExchangeServiceInterface;
use Bavix\Wallet\Services\RegulatorServiceInterface;
use Bavix\Wallet\Test\Infra\Factories\UserMultiFactory;
use Bavix\Wallet\Test\Infra\Models\UserMulti;
use Bavix\Wallet\Test\Infra\Services\ExchangeUsdToBtcService;
use Bavix\Wallet\Test\Infra\TestCase;
use Illuminate\Support\Str;

Expand Down Expand Up @@ -135,4 +138,57 @@ public function testExchange(): void

self::assertSame(1 / 67.61, (float) $rate);
}

public function testExchangeUsdToBtc(): void
{
app()->bind(ExchangeServiceInterface::class, ExchangeUsdToBtcService::class);

$rate = (float) app(ExchangeServiceInterface::class)
->convertTo('USD', 'BTC', 1)
;

self::assertSame(0.004636, $rate);

/** @var UserMulti $user */
$user = UserMultiFactory::new()->create();
$usd = $user->createWallet([
'name' => 'Dollar USA',
'slug' => 'my-usd',
'decimal_places' => 8,
'meta' => [
'currency' => 'USD',
],
]);
$btc = $user->createWallet([
'name' => 'Bitcoin',
'slug' => 'my-btc',
'decimal_places' => 8,
'meta' => [
'currency' => 'BTC',
],
]);

$usd->depositFloat(100.);
self::assertSame(100., $usd->balanceFloatNum);
self::assertSame(10000000000, $usd->balanceInt);

$usd->exchange($btc, 10000000000, new Extra(
deposit: [
'amountFloat' => 100 * $rate,
],
withdraw: [
'amountFloat' => -100.,
],
));

app(RegulatorServiceInterface::class)->missing($usd);
app(RegulatorServiceInterface::class)->missing($btc);

// get data from database
$usd->refresh();
$btc->refresh();

self::assertSame(0, $usd->balanceInt);
self::assertSame(100 * $rate, $btc->balanceFloatNum);
}
}