core: deduplicate and simplify processNEP17Transfer a bit

Just refactoring, no functional changes.
This commit is contained in:
Roman Khimov 2021-08-11 22:36:26 +03:00
parent 50ee1a1f91
commit 3e60771175

View file

@ -1066,44 +1066,32 @@ func (bc *Blockchain) processNEP17Transfer(cache *dao.Cached, h util.Uint256, b
Tx: h, Tx: h,
} }
if !fromAddr.Equals(util.Uint160{}) { if !fromAddr.Equals(util.Uint160{}) {
balances, err := cache.GetNEP17TransferInfo(fromAddr) _ = transfer.Amount.Neg(amount) // We already have the Int.
if err != nil { if appendNEP17Transfer(cache, fromAddr, transfer) != nil {
return
}
balances.LastUpdated[id] = b.Index
transfer.Amount = *new(big.Int).Sub(&transfer.Amount, amount)
balances.NewBatch, err = cache.AppendNEP17Transfer(fromAddr,
balances.NextTransferBatch, balances.NewBatch, transfer)
if err != nil {
return
}
if balances.NewBatch {
balances.NextTransferBatch++
}
if err := cache.PutNEP17TransferInfo(fromAddr, balances); err != nil {
return return
} }
} }
if !toAddr.Equals(util.Uint160{}) { if !toAddr.Equals(util.Uint160{}) {
balances, err := cache.GetNEP17TransferInfo(toAddr) _ = transfer.Amount.Set(amount) // We already have the Int.
if err != nil { _ = appendNEP17Transfer(cache, toAddr, transfer) // Nothing useful we can do.
return }
} }
balances.LastUpdated[id] = b.Index
transfer.Amount = *amount func appendNEP17Transfer(cache *dao.Cached, addr util.Uint160, transfer *state.NEP17Transfer) error {
balances.NewBatch, err = cache.AppendNEP17Transfer(toAddr, balances, err := cache.GetNEP17TransferInfo(addr)
if err != nil {
return err
}
balances.LastUpdated[transfer.Asset] = transfer.Block
balances.NewBatch, err = cache.AppendNEP17Transfer(addr,
balances.NextTransferBatch, balances.NewBatch, transfer) balances.NextTransferBatch, balances.NewBatch, transfer)
if err != nil { if err != nil {
return return err
} }
if balances.NewBatch { if balances.NewBatch {
balances.NextTransferBatch++ balances.NextTransferBatch++
} }
if err := cache.PutNEP17TransferInfo(toAddr, balances); err != nil { return cache.PutNEP17TransferInfo(addr, balances)
return
}
}
} }
// ForEachNEP17Transfer executes f for each nep17 transfer in log. // ForEachNEP17Transfer executes f for each nep17 transfer in log.