refactor: optimize AccountBalance

pull/3649/head
xiaohuo 4 years ago committed by Leo Chen
parent e5728d1e14
commit 8cf881fb30
  1. 19
      rosetta/services/account.go

@ -59,9 +59,9 @@ func (s *AccountAPI) AccountBalance(
if request.AccountIdentifier.SubAccount != nil {
subAccount := request.AccountIdentifier.SubAccount
ty, exist := subAccount.Metadata["type"]
// delegated balance
if exist && ty.(string) == Delegation {
if exist {
switch ty.(string) {
case Delegation:
validatorAddr := subAccount.Address
validators, delegations := s.hmy.GetDelegationsByDelegatorByBlock(addr, block)
for index, validator := range validators {
@ -69,8 +69,7 @@ func (s *AccountAPI) AccountBalance(
balance = new(big.Int).Add(balance, delegations[index].Amount)
}
}
// pending undelegated balance
} else if exist && ty.(string) == UnDelegation {
case UnDelegation:
validatorAddr := subAccount.Address
validators, delegations := s.hmy.GetDelegationsByDelegatorByBlock(addr, block)
for index, validator := range validators {
@ -81,11 +80,17 @@ func (s *AccountAPI) AccountBalance(
}
}
}
} else {
default:
return nil, common.NewError(common.SanityCheckError, map[string]interface{}{
"message": "invalid sub account or type",
"message": "invalid sub account type",
})
}
}
return nil, common.NewError(common.SanityCheckError, map[string]interface{}{
"message": "invalid sub account",
})
} else {
balance, err = s.hmy.GetBalance(ctx, addr, blockNum)
if err != nil {

Loading…
Cancel
Save