coinwind/app/Console/Commands/CalcProfit.php

171 lines
4.2 KiB
PHP

<?php
namespace App\Console\Commands;
use App\Balance;
use App\Base;
use App\Http\Controllers\ApiV1Controller;
use App\Single;
use App\Staking;
use App\User;
use App\Vault3;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Redis;
/**
* 计算
*/
class CalcProfit extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'coin:calc';
/**
* The console command description.
*
* @var string
*/
protected $description = "Calculating user's profits who deposited.";
/**
* Create a new command instance.
*
* @return void
*/
public function __construct()
{
parent::__construct();
}
/**
* 获取配置
*/
protected function _get_staking_plans()
{
$rows = Single::where('type', Base::SINGLE_TYPE_FLUIDITY)
->get()->toArray();
$plans = [];
foreach ($rows as $row) {
if ($row['conf']) {
$plans[$row['name']] = json_decode($row['conf'], true)['plans'];
}
}
// var_dump($plans);
return $plans;
}
/**
* calculate profit by money and plan
*
* @deposited: how much have deposited.
* @plan: the relative staking plan.
*/
protected function _calc_income($deposited, $plan)
{
foreach ($plan as $p) {
if ($deposited >= $p['min'] && $deposited <= $p['max']) {
$rate = bcdiv($p['interest'], 100, 4);
return floatval(bcmul($rate, $deposited, 4));
}
}
return 0.0;
}
/**
* Execute the console command.
*
* @return mixed
*/
public function handle()
{
$this->_daily_revenue();
$this->_pool_data();
$this->_robots();
}
protected function _daily_revenue()
{
// get plan
$plans = $this->_get_staking_plans();
$investors = User::where('balance', '>', 0)
->get()
->toArray();
$log = [];
foreach ($investors as $inve) {
$profit = $this->_calc_income($inve['balance'], $plans[Base::USDT]);
$profit = Base::ffixed($profit, 4);
if ($profit > 0) {
$suc = DB::transaction(function () use ($inve, $profit) {
User::where('id', $inve['id'])->increment('balance', $profit);
//
$newBlc = new Balance();
$newBlc->address = $inve['address'];
$newBlc->name = Base::USDT;
$newBlc->remake = 'Daily Revenue';
$newBlc->status = Base::BALANCE_STAKING_DAILY_REVENUE;
$newBlc->money = $profit;
$newBlc->created_at = \Carbon\Carbon::now();
$newBlc->save();
});
if (is_null($suc)) {
$log[$inve['address']] = $profit;
}
}
} // foreach
if ($log) {
Log::info('coin:calc executed:' . json_encode($log));
} else {
Log::debug('coin:calc executed');
}
}
protected function _pool_data()
{
// string
$s = Redis::get(ApiV1Controller::K_POOLDATA);
// pool data
$pd = json_decode($s, true);
// print_r($pd);
$base = $pd['totalOutput'];
$min = bcmul($base, 0.01, 2);
$max = bcmul($base, 0.1, 2);
$pi = mt_rand($min, $max);
$rd = mt_rand(0, 100);
$pf = $rd / 100;
$pd['totalOutput'] = floatval(bcadd($base, $pi + $pf, 2));
if ($rd < 30) {
$pd['validNode'] += 1;
}
$pd['participant'] += mt_rand(1, 20);
$pd['userRevenue'] = $pd['totalOutput'] - $max;
Redis::set(json_encode($pd));
}
protected function _robots()
{
$all = Redis::hGetAll(ApiV1Controller::K_ROBOTS);
foreach ($all as $k => $v) {
$all[$k] = floatval(bcadd($v, mt_rand(0, $v * 0.16), 2));
}
Redis::hMSet(ApiV1Controller::K_ROBOTS, $all);
}
}