From b9547c4bf9208ebb54bc87a0b7287e33a7acaa94 Mon Sep 17 00:00:00 2001 From: Maxim Babichev Date: Tue, 14 Mar 2023 20:44:00 +0300 Subject: [PATCH] [9.x] add exchange unit. decimal_places=8 --- .../Services/ExchangeUsdToBtcService.php | 27 +++++++++ tests/Units/Domain/ExchangeTest.php | 56 +++++++++++++++++++ 2 files changed, 83 insertions(+) create mode 100644 tests/Infra/Services/ExchangeUsdToBtcService.php diff --git a/tests/Infra/Services/ExchangeUsdToBtcService.php b/tests/Infra/Services/ExchangeUsdToBtcService.php new file mode 100644 index 000000000..42b85d513 --- /dev/null +++ b/tests/Infra/Services/ExchangeUsdToBtcService.php @@ -0,0 +1,27 @@ + [ + '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.); + } +} diff --git a/tests/Units/Domain/ExchangeTest.php b/tests/Units/Domain/ExchangeTest.php index f4863a829..2ad6cd1ad 100644 --- a/tests/Units/Domain/ExchangeTest.php +++ b/tests/Units/Domain/ExchangeTest.php @@ -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; @@ -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); + } }