Skip to content

Commit 3b93b64

Browse files
committed
Update calculationUserIncentives and getAddressRegisterNodeList
1 parent 8968b7a commit 3b93b64

File tree

4 files changed

+113
-23
lines changed

4 files changed

+113
-23
lines changed

back-end-projects/Explorer/src/main/java/com/github/ontio/model/dto/InspireResultDto.java

+4
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ public class InspireResultDto {
2020

2121
private String nodeGasFeeIncentiveRate;
2222

23+
private String nodeApr;
24+
2325
private String userGasFeeIncentive;
2426

2527
private String userGasFeeIncentiveRate;
@@ -31,4 +33,6 @@ public class InspireResultDto {
3133
private String userFoundationBonusIncentive;
3234

3335
private String userFoundationBonusIncentiveRate;
36+
37+
private String userApr;
3438
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package com.github.ontio.model.dto;
2+
3+
import com.github.ontio.model.dao.NodeInfoOffChain;
4+
import lombok.Data;
5+
6+
import java.util.List;
7+
8+
/**
9+
* @author lijie
10+
* @version 1.0
11+
* @date 2024/5/30
12+
*/
13+
@Data
14+
public class RegisterNodeDto {
15+
16+
List<NodeInfoOffChain> registerNodeList;
17+
18+
private String reward = "0";
19+
}

back-end-projects/Explorer/src/main/java/com/github/ontio/model/dto/UserInspireCalculationDto.java

+2
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
@JsonInclude(JsonInclude.Include.NON_NULL)
88
public class UserInspireCalculationDto {
99

10+
private Long nodeAddAmount;
11+
1012
private Long stakeAmount;
1113

1214
private String publicKey;

back-end-projects/Explorer/src/main/java/com/github/ontio/service/impl/NodesServiceImpl.java

+88-23
Original file line numberDiff line numberDiff line change
@@ -633,11 +633,13 @@ public InspireResultDto calculationUserIncentives(UserInspireCalculationDto dto)
633633
String[] addressSplit = paramsConfig.FOUNDATION_ADDRESSES.split(",");
634634
List<String> foundationAddresses = Arrays.asList(addressSplit);
635635

636-
Long stakeAmount = dto.getStakeAmount();
636+
Long nodeAddAmount = Optional.ofNullable(dto.getNodeAddAmount()).orElse(0L);
637+
Long stakeAmount = Optional.ofNullable(dto.getStakeAmount()).orElse(0L);
637638
String publicKey = dto.getPublicKey();
638-
if (stakeAmount <= 0) {
639+
if (nodeAddAmount <= 0 && stakeAmount <= 0) {
639640
throw new ExplorerException(ErrorInfo.PARAM_ERROR);
640641
}
642+
641643
Long oldCurrentStake = 0L;
642644
Long newCurrentStake = 0L;
643645
initSDK();
@@ -654,6 +656,7 @@ public InspireResultDto calculationUserIncentives(UserInspireCalculationDto dto)
654656
if (one.getPublicKey().equals(publicKey)) {
655657
oldCurrentStake = one.getCurrentStake();
656658
Long initPos = one.getInitPos();
659+
Long newInitPos = initPos + nodeAddAmount;
657660
Long totalPos = one.getTotalPos();
658661
Long maxAuthorize = one.getMaxAuthorize();
659662
Long allowMaxStake = maxAuthorize - totalPos;
@@ -662,7 +665,8 @@ public InspireResultDto calculationUserIncentives(UserInspireCalculationDto dto)
662665
stakeAmount = allowMaxStake;
663666
}
664667

665-
newCurrentStake = initPos + newTotalPos;
668+
newCurrentStake = newInitPos + newTotalPos;
669+
one.setInitPos(newInitPos);
666670
one.setTotalPos(newTotalPos);
667671
one.setCurrentStake(newCurrentStake);
668672
// 候选节点顶掉共识的情况
@@ -692,8 +696,10 @@ public InspireResultDto calculationUserIncentives(UserInspireCalculationDto dto)
692696
BigDecimal newSr = BigDecimal.ZERO;
693697
String initProportion = calculationNode.getNodeProportion().replace("%", "");
694698
String stakeProportion = calculationNode.getUserProportion().replace("%", "");
695-
BigDecimal initUserProportion = new BigDecimal(initProportion).divide(new BigDecimal(100), 2, BigDecimal.ROUND_HALF_UP);
696-
BigDecimal stakeUserProportion = new BigDecimal(stakeProportion).divide(new BigDecimal(100), 2, BigDecimal.ROUND_HALF_UP);
699+
BigDecimal initUserProportion = new BigDecimal(initProportion).divide(new BigDecimal(100), 2, RoundingMode.HALF_UP);
700+
BigDecimal initNodeProportion = new BigDecimal(1).subtract(initUserProportion);
701+
BigDecimal stakeUserProportion = new BigDecimal(stakeProportion).divide(new BigDecimal(100), 2, RoundingMode.HALF_UP);
702+
BigDecimal stakeNodeProportion = new BigDecimal(1).subtract(stakeUserProportion);
697703
if (foundationNodes.contains(publicKey)) {
698704
initSDK();
699705
Long fu = 0L;
@@ -708,13 +714,13 @@ public InspireResultDto calculationUserIncentives(UserInspireCalculationDto dto)
708714
}
709715

710716
// old sr
711-
BigDecimal decimal1 = initUserProportion.multiply(new BigDecimal(fu * oldCurrentStake)).divide(new BigDecimal(oldCurrentStake - fp), 12, BigDecimal.ROUND_HALF_UP);
717+
BigDecimal decimal1 = initUserProportion.multiply(new BigDecimal(fu * oldCurrentStake)).divide(new BigDecimal(oldCurrentStake - fp), 12, RoundingMode.HALF_UP);
712718
BigDecimal subtract1 = new BigDecimal(1).subtract(initUserProportion);
713719
BigDecimal decimal2 = new BigDecimal(fp).multiply(subtract1);
714720
oldSr = decimal1.add(decimal2);
715721

716722
// new sr
717-
BigDecimal decimal3 = initUserProportion.multiply(new BigDecimal(fu * newCurrentStake)).divide(new BigDecimal(newCurrentStake - fp), 12, BigDecimal.ROUND_HALF_UP);
723+
BigDecimal decimal3 = initUserProportion.multiply(new BigDecimal(fu * newCurrentStake)).divide(new BigDecimal(newCurrentStake - fp), 12, RoundingMode.HALF_UP);
718724
BigDecimal subtract2 = new BigDecimal(1).subtract(initUserProportion);
719725
BigDecimal decimal4 = new BigDecimal(fp).multiply(subtract2);
720726
newSr = decimal3.add(decimal4);
@@ -739,7 +745,7 @@ public InspireResultDto calculationUserIncentives(UserInspireCalculationDto dto)
739745
BigDecimal topStake = new BigDecimal(top49Stake);
740746

741747
// 第一轮
742-
BigDecimal first = new BigDecimal(10000000).divide(topStake, 12, BigDecimal.ROUND_HALF_UP);
748+
BigDecimal first = new BigDecimal(10000000).divide(topStake, 12, RoundingMode.HALF_UP);
743749
// 第二轮 数据库获取
744750
List<InspireCalculationParams> inspireCalculationParams = inspireCalculationParamsMapper.selectAll();
745751
if (CollectionUtils.isEmpty(inspireCalculationParams)) {
@@ -752,15 +758,15 @@ public InspireResultDto calculationUserIncentives(UserInspireCalculationDto dto)
752758
BigDecimal ont = params.getOntPrice();
753759
BigDecimal ong = params.getOngPrice();
754760
BigDecimal subtract = topStake.subtract(totalFpFu);
755-
BigDecimal second = totalSr.divide(subtract, 12, BigDecimal.ROUND_HALF_UP);
761+
BigDecimal second = totalSr.divide(subtract, 12, RoundingMode.HALF_UP);
756762

757763
// 候选节点的质押总和
758764
BigDecimal candidateTotalStake = getTotalStake(candidateNodes);
759765

760766
BigDecimal consensusTotalStake = getTotalStake(consensusNodes);
761767
BigDecimal consensusCount = new BigDecimal(consensusNodes.size());
762768
// 共识节点的平均质押量
763-
BigDecimal consensusAverageStake = consensusTotalStake.divide(consensusCount, 12, BigDecimal.ROUND_HALF_UP);
769+
BigDecimal consensusAverageStake = consensusTotalStake.divide(consensusCount, 12, RoundingMode.HALF_UP);
764770

765771
// A 为所有共识节点的激励系数总和
766772
Map<String, BigDecimal> consensusInspireMap = new HashMap<>();
@@ -777,6 +783,7 @@ public InspireResultDto calculationUserIncentives(UserInspireCalculationDto dto)
777783
BigDecimal finalReleaseOng = BigDecimal.ZERO;
778784
BigDecimal finalCommission = BigDecimal.ZERO;
779785
BigDecimal userFoundationInspire = BigDecimal.ZERO;
786+
BigDecimal nodeFoundationInspire = BigDecimal.ZERO;
780787

781788
Integer status = calculationNode.getStatus();
782789

@@ -802,30 +809,49 @@ public InspireResultDto calculationUserIncentives(UserInspireCalculationDto dto)
802809
BigDecimal siSubFp = currentStake.subtract(fp);
803810
// 用户收益
804811
BigDecimal siPb = currentStake.multiply(initUserProportion);
805-
BigDecimal add = siPb.divide(siSubFp, 12, BigDecimal.ROUND_HALF_UP).add(second);
812+
BigDecimal add = siPb.divide(siSubFp, 12, RoundingMode.HALF_UP).add(second);
806813
userFoundationInspire = first.multiply(stakeAmountDecimal).multiply(add);
807814
}
808815
if (totalPos.compareTo(BigDecimal.ZERO) == 0) {
809816
totalPos = new BigDecimal(1);
810817
}
811-
BigDecimal userStakePercentInTotalPos = stakeAmountDecimal.divide(totalPos, 12, BigDecimal.ROUND_HALF_UP);
812-
BigDecimal initPercent = nodeStake.divide(currentStake, 12, BigDecimal.ROUND_HALF_UP);
813-
BigDecimal stakePercent = totalPos.divide(currentStake, 12, BigDecimal.ROUND_HALF_UP);
818+
BigDecimal userStakePercentInTotalPos = stakeAmountDecimal.divide(totalPos, 12, RoundingMode.HALF_UP);
819+
BigDecimal initPercent = nodeStake.divide(currentStake, 12, RoundingMode.HALF_UP);
820+
BigDecimal stakePercent = totalPos.divide(currentStake, 12, RoundingMode.HALF_UP);
814821

815822
BigDecimal initPartFinalReleaseOng = finalReleaseOng.multiply(initPercent);
816823
BigDecimal stakePartFinalReleaseOng = finalReleaseOng.multiply(stakePercent);
817824

818825
BigDecimal initPartFinalCommission = finalCommission.multiply(initPercent);
819826
BigDecimal stakePartFinalCommission = finalCommission.multiply(stakePercent);
820827

828+
BigDecimal finalNodeReleaseOng = ((initPartFinalReleaseOng.multiply(initNodeProportion)).add((stakePartFinalReleaseOng.multiply(stakeNodeProportion))));
821829
BigDecimal finalUserReleaseOng = ((initPartFinalReleaseOng.multiply(initUserProportion)).add((stakePartFinalReleaseOng.multiply(stakeUserProportion)))).multiply(userStakePercentInTotalPos);
830+
BigDecimal finalNodeCommission = ((initPartFinalCommission.multiply(initNodeProportion)).add((stakePartFinalCommission.multiply(stakeNodeProportion))));
822831
BigDecimal finalUserCommission = ((initPartFinalCommission.multiply(initUserProportion)).add((stakePartFinalCommission.multiply(stakeUserProportion)))).multiply(userStakePercentInTotalPos);
823832

824-
BigDecimal stakeAmountUsd = stakeAmountDecimal.multiply(ont);
833+
BigDecimal nodeStakeAmountUsd = nodeStake.multiply(ont);
834+
BigDecimal nodeReleaseUsd = finalNodeReleaseOng.multiply(ong);
835+
BigDecimal nodeCommissionUsd = finalNodeCommission.multiply(ong);
836+
BigDecimal nodeFoundationUsd = nodeFoundationInspire.multiply(ong);
837+
BigDecimal userStakeAmountUsd = stakeAmountDecimal.multiply(ont);
825838
BigDecimal userReleaseUsd = finalUserReleaseOng.multiply(ong);
826839
BigDecimal userCommissionUsd = finalUserCommission.multiply(ong);
827840
BigDecimal userFoundationUsd = userFoundationInspire.multiply(ong);
828841

842+
if (initPos > 0) {
843+
nodeInspire.setNodeReleasedOngIncentive(finalNodeReleaseOng.setScale(4, RoundingMode.DOWN).toPlainString());
844+
nodeInspire.setNodeGasFeeIncentive(finalNodeCommission.setScale(4, RoundingMode.DOWN).toPlainString());
845+
nodeInspire.setNodeFoundationBonusIncentive(nodeFoundationInspire.setScale(4, RoundingMode.DOWN).toPlainString());
846+
BigDecimal nodeReleasedOngIncentiveRate = nodeReleaseUsd.divide(nodeStakeAmountUsd, 12, RoundingMode.HALF_UP).multiply(oneHundred).setScale(2, RoundingMode.HALF_UP);
847+
BigDecimal nodeGasFeeIncentiveRate = nodeCommissionUsd.divide(nodeStakeAmountUsd, 12, RoundingMode.HALF_UP).multiply(oneHundred).setScale(2, RoundingMode.HALF_UP);
848+
BigDecimal nodeFoundationBonusIncentiveRate = nodeFoundationUsd.divide(nodeStakeAmountUsd, 12, RoundingMode.HALF_UP).multiply(oneHundred).setScale(2, RoundingMode.HALF_UP);
849+
String nodeApr = nodeReleasedOngIncentiveRate.add(nodeGasFeeIncentiveRate).add(nodeFoundationBonusIncentiveRate).toPlainString() + "%";
850+
nodeInspire.setNodeReleasedOngIncentiveRate(nodeReleasedOngIncentiveRate.toPlainString() + "%");
851+
nodeInspire.setNodeGasFeeIncentiveRate(nodeGasFeeIncentiveRate.toPlainString() + "%");
852+
nodeInspire.setNodeFoundationBonusIncentiveRate(nodeFoundationBonusIncentiveRate.toPlainString() + "%");
853+
nodeInspire.setNodeApr(nodeApr);
854+
}
829855
Long maxAuthorize = calculationNode.getMaxAuthorize();
830856
// 考虑此节点用户质押部分满了的情况,此时用户不能再进行质押,收益为0
831857
if (maxAuthorize == 0 && totalPos1 == 0) {
@@ -835,13 +861,19 @@ public InspireResultDto calculationUserIncentives(UserInspireCalculationDto dto)
835861
nodeInspire.setUserReleasedOngIncentiveRate("0.00%");
836862
nodeInspire.setUserGasFeeIncentiveRate("0.00%");
837863
nodeInspire.setUserFoundationBonusIncentiveRate("0.00%");
864+
nodeInspire.setUserApr("0.00%");
838865
} else {
839-
nodeInspire.setUserReleasedOngIncentive(finalUserReleaseOng.setScale(4, BigDecimal.ROUND_DOWN).toPlainString());
840-
nodeInspire.setUserGasFeeIncentive(finalUserCommission.setScale(4, BigDecimal.ROUND_DOWN).toPlainString());
841-
nodeInspire.setUserFoundationBonusIncentive(userFoundationInspire.setScale(4, BigDecimal.ROUND_DOWN).toPlainString());
842-
nodeInspire.setUserReleasedOngIncentiveRate(userReleaseUsd.divide(stakeAmountUsd, 12, BigDecimal.ROUND_HALF_UP).multiply(oneHundred).setScale(2, BigDecimal.ROUND_HALF_UP).toPlainString() + "%");
843-
nodeInspire.setUserGasFeeIncentiveRate(userCommissionUsd.divide(stakeAmountUsd, 12, BigDecimal.ROUND_HALF_UP).multiply(oneHundred).setScale(2, BigDecimal.ROUND_HALF_UP).toPlainString() + "%");
844-
nodeInspire.setUserFoundationBonusIncentiveRate(userFoundationUsd.divide(stakeAmountUsd, 12, BigDecimal.ROUND_HALF_UP).multiply(oneHundred).setScale(2, BigDecimal.ROUND_HALF_UP).toPlainString() + "%");
866+
nodeInspire.setUserReleasedOngIncentive(finalUserReleaseOng.setScale(4, RoundingMode.DOWN).toPlainString());
867+
nodeInspire.setUserGasFeeIncentive(finalUserCommission.setScale(4, RoundingMode.DOWN).toPlainString());
868+
nodeInspire.setUserFoundationBonusIncentive(userFoundationInspire.setScale(4, RoundingMode.DOWN).toPlainString());
869+
BigDecimal userReleasedOngIncentiveRate = userReleaseUsd.divide(userStakeAmountUsd, 12, RoundingMode.HALF_UP).multiply(oneHundred).setScale(2, RoundingMode.HALF_UP);
870+
BigDecimal userGasFeeIncentiveRate = userCommissionUsd.divide(userStakeAmountUsd, 12, RoundingMode.HALF_UP).multiply(oneHundred).setScale(2, RoundingMode.HALF_UP);
871+
BigDecimal userFoundationBonusIncentiveRate = userFoundationUsd.divide(userStakeAmountUsd, 12, RoundingMode.HALF_UP).multiply(oneHundred).setScale(2, RoundingMode.HALF_UP);
872+
String userApr = userReleasedOngIncentiveRate.add(userGasFeeIncentiveRate).add(userFoundationBonusIncentiveRate).toPlainString() + "%";
873+
nodeInspire.setUserReleasedOngIncentiveRate(userReleasedOngIncentiveRate.toPlainString() + "%");
874+
nodeInspire.setUserGasFeeIncentiveRate(userGasFeeIncentiveRate.toPlainString() + "%");
875+
nodeInspire.setUserFoundationBonusIncentiveRate(userFoundationBonusIncentiveRate.toPlainString() + "%");
876+
nodeInspire.setUserApr(userApr);
845877
}
846878

847879
return nodeInspire;
@@ -1002,10 +1034,24 @@ public PageResponseBean getNodesByFilter(NodesInfoDto dto) {
10021034

10031035
@Override
10041036
public ResponseBean getAddressRegisterNodeList(String address) {
1037+
RegisterNodeDto result = new RegisterNodeDto();
1038+
initSDK();
1039+
try {
1040+
String splitFeeStr = sdk.getSplitFee(address);
1041+
if (StringUtils.hasLength(splitFeeStr)) {
1042+
JSONObject splitFee = JSONObject.parseObject(splitFeeStr);
1043+
String reward = splitFee.getBigDecimal("amount").divide(ConstantParam.NINE_BIT_DECIMAL, 9, RoundingMode.DOWN).stripTrailingZeros().toPlainString();
1044+
result.setReward(reward);
1045+
}
1046+
} catch (Exception e) {
1047+
log.error("getAddressRegisterNodeInfo error:{},{}", address, e.getMessage());
1048+
}
1049+
10051050
List<NodeInfoOffChain> registerNodeList = nodeInfoOffChainMapper.selectAllRegisterNodeInfo(address);
1051+
List<NodeInfoOffChain> list = Collections.emptyList();
10061052
if (!CollectionUtils.isEmpty(registerNodeList)) {
1053+
list = new ArrayList<>();
10071054
try {
1008-
initSDK();
10091055
Map peerPoolMap = sdk.getPeerPoolMap();
10101056
for (NodeInfoOffChain registerNodeInfo : registerNodeList) {
10111057
String publicKey = registerNodeInfo.getPublicKey();
@@ -1027,17 +1073,36 @@ public ResponseBean getAddressRegisterNodeList(String address) {
10271073
if (StringUtils.hasLength(authorizeInfo)) {
10281074
JSONObject jsonObject = JSONObject.parseObject(authorizeInfo);
10291075
initPos = jsonObject.getLong("withdrawUnfreezePos");
1076+
} else {
1077+
continue;
10301078
}
10311079
}
10321080
registerNodeInfo.setStatus(status);
10331081
registerNodeInfo.setInitPos(initPos);
10341082
registerNodeInfo.setTotalPos(totalPos);
1083+
list.add(registerNodeInfo);
10351084
}
1085+
list.sort((o1, o2) -> {
1086+
// 先按照status排序(1-在线->3-已退出->2-正在退出),status相同的情况下按照节点质押数量倒序
1087+
int compareByPrice = Integer.compare(o1.getStatus(), o2.getStatus());
1088+
if (compareByPrice != 0) {
1089+
if (o1.getStatus() == 2 && o2.getStatus() == 3) {
1090+
return 1;
1091+
} else if (o1.getStatus() == 3 && o2.getStatus() == 2) {
1092+
return -1;
1093+
} else {
1094+
return compareByPrice;
1095+
}
1096+
} else {
1097+
return Long.compare(o2.getInitPos(), o1.getInitPos());
1098+
}
1099+
});
10361100
} catch (Exception e) {
10371101
log.error("getAddressRegisterNodeInfo error:{},{}", address, e.getMessage());
10381102
}
10391103
}
1040-
return new ResponseBean(ErrorInfo.SUCCESS.code(), ErrorInfo.SUCCESS.desc(), registerNodeList);
1104+
result.setRegisterNodeList(list);
1105+
return new ResponseBean(ErrorInfo.SUCCESS.code(), ErrorInfo.SUCCESS.desc(), result);
10411106
}
10421107

10431108
@Override

0 commit comments

Comments
 (0)