ledger-core
AbstractAccount.hpp
1 /*
2  *
3  * AbstractAccount
4  * ledger-core
5  *
6  * Created by Pierre Pollastri on 28/04/2017.
7  *
8  * The MIT License (MIT)
9  *
10  * Copyright (c) 2016 Ledger
11  *
12  * Permission is hereby granted, free of charge, to any person obtaining a copy
13  * of this software and associated documentation files (the "Software"), to deal
14  * in the Software without restriction, including without limitation the rights
15  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
16  * copies of the Software, and to permit persons to whom the Software is
17  * furnished to do so, subject to the following conditions:
18  *
19  * The above copyright notice and this permission notice shall be included in all
20  * copies or substantial portions of the Software.
21  *
22  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
25  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
26  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
27  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
28  * SOFTWARE.
29  *
30  */
31 #ifndef LEDGER_CORE_ABSTRACTACCOUNT_HPP
32 #define LEDGER_CORE_ABSTRACTACCOUNT_HPP
33 
34 #include <api/Account.hpp>
35 #include "AbstractWallet.hpp"
36 #include "OperationQuery.h"
37 #include <async/Future.hpp>
38 #include <wallet/common/Amount.h>
39 #include <events/EventPublisher.hpp>
40 #include <api/Block.hpp>
41 #include <api/BlockCallback.hpp>
42 #include <api/BitcoinLikeAccount.hpp>
43 #include <api/EthereumLikeAccount.hpp>
44 #include <api/RippleLikeAccount.hpp>
45 #include <api/TezosLikeAccount.hpp>
46 #include <api/AddressListCallback.hpp>
47 #include <api/Address.hpp>
48 #include <api/AmountListCallback.hpp>
49 #include <api/ErrorCodeCallback.hpp>
50 #include <api/TimePeriod.hpp>
51 #include <mutex>
52 
53 namespace ledger {
54  namespace core {
55  class AbstractAccount : public DedicatedContext, public api::Account, public std::enable_shared_from_this<AbstractAccount> {
56  public:
57  using AddressList = std::vector<std::shared_ptr<api::Address>>;
58 
59  AbstractAccount(const std::shared_ptr<AbstractWallet>& wallet, int32_t index);
60  int32_t getIndex() override;
61  std::shared_ptr<api::Preferences> getPreferences() override;
62  std::shared_ptr<api::Logger> getLogger() override;
63  bool isInstanceOfBitcoinLikeAccount() override;
64  bool isInstanceOfEthereumLikeAccount() override;
65  bool isInstanceOfRippleLikeAccount() override;
66  api::WalletType getWalletType() override;
67  std::shared_ptr<api::Preferences> getOperationPreferences(const std::string &uid) override;
68  std::shared_ptr<api::BitcoinLikeAccount> asBitcoinLikeAccount() override;
69  std::shared_ptr<api::EthereumLikeAccount> asEthereumLikeAccount() override;
70  std::shared_ptr<api::RippleLikeAccount> asRippleLikeAccount() override;
71  std::shared_ptr<api::TezosLikeAccount> asTezosLikeAccount() override;
72  virtual std::shared_ptr<Preferences> getOperationExternalPreferences(const std::string &uid);
73  virtual std::shared_ptr<Preferences> getOperationInternalPreferences(const std::string &uid);
74  virtual std::shared_ptr<Preferences> getInternalPreferences() const;
75  virtual std::shared_ptr<Preferences> getExternalPreferences() const;
76  virtual std::shared_ptr<spdlog::logger> logger() const;
77  virtual const std::string& getAccountUid() const;
78  virtual std::shared_ptr<AbstractWallet> getWallet() const;
79  virtual std::shared_ptr<AbstractWallet> getWallet();
80  const std::shared_ptr<api::ExecutionContext> getMainExecutionContext() const;
81 
82  void getLastBlock(const std::shared_ptr<api::BlockCallback> &callback) override;
83  Future<api::Block> getLastBlock();
84 
85  void getBalance(const std::shared_ptr<api::AmountCallback> &callback) override;
86  virtual FuturePtr<Amount> getBalance() = 0;
87 
88  void getFreshPublicAddresses(const std::shared_ptr<api::AddressListCallback> &callback) override;
89  virtual Future<AddressList> getFreshPublicAddresses() = 0;
90  void getBalanceHistory(const std::string & start,
91  const std::string & end,
92  api::TimePeriod precision,
93  const std::shared_ptr<api::AmountListCallback> & callback) override;
94  virtual Future<std::vector<std::shared_ptr<api::Amount>>> getBalanceHistory(const std::string & start,
95  const std::string & end,
96  api::TimePeriod precision) = 0;
97 
98  std::shared_ptr<api::OperationQuery> queryOperations() override;
99 
100  std::shared_ptr<api::EventBus> getEventBus() override;
101 
102  void emitEventsNow();
103 
104  void eraseDataSince(const std::chrono::system_clock::time_point & date, const std::shared_ptr<api::ErrorCodeCallback> & callback) override ;
105  virtual Future<api::ErrorCode> eraseDataSince(const std::chrono::system_clock::time_point & date) = 0;
106 
107  protected:
108  void emitNewOperationEvent(const Operation& operation);
109  void emitNewBlockEvent(const Block& block);
110  void pushEvent(const std::shared_ptr<api::Event>& event);
111 
112  private:
113  api::WalletType _type;
114  int32_t _index;
115  std::string _uid;
116  std::shared_ptr<spdlog::logger> _logger;
117  std::shared_ptr<api::Logger> _loggerApi;
118  std::shared_ptr<Preferences> _internalPreferences;
119  std::shared_ptr<Preferences> _externalPreferences;
120  std::shared_ptr<api::ExecutionContext> _mainExecutionContext;
121  std::shared_ptr<api::ExecutionContext> _threadPoolExecutionContext;
122  std::weak_ptr<AbstractWallet> _wallet;
123  std::shared_ptr<EventPublisher> _publisher;
124  std::mutex _eventsLock;
125  std::list<std::shared_ptr<api::Event>> _events;
126  };
127  }
128 }
129 
130 
131 #endif //LEDGER_CORE_ABSTRACTACCOUNT_HPP
std::shared_ptr< api::BitcoinLikeAccount > asBitcoinLikeAccount() override
Definition: AbstractAccount.cpp:92
api::WalletType getWalletType() override
Definition: AbstractAccount.cpp:84
Definition: AbstractAccount.hpp:55
Definition: Deffered.hpp:49
Definition: Account.hpp:38
std::shared_ptr< api::RippleLikeAccount > asRippleLikeAccount() override
Definition: AbstractAccount.cpp:100
std::shared_ptr< api::EventBus > getEventBus() override
Definition: AbstractAccount.cpp:176
bool isInstanceOfBitcoinLikeAccount() override
Definition: AbstractAccount.cpp:72
Definition: Operation.h:53
bool isInstanceOfRippleLikeAccount() override
Definition: AbstractAccount.cpp:80
std::shared_ptr< api::OperationQuery > queryOperations() override
Definition: AbstractAccount.cpp:144
int32_t getIndex() override
Definition: AbstractAccount.cpp:60
Definition: Account.cpp:8
std::shared_ptr< api::TezosLikeAccount > asTezosLikeAccount() override
Definition: AbstractAccount.cpp:104
WalletType
Definition: WalletType.hpp:20
Definition: DedicatedContext.hpp:39
std::shared_ptr< api::Preferences > getPreferences() override
Definition: AbstractAccount.cpp:64
bool isInstanceOfEthereumLikeAccount() override
Definition: AbstractAccount.cpp:76
std::shared_ptr< api::Logger > getLogger() override
Definition: AbstractAccount.cpp:68
Definition: Block.h:40
std::shared_ptr< api::Preferences > getOperationPreferences(const std::string &uid) override
Definition: AbstractAccount.cpp:88
std::shared_ptr< api::EthereumLikeAccount > asEthereumLikeAccount() override
Definition: AbstractAccount.cpp:96
Definition: logger.hpp:44