From eaf51b814b77fe5d0614f2b013d5d16a7571dfa7 Mon Sep 17 00:00:00 2001 From: Janet Liang <56005637+janet-harmony@users.noreply.github.com> Date: Mon, 14 Sep 2020 13:29:33 -0700 Subject: [PATCH] [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 --- rpc/staking.go | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/rpc/staking.go b/rpc/staking.go index 143e84260..2758c72da 100644 --- a/rpc/staking.go +++ b/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 }