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

Wallet balance not changing on deposit #729

Closed
rrutwik opened this issue Jul 18, 2023 Discussed in #728 · 18 comments
Closed

Wallet balance not changing on deposit #729

rrutwik opened this issue Jul 18, 2023 Discussed in #728 · 18 comments
Assignees
Labels
question Further information is requested

Comments

@rrutwik
Copy link

rrutwik commented Jul 18, 2023

Discussed in #728

Originally posted by rrutwik July 18, 2023
I am using
$this->deposit($amount, $meta);
to deposit amount in a user wallet.
In logs, i see balance changing.. but it does not change in database.
Transaction is updated fine, in database

@rez1dent3
Copy link
Member

Hello. Attach an example. And also, which version of the wallet, php and db you use.

@rrutwik
Copy link
Author

rrutwik commented Jul 18, 2023

PHP 8.1.2
Wallet 7.3.5
DB MySQL 8.0
Example is i am depositing amount lets say 80. It is adding to transaction but not updating in wallet. Though logs before and after this function shows balance in updated. But it is not updated in DB.

@rez1dent3
Copy link
Member

Does the update happen in a transaction?

If yes, then you will have to replace laravel transactions with package transactions: https://bavix.github.io/laravel-wallet/#/transaction?id=if-you-are-using-a-version-below-96-then-the-following-is-relevant-for-you

If you do not want or cannot use package transactions, then you need to upgrade your wallet to version 9.6+, because. for the wallet, changes were made to the framework itself: laravel/framework#44608

@rrutwik
Copy link
Author

rrutwik commented Jul 18, 2023

It works find with ->forceWithdraw.. not sure why..

@rez1dent3
Copy link
Member

I won't tell you either. Both methods should not work correctly on your version.

public function deposit($amount, ?array $meta = null, bool $confirmed = true): Transaction
{
return app(AtomicServiceInterface::class)->block(
$this,
fn () => app(CommonServiceLegacy::class)
->makeTransaction($this, Transaction::TYPE_DEPOSIT, $amount, $meta, $confirmed)
);
}

public function forceWithdraw($amount, ?array $meta = null, bool $confirmed = true): Transaction
{
return app(AtomicServiceInterface::class)->block(
$this,
fn () => app(CommonServiceLegacy::class)
->makeTransaction($this, Transaction::TYPE_WITHDRAW, $amount, $meta, $confirmed)
);
}

Well, yes, if you need transactions - upgrade the package to 9.6+

@rrutwik
Copy link
Author

rrutwik commented Jul 18, 2023

yes, i checked that but deposit is not committing changes.

@rez1dent3
Copy link
Member

@rrutwik
Copy link
Author

rrutwik commented Jul 18, 2023

I followed first point, still same transaction is added but wallet is not updated.
public function depositPayment($amount, $meta = []) {
$driver = $this;
$this->getConnection()->transaction(static function () use ($driver, $amount, $meta) {
$payer = $driver;
$payer->deposit($amount, $meta);
});
}

@rez1dent3
Copy link
Member

@rrutwik what are your wallet settings (wallet.php)?

@rez1dent3 rez1dent3 added the question Further information is requested label Jul 18, 2023
@rez1dent3 rez1dent3 self-assigned this Jul 18, 2023
@rrutwik
Copy link
Author

rrutwik commented Jul 18, 2023

