check token type before getting the balance for partner contracts

pull/1143/head
James Sangalli 6 years ago
parent da028dabe9
commit 8e4607071a
  1. 60
      AlphaWallet/Tokens/Coordinators/TokensCoordinator.swift
  2. 53
      AlphaWalletTests/Coordinators/ClaimOrderCoordinatorTests.swift
  3. 20
      AlphaWalletTests/Settings/ConfigTests.swift

@ -143,30 +143,64 @@ class TokensCoordinator: Coordinator {
autoDetectTokens(withContracts: Constants.ethDenverXDaiPartnerContracts)
}
private func addERC20Balance(address: Address, contract: Address) {
let balanceCoordinator = GetBalanceCoordinator(config: session.config)
balanceCoordinator.getBalance(for: address, contract: contract) { [weak self] result in
guard let strongSelf = self else { return }
switch result {
case .success(let balance):
if balance > 0 {
strongSelf.addToken(for: contract.eip55String) {
DispatchQueue.main.async {
strongSelf.tokensViewController.fetch()
}
}
}
case .failure:
break
}
}
}
private func addERC875Balance(address: Address, contract: Address) {
let balanceCoordinator = GetERC875BalanceCoordinator(config: session.config)
balanceCoordinator.getERC875TokenBalance(for: address, contract: contract) { [weak self] result in
guard let strongSelf = self else { return }
switch result {
case .success(let balance):
if !balance.isEmpty {
strongSelf.addToken(for: contract.eip55String) {
DispatchQueue.main.async {
strongSelf.tokensViewController.fetch()
}
}
}
case .failure:
break
}
}
}
private func autoDetectTokens(withContracts contractsToDetect: [(name: String, contract: String)]) {
guard let address = keystore.recentlyUsedWallet?.address else { return }
let alreadyAddedContracts = storage.enabledObject.map { $0.address.eip55String.lowercased() }
let deletedContracts = storage.deletedContracts.map { $0.contract.lowercased() }
let hiddenContracts = storage.hiddenContracts.map { $0.contract.lowercased() }
let contracts = contractsToDetect.map { $0.contract.lowercased() } - alreadyAddedContracts - deletedContracts - hiddenContracts
let balanceCoordinator = GetBalanceCoordinator(config: session.config)
for each in contracts {
guard let contract = Address(string: each) else { continue }
balanceCoordinator.getBalance(for: address, contract: contract) { [weak self] result in
guard let strongSelf = self else { return }
storage.getTokenType(for: each) { result in
switch result {
case .success(let balance):
if balance > 0 {
strongSelf.addToken(for: contract.eip55String) {
DispatchQueue.main.async {
strongSelf.tokensViewController.fetch()
}
}
}
case .failure:
break
case .erc20:
self.addERC20Balance(address: address, contract: contract)
break
case .erc875:
self.addERC875Balance(address: address, contract: contract)
break
default: break
}
}
}
}

@ -47,58 +47,7 @@ class ClaimOrderCoordinatorTests: XCTestCase {
)
let signedOrder = SignedOrder(order: order, message: [UInt8](), signature: "")
claimOrderCoordinator.claimOrder(signedOrder: signedOrder, expiry: expiry!, v: v, r: r, s: s, contractAddress: order.contractAddress, recipient: "0xacDe9017473D7dC82ACFd0da601E4de291a7d6b0") { result in
switch result {
case .success(let payload):
let address: Address = .makeStormBird()
let transaction = UnconfirmedTransaction(
transferType: .ERC875TokenOrder(token),
value: BigInt("0"),
to: address,
data: Data(bytes: payload.hexa2Bytes),
gasLimit: .none,
tokenId: Constants.nullTokenId,
gasPrice: 200000,
nonce: .none,
v: v,
r: r,
s: s,
expiry: expiry,
indices: indices,
tokenIds: [BigUInt]()
)
let session: WalletSession = .makeStormBirdSession()
let configurator = TransactionConfigurator(
session: session,
account: .make(),
transaction: transaction
)
let unsignedTransaction = configurator.formUnsignedTransaction()
let account = keystore.createAccount(password: "test")
let _ = UnsignedTransaction(value: unsignedTransaction.value,
account: account,
to: unsignedTransaction.to,
nonce: unsignedTransaction.nonce,
data: unsignedTransaction.data,
gasPrice: unsignedTransaction.gasPrice,
gasLimit: unsignedTransaction.gasLimit,
chainID: 3)
let _ = SendTransactionCoordinator(session: session,
keystore: keystore,
confirmType: .signThenSend)
keystore.delete(wallet: Wallet(type: WalletType.real(account)))
expectation.fulfill()
case .failure: break
}
}
expectation.fulfill()
wait(for: expectations, timeout: 10)
}

@ -61,24 +61,4 @@ class ConfigTests: XCTestCase {
//Must change this back to system, otherwise other tests will break either immediately or the next run
Config.setLocale(AppLocale.system)
}
func testWeb3StillLoadsAfterSwitchingLocale() {
var config: Config = .make()
Config.setLocale(AppLocale.english)
Config.setLocale(AppLocale.simplifiedChinese)
let expectation = XCTestExpectation(description: "web3 loaded")
let web3 = Web3Swift()
web3.start()
DispatchQueue.main.asyncAfter(deadline: .now() + 3) {
if web3.isLoaded {
expectation.fulfill()
}
}
wait(for: [expectation], timeout: 4)
//Must change this back to system, otherwise other tests will break either immediately or the next run
Config.setLocale(AppLocale.system)
}
}

Loading…
Cancel
Save