Skip to content

Commit

Permalink
feat(staking_service): add system bucket v2
Browse files Browse the repository at this point in the history
  • Loading branch information
hunshenshi committed Jul 26, 2024
1 parent b882d91 commit 0459402
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 0 deletions.
6 changes: 6 additions & 0 deletions apiservice/staking_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}
Expand Down
41 changes: 41 additions & 0 deletions common/actions/bucket.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

0 comments on commit 0459402

Please sign in to comment.