-
Notifications
You must be signed in to change notification settings - Fork 79
Jira/coin 1151: Tezos: rework getting balance for originated accounts #679
base: develop
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -40,6 +40,7 @@ | |
| #include <api/TezosLikeOperation.hpp> | ||
| #include <api/TezosLikeTransaction.hpp> | ||
| #include <wallet/pool/WalletPool.hpp> | ||
| #include <wallet/tezos/TezosLikeWallet.h> | ||
|
|
||
| namespace ledger { | ||
| namespace core { | ||
|
|
@@ -62,9 +63,11 @@ namespace ledger { | |
| } | ||
|
|
||
| static inline void update_balance(std::shared_ptr<api::Operation> const& op, BigInt& sum) { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe rename this function
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This function is called through a template call (see getBalanceHistoryFor in core/src/wallet/common/BalanceHistory.h) |
||
| auto value = BigInt(op->getAmount()->toString()); | ||
| auto tzOp = op->asTezosLikeOperation(); | ||
| auto tzTx = tzOp->getTransaction(); | ||
| auto value = (tzTx->getType() == api::TezosOperationTag::OPERATION_TAG_TRANSACTION) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe we could just not call
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. update_balance is called by a generic code (shared between several coins), we cannot use tezos specific functions (as getType)
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I see. Then maybe quit the function asap?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. sum can be impacted by fees (even if value is equal to 0). We cannot quit the function |
||
| ? BigInt(op->getAmount()->toString()) | ||
| : BigInt::ZERO; | ||
|
|
||
| switch (op->getOperationType()) { | ||
| case api::OperationType::RECEIVE: { | ||
|
|
@@ -142,19 +145,24 @@ namespace ledger { | |
| } | ||
|
|
||
| FuturePtr<api::Amount> TezosLikeOriginatedAccount::getBalance(const std::shared_ptr<api::ExecutionContext>& context) { | ||
| return std::dynamic_pointer_cast<OperationQuery>(queryOperations()->complete())->execute() | ||
| .mapPtr<api::Amount>(context, [] (const std::vector<std::shared_ptr<api::Operation>> &ops) { | ||
| auto result = BigInt::ZERO; | ||
|
|
||
| for (auto const &op : ops) { | ||
| OperationStrategy::update_balance(op, result); | ||
| } | ||
|
|
||
| return std::make_shared<Amount>(currencies::TEZOS, 0, result); | ||
| auto localAccount = _originatorAccount.lock(); | ||
| if (!localAccount) { | ||
| throw make_exception(api::ErrorCode::NULL_POINTER, "Account was released."); | ||
| } | ||
| auto wallet = std::dynamic_pointer_cast<TezosLikeWallet>(localAccount->getWallet()); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. we should check for null pointer here to avoid segfaults later
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The only case that I can see to get a sigfault here is to have a TezosLikeAccount inside a wallet different from a TezosLikeWallet, which is not possible. |
||
| auto cachedBalance = wallet->getBalanceFromCache(localAccount->getIndex(), _address); | ||
| if (cachedBalance.hasValue()) { | ||
| return FuturePtr<api::Amount>::successful(std::make_shared<Amount>(cachedBalance.getValue())); | ||
| } | ||
| const auto address = _address; | ||
| return wallet->getBlockchainExplorer()->getBalance(_address).mapPtr<api::Amount>(context, [wallet, localAccount, address]( | ||
| const std::shared_ptr<BigInt> &balance) -> std::shared_ptr<Amount> { | ||
| Amount b(wallet->getCurrency(), 0, BigInt(balance->toString())); | ||
| wallet->updateBalanceCache(localAccount->getIndex(), address, b); | ||
| return std::make_shared<Amount>(b); | ||
| }); | ||
| } | ||
|
|
||
|
|
||
| void TezosLikeOriginatedAccount::getBalanceHistory(const std::chrono::system_clock::time_point & start, | ||
| const std::chrono::system_clock::time_point & end, | ||
| api::TimePeriod period, | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -115,7 +115,7 @@ namespace ledger { | |
| const std::vector<std::string> &matchableKeys); | ||
|
|
||
| virtual Future<std::shared_ptr<BigInt>> | ||
| getBalance(const std::vector<TezosLikeKeychain::Address> &addresses) = 0; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe we could keep
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 1- As you can see for all other functions in this class, address was always a string. |
||
| getBalance(const std::string &address) = 0; | ||
|
|
||
| virtual Future<std::shared_ptr<BigInt>> | ||
| getFees() = 0; | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this method useful? I don't see any references to it in the code
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes it is not called. I added it as a helper function for, may be, a future use.