Skip to content

Commit

Permalink
Add data interface
Browse files Browse the repository at this point in the history
  • Loading branch information
leej1012 committed Dec 20, 2024
1 parent 698a9fc commit 2eb4c85
Show file tree
Hide file tree
Showing 11 changed files with 160 additions and 8 deletions.
5 changes: 4 additions & 1 deletion back-end-projects/Explorer/config/application-dev.properties
Original file line number Diff line number Diff line change
Expand Up @@ -79,4 +79,7 @@ foundation.addresses=ANZH6McmwUKABVKRbduGXfbbqhaz6Fxokd,AUtZgQxZQwnmzPa68zAphyxy

eth.web3j.url=http://polaris1.ont.io:20339

recaptcha.secret.key=
recaptcha.secret.key=

#block.end.2024 = 612800
block.end.2024 = 18316350
Original file line number Diff line number Diff line change
Expand Up @@ -178,5 +178,6 @@ public String getContractHash(String token) {
@Value("${recaptcha.secret.key}")
public String RECAPTCHA_SECRET_KEY;


@Value("${block.end.2024}")
public int BLOCK_END_2024;
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.github.ontio.controller;

import com.alibaba.fastjson.JSONObject;
import com.github.ontio.aop.RequestLimit;
import com.github.ontio.model.dto.Anniversary6thDataDto;
import com.github.ontio.service.IActivityDataService;
Expand Down Expand Up @@ -39,4 +40,25 @@ public Integer queryAddressTxCountInPeriod(@RequestParam @Length(min = 34, max =
public String queryAddressCertainTimeBalance(@RequestParam @Length(min = 34, max = 42, message = "Incorrect address format") String address, @RequestParam Integer timestamp) {
return activityDataService.queryAddressCertainTimeBalance(address, timestamp);
}

@RequestLimit(count = 120)
@ApiOperation(value = "Get ont id or address active time")
@GetMapping(value = "/active_time")
public JSONObject queryAddressActiveTime(@RequestParam @Length(min = 34, max = 42, message = "Incorrect address format") String address) {
return activityDataService.queryAddressActiveTime(address);
}

@RequestLimit(count = 120)
@ApiOperation(value = "Get address send tx count and first tx time")
@GetMapping(value = "/send_tx_info")
public JSONObject queryAddressSendTxInfo(@RequestParam @Length(min = 34, max = 42, message = "Incorrect address format") String address) {
return activityDataService.queryAddressSendTxInfo(address);
}

@RequestLimit(count = 120)
@ApiOperation(value = "Get running node info")
@GetMapping(value = "/running_node_info")
public JSONObject queryRunningNodeInfo(@RequestParam @Length(min = 34, max = 42, message = "Incorrect address format") String address) {
return activityDataService.queryRunningNodeInfo(address);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,6 @@ public interface GovernanceMapper {
List<String> getAllStakingAddress();

List<StakingRewardsDto> getStakingRewardsByAddress(String address, String publicKey, Integer round);

int getStakingAddressCount(List<String> publicKeyList);
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,5 @@ public interface OntidTxDetailMapper extends Mapper<OntidTxDetailDto> {

OntidTxDetailDto selectOneByTxHash(@Param("txHash") String txHash);

OntidTxDetailDto selectOntIdCreateTime(@Param("ontId") String ontId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -85,4 +85,8 @@ List<TransferTxDto> selectTransferTxsOfHashes(@Param("address") String address,
Integer selectFromTxCountInPeriod(String address, Integer startTime, Integer endTime);

BigDecimal selectAssetTransferAmountByAddress(String assetName, String fromAddress, String toAddress, Integer time);

int selectSendTxCountByBlockHeight(String address, int startBlock, int endBlock);

TxDetailDto selectFirstSendTxByBlockHeight(String address, int startBlock, int endBlock);
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.github.ontio.service;

import com.alibaba.fastjson.JSONObject;
import com.github.ontio.model.dto.Anniversary6thDataDto;

public interface IActivityDataService {
Expand All @@ -9,4 +10,10 @@ public interface IActivityDataService {
Integer queryAddressTxCountInPeriod(String address, Integer startTime, Integer endTime);

String queryAddressCertainTimeBalance(String address, Integer timestamp);

JSONObject queryAddressActiveTime(String address);

JSONObject queryAddressSendTxInfo(String address);

JSONObject queryRunningNodeInfo(String address);
}
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
package com.github.ontio.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.github.benmanes.caffeine.cache.Caffeine;
import com.github.benmanes.caffeine.cache.LoadingCache;
import com.github.ontio.common.Address;
import com.github.ontio.common.Common;
import com.github.ontio.common.Helper;
import com.github.ontio.mapper.GovernanceMapper;
import com.github.ontio.mapper.NodeInfoOnChainMapper;
import com.github.ontio.mapper.Oep4TxDetailMapper;
import com.github.ontio.mapper.TxDetailMapper;
import com.github.ontio.config.ParamsConfig;
import com.github.ontio.mapper.*;
import com.github.ontio.model.dao.NodeInfoOnChain;
import com.github.ontio.model.dto.Anniversary6thDataDto;
import com.github.ontio.model.dto.GovernanceInfoDto;
import com.github.ontio.model.dto.*;
import com.github.ontio.service.IActivityDataService;
import com.github.ontio.util.ConstantParam;
import com.github.ontio.util.OntologySDKService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import tk.mybatis.mapper.entity.Example;

import java.math.BigDecimal;
Expand All @@ -27,6 +27,8 @@
@Slf4j
@Service
public class ActivityDataServiceImpl implements IActivityDataService {
@Autowired
private ParamsConfig paramsConfig;
@Autowired
private GovernanceMapper governanceMapper;
@Autowired
Expand All @@ -36,6 +38,8 @@ public class ActivityDataServiceImpl implements IActivityDataService {
@Autowired
private NodeInfoOnChainMapper nodeInfoOnChainMapper;
@Autowired
private OntidTxDetailMapper ontidTxDetailMapper;
@Autowired
private OntologySDKService sdk;


Expand Down Expand Up @@ -121,4 +125,71 @@ public String queryAddressCertainTimeBalance(String address, Integer timestamp)
BigDecimal certainTimeBalance = ontBalance.add(fromAmount).subtract(toAmount);
return certainTimeBalance.stripTrailingZeros().toPlainString();
}

@Override
public JSONObject queryAddressActiveTime(String address) {
Integer activeTime = 0;
boolean registerOntId = false;
String ontId = Common.didont + address;
OntidTxDetailDto ontidTxDetailDto = ontidTxDetailMapper.selectOntIdCreateTime(ontId);
if (ontidTxDetailDto != null) {
activeTime = ontidTxDetailDto.getTxTime();
registerOntId = true;
} else {
Example example = new Example(TxDetailDto.class);
example.selectProperties("txTime");
example.createCriteria().andEqualTo("toAddress", address);
example.setOrderByClause("id LIMIT 1");
TxDetailDto txDetailDto = txDetailMapper.selectOneByExample(example);
if (txDetailDto != null) {
activeTime = txDetailDto.getTxTime();
}
}
JSONObject result = new JSONObject();
result.put("registerOntId", registerOntId);
result.put("activeTime", activeTime);
return result;
}

@Override
public JSONObject queryAddressSendTxInfo(String address) {
int firstTxTime = 0;
int startBlock2024 = 16980450;
int txCount = txDetailMapper.selectSendTxCountByBlockHeight(address, startBlock2024, paramsConfig.BLOCK_END_2024);
if (txCount > 0) {
TxDetailDto txDetailDto = txDetailMapper.selectFirstSendTxByBlockHeight(address, startBlock2024, paramsConfig.BLOCK_END_2024);
if (txDetailDto != null) {
firstTxTime = txDetailDto.getTxTime();
}
}
JSONObject result = new JSONObject();
result.put("txCount", txCount);
result.put("firstTxTime", firstTxTime);
return result;
}

@Override
public JSONObject queryRunningNodeInfo(String address) {
String name = null;
int stakingAddress = 0;
List<NodeInfoOnChainDto> nodeInfoOnChainDtos = nodeInfoOnChainMapper.selectNodesByFilter("", address, "", 0, 0);
if (!CollectionUtils.isEmpty(nodeInfoOnChainDtos)) {
List<String> publicKeyList = new ArrayList<>();
for (NodeInfoOnChainDto nodeInfoOnChainDto : nodeInfoOnChainDtos) {
if (name == null) {
name = nodeInfoOnChainDto.getName();
}
String publicKey = nodeInfoOnChainDto.getPublicKey();
publicKeyList.add(publicKey);
}
stakingAddress = governanceMapper.getStakingAddressCount(publicKeyList);
}
boolean multipleNode = nodeInfoOnChainDtos.size() > 1;

JSONObject result = new JSONObject();
result.put("name", name);
result.put("stakingAddress", stakingAddress);
result.put("multipleNode", multipleNode);
return result;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
withdraw_unfreeze_pos AS withdrawUnfreezePos
FROM tbl_governance_info
WHERE peer_pub_key = #{pubKey}
AND (consensus_pos > 0 OR candidate_pos > 0)
ORDER BY consensus_pos + candidate_pos + new_pos DESC
LIMIT #{start}, #{size}
</select>
Expand All @@ -20,6 +21,7 @@
SELECT count(0)
FROM tbl_governance_info
WHERE peer_pub_key = #{pubKey}
AND (consensus_pos > 0 OR candidate_pos > 0)
</select>

<select id="getStakingInfoByAddress" resultType="com.github.ontio.model.dto.GovernanceInfoDto">
Expand Down Expand Up @@ -67,4 +69,16 @@
</where>
ORDER BY cycle DESC
</select>

<select id="getStakingAddressCount" resultType="java.lang.Integer">
SELECT count(DISTINCT address)
FROM tbl_governance_info
WHERE peer_pub_key IN
(
<foreach collection="publicKeyList" item="publicKey" separator=",">
#{publicKey}
</foreach>
)
AND (consensus_pos > 0 OR candidate_pos > 0)
</select>
</mapper>
Original file line number Diff line number Diff line change
Expand Up @@ -60,4 +60,13 @@
where tx_hash = #{txHash}
</select>

<select id="selectOntIdCreateTime" resultType="com.github.ontio.model.dto.OntidTxDetailDto">
select <include refid="detailColumns"/>
from tbl_ontid_tx_detail
where ontid = #{ontId}
AND description LIKE 'Register%'
ORDER BY id
LIMIT 1
</select>

</mapper>
Original file line number Diff line number Diff line change
Expand Up @@ -663,4 +663,22 @@
AND tx_time > #{time}
</where>
</select>

<select id="selectSendTxCountByBlockHeight" resultType="Integer">
SELECT COUNT(DISTINCT tx_hash)
FROM tbl_tx_detail
WHERE from_address = #{address}
AND block_height > #{startBlock}
AND block_height <![CDATA[<=]]> #{endBlock};
</select>

<select id="selectFirstSendTxByBlockHeight" resultType="com.github.ontio.model.dto.TxDetailDto">
SELECT
<include refid="txDetailColumns"/>
FROM tbl_tx_detail
WHERE from_address = #{address}
AND block_height > #{startBlock}
AND block_height <![CDATA[<=]]> #{endBlock}
ORDER BY block_height LIMIT 1
</select>
</mapper>

0 comments on commit 2eb4c85

Please sign in to comment.