[rpc] Add RPC to return pool of locked undelegated tokens (#3343)

* [rpc] Add RPC to return pool of locked undelegated tokens

* [rpc] Fix calculation for available redelegation balance & rename function
pull/3350/head
Janet Liang 4 years ago committed by GitHub
parent e74ab0bc0a
commit eaf51b814b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 24
      rpc/staking.go

@ -615,6 +615,30 @@ func (s *PublicStakingService) GetDelegationByDelegatorAndValidator(
return nil, nil
}
// GetAvailableRedelegationBalance returns the amount of locked undelegated tokens
func (s *PublicStakingService) GetAvailableRedelegationBalance(
ctx context.Context, address string,
) (*big.Int, error) {
if !isBeaconShard(s.hmy) {
return nil, ErrNotBeaconShard
}
currEpoch := s.hmy.BlockChain.CurrentHeader().Epoch()
delegatorAddr := internal_common.ParseAddr(address)
_, delegations := s.hmy.GetDelegationsByDelegator(delegatorAddr)
redelegationTotal := big.NewInt(0)
for _, d := range delegations {
for _, u := range d.Undelegations {
if u.Epoch.Cmp(currEpoch) < 1 { // Undelegation.Epoch < currentEpoch
redelegationTotal.Add(redelegationTotal, u.Amount)
}
}
}
return redelegationTotal, nil
}
func isBeaconShard(hmy *hmy.Harmony) bool {
return hmy.ShardID == shard.BeaconChainShardID
}

Loading…
Cancel
Save