Skip to content

Commit

Permalink
voteByHeight support systemStakingBucket
Browse files Browse the repository at this point in the history
  • Loading branch information
millken committed Sep 28, 2023
1 parent 2241fb7 commit f09e6e1
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 18 deletions.
6 changes: 6 additions & 0 deletions apiservice/staking_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,12 @@ func (s *StakingService) VoteByHeight(ctx context.Context, req *api.VoteByHeight
if err != nil {
return nil, err
}
systemStakeAmounts, systemVoteWeights, err := actions.GetSystemStakedBucketByVoterAndHeight(addr, height)
if err != nil {
return nil, err
}
stakeAmounts = stakeAmounts.Add(stakeAmounts, systemStakeAmounts)
voteWeights = voteWeights.Add(voteWeights, systemVoteWeights)
resp.StakeAmount = append(resp.StakeAmount, util.RauToString(stakeAmounts, util.IotxDecimalNum))
resp.VoteWeight = append(resp.VoteWeight, util.RauToString(voteWeights, util.IotxDecimalNum))
}
Expand Down
59 changes: 41 additions & 18 deletions common/actions/bucket.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,23 +104,46 @@ func GetStakedBucketByVoterAndHeight(addr string, height uint64) (*big.Int, *big
votingPower, _ := big.NewInt(0).SetString(stakingBucket.VotingPower, 0)
totalVotingPower.Add(totalVotingPower, votingPower)
}
// if err := db.Table("staking_buckets").Distinct("bucket_id").Where("block_height<=? and owner_address=?", height, addr).Find(&ids).Error; err != nil {
// return nil, nil, err
// }
// totalStakeAmount := big.NewInt(0)
// totalVotingPower := big.NewInt(0)
// for _, id := range ids {
// stakingBucket, err := getLatestStakingBucketOwnerWithHeight(id.BucketID, height)
// if err != nil {
// return nil, nil, err
// }
// if addr != stakingBucket.OwnerAddress {
// continue
// }
// stakeAmount, _ := big.NewInt(0).SetString(stakingBucket.StakedAmount, 0)
// totalStakeAmount.Add(totalStakeAmount, stakeAmount)
// votingPower, _ := big.NewInt(0).SetString(stakingBucket.VotingPower, 0)
// totalVotingPower.Add(totalVotingPower, votingPower)
// }
return totalStakeAmount, totalVotingPower, nil
}

func GetSystemBucketIDsByVoterAndHeight(addr string, height uint64) ([]uint64, error) {
db := db.DB()
var ids []struct {
BucketID uint64
}
if err := db.Table("system_staking_buckets").Distinct("bucket_id").Where("block_height<=? and owner_address=?", height, addr).Find(&ids).Error; err != nil {
return nil, err
}
bucketID := []uint64{}
for _, id := range ids {
bucketID = append(bucketID, id.BucketID)
}
return bucketID, nil
}

func GetSystemStakedBucketByVoterAndHeight(addr string, height uint64) (*big.Int, *big.Int, error) {
db := db.DB()

bucketIDs, err := GetSystemBucketIDsByVoterAndHeight(addr, height)
if err != nil {
return nil, nil, err
}
var stakingBuckets []*model.StakingBucket
query := "select t1.* from system_staking_buckets t1 INNER JOIN (select MAX(id)AS max_id from system_staking_buckets t4 where block_height<=? and bucket_id in ? GROUP BY bucket_id) as t2 on t2.max_id=t1.id"
if err := db.Raw(query, height, bucketIDs).Scan(&stakingBuckets).Error; err != nil {
return nil, nil, err
}
totalStakeAmount := big.NewInt(0)
totalVotingPower := big.NewInt(0)
for _, stakingBucket := range stakingBuckets {
if addr != stakingBucket.OwnerAddress {
continue
}
stakeAmount, _ := big.NewInt(0).SetString(stakingBucket.StakedAmount, 0)
totalStakeAmount.Add(totalStakeAmount, stakeAmount)
votingPower, _ := big.NewInt(0).SetString(stakingBucket.VotingPower, 0)
totalVotingPower.Add(totalVotingPower, votingPower)
}
return totalStakeAmount, totalVotingPower, nil
}
20 changes: 20 additions & 0 deletions model/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,23 @@ type StakingBucket struct {
AutoStake bool
Duration uint32
}

type SystemStakingBucket struct {
ID uint64
BucketID uint64
BlockHeight uint64
CreateTime int64
StakeStartTime int64
UnstakeStartTime int64
StakedAmount string
VotingPower string
OwnerAddress string
Candidate string
Amount string
EventType string
Sender string
ActHash string
Timestamp int64
AutoStake bool
Duration uint32
}

0 comments on commit f09e6e1

Please sign in to comment.