Merge pull request #2895 from AlphaWallet/fix-erc721-balance-fetching

Fix ERC721 balance fetching
pull/2899/head
Hwee-Boon Yar 3 years ago committed by GitHub
commit b204eadabd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 7
      AlphaWallet/EtherClient/OpenSea.swift
  2. 34
      AlphaWallet/Tokens/Coordinators/GetContractInteractions.swift

@ -121,7 +121,6 @@ class OpenSea {
}
DispatchQueue.global(qos: .userInitiated).async {
var results = sum
var currentPageCount = 0
for (_, each): (String, JSON) in json["assets"] {
let type = each["asset_contract"]["schema_name"].stringValue
guard type == "ERC721" else { continue }
@ -149,7 +148,6 @@ class OpenSea {
}
if let contract = AlphaWallet.Address(string: each["asset_contract"]["address"].stringValue) {
let cat = OpenSeaNonFungible(tokenId: tokenId, contractName: contractName, symbol: symbol, name: name, description: description, thumbnailUrl: thumbnailUrl, imageUrl: imageUrl, contractImageUrl: contractImageUrl, externalLink: externalLink, backgroundColor: backgroundColor, traits: traits)
currentPageCount += 1
if var list = results[contract] {
list.append(cat)
results[contract] = list
@ -161,8 +159,9 @@ class OpenSea {
}
DispatchQueue.main.async { [weak self] in
guard let strongSelf = self else { return }
if currentPageCount > 0 {
strongSelf.fetchPage(forOwner: owner, offset: offset + currentPageCount, sum: results) { results in
let fetchedCount = json["assets"].count
if fetchedCount > 0 {
strongSelf.fetchPage(forOwner: owner, offset: offset + fetchedCount, sum: results) { results in
completion(results)
}
} else {

@ -79,8 +79,8 @@ class GetContractInteractions {
isErc20Interaction: true
)
}
completion(transactions)
let results = self.mergeTransactionOperationsIntoSingleTransaction(transactions)
completion(results)
case .failure:
completion([])
}
@ -88,7 +88,7 @@ class GetContractInteractions {
}
//TODO Almost a duplicate of the the ERC20 version. De-dup maybe?
func getErc721Interactions(contractAddress: AlphaWallet.Address? = nil, address: AlphaWallet.Address, server: RPCServer, startBlock: Int? = nil, completion: @escaping ([TransactionInstance]) -> Void) {
func getErc721Interactions(address: AlphaWallet.Address, server: RPCServer, startBlock: Int? = nil, completion: @escaping ([TransactionInstance]) -> Void) {
guard let etherscanURL = server.getEtherscanURLForERC721TransactionHistory(for: address, startBlock: startBlock) else { return }
Alamofire.request(etherscanURL).validate().responseJSON(queue: queue, options: [], completionHandler: { response in
@ -96,17 +96,9 @@ class GetContractInteractions {
case .success(let value):
//Performance: process in background so UI don't have a chance of blocking if there's a long list of contracts
let json = JSON(value)
let filteredResult: [(String, JSON)]
if let contractAddress = contractAddress {
//filter based on what contract you are after
filteredResult = json["result"].filter {
$0.1["contractAddress"].stringValue == contractAddress.eip55String.lowercased()
}
} else {
filteredResult = json["result"].filter {
let filteredResult: [(String, JSON)] = json["result"].filter {
$0.1["to"].stringValue.hasPrefix("0x")
}
}
let transactions: [TransactionInstance] = filteredResult.map { result in
let transactionJson = result.1
@ -151,14 +143,28 @@ class GetContractInteractions {
isErc20Interaction: true
)
}
completion(transactions)
let results = self.mergeTransactionOperationsIntoSingleTransaction(transactions)
completion(results)
case .failure:
completion([])
}
})
}
private func mergeTransactionOperationsIntoSingleTransaction(_ transactions: [TransactionInstance]) -> [TransactionInstance] {
var results: [TransactionInstance] = .init()
for each in transactions {
if let index = results.firstIndex(where: { $0.blockNumber == each.blockNumber }) {
var found = results[index]
found.localizedOperations.append(contentsOf: each.localizedOperations)
results[index] = found
} else {
results.append(each)
}
}
return results
}
func getContractList(address: AlphaWallet.Address, server: RPCServer, startBlock: Int? = nil, erc20: Bool, completion: @escaping ([AlphaWallet.Address], Int?) -> Void) {
let etherscanURL: URL
if erc20 {

Loading…
Cancel
Save