Giter Club home page Giter Club logo

balance-manager's Introduction

Laravel Balance manager

in many projects, you need to have credit (balance) concept for your user that make you distract from your main business logic.

with this package you will have balance for your users easily without being worry about Race Condition and Double Spending.

this project mainly designed for exchange systems, but you can use it in any project.

Installation

composer require roboticsexpert/balance-manager

after instaling composer package, if you use auto discovery for service providers, everything ok, but if you blocked that just add this line to config/app.php in providers section:

Roboticsexpert\BalanceManager\BalanceManagerServiceProvider::class

after that you should run

php artisan migrate

and this:

php artisan vendor:publish --provider="Roboticsexpert\BalanceManager\BalanceManagerServiceProvider"

it will create balance.php file in your config file.

Usage

First of all, you should decide with currencies you want to have in your system, and consider a KEY for each currency and add those keys in balance.php config file

return [

    // add symbols of your currencies
    // symbols must be lower than 16 char

    'currencies'=>[
        'BTC',
        'TMN',
        'DOGE',
    ]
];

you can use this project with 2 strategy,Facade or Dependency injection!

I suggest to you to use it with dependecy injection for IDE auto complete feature but use it as you prefer!

after that you can get BalanceManager from with these to methods:

Facade

you can get BalanceManager service like this:

use Roboticsexpert\BalanceManager\BalanceManagerFacade as BalanceManager;


BalanceManager::getAllBalancesByUserId(1)

Dependency Injection

you can get BalanceManager service from app() like this:

use Roboticsexpert\BalanceManager\Services\BalanceManager;

$balanceManager=app(BalanceManager::class); 

$balanceManager->getAllBalancesByUserId(1);

or get from laravel automatic dependency injection

use Roboticsexpert\BalanceManager\Services\BalanceManager;
class Controller extends BaseController
{
    use AuthorizesRequests, DispatchesJobs, ValidatesRequests;

    public function index(BalanceManager $balanceManager){
        dd($balanceManager->getAllBalancesByUserId(1));
    }
}

Methods

Get all balance for user

it will return array of Balance model

$balances= $balanceManager->getAllBalancesByUserId("USER_ID");
dd($balances);

Change balance of user

$balanceChangeResult= $balanceManager->changeBalanceByUserIdAndCurrency(
        int $userId, //user id
        string $currency, // like USDT, TMN
        string $reason, // a unique string for each action
        IBalanceHistoryRelated $model, // a model that is author of change balance
        Decimal $valueChange, // value you want to add or sub from user balance
        Decimal $lockedValueChange // in general usage it should be new \Decimal\Decimal(0)
);

balance-manager's People

Contributors

azibom avatar roboticsexpert avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar

Forkers

azibom

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.