Skip to content

Commit

Permalink
Issues/560 (trustwallet#475)
Browse files Browse the repository at this point in the history
* Remove of the additional operation with transaction parsing.

* We prefetch 5 pages of the latest transactions.

* Add new property to fetch trust wallet token images.

* Add decodable TokenObject.

* Remove of the unused wrapper.

* Add operations for tokens list.

* Add usage of the operations.

* Code cleaning.

* Remove of the additional operation.

* Code cleaning.

* Update with realm interaction.

* Add new operation for the eth balance.

* Code cleaning.

* Update of the transaction update

* Update balance coordinator.

* Add lover case style for contract.

* Add handling of the contracts.

* Update of the image path generation.

* Code cleaning.

* Add method for timer.

* Temp fix for the transactions.
  • Loading branch information
vikmeup authored Mar 14, 2018
1 parent 32837d2 commit ca5615c
Show file tree
Hide file tree
Showing 16 changed files with 309 additions and 116 deletions.
20 changes: 16 additions & 4 deletions Trust.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,10 @@
61FC5ED11FCFBDEB00CCB12A /* EtherNumberFormatterTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 61FC5ED01FCFBDEB00CCB12A /* EtherNumberFormatterTests.swift */; };
664D11A12007D59F0041A0B0 /* EstimateGasRequest.swift in Sources */ = {isa = PBXBuildFile; fileRef = 664D11A02007D59F0041A0B0 /* EstimateGasRequest.swift */; };
7302405C204C65DF00B327DF /* NonFungibleTokenCategory.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7302405B204C65DF00B327DF /* NonFungibleTokenCategory.swift */; };
73065AEE205809CA00D69722 /* TokensOperation.swift in Sources */ = {isa = PBXBuildFile; fileRef = 73065AED205809CA00D69722 /* TokensOperation.swift */; };
73065AF0205809D900D69722 /* TokensBalanceOperation.swift in Sources */ = {isa = PBXBuildFile; fileRef = 73065AEF205809D900D69722 /* TokensBalanceOperation.swift */; };
73065AF220580A1200D69722 /* TokensTickerOperation.swift in Sources */ = {isa = PBXBuildFile; fileRef = 73065AF120580A1200D69722 /* TokensTickerOperation.swift */; };
73065AF42058807B00D69722 /* EthBalanceOperation.swift in Sources */ = {isa = PBXBuildFile; fileRef = 73065AF32058807B00D69722 /* EthBalanceOperation.swift */; };
73200B642040B9D700118A82 /* CryptoAddressValidatorTest.swift in Sources */ = {isa = PBXBuildFile; fileRef = 73200B632040B9D700118A82 /* CryptoAddressValidatorTest.swift */; };
73200B672044B22600118A82 /* TokensDataStore.swift in Sources */ = {isa = PBXBuildFile; fileRef = 73200B662044B22600118A82 /* TokensDataStore.swift */; };
73200B6A2045B8AC00118A82 /* TokensNetwork.swift in Sources */ = {isa = PBXBuildFile; fileRef = 73200B692045B8AC00118A82 /* TokensNetwork.swift */; };
Expand Down Expand Up @@ -343,7 +347,6 @@
771AA964200D5EDB00D25403 /* WordCollectionViewCell.swift in Sources */ = {isa = PBXBuildFile; fileRef = 771AA963200D5EDB00D25403 /* WordCollectionViewCell.swift */; };
771AA966200D5F1900D25403 /* WordCollectionViewCell.xib in Resources */ = {isa = PBXBuildFile; fileRef = 771AA965200D5F1900D25403 /* WordCollectionViewCell.xib */; };
771D53632050AFC700C79485 /* WalletType.swift in Sources */ = {isa = PBXBuildFile; fileRef = 771D53622050AFC700C79485 /* WalletType.swift */; };
771D53652055F39700C79485 /* TokenItem.swift in Sources */ = {isa = PBXBuildFile; fileRef = 771D53642055F39700C79485 /* TokenItem.swift */; };
7721A6BE202A5677004DB16C /* DecryptError.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7721A6BD202A5677004DB16C /* DecryptError.swift */; };
7721A6C8202EF81B004DB16C /* CustomRPC.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7721A6C7202EF81B004DB16C /* CustomRPC.swift */; };
7721A6CC202EFBC5004DB16C /* AddCustomNetworkViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7721A6CB202EFBC5004DB16C /* AddCustomNetworkViewController.swift */; };
Expand Down Expand Up @@ -743,6 +746,10 @@
646C8C822C986358D7388602 /* Pods_Trust.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = Pods_Trust.framework; sourceTree = BUILT_PRODUCTS_DIR; };
664D11A02007D59F0041A0B0 /* EstimateGasRequest.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = EstimateGasRequest.swift; sourceTree = "<group>"; };
7302405B204C65DF00B327DF /* NonFungibleTokenCategory.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NonFungibleTokenCategory.swift; sourceTree = "<group>"; };
73065AED205809CA00D69722 /* TokensOperation.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TokensOperation.swift; sourceTree = "<group>"; };
73065AEF205809D900D69722 /* TokensBalanceOperation.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TokensBalanceOperation.swift; sourceTree = "<group>"; };
73065AF120580A1200D69722 /* TokensTickerOperation.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TokensTickerOperation.swift; sourceTree = "<group>"; };
73065AF32058807B00D69722 /* EthBalanceOperation.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = EthBalanceOperation.swift; sourceTree = "<group>"; };
73200B632040B9D700118A82 /* CryptoAddressValidatorTest.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CryptoAddressValidatorTest.swift; sourceTree = "<group>"; };
73200B662044B22600118A82 /* TokensDataStore.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = TokensDataStore.swift; sourceTree = "<group>"; };
73200B692045B8AC00118A82 /* TokensNetwork.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TokensNetwork.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -803,7 +810,6 @@
771AA963200D5EDB00D25403 /* WordCollectionViewCell.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = WordCollectionViewCell.swift; sourceTree = "<group>"; };
771AA965200D5F1900D25403 /* WordCollectionViewCell.xib */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = file.xib; path = WordCollectionViewCell.xib; sourceTree = "<group>"; };
771D53622050AFC700C79485 /* WalletType.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = WalletType.swift; sourceTree = "<group>"; };
771D53642055F39700C79485 /* TokenItem.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TokenItem.swift; sourceTree = "<group>"; };
7721A6BD202A5677004DB16C /* DecryptError.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DecryptError.swift; sourceTree = "<group>"; };
7721A6BF202B1D3E004DB16C /* zh-Hans */ = {isa = PBXFileReference; lastKnownFileType = text.plist.strings; name = "zh-Hans"; path = "zh-Hans.lproj/Localizable.strings"; sourceTree = "<group>"; };
7721A6C0202B1D43004DB16C /* zh-Hant */ = {isa = PBXFileReference; lastKnownFileType = text.plist.strings; name = "zh-Hant"; path = "zh-Hant.lproj/Localizable.strings"; sourceTree = "<group>"; };
Expand Down Expand Up @@ -1241,7 +1247,6 @@
296105921FA2AA2100292494 /* SignTransaction.swift */,
29C80D361FB2CD230037B1E0 /* PendingTransaction.swift */,
299B5E2C1FCBC0660051361C /* BalanceProtocol.swift */,
771D53642055F39700C79485 /* TokenItem.swift */,
);
path = Models;
sourceTree = "<group>";
Expand Down Expand Up @@ -1534,6 +1539,10 @@
29C0FCE4200EBAF6004A13CB /* TokenType.swift */,
73200B742048B67E00118A82 /* NonFungibleTokenObject.swift */,
7302405B204C65DF00B327DF /* NonFungibleTokenCategory.swift */,
73065AED205809CA00D69722 /* TokensOperation.swift */,
73065AEF205809D900D69722 /* TokensBalanceOperation.swift */,
73065AF120580A1200D69722 /* TokensTickerOperation.swift */,
73065AF32058807B00D69722 /* EthBalanceOperation.swift */,
);
path = Types;
sourceTree = "<group>";
Expand Down Expand Up @@ -2972,6 +2981,7 @@
294EC1DA1FD8E4E60065EB20 /* GasPriceRequest.swift in Sources */,
295A59381F71C1B90092F0FC /* AccountsCoordinator.swift in Sources */,
903682F8204AC52F00013479 /* BookmarksViewModel.swift in Sources */,
73065AF42058807B00D69722 /* EthBalanceOperation.swift in Sources */,
299B5E2D1FCBC0660051361C /* BalanceProtocol.swift in Sources */,
296421971F70C1F200EB363B /* ErrorView.swift in Sources */,
903682F1204A6DB800013479 /* BookmarkViewController.swift in Sources */,
Expand All @@ -2980,15 +2990,16 @@
77872D232023F43B0032D687 /* TransactionsTracker.swift in Sources */,
29FA00CE201CA64E002F7DC5 /* DappCommand.swift in Sources */,
29FF12F61F74799D00AFD326 /* NSAttributedString.swift in Sources */,
73065AF0205809D900D69722 /* TokensBalanceOperation.swift in Sources */,
290B2B651F91A4880053C83E /* TransactionsFooterView.swift in Sources */,
771D53652055F39700C79485 /* TokenItem.swift in Sources */,
293B8B431F70815900356286 /* BalanceTitleView.swift in Sources */,
29DBF2A11F9DA6EF00327C60 /* BackupViewController.swift in Sources */,
29FC9BC61F830899000209CD /* MirgrationInitializer.swift in Sources */,
29AD8A091F93F8B2008E10E7 /* Session.swift in Sources */,
294DFB981FDF8CBB004CEB56 /* CrashReportInitializer.swift in Sources */,
291F52B91F6B880F00B369AB /* EtherKeystore.swift in Sources */,
29BB94971F6FCD60009B09CC /* SendViewModel.swift in Sources */,
73065AF220580A1200D69722 /* TokensTickerOperation.swift in Sources */,
296106D01F778A8D0006164B /* TransferType.swift in Sources */,
29E14FDB1F7F4F3D00185568 /* Transaction.swift in Sources */,
BBF4F9B72029D0B3009E04C0 /* GasViewModel.swift in Sources */,
Expand Down Expand Up @@ -3087,6 +3098,7 @@
7721A6CC202EFBC5004DB16C /* AddCustomNetworkViewController.swift in Sources */,
77872D322027AA4A0032D687 /* SliderTextFieldRow.swift in Sources */,
298542E31FBA722F00CB5081 /* ContainerView.swift in Sources */,
73065AEE205809CA00D69722 /* TokensOperation.swift in Sources */,
2923D9B11FDA49D8000CF3F8 /* Initializer.swift in Sources */,
294DFBAE1FE6F254004CEB56 /* TokenObject.swift in Sources */,
29CAEB8E1F70A2FB00F7357D /* TransactionCellViewModel.swift in Sources */,
Expand Down
26 changes: 0 additions & 26 deletions Trust/Models/TokenItem.swift

