Skip to content

Commit

Permalink
Improve handling address format for currency key
Browse files Browse the repository at this point in the history
  • Loading branch information
vikmeup committed Jun 27, 2018
1 parent 9103cf7 commit e36394b
Show file tree
Hide file tree
Showing 12 changed files with 30 additions and 29 deletions.
2 changes: 1 addition & 1 deletion Trust/Core/Network/TrustNetwork.swift
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ class TrustNetwork: NetworkProtocol {
symbol: rawTicker.symbol,
price: rawTicker.price,
percent_change_24h: rawTicker.percent_change_24h,
contract: rawTicker.contract,
contract: Address(string: rawTicker.contract) ?? Address.zero, // This should not happen
tickersKey: tickersKey
)
}
Expand Down
4 changes: 2 additions & 2 deletions Trust/Settings/Types/RPCServers.swift
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,8 @@ enum RPCServer {
}
}

var address: String {
return "0x0000000000000000000000000000000000000000"
var address: Address {
return Address(string: "0x0000000000000000000000000000000000000000")!
}

var decimals: Int {
Expand Down
5 changes: 3 additions & 2 deletions Trust/Tokens/Storage/TokensDataStore.swift
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,9 @@ class TokensDataStore {
}

func coinTicker(for token: TokenObject) -> CoinTicker? {
guard let contract = Address(string: token.contract) else { return .none }
return tickers().first(where: {
return $0.key == CoinTickerKeyMaker.makePrimaryKey(symbol: $0.symbol, contract: token.contract, currencyKey: $0.tickersKey)
return $0.key == CoinTickerKeyMaker.makePrimaryKey(symbol: $0.symbol, contract: contract, currencyKey: $0.tickersKey)
})
}

Expand Down Expand Up @@ -183,7 +184,7 @@ class TokensDataStore {

static func etherToken(for config: Config = .current) -> TokenObject {
return TokenObject(
contract: config.server.address,
contract: config.server.address.description,
name: config.server.name,
symbol: config.server.symbol,
decimals: config.server.decimals,
Expand Down
8 changes: 5 additions & 3 deletions Trust/Tokens/Types/CoinTicker.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import Foundation
import Realm
import RealmSwift
import TrustCore

class CoinTicker: Object, Decodable {
@objc dynamic var symbol: String = ""
Expand All @@ -16,15 +17,16 @@ class CoinTicker: Object, Decodable {
symbol: String = "",
price: String = "",
percent_change_24h: String = "",
contract: String = "",
contract: Address,
tickersKey: String = ""
) {
self.init()
self.symbol = symbol
self.price = price
self.percent_change_24h = percent_change_24h
self.contract = contract
self.contract = contract.description
self.tickersKey = tickersKey

self.key = CoinTickerKeyMaker.makePrimaryKey(symbol: symbol, contract: contract, currencyKey: tickersKey)
}

Expand Down Expand Up @@ -67,7 +69,7 @@ extension CoinTicker {
}

struct CoinTickerKeyMaker {
static func makePrimaryKey(symbol: String, contract: String, currencyKey: String) -> String {
static func makePrimaryKey(symbol: String, contract: Address, currencyKey: String) -> String {
return "\(symbol)_\(contract)_\(currencyKey)"
}

Expand Down
2 changes: 1 addition & 1 deletion Trust/Transactions/ViewModels/BalanceViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ struct BalanceViewModel: BalanceBaseViewModel {
var currencyAmount: String? {
guard let rate = rate else { return nil }
guard
let currentRate = (rate.rates.filter { $0.contract == config.server.address }.first),
let currentRate = (rate.rates.filter { $0.contract == config.server.address.description }.first),
currentRate.price > 0,
amount > 0
else { return nil }
Expand Down
5 changes: 3 additions & 2 deletions Trust/Transfer/Types/CurrencyRate+Fee.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@

import Foundation
import BigInt
import TrustCore

extension CurrencyRate {
func estimate(fee: String, with address: String) -> Double? {
func estimate(fee: String, with address: Address) -> Double? {
guard let feeInDouble = Double(fee) else {
return nil
}
guard let price = self.rates.filter({ $0.contract == address }).first else {
guard let price = self.rates.filter({ $0.contract == address.description }).first else {
return nil
}
return price.price * feeInDouble
Expand Down
2 changes: 1 addition & 1 deletion Trust/Transfer/ViewModels/MonetaryAmountViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ struct MonetaryAmountViewModel {
guard let address = address else {
return .none
}
return currencyRate?.estimate(fee: amount, with: address.eip55String)
return currencyRate?.estimate(fee: amount, with: address)
}

var amountText: String? {
Expand Down
2 changes: 1 addition & 1 deletion TrustTests/Factories/Address.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import TrustCore

extension Address {
static func make(
address: String = "0x1000000000000000000000000000000000000000"
address: String = "0x0000000000000000000000000000000000000001"
) -> Address {
return Address(
string: address
Expand Down
3 changes: 2 additions & 1 deletion TrustTests/Factories/CoinTicker.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@

import Foundation
@testable import Trust
import TrustCore

extension CoinTicker {
static func make(
symbol: String = "symbol",
price: String = "0",
percent_change_24h: String = "0",
contract: String = "contract",
contract: Address = .zero,
currencyKey: String = "currencyKey",
key: String? = nil
) -> CoinTicker {
Expand Down
10 changes: 5 additions & 5 deletions TrustTests/Factories/FakeTokensDataStore.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,16 @@ class FakeCoinTickerFactory {

class func make3UniqueCionTickers() -> [CoinTicker] {
return [
CoinTicker.make(symbol: "symbol1", price: "10", contract: "contract1", currencyKey: currencyKey),
CoinTicker.make(symbol: "symbol2", price: "20", contract: "contract2", currencyKey: currencyKey),
CoinTicker.make(symbol: "symbol3", price: "30", contract: "contract3", currencyKey: currencyKey),
CoinTicker.make(symbol: "symbol1", price: "10", contract: .make(address: "0x0000000000000000000000000000000000000001"), currencyKey: currencyKey),
CoinTicker.make(symbol: "symbol2", price: "20", contract: .make(address: "0x0000000000000000000000000000000000000002"), currencyKey: currencyKey),
CoinTicker.make(symbol: "symbol3", price: "30", contract: .make(address: "0x0000000000000000000000000000000000000003"), currencyKey: currencyKey),
]
}

class func make2DuplicateCionTickersWithDifferentKey() -> [CoinTicker] {
return [
CoinTicker.make(symbol: "same-symbol", contract: "same-contract-address", currencyKey: currencyKey, key: "old-key"),
CoinTicker.make(symbol: "same-symbol", contract: "same-contract-address", currencyKey: currencyKey),
CoinTicker.make(symbol: "symbol1", contract: .make(), currencyKey: currencyKey, key: "old-key"),
CoinTicker.make(symbol: "symbol1", contract: .make(), currencyKey: currencyKey),
]
}
}
2 changes: 1 addition & 1 deletion TrustTests/Factories/Transaction.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ extension Transaction {
static func make(
id: String = "0x1",
blockNumber: Int = 1,
from: String = "0x1000000000000000000000000000000000000000",
from: String = "0x0000000000000000000000000000000000000001",
to: String = "0x1",
value: String = "1",
gas: String = "0x1",
Expand Down
14 changes: 5 additions & 9 deletions TrustTests/Tokens/TokensDataStoreTest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ class TokensDataStoreTest: XCTestCase {

func testGetBalance() {
var tokenObject = TokenObject(
contract: "contract1",
contract: "0x0000000000000000000000000000000000000001",
decimals: 2,
value: "10000"
)
Expand All @@ -57,11 +57,11 @@ class TokensDataStoreTest: XCTestCase {

XCTAssertEqual(1000.00, tokensDataStore.getBalance(for: tokenObject, with: coinTickers))

XCTAssertEqual(0.00, tokensDataStore.getBalance(for: tokenObject, with: [CoinTicker(price: "", contract: "contract1")]))
XCTAssertEqual(0.00, tokensDataStore.getBalance(for: tokenObject, with: [CoinTicker(price: "", contract: .make())]))
XCTAssertEqual(0.00, tokensDataStore.getBalance(for: tokenObject, with: [CoinTicker]()))

tokenObject = TokenObject(
contract: "contract2",
contract: "0x0000000000000000000000000000000000000002",
decimals: 3,
value: "20000"
)
Expand All @@ -81,14 +81,10 @@ class TokensDataStoreTest: XCTestCase {
func testGetCoinTickerForAParticularToken() {
tokensDataStore.saveTickers(tickers: FakeCoinTickerFactory.make2DuplicateCionTickersWithDifferentKey())

let token: TokenObject = {
let token = TokenObject()
token.contract = "same-contract-address"
return token
}()
let token = TokenObject(contract: "0x0000000000000000000000000000000000000001", name: "", symbol: "symbol1", decimals: 18, value: "", isCustom: false, isDisabled: false)

let coinTicker = tokensDataStore.coinTicker(for: token)

XCTAssertEqual("same-symbol_same-contract-address_tickers-USD-1", coinTicker?.key)
XCTAssertEqual("symbol1_0x0000000000000000000000000000000000000001_tickers-USD-1", coinTicker?.key)
}
}

0 comments on commit e36394b

Please sign in to comment.