Skip to content

Commit fc83548

Browse files
authored
Merge pull request #824 from bavix/docs-float
[docs] documentation of float wallets has been expanded
2 parents a8d79a3 + 8e78bcc commit fc83548

File tree

9 files changed

+215
-22
lines changed

9 files changed

+215
-22
lines changed
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
It is necessary to expand the model that will have the wallet.
2+
This is done in two stages:
3+
- Add `Wallet` interface;
4+
- Add the `HasWalletFloat` trait;
5+
6+
Let's get started.
7+
```php
8+
use Bavix\Wallet\Traits\HasWalletFloat;
9+
use Bavix\Wallet\Interfaces\Wallet;
10+
11+
class User extends Model implements Wallet
12+
{
13+
use HasWalletFloat;
14+
}
15+
```
16+
17+
The model is prepared to work with a wallet.

docs/_sidebar.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,12 @@
2424
- [Transfer](wallet-transfer)
2525
- [Transaction Filter](transaction-filter)
2626

27+
- Fractional Wallet
28+
29+
- [Deposit](deposit-float)
30+
- [Withdraw](withdraw-float)
31+
- [Transfer](transfer-float)
32+
2733
- High performance api handles
2834

2935
- [Batch transactions](batch-transactions)

docs/configuration.md

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ Though this package is crafted to suit most of your needs by default, you can ed
44
## Configure default wallet
55
Customize `name`,`slug` and `meta` of default wallet.
66

7-
```php[config/wallet.php]
7+
config/wallet.php:
8+
```php
89
'default' => [
910
'name' => 'Ethereum',
1011
'slug' => 'ETH',
@@ -15,7 +16,8 @@ Customize `name`,`slug` and `meta` of default wallet.
1516
You can extend base Wallet model by creating a new class that extends `Bavix\Wallet\Models\Wallet` and registering the new class in `config/wallet.php`.
1617
Example `MyWallet.php`
1718

18-
```php[App/Models/MyWallet.php]
19+
App/Models/MyWallet.php:
20+
```php
1921
use Bavix\Wallet\Models\Wallet as WalletBase;
2022

2123
class MyWallet extends WalletBase {
@@ -24,7 +26,8 @@ class MyWallet extends WalletBase {
2426
```
2527
### Register base Wallet model
2628

27-
```php[config/wallet.php]
29+
config/wallet.php:
30+
```php
2831
'wallet' => [
2932
'table' => 'wallets',
3033
'model' => MyWallet::class,
@@ -44,7 +47,8 @@ This same method above, can be used to extend the base `Transfer` and `Transacti
4447

4548
You can change the default wallet decimal places, in wallet config file. This can be useful when working with fractional numbers.
4649

47-
```php[config/wallet.php]
50+
config/wallet.php:
51+
```php
4852
/**
4953
* Base model 'wallet'.
5054
*/

docs/deposit-float.md

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# Deposit float
2+
3+
A deposit is a sum of money which is part of the full price of something,
4+
and which you pay when you agree to buy it.
5+
6+
In this case, the Deposit is the replenishment of the wallet.
7+
8+
---
9+
10+
## User Model
11+
12+
[User Simple](_include/models/user_simple_float.md ':include')
13+
14+
## Make a Deposit
15+
16+
Find user:
17+
18+
```php
19+
$user = User::first();
20+
```
21+
22+
As the user uses `HasWalletFloat`, he will have `balance` property.
23+
Check the user's balance.
24+
25+
```php
26+
$user->balance; // 0
27+
$user->balanceInt; // 0
28+
$user->balanceFloatNum; // 0
29+
```
30+
31+
The balance is zero, which is what we expected.
32+
33+
```php
34+
$user->depositFloat(10.1);
35+
$user->balance; // 1010
36+
$user->balanceInt; // 1010
37+
$user->balanceFloatNum; // 10.1
38+
```
39+
40+
Wow!

docs/new-wallet.md

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,15 @@ You can create an unlimited number of wallets, but the `slug` for each wallet sh
66

77
## User Model
88

9-
Add the `HasWallet`, `HasWallets` trait's and `Wallet` interface to model.
9+
Add the `HasWallets` trait's and `Wallet` interface to model.
1010

1111
```php
12-
use Bavix\Wallet\Traits\HasWallet;
1312
use Bavix\Wallet\Traits\HasWallets;
1413
use Bavix\Wallet\Interfaces\Wallet;
1514

1615
class User extends Model implements Wallet
1716
{
18-
use HasWallet, HasWallets;
17+
use HasWallets;
1918
}
2019
```
2120

@@ -27,14 +26,6 @@ Find user:
2726
$user = User::first();
2827
```
2928

30-
As the user uses `HasWallet`, he will have `balance` property.
31-
Check the user's balance.
32-
33-
```php
34-
$user->balance; // 0
35-
```
36-
37-
It is the balance of the wallet by default.
3829
Create a new wallet.
3930

4031
```php
@@ -48,23 +39,38 @@ $user->hasWallet('my-wallet'); // bool(true)
4839

4940
$wallet->deposit(100);
5041
$wallet->balance; // 100
51-
52-
$user->deposit(10);
53-
$user->balance; // 10
42+
$wallet->balanceFloatNum; // 1.00
5443
```
5544

5645
## How to get the right wallet?
5746

5847
```php
5948
$myWallet = $user->getWallet('my-wallet');
6049
$myWallet->balance; // 100
50+
$myWallet->balanceFloatNum; // 1.00
51+
```
52+
53+
## Default Wallet + MultiWallet
54+
55+
Is it possible to use the default wallet and multi-wallets at the same time? Yes.
56+
57+
```php
58+
use Bavix\Wallet\Traits\HasWallet;
59+
use Bavix\Wallet\Traits\HasWallets;
60+
use Bavix\Wallet\Interfaces\Wallet;
61+
62+
class User extends Model implements Wallet
63+
{
64+
use HasWallet, HasWallets;
65+
}
6166
```
6267

63-
## How to get the default wallet?
68+
How to get the default wallet?
6469

6570
```php
6671
$wallet = $user->wallet;
6772
$wallet->balance; // 10
73+
$wallet->balanceFloatNum; // 0.10
6874
```
6975

7076
It worked!

docs/transaction-filter.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ Let's take a look at a livelier code example:
2020
```php
2121
$user->transactions()->count(); // 0
2222

23+
// Multi wallets and default wallet can be used together
2324
// default wallet
2425
$user->deposit(100);
2526
$user->wallet->deposit(200);

docs/transfer-float.md

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
# Transfer
2+
3+
Transfer in our system are two well-known [Deposit](deposit-float) and [Withdraw](withdraw-float)
4+
operations that are performed in one transaction.
5+
6+
The transfer takes place between wallets.
7+
8+
---
9+
10+
## User Model
11+
12+
[User Simple](_include/models/user_simple_float.md ':include')
13+
14+
## Make a Transfer
15+
16+
Find user:
17+
18+
```php
19+
$first = User::first();
20+
$last = User::orderBy('id', 'desc')->first(); // last user
21+
$first->getKey() !== $last->getKey(); // true
22+
```
23+
24+
As the user uses `HasWalletFloat`, he will have `balance` property.
25+
Check the user's balance.
26+
27+
```php
28+
$fist->balanceFloatNum; // 100.00
29+
$last->balanceFloatNum; // 0
30+
```
31+
32+
The transfer will be from the first user to the second.
33+
34+
```php
35+
$first->transferFloat($last, 5);
36+
$first->balanceFloatNum; // 95
37+
$last->balanceFloatNum; // 5
38+
```
39+
40+
It worked!
41+
42+
## Force Transfer
43+
44+
Check the user's balance.
45+
46+
```php
47+
$first->balanceFloatNum; // 100
48+
$last->balanceFloatNum; // 0
49+
```
50+
51+
The transfer will be from the first user to the second.
52+
53+
```php
54+
$first->forceTransferFloat($last, 500);
55+
$first->balanceFloatNum; // -400
56+
$last->balanceFloatNum; // 500
57+
```
58+
59+
It worked!

docs/wallet-transfer.md

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,15 @@ The transfer takes place between wallets.
99

1010
## User Model
1111

12-
Prepare the model, add the `HasWallet`, `HasWallets` trait's and `Wallet` interface.
12+
Prepare the model, add the `HasWallets` trait's and `Wallet` interface.
1313

1414
```php
15-
use Bavix\Wallet\Traits\HasWallet;
1615
use Bavix\Wallet\Traits\HasWallets;
1716
use Bavix\Wallet\Interfaces\Wallet;
1817

1918
class User extends Model implements Wallet
2019
{
21-
use HasWallet, HasWallets;
20+
use HasWallets;
2221
}
2322
```
2423

docs/withdraw-float.md

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
# Withdraw
2+
3+
When there is enough money in the account, you can transfer/withdraw
4+
it or buy something in the system.
5+
6+
Since the currency is virtual, you can buy any services on your website.
7+
For example, priority in search results.
8+
9+
---
10+
11+
## User Model
12+
13+
[User Simple](_include/models/user_simple_float.md ':include')
14+
15+
## Make a Withdraw
16+
17+
Find user:
18+
19+
```php
20+
$user = User::first();
21+
```
22+
23+
As the user uses `HasWalletFloat`, he will have `balance` property.
24+
Check the user's balance.
25+
26+
```php
27+
$user->balance; // 10000
28+
$user->balanceInt; // 10000
29+
$user->balanceFloatNum; // 100.00
30+
```
31+
32+
The balance is not empty, so you can withdraw funds.
33+
34+
```php
35+
$user->withdrawFloat(10);
36+
$user->balance; // 9000
37+
$user->balanceInt; // 9000
38+
$user->balanceFloatNum; // 90.00
39+
```
40+
41+
It worked!
42+
43+
## Force Withdraw
44+
45+
Forced withdrawal is necessary for those cases when
46+
the user has no funds. For example, a fine for spam.
47+
48+
```php
49+
$user->balanceFloatNum; // 90.00
50+
$user->forceWithdrawFloat(101);
51+
$user->balanceFloatNum; // -11.00
52+
```
53+
54+
## And what will happen if the money is not enough?
55+
56+
There can be two situations:
57+
58+
- The user's balance is zero, then we get an error
59+
`Bavix\Wallet\Exceptions\BalanceIsEmpty`
60+
- If the balance is greater than zero, but it is not enough
61+
`Bavix\Wallet\Exceptions\InsufficientFunds`

0 commit comments

Comments
 (0)