ledger-core
TezosLikeAccount.h
1 /*
2  *
3  * TezosLikeAccount
4  *
5  * Created by El Khalil Bellakrid on 27/04/2019.
6  *
7  * The MIT License (MIT)
8  *
9  * Copyright (c) 2019 Ledger
10  *
11  * Permission is hereby granted, free of charge, to any person obtaining a copy
12  * of this software and associated documentation files (the "Software"), to deal
13  * in the Software without restriction, including without limitation the rights
14  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
15  * copies of the Software, and to permit persons to whom the Software is
16  * furnished to do so, subject to the following conditions:
17  *
18  * The above copyright notice and this permission notice shall be included in all
19  * copies or substantial portions of the Software.
20  *
21  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
22  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
24  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
25  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
26  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
27  * SOFTWARE.
28  *
29  */
30 
31 
32 #ifndef LEDGER_CORE_TEZOSLIKEACCOUNT_H
33 #define LEDGER_CORE_TEZOSLIKEACCOUNT_H
34 #include <time.h>
35 #include <api/AddressListCallback.hpp>
36 #include <api/Address.hpp>
37 #include <api/TezosLikeAccount.hpp>
38 #include <api/TezosLikeTransactionBuilder.hpp>
39 #include <api/StringCallback.hpp>
40 #include <api/Event.hpp>
41 #include <api/BigIntCallback.hpp>
42 #include <wallet/common/AbstractWallet.hpp>
43 #include <wallet/common/AbstractAccount.hpp>
44 #include <wallet/common/Amount.h>
45 #include <wallet/tezos/explorers/TezosLikeBlockchainExplorer.h>
46 #include <wallet/tezos/synchronizers/TezosLikeAccountSynchronizer.h>
47 #include <wallet/tezos/observers/TezosLikeBlockchainObserver.h>
48 #include <wallet/tezos/keychains/TezosLikeKeychain.h>
49 #include <wallet/tezos/database/TezosLikeAccountDatabaseEntry.h>
50 
51 namespace ledger {
52  namespace core {
53 
55  public:
56  TezosOperationQuery(const std::shared_ptr<api::QueryFilter>& headFilter,
57  const std::shared_ptr<DatabaseSessionPool>& pool,
58  const std::shared_ptr<api::ExecutionContext>& context,
59  const std::shared_ptr<api::ExecutionContext>& mainContext) : OperationQuery(headFilter, pool, context, mainContext) {
60 
61  };
62  protected:
63  virtual soci::rowset<soci::row> performExecute(soci::session &sql) {
64  return _builder.select("o.account_uid, o.uid, o.wallet_uid, o.type, o.date, o.senders, o.recipients,"
65  "o.amount, o.fees, o.currency_name, o.trust, b.hash, b.height, b.time, orig_op.uid"
66  )
67  .from("operations").to("o")
68  .outerJoin("blocks AS b", "o.block_uid = b.uid")
69  .outerJoin("tezos_originated_operations AS orig_op", "o.uid = orig_op.uid")
70  .execute(sql);
71 
72  };
73  };
74 
77  public:
78  static const int FLAG_TRANSACTION_IGNORED = 0x00;
79 
80  TezosLikeAccount(const std::shared_ptr<AbstractWallet> &wallet,
81  int32_t index,
82  const std::shared_ptr<TezosLikeBlockchainExplorer> &explorer,
83  const std::shared_ptr<TezosLikeBlockchainObserver> &observer,
84  const std::shared_ptr<TezosLikeAccountSynchronizer> &synchronizer,
85  const std::shared_ptr<TezosLikeKeychain> &keychain);
86 
87  void inflateOperation(Operation &out,
88  const std::shared_ptr<const AbstractWallet> &wallet,
90 
91  int putTransaction(soci::session &sql,
92  const TezosLikeBlockchainExplorerTransaction &transaction,
93  const std::string &originatedAccountUid = "",
94  const std::string &originatedAccountAddress = "");
95 
96  void updateOriginatedAccounts(soci::session &sql, const Operation &operation);
97 
98  bool putBlock(soci::session &sql, const TezosLikeBlockchainExplorer::Block &block);
99 
100  std::shared_ptr<TezosLikeKeychain> getKeychain() const;
101 
102  FuturePtr<Amount> getBalance() override;
103 
104  Future<AbstractAccount::AddressList> getFreshPublicAddresses() override;
105 
107  getBalanceHistory(const std::string &start,
108  const std::string &end,
109  api::TimePeriod precision) override;
110 
111  Future<api::ErrorCode> eraseDataSince(const std::chrono::system_clock::time_point &date) override;
112 
113  bool isSynchronizing() override;
114 
115  std::shared_ptr<api::EventBus> synchronize() override;
116 
117  void startBlockchainObservation() override;
118 
119  void stopBlockchainObservation() override;
120 
121  bool isObservingBlockchain() override;
122 
123  std::string getRestoreKey() override;
124 
125  void broadcastRawTransaction(const std::vector<uint8_t> &transaction,
126  const std::shared_ptr<api::StringCallback> &callback) override;
127 
128  void broadcastTransaction(const std::shared_ptr<api::TezosLikeTransaction> &transaction,
129  const std::shared_ptr<api::StringCallback> &callback) override;
130 
131  std::shared_ptr<api::TezosLikeTransactionBuilder> buildTransaction() override;
132  std::shared_ptr<api::TezosLikeTransactionBuilder> buildTransaction(const std::string &senderAddress);
133 
134  std::shared_ptr<api::OperationQuery> queryOperations() override;
135 
136  void getEstimatedGasLimit(const std::string & address, const std::shared_ptr<api::BigIntCallback> & callback) override;
137  FuturePtr<BigInt> getEstimatedGasLimit(const std::string &address);
138 
139  void getStorage(const std::string & address, const std::shared_ptr<api::BigIntCallback> & callback) override;
140  FuturePtr<BigInt> getStorage(const std::string &address);
141 
142  std::vector<std::shared_ptr<api::TezosLikeOriginatedAccount>> getOriginatedAccounts() override;
143 
144  void addOriginatedAccounts(soci::session &sql, const std::vector<TezosLikeOriginatedAccountDatabaseEntry> &originatedEntries);
145 
146  void getFees(const std::shared_ptr<api::BigIntCallback> & callback) override;
147  FuturePtr<BigInt> getFees();
148 
149  private:
150  std::shared_ptr<TezosLikeAccount> getSelf();
151 
152  std::shared_ptr<TezosLikeKeychain> _keychain;
153  std::string _accountAddress;
154  std::shared_ptr<TezosLikeBlockchainExplorer> _explorer;
155  std::shared_ptr<TezosLikeAccountSynchronizer> _synchronizer;
156  std::shared_ptr<TezosLikeBlockchainObserver> _observer;
157  std::shared_ptr<api::EventBus> _currentSyncEventBus;
158  std::mutex _synchronizationLock;
159  std::vector<std::shared_ptr<api::TezosLikeOriginatedAccount>> _originatedAccounts;
160  };
161  }
162 }
163 #endif //LEDGER_CORE_TEZOSLIKEACCOUNT_H
Definition: OperationQuery.h:48
Definition: TezosLikeBlockchainExplorer.h:68
Definition: AbstractAccount.hpp:55
Definition: Deffered.hpp:49
Definition: TezosLikeAccount.hpp:28
Definition: TezosLikeAccount.h:54
Definition: Operation.h:53
Definition: TezosLikeOriginatedAccount.h:67
Definition: Account.cpp:8
Definition: TezosLikeAccount.h:76
Definition: Block.h:40