From 0459402aeee92f1a4f357b6fc3f49a8677444ff7 Mon Sep 17 00:00:00 2001 From: hunshenshi <289517357@qq.com> Date: Fri, 26 Jul 2024 15:25:37 +0800 Subject: [PATCH] feat(staking_service): add system bucket v2 --- apiservice/staking_service.go | 6 +++++ common/actions/bucket.go | 41 +++++++++++++++++++++++++++++++++++ 2 files changed, 47 insertions(+) diff --git a/apiservice/staking_service.go b/apiservice/staking_service.go index 0dcc173..af94ef3 100644 --- a/apiservice/staking_service.go +++ b/apiservice/staking_service.go @@ -44,8 +44,14 @@ func (s *StakingService) VoteByHeight(ctx context.Context, req *api.VoteByHeight if err != nil { return nil, err } + systemV2StakeAmounts, systemV2VoteWeights, err := actions.GetSystemV2StakedBucketByVoterAndHeight(addr, height) + if err != nil { + return nil, err + } stakeAmounts = stakeAmounts.Add(stakeAmounts, systemStakeAmounts) voteWeights = voteWeights.Add(voteWeights, systemVoteWeights) + stakeAmounts = stakeAmounts.Add(stakeAmounts, systemV2StakeAmounts) + voteWeights = voteWeights.Add(voteWeights, systemV2VoteWeights) 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 e0ee616..5e205ab 100644 --- a/common/actions/bucket.go +++ b/common/actions/bucket.go @@ -138,3 +138,44 @@ func GetSystemStakedBucketByVoterAndHeight(addr string, height uint64) (*big.Int } return totalStakeAmount, totalVotingPower, nil } + +func GetSystemV2BucketIDsByVoterAndHeight(addr string, height uint64) ([]uint64, error) { + db := db.DB() + var ids []struct { + BucketID uint64 + } + if err := db.Table("system_staking_buckets_v2").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 GetSystemV2StakedBucketByVoterAndHeight(addr string, height uint64) (*big.Int, *big.Int, error) { + db := db.DB() + + bucketIDs, err := GetSystemV2BucketIDsByVoterAndHeight(addr, height) + if err != nil { + return nil, nil, err + } + var stakingBuckets []*model.StakingBucket + query := "select t1.* from system_staking_buckets_v2 t1 INNER JOIN (select MAX(id)AS max_id from system_staking_buckets_v2 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 +} \ No newline at end of file