diff --git a/doc/Staking.py b/doc/Staking.py index d1959106..90a515a3 100644 --- a/doc/Staking.py +++ b/doc/Staking.py @@ -16,8 +16,8 @@ class Staking(object): ], 'response': [ _common.RspHead(), - ['total_staking_value', '', '质押总ceg价值'], - ['daily_staking_value', '', '当天总ceg价值'], + ['total_staking_value', '', '质押总ceg价值,包含利息'], + ['daily_staking_value', '', '当天总ceg价值,包含利息'], ['planet', _common.Staking(), '星球质押'], ['badge', _common.Staking(), '徽章质押'], ['cec', _common.Staking(), 'cec质押'], @@ -33,9 +33,9 @@ class Staking(object): ['type', '', '1: planet 2: badge 3:cec'], ], 'response': [ + _common.RspHead(), ['type', '', '1: planet 2: badge 3:cec'], ['stacked_quant', '', '当前该类型质押总数'], - _common.RspHead(), ['!items', [_common.StakingDetail()], '质押列表'], ] }, diff --git a/webapp/controller/StakingController.class.php b/webapp/controller/StakingController.class.php index 98734916..3c5bf8ca 100644 --- a/webapp/controller/StakingController.class.php +++ b/webapp/controller/StakingController.class.php @@ -2,8 +2,10 @@ require_once("mt/Stacking.php"); +require_once("models/Nft.php"); require_once("models/Stacking.php"); +use models\Nft; use models\Statcking; class StackingController extends BaseAuthedController { @@ -19,77 +21,51 @@ class StackingController extends BaseAuthedController { 'daily_staking_value' => '0', 'planet' => array( 'type' => self::PLANET_TYPE, - 'is_open' => 1, - 'staked_quant' => 0, - 'claim_rewards' => 0, - 'daily_rewards' => 0, - 'staking_value' => 0, - 'maturity_quant' => 0, + 'is_open' => 1 ), 'badge' => array( 'type' => self::BADGE_TYPE, - 'is_open' => 1, - 'staked_quant' => 0, - 'claim_rewards' => 0, - 'daily_rewards' => 0, - 'staking_value' => 0, - 'maturity_quant' => 0, + 'is_open' => 1 ), 'cec' => array( 'type' => self::CEC_TYPE, - 'is_open' => 0, - 'staked_quant' => 0, - 'claim_rewards' => 0, - 'daily_rewards' => 0, - 'staking_value' => 0, - 'maturity_quant' => 0, + 'is_open' => 0 ), ); $rows = Stacking::all(myself()->_getAddress()); - $this->fillStackingInfo($info['planet'], $rows); - $this->fillStackingInfo($info['badge'], $rows); - $this->fillStackingInfo($info['cec'], $rows); + $this->fillStackingInfo($info, 'planet', $rows); + $this->fillStackingInfo($info, 'badge', $rows); + $this->fillStackingInfo($info, 'cec', $rows); myself()->_rspData($info); } - private function fillStackingInfo($info, $rows) + private function fillStackingInfo(&$info, $fieldName, $rows) { + $stackingInfo = $info[$fieldName]; + $stackingInfo['staked_quant'] = 0; + $stackingInfo['claim_rewards'] = 0; + $stackingInfo['daily_rewards'] = 0; + $stackingInfo['staking_value'] = 0; + $stackingInfo['maturity_quant'] = 0; foreach ($rows as $row) { - switch ($info['type']) { - case self::PLANET_TYPE: - { - if ($row['status'] == Stacking::STAKING_STATUS) { - if ($row['start_time'] + $row['stake_time'] < myself()->_getNowTime()) { - $info['staked_quant'] += $row['stacked_num']; + $stakingMeta = mt\Staking::get($row['item_id']); + if ($this->isTypeNft($stackingInfo['type'], $row) && $stakingMeta) { + $stakingDto = Staking::toDto($row); + if ($stakingDto['status'] == Stacking::STAKING_STATUS) { + if ($stakingDto['remain_days'] <= 0) { + $stackingInfo['maturity_quant'] += $stakingDto['stacked_num']; } else { - $info['maturity_quant'] += $row['stacked_num']; - } - $stakingMeta = mt\Staking::get($row['item_id']); - if ($stakingMeta) { - $info['staking_value'] += $stakingMeta['stake_value']; + $stackingInfo['daily_rewards'] += $stakingMeta['cec_value'] * + $stakingMeta['daily_rewards']; + $info['daily_staking_value'] += $stakingMeta['cec_value'] * + $stakingMeta['daily_rewards']; } + $stackingInfo['staked_quant'] += $stakingDto['stacked_num']; + $stackingInfo['staking_value'] += $stakingMeta['stake_value']; + } else { + $stackingInfo['claim_rewards'] += $stakingMeta['total_rewards']; } - } - break; - case self::BADGE_TYPE: - { - if ($row['status'] == Stacking::STAKING_STATUS) { - if ($row['start_time'] + $row['stake_time'] < myself()->_getNowTime()) { - $info['staked_quant'] += $row['stacked_num']; - } else { - $info['maturity_quant'] += $row['stacked_num']; - } - if ($stakingMeta) { - $info['staking_value'] += $stakingMeta['stake_value']; - } - } - } - break; - case self::CEC_TYPE: - { - - } - break; + $info['total_staking_value'] += $stakingMeta['stake_value'] + $stackingInfo['daily_rewards']; } } } @@ -102,21 +78,10 @@ class StackingController extends BaseAuthedController { $rows = Stacking::all(myself()->_getAddress()); foreach ($rows as $row) { if ($row['status'] == Stacking::STAKING_STATUS) { - $info = array(); - $info['token_id'] = $row['token_id']; - $info['token_type'] = $row['token_type']; - $info['contract_address'] = $row['contract_address']; - $info['net_id'] = $row['net_id']; - $info['start_time'] = $row['start_time']; - $info['stake_time'] = $row['stake_time']; - $info['txhash'] = $row['txhash']; - $info['item_id'] = $row['item_id']; - $info['stacked_days'] = intval((myself()->_getNowDaySeconds() - - myself()->_getDaySeconds($row['start_time'])) / 3600 / 24); - $info['remain_days'] = max(0, intval($row['stake_time'] / 3600 / 24) - $info['stacked_days']); - - - array_push($items, $info); + if ($this->isTypeNft($type, $row)) { + array_push($items, Stacking::toDto($info)); + ++$stackedQuant; + } } } myself()->_rspData(array( @@ -134,22 +99,7 @@ class StackingController extends BaseAuthedController { $rows = Stacking::all(myself()->_getAddress()); foreach ($rows as $row) { if ($row['status'] == Stacking::REDEEM_STATUS) { - $info = array(); - $info['token_id'] = $row['token_id']; - $info['token_type'] = $row['token_type']; - $info['contract_address'] = $row['contract_address']; - $info['net_id'] = $row['net_id']; - $info['start_time'] = $row['start_time']; - $info['stake_time'] = $row['stake_time']; - $info['txhash'] = $row['txhash']; - $info['item_id'] = $row['item_id']; - $info['stacked_days'] = intval( - (myself()->_getNowDaySeconds() - - myself()->_getDaySeconds($row['start_time']) + 3600 *24) / 3600 / 24); - $info['remain_days'] = max(0, intval($row['stake_time'] / 3600 / 24) - $info['stacked_days']); - - $info['total_rewards'] = $row['stacked_days'] *$info['daily_rewards']; - array_push($items, $info); + array_push($items, Staking::toDto($info)); } } myself()->_rspData(array( @@ -157,4 +107,33 @@ class StackingController extends BaseAuthedController { )); } + private function isTypeNft($type, $dbInfo) + { + switch ($type) { + case self::PLANET_TYPE: + { + if (in_array($dbInfo['token_type'], array( + ))) { + + } + } + break; + case self::BADGE_TYPE: + { + if (in_array($dbInfo['token_type'], array( + Nft::HONOR1_TYPE + ))) { + return true; + } + } + break; + case self::CEC_TYPE: + { + + } + break; + } + return false; + } + } diff --git a/webapp/models/Staking.php b/webapp/models/Staking.php index 4180700b..0a420cb7 100644 --- a/webapp/models/Staking.php +++ b/webapp/models/Staking.php @@ -164,13 +164,65 @@ class Staking extends BaseModel { } } - public static function calcReward($row, &$reward) + public static function toDto($row) { $stakingMeta = mt\Staking::get($row['item_id']); - if (!$stakingMeta) { - return false; + $dto = array( + 'token_id' => $row['token_id'], + 'token_type' => $row['token_type'], + 'contract_address' => $row['contract_address'], + 'net_id' => $row['net_id'], + 'start_time' => $row['start_time'], + 'stake_time' => $row['stake_time'], + 'txhash' => $row['txhash'], + + 'item_id' => $row['item_id'], + 'cec_value' => $stakingMeta['cec_value'] + ); + $yesterDay = myself()->_getNowDaySeconds() - 3600 * 24; + $passedDays = intval((myself()->_getNowDaySeconds() - + myself()->_getDaySeconds($row['start_time'])) / + 3600 / 24); + $saveDays = intval($row['stake_time'] / 3600 / 24); + $dailyInterest = self::getDailyInterest($row['stake_time']); + $dto['stacked_days'] = max(0, $passedDays); + $dto['remain_days'] = max(0, $saveDays - $dto['stacked_days']); + $dto['daily_rewards'] = $dto['cec_value'] * $dailyInterest; + if ($dto['remain_days'] <= 0 ) { + $dto['cec_rewards'] = $dto['daily_rewards'] * $saveDays; + $dto['total_rewards'] = $dto['daily_rewards'] * $saveDays; + } else { + $dto['cec_rewards'] = 0; + $dto['total_rewards'] = $dto['daily_rewards'] * ($saveDays - $passedDays); + } + return $dto; + } + + public static function getCecPrice() + { + return 1; + } + + public static function getDailyInterest($stakeTime) + { + $months = intval($stakeTime / 3600 / 24 / 30); + if ($months <= 0) { + return 0; + } + //1 3 6 12 24 + if ($months <= 1) { + return 0.01 / 30 * $months; + } else if ($months <= 3) { + return 0.05 / 30 * $months; + } else if ($months <= 6) { + return 0.15 / 30 * $months; + } else if ($months <= 12) { + return 0.4 / 30 * $months; + } else if ($months <= 24) { + return 1 / 30 * $months; + } else { + return 0; } - return true; } }