refactor: optimize AccountBalance

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

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

Loading…
Cancel
Save