game2006api/webapp/models/Staking.php
aozhiwei f55f5422ad 1
2023-08-25 16:41:51 +08:00

169 lines
5.3 KiB
PHP

<?php
namespace models;
require_once("mt/Staking.php");
use phpcommon\SqlHelper;
class Staking extends BaseModel {
const STAKING_STATUS = 0;
const REDEEM_STATUS = 1;
const NFT721 = 1;
public static function all($address)
{
$result = array();
$rows = SqlHelper::ormSelect(
myself()->_getMysql(''),
't_staking',
array(
'address' => $address
)
);
foreach ($rows as $row) {
if ($row['nft_type'] == self::NFT721) {
if (!$row['item_id']) {
$itemId = 0;
$tokenType = 0;
if (!self::repair721NftInfo($row['token_id'], $row['contract_address'], $row['net_id'],
$row['start_time'], $itemId, $tokenType)) {
continue;
}
$row['item_id'] = $itemId;
$row['token_type'] = $tokenType;
}
}
array_push($result, $row);
}
return $result;
}
public static function staked721($data, $netId)
{
$address = $data['address'];
foreach ($data['infos'] as $info) {
SqlHelper::upsert(
myself()->_getMysql(''),
't_staking',
array(
'token_id' => $info['tokenid'],
'contract_address' => $info['nft'],
'net_id' => $netId,
'start_time' => $info['start'],
),
array(
),
array(
'address' => $address,
'token_id' => $info['tokenid'],
'token_type' => '0',
'net_id' => $netId,
'contract_address' => $info['nft'],
'stacked_num' => 1,
'start_time' => $info['start'],
'stake_time' => $info['stakeTime'],
'status' => self::STAKING_STATUS,
'nft_type' => self::NFT721,
'createtime' => myself()->_getNowTime(),
'modifytime' => myself()->_getNowTime(),
)
);
$itemId = 0;
$tokenType = 0;
self::repair721NftInfo($info['tokenid'], $info['nft'], $netId, $info['start'],
$itemId, $tokenId);
}
}
public static function repair721NftInfo($tokenId, $contractAddress, $netId, $startTime,
&$itemId, &$tokenType)
{
$row = SqlHelper::ormSelect(
myself()->_getMarketMysql(),
't_nft',
array(
'token_id' => $tokenId,
'contract_address' => $contractAddress,
'net_id' => $netId,
)
);
if (!$row) {
return false;
}
$itemId = $row['item_id'];
$tokenType = $row['token_type'];
SqlHelper::update(
myself()->_getMysql(''),
't_staking',
array(
'token_id' => $tokenId,
'contract_address' => $contractAddress,
'net_id' => $netId,
'start_time' => $startTime,
'nft_type' => self::NFT721,
),
array(
'item_id' => $itemId,
'token_type' => $tokenType
)
);
return true;
}
public static function redeem721($data, $netId, $redeemTime, $txHash)
{
$address = $data['address'];
foreach ($data['infos'] as $info) {
SqlHelper::upsert(
myself()->_getMysql(''),
't_staking',
array(
'token_id' => $info['tokenid'],
'contract_address' => $info['nft'],
'net_id' => $netId,
'start_time' => $info['start'],
),
array(
'txhash' => $txHash,
'redeem_time' => $redeemTime,
'status' => self::REDEEM_STATUS,
),
array(
'address' => $address,
'token_id' => $info['tokenid'],
'token_type' => '0',
'net_id' => $netId,
'contract_address' => $info['nft'],
'stacked_num' => 1,
'start_time' => $info['start'],
'stake_time' => $info['stakeTime'],
'txhash' => $txHash,
'redeem_time' => $redeemTime,
'status' => self::REDEEM_STATUS,
'nft_type' => self::NFT721,
'createtime' => myself()->_getNowTime(),
'modifytime' => myself()->_getNowTime(),
)
);
$itemId = 0;
$tokenType = 0;
self::repair721NftInfo($info['tokenid'], $info['nft'], $netId, $info['start'],
$itemId, $tokenId);
}
}
public static function calcReward($row, &$reward)
{
$stakingMeta = mt\Staking::get($row['item_id']);
if (!$stakingMeta) {
return false;
}
return true;
}
}