ledger-core
EthereumLikeAccount.h
1 /*
2  *
3  * EthereumLikeAccount
4  *
5  * Created by El Khalil Bellakrid on 12/07/2018.
6  *
7  * The MIT License (MIT)
8  *
9  * Copyright (c) 2018 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_ETHEREUMLIKEACCOUNT_H
33 #define LEDGER_CORE_ETHEREUMLIKEACCOUNT_H
34 
35 #include <time.h>
36 #include <api/AddressListCallback.hpp>
37 #include <api/Address.hpp>
38 #include <api/EthereumLikeAccount.hpp>
39 #include <api/EthereumLikeTransactionBuilder.hpp>
40 #include <api/StringCallback.hpp>
41 #include <api/Event.hpp>
42 #include <api/BigIntCallback.hpp>
43 #include <api/BigIntListCallback.hpp>
44 #include <wallet/common/AbstractWallet.hpp>
45 #include <wallet/common/AbstractAccount.hpp>
46 #include <wallet/common/Amount.h>
47 #include <wallet/ethereum/api_impl/InternalTransaction.h>
48 #include <wallet/ethereum/explorers/EthereumLikeBlockchainExplorer.h>
49 #include <wallet/ethereum/synchronizers/EthereumLikeAccountSynchronizer.h>
50 #include <wallet/ethereum/observers/EthereumLikeBlockchainObserver.h>
51 #include <wallet/ethereum/keychains/EthereumLikeKeychain.hpp>
52 #include <wallet/ethereum/ERC20/ERC20LikeAccount.h>
53 #include <wallet/ethereum/database/EthereumLikeAccountDatabaseEntry.h>
54 
55 namespace ledger {
56  namespace core {
58  public:
59 
60  static const int FLAG_TRANSACTION_IGNORED = 0x00;
61  static const int FLAG_TRANSACTION_CREATED_SENDING_OPERATION = 0x01;
62  static const int FLAG_TRANSACTION_CREATED_RECEPTION_OPERATION = 0x01 << 1;
63  static const int FLAG_TRANSACTION_CREATED_EXTERNAL_OPERATION = 0x01 << 2; // When a tx from other account creates side effects on current account
64 
65  EthereumLikeAccount(const std::shared_ptr<AbstractWallet>& wallet,
66  int32_t index,
67  const std::shared_ptr<EthereumLikeBlockchainExplorer>& explorer,
68  const std::shared_ptr<EthereumLikeBlockchainObserver>& observer,
69  const std::shared_ptr<EthereumLikeAccountSynchronizer>& synchronizer,
70  const std::shared_ptr<EthereumLikeKeychain>& keychain);
71 
72  FuturePtr<EthereumLikeBlockchainExplorerTransaction> getTransaction(const std::string& hash);
73  void inflateOperation(Operation &out,
74  const std::shared_ptr<const AbstractWallet>& wallet,
76 
77  int putTransaction(soci::session& sql, const EthereumLikeBlockchainExplorerTransaction &transaction);
79  std::vector<Operation> getInternalOperations(soci::session &sql);
80 
81  void updateERC20Accounts(soci::session &sql, const Operation &operation);
82  void updateERC20Operation(soci::session &sql,
83  const Operation &operation,
84  const ERC20Transaction &erc20Tx);
85  void updateInternalTransactions(soci::session &sql, const Operation &operation);
86  bool putBlock(soci::session& sql, const EthereumLikeBlockchainExplorer::Block& block);
87 
88  std::shared_ptr<EthereumLikeKeychain> getKeychain() const;
89 
90  FuturePtr<Amount> getBalance() override ;
91  Future<AbstractAccount::AddressList> getFreshPublicAddresses() override ;
93  getBalanceHistory(const std::string & start,
94  const std::string & end,
95  api::TimePeriod precision) override ;
96  Future<api::ErrorCode> eraseDataSince(const std::chrono::system_clock::time_point & date) override ;
97 
98  bool isSynchronizing() override;
99  std::shared_ptr<api::EventBus> synchronize() override ;
100  void startBlockchainObservation() override ;
101  void stopBlockchainObservation() override ;
102  bool isObservingBlockchain() override ;
103  std::string getRestoreKey() override ;
104 
105  static EthereumLikeBlockchainExplorerTransaction getETHLikeBlockchainExplorerTxFromRawTx(const std::shared_ptr<EthereumLikeAccount> &account,
106  const std::string &txHash,
107  const std::vector<uint8_t> &rawTx);
108 
109  void broadcastRawTransaction(const std::vector<uint8_t> & transaction, const std::shared_ptr<api::StringCallback> & callback) override;
110 
111  void broadcastTransaction(const std::shared_ptr<api::EthereumLikeTransaction> & transaction,
112  const std::shared_ptr<api::StringCallback> & callback) override;
113 
114  std::shared_ptr<api::EthereumLikeTransactionBuilder> buildTransaction() override;
115  std::shared_ptr<api::OperationQuery> queryOperations() override;
116 
117  std::shared_ptr<api::EthereumLikeAccount> asEthereumLikeAccount() override ;
118 
119  std::vector<std::shared_ptr<api::ERC20LikeAccount>> getERC20Accounts() override ;
120 
121  void getGasPrice(const std::shared_ptr<api::BigIntCallback> & callback) override;
122 
123  void getEstimatedGasLimit(const std::string & address, const std::shared_ptr<api::BigIntCallback> & callback) override ;
124  FuturePtr<api::BigInt> getERC20Balance(const std::string & erc20Address);
125  void getERC20Balance(const std::string & erc20Address, const std::shared_ptr<api::BigIntCallback> & callback) override;
126  Future<std::vector<std::shared_ptr<api::BigInt>>> getERC20Balances(const std::vector<std::string> &erc20Addresses);
127  void getERC20Balances(const std::vector<std::string> &erc20Addresses, const std::shared_ptr<api::BigIntListCallback> & callback) override;
128 
129  void addERC20Accounts(soci::session &sql,
130  const std::vector<ERC20LikeAccountDatabaseEntry> &erc20Entries);
131 
132 
133  private:
134  std::shared_ptr<EthereumLikeAccount> getSelf();
135  std::shared_ptr<EthereumLikeKeychain> _keychain;
136  std::string _accountAddress;
137  std::shared_ptr<Preferences> _internalPreferences;
138  std::shared_ptr<Preferences> _externalPreferences;
139  std::shared_ptr<EthereumLikeBlockchainExplorer> _explorer;
140  std::shared_ptr<EthereumLikeAccountSynchronizer> _synchronizer;
141  std::shared_ptr<EthereumLikeBlockchainObserver> _observer;
142  std::shared_ptr<api::EventBus> _currentSyncEventBus;
143  std::mutex _synchronizationLock;
144  uint64_t _currentBlockHeight;
145  std::vector<ERC20LikeAccountDatabaseEntry> erc20Entries;
146  std::vector<std::shared_ptr<api::ERC20LikeAccount> >_erc20LikeAccounts;
147  };
148  }
149 }
150 
151 #endif //LEDGER_CORE_ETHEREUMLIKEACCOUNT_H
std::shared_ptr< api::EventBus > synchronize() override
Definition: EthereumLikeAccount.cpp:490
Definition: EthereumLikeBlockchainExplorer.h:71
void stopBlockchainObservation() override
Definition: EthereumLikeAccount.cpp:537
void startBlockchainObservation() override
Definition: EthereumLikeAccount.cpp:533
Definition: AbstractAccount.hpp:55
std::vector< std::shared_ptr< api::ERC20LikeAccount > > getERC20Accounts() override
Definition: EthereumLikeAccount.cpp:618
Definition: Deffered.hpp:49
std::shared_ptr< api::EthereumLikeTransactionBuilder > buildTransaction() override
Definition: EthereumLikeAccount.cpp:674
Definition: EthereumLikeAccount.h:57
Definition: Operation.h:53
bool isSynchronizing() override
Definition: EthereumLikeAccount.cpp:485
Definition: EthereumLikeBlockchainExplorer.h:53
bool isObservingBlockchain() override
Definition: EthereumLikeAccount.cpp:541
std::vector< Operation > getInternalOperations(soci::session &sql)
Get internal transactions related to the parent operation.
Definition: EthereumLikeAccount.cpp:261
Definition: EthereumLikeAccount.hpp:29
Definition: Account.cpp:8
std::shared_ptr< api::EthereumLikeAccount > asEthereumLikeAccount() override
Definition: EthereumLikeAccount.cpp:613
std::shared_ptr< api::OperationQuery > queryOperations() override
Definition: EthereumLikeAccount.cpp:351
Definition: Block.h:40
std::string getRestoreKey() override
Definition: EthereumLikeAccount.cpp:545