This file was deleted.

16 changes: 6 additions & 10 deletions Trust/Tokens/Network/TokensNetwork.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,10 @@ protocol TokensNetworkProtocol: TrustNetworkProtocol {
func ethBalance(completion: @escaping (_ balance: Balance?) -> Void)
func tokenBalance(for token: TokenObject, completion: @escaping (_ result: (TokenObject, Balance?)) -> Void)
func assets(completion: @escaping (_ result: ([NonFungibleTokenCategory]?)) -> Void)
func tokensList(for address: Address, completion: @escaping (_ result: ([TokenListItem]?)) -> Void)
func tokensList(for address: Address, completion: @escaping (_ result: ([TokenObject]?)) -> Void)
}

class TokensNetwork: TokensNetworkProtocol {

let provider: MoyaProvider<TrustService>

let config: Config
Expand Down Expand Up @@ -59,11 +58,7 @@ class TokensNetwork: TokensNetworkProtocol {
}

func tokenBalance(for token: TokenObject, completion: @escaping (_ result: (TokenObject, Balance?)) -> Void) {
guard let contract = Address(string: token.contract) else {
completion((token, nil))
return
}
balanceService.getBalance(for: account.address, contract: contract) { result in
balanceService.getBalance(for: account.address, contract: token.address) { result in
switch result {
case .success(let balance):
completion((token, Balance(value: balance)))
Expand All @@ -73,13 +68,14 @@ class TokensNetwork: TokensNetworkProtocol {
}
}

func tokensList(for address: Address, completion: @escaping (([TokenListItem]?)) -> Void) {
func tokensList(for address: Address, completion: @escaping (([TokenObject]?)) -> Void) {
provider.request(.getTokens(address: address.description, showBalance: false)) { result in
switch result {
case .success(let response):
do {
let items = try response.map(ArrayResponse<TokenListItem>.self).docs
completion(items)
let items = try response.map(ArrayResponse<TokenObjectList>.self).docs
let tokens = items.map { $0.contract }
completion(tokens)
} catch {
completion(nil)
}
Expand Down
26 changes: 6 additions & 20 deletions Trust/Tokens/Storage/TokensDataStore.swift
Original file line number Diff line number Diff line change
Expand Up @@ -71,15 +71,15 @@ class TokensDataStore {
}

func add(tokens: [Object]) {
realm.beginWrite()
realm.add(tokens, update: true)
try! realm.commitWrite()
try! realm.write {
realm.add(tokens, update: true)
}
}

func delete(tokens: [Object]) {
realm.beginWrite()
realm.delete(tokens)
try! realm.commitWrite()
try! realm.write {
realm.delete(tokens)
}
}

func deleteAll() {
Expand All @@ -100,20 +100,6 @@ class TokensDataStore {
}
}

static func update(in realm: Realm, tokens: [Token]) {
realm.beginWrite()
for token in tokens {
let update: [String: Any] = [
"contract": token.address.description,
"name": token.name,
"symbol": token.symbol,
"decimals": token.decimals,
]
realm.create(TokenObject.self, value: update, update: true)
}
try! realm.commitWrite()
}

static func etherToken(for config: Config) -> TokenObject {
return TokenObject(
contract: "0x0000000000000000000000000000000000000000",
Expand Down
30 changes: 30 additions & 0 deletions Trust/Tokens/Types/EthBalanceOperation.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// Copyright SIX DAY LLC. All rights reserved.

import UIKit
import BigInt

class EthBalanceOperation: TrustOperation {
private var network: TokensNetworkProtocol
var balance: Balance = Balance(value: BigInt(0))

init(network: TokensNetworkProtocol) {
self.network = network
}

override func main() {
guard isCancelled == false else {
finish(true)
return
}
fetchBalance()
}

private func fetchBalance() {
executing(true)
network.ethBalance { [weak self] result in
self?.balance = result ?? Balance(value: BigInt(0))
self?.executing(false)
self?.finish(true)
}
}
}
42 changes: 41 additions & 1 deletion Trust/Tokens/Types/TokenObject.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,13 @@ import Foundation
import RealmSwift
import BigInt
import TrustKeystore
import Realm

class TokenObject: Object {
struct TokenObjectList: Decodable {
let contract: TokenObject
}

class TokenObject: Object, Decodable {
@objc dynamic var contract: String = ""
@objc dynamic var name: String = ""
@objc dynamic var symbol: String = ""
Expand Down Expand Up @@ -33,6 +38,37 @@ class TokenObject: Object {
self.isDisabled = isDisabled
}

private enum TokenObjectCodingKeys: String, CodingKey {
case address
case name
case symbol
case decimals
}

convenience required init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: TokenObjectCodingKeys.self)
var contract = try container.decode(String.self, forKey: .address)
let name = try container.decode(String.self, forKey: .name)
let symbol = try container.decode(String.self, forKey: .symbol)
let decimals = try container.decode(Int.self, forKey: .decimals)
if let convertedAddress = Address(string: contract)?.description {
contract = convertedAddress
}
self.init(contract: contract, name: name, symbol: symbol, decimals: decimals, value: "0", isCustom: false, isDisabled: false)
}

required init() {
super.init()
}

required init(value: Any, schema: RLMSchema) {
super.init(value: value, schema: schema)
}

required init(realm: RLMRealm, schema: RLMObjectSchema) {
super.init(realm: realm, schema: schema)
}

var address: Address {
return Address(string: contract)!
}
Expand All @@ -57,4 +93,8 @@ class TokenObject: Object {
var title: String {
return name.isEmpty ? symbol : (name + " (" + symbol + ")")
}

var imagePath: String {
return "https://trustwalletapp.com/images/tokens/\(contract.lowercased()).png"
}
}
59 changes: 59 additions & 0 deletions Trust/Tokens/Types/TokensBalanceOperation.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
// Copyright SIX DAY LLC. All rights reserved.

import UIKit
import TrustKeystore

class TokensBalanceOperation: TrustOperation {
private var network: TokensNetworkProtocol
private let address: Address
var tokens: [TokenObject] = [TokenObject]()
private var pos = 0

init(
network: TokensNetworkProtocol,
address: Address
) {
self.network = network
self.address = address
}

override func main() {
guard isCancelled == false, !tokens.isEmpty else {
finish(true)
return
}
executing(true)
updateTokens()
}

private func updateTokens() {
updateBalance(for: tokens[pos]) {[weak self] token in
guard let strongSelf = self else {
self?.executing(false)
self?.finish(true)
return
}
let currentPos = strongSelf.pos
strongSelf.tokens[currentPos] = token
strongSelf.pos += 1
if strongSelf.pos < strongSelf.tokens.count {
strongSelf.updateTokens()
} else {
self?.executing(false)
self?.finish(true)
}
}
}

private func updateBalance(for token: TokenObject, completion: @escaping ((TokenObject) -> Void)) {
network.tokenBalance(for: token) { result in
guard let balance = result.1 else {
completion(token)
return
}
let tempToken = token
tempToken.value = balance.value.description
completion(tempToken)
}
}
}
39 changes: 39 additions & 0 deletions Trust/Tokens/Types/TokensOperation.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// Copyright SIX DAY LLC. All rights reserved.

import TrustKeystore

class TokensOperation: TrustOperation {
private var network: TokensNetworkProtocol
private let address: Address
var tokens: [TokenObject] = [TokenObject]()

init(
network: TokensNetworkProtocol,
address: Address
) {
self.network = network
self.address = address
}

override func main() {
guard isCancelled == false else {
finish(true)
return
}
fetchTokensList()
}

private func fetchTokensList() {
executing(true)
network.tokensList(for: address) { result in
guard let tokensList = result else {
self.executing(false)
self.finish(true)
return
}
self.tokens.append(contentsOf: tokensList)
self.executing(false)
self.finish(true)
}
}
}
Loading

0 comments on commit ca5615c

Please sign in to comment.