From f09e6e1188d1a4aa3ec63fa22c1d3d492620234c Mon Sep 17 00:00:00 2001 From: millken Date: Thu, 28 Sep 2023 14:33:24 +0800 Subject: [PATCH] voteByHeight support systemStakingBucket --- apiservice/staking_service.go | 6 ++++ common/actions/bucket.go | 59 ++++++++++++++++++++++++----------- model/models.go | 20 ++++++++++++ 3 files changed, 67 insertions(+), 18 deletions(-) diff --git a/apiservice/staking_service.go b/apiservice/staking_service.go index 4550aef..0dcc173 100644 --- a/apiservice/staking_service.go +++ b/apiservice/staking_service.go @@ -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)) } diff --git a/common/actions/bucket.go b/common/actions/bucket.go index 0d14ebe..dc9fc9d 100644 --- a/common/actions/bucket.go +++ b/common/actions/bucket.go @@ -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 } diff --git a/model/models.go b/model/models.go index fe62db1..b15d16c 100644 --- a/model/models.go +++ b/model/models.go @@ -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 +}