Skip to content

Commit

Permalink
Merge pull request #140 from bavix/develop
Browse files Browse the repository at this point in the history
5.0.x - Arbitrary Precision Mathematics
  • Loading branch information
rez1dent3 authored Mar 13, 2020
2 parents 16f2ad8 + 29e53f2 commit 0b7e9e8
Show file tree
Hide file tree
Showing 67 changed files with 1,349 additions and 360 deletions.
4 changes: 2 additions & 2 deletions .phpstorm.meta.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace PHPSTORM_META {

use Bavix\Wallet\Interfaces\Mathable;
use Bavix\Wallet\Interfaces\Rateable;
use Bavix\Wallet\Interfaces\Storable;
use Bavix\Wallet\Models\Transaction;
Expand All @@ -13,7 +14,6 @@
use Bavix\Wallet\Objects\Operation;
use Bavix\Wallet\Services\CommonService;
use Bavix\Wallet\Services\ExchangeService;
use Bavix\Wallet\Services\ProxyService;
use Bavix\Wallet\Services\WalletService;

override(\app(0), map([
Expand All @@ -23,11 +23,11 @@
EmptyLock::class => EmptyLock::class,
ExchangeService::class => ExchangeService::class,
CommonService::class => CommonService::class,
ProxyService::class => ProxyService::class,
WalletService::class => WalletService::class,
Wallet::class => Wallet::class,
Transfer::class => Transfer::class,
Transaction::class => Transaction::class,
Mathable::class => Mathable::class,
Rateable::class => Rateable::class,
Storable::class => Storable::class,
]));
Expand Down
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ laravel-wallet - Easy work with virtual wallet.

### Upgrade Guide

> Starting with version 5.x, support for Laravel 5 has been discontinued.
> Update laravel or use version 4.x.
To perform the migration, you will be [helped by the instruction](https://bavix.github.io/laravel-wallet/#/upgrade-guide).

### Extensions
Expand Down Expand Up @@ -98,7 +101,7 @@ class Item extends Model implements Product
return true;
}

public function getAmountProduct(Customer $customer): int
public function getAmountProduct(Customer $customer)
{
return 100;
}
Expand Down
18 changes: 16 additions & 2 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,20 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

### Added
- add support "Arbitrary Precision Mathematics" (`ext-bcmath`) #139 #146
- add `Mathable` service (helps switch quickly from bcmath to php computing)

### Changed
- add unit cases
- upgrade composer packages
- Now all casts are in the config, not in the model. If you use bcmath, then all values are reduced to a string.

### Removed
- Strong typing (models, interfaces, etc.)
- all deprecated methods are removed
- `nesbot/carbon` is no longer needed for the library to work

## [4.2.0] - 2020-03-08

### Added
Expand Down Expand Up @@ -142,7 +156,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Deprecated
- class `Taxing`.

### Remove
### Removed
- The ability to change the ratio `coefficient`.
- Removed private and protected methods, the traits turned out to be more clean.

Expand Down Expand Up @@ -453,7 +467,7 @@ The operation is now executed in the transaction and updates the new `refund` fi
- Exceptions: AmountInvalid, BalanceIsEmpty.
- Models: Transfer, Transaction.

[Unreleased]: https://github.com/bavix/laravel-wallet/compare/4.2.0...HEAD
[Unreleased]: https://github.com/bavix/laravel-wallet/compare/4.2.0...develop
[4.2.0]: https://github.com/bavix/laravel-wallet/compare/4.1.2...4.2.0
[4.1.2]: https://github.com/bavix/laravel-wallet/compare/4.1.1...4.1.2
[4.1.1]: https://github.com/bavix/laravel-wallet/compare/4.1.0...4.1.1
Expand Down
5 changes: 4 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
"php": "^7.2|^8.0",
"ext-pdo": "*",
"illuminate/database": "^6.0|^7.0",
"nesbot/carbon": "^2.0",
"doctrine/dbal": "^2.8",
"ramsey/uuid": "^3.0"
},
Expand All @@ -40,6 +39,7 @@
"laravel/cashier": "^10.0"
},
"suggest": {
"ext-bcmath": "Used for accurate calculations of large numbers",
"bavix/laravel-wallet-swap": "Addition to the laravel-wallet library for quick setting of exchange rates",
"bavix/laravel-wallet-vacuum": "Addition to the laravel-wallet library for quick fix race condition"
},
Expand All @@ -54,6 +54,9 @@
}
},
"extra": {
"branch-alias": {
"dev-develop": "5.0.x-dev"
},
"laravel": {
"providers": [
"Bavix\\Wallet\\WalletServiceProvider"
Expand Down
29 changes: 24 additions & 5 deletions config/config.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,37 @@
use Bavix\Wallet\Objects\Operation;
use Bavix\Wallet\Services\ExchangeService;
use Bavix\Wallet\Services\CommonService;
use Bavix\Wallet\Services\ProxyService;
use Bavix\Wallet\Services\WalletService;
use Bavix\Wallet\Services\LockService;
use Bavix\Wallet\Models\Transaction;
use Bavix\Wallet\Models\Transfer;
use Bavix\Wallet\Models\Wallet;
use Bavix\Wallet\Simple\Rate;
use Bavix\Wallet\Simple\Store;
use Bavix\Wallet\Simple\BCMath;
use Bavix\Wallet\Simple\Math;

$bcLoaded = extension_loaded('bcmath');

return [
/**
* This parameter is necessary for more accurate calculations.
* PS, Arbitrary Precision Calculations
*/
'math' => [
'scale' => 64,
],

/**
* The parameter is used for fast packet overload.
* You do not need to search for the desired class by code, the library will do it itself.
*/
'package' => [
'rateable' => Rate::class,
'storable' => Store::class,
'mathable' => $bcLoaded ?
BCMath::class :
Math::class,
],

/**
Expand Down Expand Up @@ -60,7 +74,6 @@
'services' => [
'exchange' => ExchangeService::class,
'common' => CommonService::class,
'proxy' => ProxyService::class,
'wallet' => WalletService::class,
'lock' => LockService::class,
],
Expand All @@ -78,7 +91,9 @@
'transaction' => [
'table' => 'transactions',
'model' => Transaction::class,
'casts' => [],
'casts' => [
'amount' => $bcLoaded ? 'string' : 'int',
],
],

/**
Expand All @@ -87,7 +102,9 @@
'transfer' => [
'table' => 'transfers',
'model' => Transfer::class,
'casts' => [],
'casts' => [
'fee' => $bcLoaded ? 'string' : 'int',
],
],

/**
Expand All @@ -96,7 +113,9 @@
'wallet' => [
'table' => 'wallets',
'model' => Wallet::class,
'casts' => [],
'casts' => [
'balance' => $bcLoaded ? 'string' : 'int',
],
'default' => [
'name' => 'Default Wallet',
'slug' => 'default',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class CreateTransactionsTable extends Migration
public function up(): void
{
Schema::create($this->table(), function (Blueprint $table) {
$table->increments('id');
$table->bigIncrements('id');
$table->morphs('payable');
$table->enum('type', ['deposit', 'withdraw'])->index();
$table->bigInteger('amount');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@ class CreateTransfersTable extends Migration
public function up(): void
{
Schema::create($this->table(), function (Blueprint $table) {
$table->increments('id');
$table->bigIncrements('id');
$table->morphs('from');
$table->morphs('to');
$table->unsignedInteger('deposit_id');
$table->unsignedInteger('withdraw_id');
$table->unsignedBigInteger('deposit_id');
$table->unsignedBigInteger('withdraw_id');
$table->uuid('uuid')->unique();
$table->timestamps();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

use Bavix\Wallet\Models\Transaction;
use Bavix\Wallet\Models\Wallet;
use Carbon\Carbon;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
Expand All @@ -26,7 +25,7 @@ protected function table(): string
public function up(): void
{
Schema::create($this->table(), function (Blueprint $table) {
$table->increments('id');
$table->bigIncrements('id');
$table->morphs('holder');
$table->string('name');
$table->string('slug')->index();
Expand All @@ -48,8 +47,8 @@ public function up(): void
->selectRaw('? as name', [$default])
->selectRaw('? as slug', [$slug])
->selectRaw('sum(amount) as balance')
->selectRaw('? as created_at', [Carbon::now()])
->selectRaw('? as updated_at', [Carbon::now()])
->selectRaw('? as created_at', [DB::raw('now()')])
->selectRaw('? as updated_at', [DB::raw('now()')])
->groupBy('holder_type', 'holder_id')
->orderBy('holder_type');

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ protected function walletTable(): string
public function up(): void
{
Schema::table($this->table(), function (Blueprint $table) {
$table->unsignedInteger('wallet_id')
$table->unsignedBigInteger('wallet_id')
->nullable()
->after('payable_id');

Expand Down
2 changes: 1 addition & 1 deletion docs/basic-usage.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ class Item extends Model implements Product
return true;
}

public function getAmountProduct(Customer $customer): int
public function getAmountProduct(Customer $customer)
{
return 100;
}
Expand Down
2 changes: 1 addition & 1 deletion docs/cart.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ class Item extends Model implements Product
return true;
}

public function getAmountProduct(Customer $customer): int
public function getAmountProduct(Customer $customer)
{
return round($this->price * 100);
}
Expand Down
4 changes: 2 additions & 2 deletions docs/exchange.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ class MyRateService extends \Bavix\Wallet\Simple\Rate
],
];

protected function rate(Wallet $wallet): float
protected function rate(Wallet $wallet)
{
$from = app(WalletService::class)->getWallet($this->withCurrency);
$to = app(WalletService::class)->getWallet($wallet);
Expand All @@ -54,7 +54,7 @@ class MyRateService extends \Bavix\Wallet\Simple\Rate
);
}

public function convertTo(Wallet $wallet): float
public function convertTo(Wallet $wallet)
{
return parent::convertTo($wallet) * $this->rate($wallet);
}
Expand Down
2 changes: 1 addition & 1 deletion docs/gift.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ class Item extends Model implements Product
return true;
}

public function getAmountProduct(Customer $customer): int
public function getAmountProduct(Customer $customer)
{
return 100;
}
Expand Down
2 changes: 1 addition & 1 deletion docs/pay-free.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ class Item extends Model implements Product
return true;
}

public function getAmountProduct(Customer $customer): int
public function getAmountProduct(Customer $customer)
{
return 100;
}
Expand Down
2 changes: 1 addition & 1 deletion docs/payment.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ class Item extends Model implements Product
return true;
}

public function getAmountProduct(Customer $customer): int
public function getAmountProduct(Customer $customer)
{
return 100;
}
Expand Down
2 changes: 1 addition & 1 deletion docs/refund.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ class Item extends Model implements Product
return true;
}

public function getAmountProduct(Customer $customer): int
public function getAmountProduct(Customer $customer)
{
return 100;
}
Expand Down
2 changes: 1 addition & 1 deletion docs/ru/basic-usage.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ class Item extends Model implements Product
return true;
}

public function getAmountProduct(Customer $customer): int
public function getAmountProduct(Customer $customer)
{
return 100;
}
Expand Down
2 changes: 1 addition & 1 deletion docs/ru/cart.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ class Item extends Model implements Product
return true;
}

public function getAmountProduct(Customer $customer): int
public function getAmountProduct(Customer $customer)
{
return round($this->price * 100);
}
Expand Down
4 changes: 2 additions & 2 deletions docs/ru/exchange.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ class MyRateService extends \Bavix\Wallet\Simple\Rate
],
];

protected function rate(Wallet $wallet): float
protected function rate(Wallet $wallet)
{
$from = app(WalletService::class)->getWallet($this->withCurrency);
$to = app(WalletService::class)->getWallet($wallet);
Expand All @@ -53,7 +53,7 @@ class MyRateService extends \Bavix\Wallet\Simple\Rate
);
}

public function convertTo(Wallet $wallet): float
public function convertTo(Wallet $wallet)
{
return parent::convertTo($wallet) * $this->rate($wallet);
}
Expand Down
2 changes: 1 addition & 1 deletion docs/ru/gift.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ class Item extends Model implements Product
return true;
}

public function getAmountProduct(Customer $customer): int
public function getAmountProduct(Customer $customer)
{
return 100;
}
Expand Down
2 changes: 1 addition & 1 deletion docs/ru/pay-free.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ class Item extends Model implements Product
return true;
}

public function getAmountProduct(Customer $customer): int
public function getAmountProduct(Customer $customer)
{
return 100;
}
Expand Down
2 changes: 1 addition & 1 deletion docs/ru/payment.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ class Item extends Model implements Product
return true;
}

public function getAmountProduct(Customer $customer): int
public function getAmountProduct(Customer $customer)
{
return 100;
}
Expand Down
2 changes: 1 addition & 1 deletion docs/ru/refund.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ class Item extends Model implements Product
return true;
}

public function getAmountProduct(Customer $customer): int
public function getAmountProduct(Customer $customer)
{
return 100;
}
Expand Down
Loading

0 comments on commit 0b7e9e8

Please sign in to comment.