CatapultServer  v0.5.0.1 (Elephant)
LockStatusAccountBalanceObserver.h
Go to the documentation of this file.
1 
21 #pragma once
23 
24 namespace catapult { namespace observers {
25 
30  template<typename TTraits>
31  void LockStatusAccountBalanceObserver(const typename TTraits::Notification& notification, ObserverContext& context) {
32  auto& accountStateCache = context.Cache.sub<cache::AccountStateCache>();
33  auto& cache = context.Cache.template sub<typename TTraits::CacheType>();
34  const auto& key = TTraits::NotificationToKey(notification, context.Resolvers);
35  auto lockInfoIter = cache.find(key);
36  auto& lockInfo = lockInfoIter.get();
37 
38  auto accountStateIter = accountStateCache.find(TTraits::DestinationAccount(lockInfo));
39  auto& accountState = accountStateIter.get();
40 
41  if (NotifyMode::Rollback == context.Mode) {
42  lockInfo.Status = state::LockStatus::Unused;
43  accountState.Balances.debit(lockInfo.MosaicId, lockInfo.Amount);
44  return;
45  }
46 
47  lockInfo.Status = state::LockStatus::Used;
48  accountState.Balances.credit(lockInfo.MosaicId, lockInfo.Amount);
49 
50  model::BalanceChangeReceipt receipt(TTraits::Receipt_Type, accountState.PublicKey, lockInfo.MosaicId, lockInfo.Amount);
51  context.StatementBuilder().addReceipt(receipt);
52  }
53 }}
catapult::observers::ObserverContext::StatementBuilder
ObserverStatementBuilder & StatementBuilder()
Statement builder.
Definition: ObserverContext.cpp:82
catapult::state::LockStatus::Unused
Lock is unused.
catapult::observers::ObserverContext::Cache
cache::CatapultCacheDelta & Cache
Catapult cache.
Definition: ObserverContext.h:86
catapult::observers::ObserverContext
Context passed to all the observers.
Definition: ObserverContext.h:78
catapult::cache::CatapultCacheDelta::sub
const TCache::CacheDeltaType & sub() const
Gets a specific sub cache delta view.
Definition: CatapultCacheDelta.h:47
catapult::validators::Notification
model::AccountAddressNotification Notification
Definition: AddressValidator.cpp:27
catapult::model::BalanceChangeReceipt
Binary layout for a balance change receipt.
Definition: Receipt.h:73
Receipt_Type
static constexpr auto Receipt_Type
Definition: CompletedAggregateObserver.cpp:36
catapult::observers::LockStatusAccountBalanceObserver
void LockStatusAccountBalanceObserver(const typename TTraits::Notification &notification, ObserverContext &context)
Definition: LockStatusAccountBalanceObserver.h:31
catapult::observers::ObserverContext::Resolvers
const model::ResolverContext Resolvers
Alias resolvers.
Definition: ObserverContext.h:98
catapult::state::LockStatus::Used
Lock was already used.
AccountStateCache.h
catapult::cache::AccountStateCache
Synchronized cache composed of stateful account information.
Definition: AccountStateCache.h:72
catapult::observers::ObserverContext::Mode
const NotifyMode Mode
Notification mode.
Definition: ObserverContext.h:95
catapult
Definition: AddressExtractionExtension.cpp:28
catapult::observers::ObserverStatementBuilder::addReceipt
void addReceipt(const model::Receipt &receipt)
Adds receipt to this builder.
Definition: ObserverStatementBuilder.cpp:43