`<?php

declare(strict_types=1);

use Bavix\Wallet\Internal\Assembler\AvailabilityDtoAssembler;
use Bavix\Wallet\Internal\Assembler\BalanceUpdatedEventAssembler;
use Bavix\Wallet\Internal\Assembler\TransactionDtoAssembler;
use Bavix\Wallet\Internal\Assembler\TransactionQueryAssembler;
use Bavix\Wallet\Internal\Assembler\TransferDtoAssembler;
use Bavix\Wallet\Internal\Assembler\TransferLazyDtoAssembler;
use Bavix\Wallet\Internal\Assembler\TransferQueryAssembler;
use Bavix\Wallet\Internal\Events\BalanceUpdatedEvent;
use Bavix\Wallet\Internal\Events\WalletCreatedEvent;
use Bavix\Wallet\Internal\Repository\TransactionRepository;
use Bavix\Wallet\Internal\Repository\TransferRepository;
use Bavix\Wallet\Internal\Repository\WalletRepository;
use Bavix\Wallet\Internal\Service\ClockService;
use Bavix\Wallet\Internal\Service\DatabaseService;
use Bavix\Wallet\Internal\Service\DispatcherService;
use Bavix\Wallet\Internal\Service\JsonService;
use Bavix\Wallet\Internal\Service\LockService;
use Bavix\Wallet\Internal\Service\MathService;
use Bavix\Wallet\Internal\Service\StorageService;
use Bavix\Wallet\Internal\Service\TranslatorService;
use Bavix\Wallet\Internal\Service\UuidFactoryService;
use Bavix\Wallet\Internal\Transform\TransactionDtoTransformer;
use Bavix\Wallet\Internal\Transform\TransferDtoTransformer;
use Bavix\Wallet\Models\Transaction;
use Bavix\Wallet\Models\Transfer;
use Bavix\Wallet\Models\Wallet;
use Bavix\Wallet\Services\AssistantService;
use Bavix\Wallet\Services\AtmService;
use Bavix\Wallet\Services\AtomicService;
use Bavix\Wallet\Services\BasketService;
use Bavix\Wallet\Services\BookkeeperService;
use Bavix\Wallet\Services\CastService;
use Bavix\Wallet\Services\ConsistencyService;
use Bavix\Wallet\Services\DiscountService;
use Bavix\Wallet\Services\ExchangeService;
use Bavix\Wallet\Services\PrepareService;
use Bavix\Wallet\Services\PurchaseService;
use Bavix\Wallet\Services\RegulatorService;
use Bavix\Wallet\Services\TaxService;
use Bavix\Wallet\Services\WalletService;

return [
/**
* Arbitrary Precision Calculator.
*
* 'scale' - length of the mantissa
*/
'math' => ['scale' => 64],

/**
 * Storage of the state of the balance of wallets.
 */
'cache' => ['driver' => 'array'],

/**
 * A system for dealing with race conditions.
 */
'lock' => [
    'driver' => 'array',
    'seconds' => 1,
],

/**
 * Internal services that can be overloaded.
 */
'internal' => [
    'clock' => ClockService::class,
    'database' => DatabaseService::class,
    'dispatcher' => DispatcherService::class,
    'json' => JsonService::class,
    'lock' => LockService::class,
    'math' => MathService::class,
    'storage' => StorageService::class,
    'translator' => TranslatorService::class,
    'uuid' => UuidFactoryService::class,
],

/**
 * Services that can be overloaded.
 */
'services' => [
    'assistant' => AssistantService::class,
    'atm' => AtmService::class,
    'atomic' => AtomicService::class,
    'basket' => BasketService::class,
    'bookkeeper' => BookkeeperService::class,
    'regulator' => RegulatorService::class,
    'cast' => CastService::class,
    'consistency' => ConsistencyService::class,
    'discount' => DiscountService::class,
    'exchange' => ExchangeService::class,
    'prepare' => PrepareService::class,
    'purchase' => PurchaseService::class,
    'tax' => TaxService::class,
    'wallet' => WalletService::class,
],

/**
 * Repositories for fetching data from the database.
 */
'repositories' => [
    'transaction' => TransactionRepository::class,
    'transfer' => TransferRepository::class,
    'wallet' => WalletRepository::class,
],

/**
 * Objects of transformer from DTO to array.
 */
'transformers' => [
    'transaction' => TransactionDtoTransformer::class,
    'transfer' => TransferDtoTransformer::class,
],

/**
 * Builder class, needed to create DTO.
 */
'assemblers' => [
    'availability' => AvailabilityDtoAssembler::class,
    'balance_updated_event' => BalanceUpdatedEventAssembler::class,
    'transaction' => TransactionDtoAssembler::class,
    'transfer_lazy' => TransferLazyDtoAssembler::class,
    'transfer' => TransferDtoAssembler::class,
    'transaction_query' => TransactionQueryAssembler::class,
    'transfer_query' => TransferQueryAssembler::class,
],

/**
 * Package system events.
 */
'events' => [
    'balance_updated' => BalanceUpdatedEvent::class,
    'wallet_created' => WalletCreatedEvent::class,
],

/**
 * Base model 'transaction'.
 */
'transaction' => [
    'table' => 'transactions',
    'model' => Transaction::class,
],

/**
 * Base model 'transfer'.
 */
'transfer' => [
    'table' => 'transfers',
    'model' => Transfer::class,
],

/**
 * Base model 'wallet'.
 */
'wallet' => [
    'table' => 'wallets',
    'model' => Wallet::class,
    'creating' => [],
    'default' => [
        'name' => 'Default Wallet',
        'slug' => 'default',
        'meta' => [],
    ],
],

];
`

@rez1dent3
Copy link
Member

I suspect that you have db transaction level > 0 inside the controller. You can do:

dd(DB::transactionLevel());

If the value before the start of the transaction is greater than zero, then this is the problem.

Without an example, I can't help. I do not reproduce.

@rrutwik
Copy link
Author

rrutwik commented Jul 18, 2023

local: INFO: db level 1 , not sure about this value. What does it do ?

@rez1dent3
Copy link
Member

This value indicates that you already have a transaction running. Therefore, it doesn't work. If you do DB::rollBack() before the call, then everything will work.

And here is where your transaction starts - I will not tell you.

@rez1dent3
Copy link
Member

If the code

dd(DB::transactionLevel());

starts to return =0, then everything will start working correctly.

@rrutwik
Copy link
Author

rrutwik commented Jul 18, 2023

hmm kool

@rrutwik
Copy link
Author

rrutwik commented Jul 18, 2023

is this a bug or something? how should i fix it ?

@rez1dent3
Copy link
Member

If you cannot find where the transaction starts, then you will only have to update the package version. Starting from version 9.6, the package listens for events from Laravel DB. When the transaction closes, the changes will be applied.

@rrutwik
Copy link
Author

rrutwik commented Jul 18, 2023

kool

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Aug 3, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants