ledger-core
ERC20LikeAccount.h
1 /*
2  *
3  * ERC20LikeAccount
4  *
5  * Created by El Khalil Bellakrid on 26/08/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_ERC20LIKEACCOUNT_H
33 #define LEDGER_CORE_ERC20LIKEACCOUNT_H
34 
35 #include <chrono>
36 #include <api/ERC20LikeAccount.hpp>
37 #include <api/ERC20Token.hpp>
38 #include <wallet/ethereum/ERC20/ERC20LikeOperation.h>
39 #include <api/BigInt.hpp>
40 #include <api/Currency.hpp>
41 #include <api/OperationQuery.hpp>
42 #include <wallet/ethereum/EthereumLikeWallet.h>
43 #include <wallet/ethereum/EthereumLikeAccount.h>
44 #include <database/soci-number.h>
45 #include <api/BinaryCallback.hpp>
46 
47 namespace ledger {
48  namespace core {
50  public:
51  ERC20OperationQuery(const std::shared_ptr<api::QueryFilter>& headFilter,
52  const std::shared_ptr<DatabaseSessionPool>& pool,
53  const std::shared_ptr<api::ExecutionContext>& context,
54  const std::shared_ptr<api::ExecutionContext>& mainContext) : OperationQuery(headFilter, pool, context, mainContext) {
55 
56  };
57  protected:
58  virtual soci::rowset<soci::row> performExecute(soci::session &sql) {
59  return _builder.select("o.account_uid, o.uid, o.wallet_uid, o.type, o.date, o.senders, o.recipients,"
60  "o.amount, o.fees, o.currency_name, o.trust, b.hash, b.height, b.time, e.uid"
61  )
62  .from("operations").to("o")
63  .outerJoin("blocks AS b", "o.block_uid = b.uid")
64  .outerJoin("erc20_operations AS e", "o.uid = e.ethereum_operation_uid")
65  .execute(sql);
66 
67  };
68  };
70  public:
71  ERC20LikeAccount(const std::string &accountUid,
72  const api::ERC20Token &erc20Token,
73  const std::string &accountAddress,
74  const api::Currency &parentCurrency,
75  const std::shared_ptr<EthereumLikeAccount> &parentAccount);
76  api::ERC20Token getToken() override ;
77  std::string getAddress() override ;
78  FuturePtr<api::BigInt> getBalance();
79  void getBalance(const std::shared_ptr<api::BigIntCallback> & callback) override ;
80 
81  std::vector<std::shared_ptr<api::BigInt>> getBalanceHistoryFor(
82  const std::chrono::system_clock::time_point& startDate,
83  const std::chrono::system_clock::time_point& endDate,
84  api::TimePeriod period
85  ) override;
86 
87  // A helper function to take an operation into an account while computing balances.
88  static BigInt accumulateBalanceWithOperation(
89  const BigInt& balance,
91  );
92 
93  std::vector<std::shared_ptr<api::ERC20LikeOperation>> getOperations() override ;
94 
95  Future<std::vector<uint8_t>> getTransferToAddressData(const std::shared_ptr<api::BigInt> &amount,
96  const std::string &address);
97 
98  void getTransferToAddressData(const std::shared_ptr<api::BigInt> &amount,
99  const std::string &address,
100  const std::shared_ptr<api::BinaryCallback> &data) override;
101 
102  std::shared_ptr<api::OperationQuery> queryOperations() override ;
103  void putOperation(soci::session &sql, const std::shared_ptr<ERC20LikeOperation> &operation, bool newOperation = false);
104  private:
105  std::shared_ptr<api::ExecutionContext> getContext();
106 
107  api::ERC20Token _token;
108  std::string _accountAddress;
109  api::Currency _parentCurrency;
110  std::string _accountUid;
111  std::weak_ptr<EthereumLikeAccount> _account;
112  };
113  }
114 }
115 
116 
117 #endif //LEDGER_CORE_ERC20LIKEACCOUNT_H
Definition: ERC20LikeOperation.hpp:27
Definition: OperationQuery.h:48
Definition: Deffered.hpp:49
Definition: ERC20LikeAccount.h:69
Definition: ERC20LikeAccount.hpp:30
Definition: Currency.hpp:23
Definition: Account.cpp:8
Definition: ERC20Token.hpp:15
Definition: BigInt.h:56
Definition: ERC20LikeAccount.h:49