Skip to content

Commit

Permalink
Add getAddressStakingSnapshot
Browse files Browse the repository at this point in the history
  • Loading branch information
leej1012 committed Jan 24, 2025
1 parent f84dc26 commit 4b13518
Show file tree
Hide file tree
Showing 8 changed files with 101 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -446,4 +446,13 @@ public ResponseBean getAddressStakingRewards(@RequestParam @Length(min = 34, max
@RequestParam(required = false) Integer round) {
return nodesService.getAddressStakingRewards(address, publicKey, round);
}

@RequestLimit(count = 60)
@ApiOperation(value = "get address staking snapshot")
@GetMapping(value = "/staking-snapshot")
public ResponseBean getAddressStakingSnapshot(@RequestParam @Length(min = 34, max = 34, message = "Incorrect address format") String address,
@RequestParam(value = "public_key", required = false) String publicKey,
@RequestParam(required = false) Integer round) {
return nodesService.getAddressStakingSnapshot(address, publicKey, round);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,7 @@ public interface GovernanceMapper {

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

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

int getStakingAddressCount(List<String> publicKeyList);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.github.ontio.model.dto;

import lombok.Data;

@Data
public class StakingInfoDto {
private String address;
private String publicKey;
private Integer processing;
private Integer unstaking;
private Integer staked;
private Integer withdrawable;
private String rewards;
private Integer round;
}

Original file line number Diff line number Diff line change
@@ -1,12 +1,21 @@
package com.github.ontio.model.dto;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonInclude;
import lombok.Data;

@Data
@JsonInclude(JsonInclude.Include.NON_NULL)
public class StakingRewardsDto {
private String address;
private String publicKey;
private String rewards;
private int round;
private Integer staked;
private Integer processing;
private Integer unstaking;
private Integer withdrawable;
private Integer round;
@JsonIgnore
private Integer nextWithdrawable;
}

Original file line number Diff line number Diff line change
Expand Up @@ -115,4 +115,6 @@ public interface INodesService {
ResponseBean getAddressRegisterNodeOnt(String address);

ResponseBean getAddressStakingRewards(String address, String publicKey, Integer round);

ResponseBean getAddressStakingSnapshot(String address, String publicKey, Integer round);
}
Original file line number Diff line number Diff line change
Expand Up @@ -1348,4 +1348,10 @@ public ResponseBean getAddressStakingRewards(String address, String publicKey, I
List<StakingRewardsDto> list = governanceMapper.getStakingRewardsByAddress(address, publicKey, round);
return new ResponseBean(ErrorInfo.SUCCESS.code(), ErrorInfo.SUCCESS.desc(), list);
}

@Override
public ResponseBean getAddressStakingSnapshot(String address, String publicKey, Integer round) {
List<StakingRewardsDto> list = governanceMapper.getStakingSnapshotByAddress(address, publicKey, round);
return new ResponseBean(ErrorInfo.SUCCESS.code(), ErrorInfo.SUCCESS.desc(), list);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
ALTER TABLE tbl_income_info ADD COLUMN withdraw_pos int(11) DEFAULT NULL AFTER staking_pos;
ALTER TABLE tbl_income_info ADD COLUMN new_pos int(11) DEFAULT NULL AFTER withdraw_pos;
ALTER TABLE tbl_income_info ADD COLUMN next_withdrawable_pos int(11) DEFAULT NULL AFTER new_pos;
ALTER TABLE tbl_income_info ADD COLUMN peer int(2) DEFAULT NULL AFTER next_withdrawable_pos;

CREATE TABLE `tbl_node_staking_info` (
`id` bigint(20) NOT NULL AUTO_INCREMENT,
`peer_pub_key` char(80) NOT NULL COMMENT '节点公钥',
`address` char(80) NOT NULL COMMENT '质押地址',
`amount` int(11) NOT NULL COMMENT '数量',
`action` varchar(50) NOT NULL COMMENT '质押行为',
`node_status` int(2) NOT NULL COMMENT '1-候选节点;2-共识节点',
`block_height` int(11) NOT NULL COMMENT '块高',
`tx_hash` varchar(100) NOT NULL COMMENT '交易hash',
`cycle` int(11) NOT NULL COMMENT '周期',
PRIMARY KEY (`id`) USING BTREE,
KEY `idx_peer_address` (`peer_pub_key`,`address`) USING BTREE,
KEY `idx_address` (`address`,`cycle`) USING BTREE,
KEY `idx_tx_hash` (`tx_hash`) USING BTREE
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
Original file line number Diff line number Diff line change
Expand Up @@ -53,13 +53,14 @@

<select id="getStakingRewardsByAddress" resultType="com.github.ontio.model.dto.StakingRewardsDto">
SELECT
address,
peer_pub_key AS publicKey,
ong_income AS rewards,
cycle AS round
address,
peer_pub_key AS publicKey,
ong_income AS rewards,
cycle AS round
FROM tbl_income_info
<where>
address = #{address}
AND ong_income+0 > 0
<if test="publicKey !=null and publicKey != ''">
AND peer_pub_key = #{publicKey}
</if>
Expand All @@ -70,6 +71,37 @@
ORDER BY cycle DESC
</select>

<select id="getStakingSnapshotByAddress" resultType="com.github.ontio.model.dto.StakingRewardsDto">
SELECT
a.address,
a.peer_pub_key AS publicKey,
a.ong_income AS rewards,
a.staking_pos AS staked,
a.withdraw_pos AS unstaking,
CASE
WHEN a.peer = 1 THEN
GREATEST(IFNULL(c.staking_pos,0) - a.staking_pos,0)
ELSE a.new_pos
END AS processing,
IFNULL(b.next_withdrawable_pos,0) AS withdrawable,
a.cycle AS round
FROM tbl_income_info a
LEFT JOIN tbl_income_info b
ON a.address = b.address AND a.peer_pub_key = b.peer_pub_key AND a.cycle-1 = b.cycle
LEFT JOIN tbl_income_info c
ON a.address = c.address AND a.peer_pub_key = c.peer_pub_key AND a.cycle = c.cycle-1
<where>
a.address = #{address}
<if test="publicKey !=null and publicKey != ''">
AND a.peer_pub_key = #{publicKey}
</if>
<if test="round !=null">
AND a.cycle = #{round}
</if>
</where>
ORDER BY a.cycle DESC, a.peer_pub_key
</select>

<select id="getStakingAddressCount" resultType="java.lang.Integer">
SELECT count(DISTINCT address)
FROM tbl_governance_info
Expand Down

0 comments on commit 4b13518

Please sign in to comment.