Skip to content

Commit

Permalink
[9.x] add exchange unit. decimal_places=8
Browse files Browse the repository at this point in the history
  • Loading branch information
rez1dent3 committed Mar 18, 2023
1 parent 5bdc0de commit b9547c4
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 0 deletions.
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, (float) $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);
}
}

0 comments on commit b9547c4

Please sign in to comment.