replace Bundle.main.path() with R.file.tokensJson() for TokenJsonReader #6646

pull/6731/head
Krypto Pank 2 years ago
parent 215c9bc0dd
commit ae10399371
  1. 2
      AlphaWallet.xcodeproj/project.pbxproj
  2. 2
      AlphaWallet/AppCoordinator.swift
  3. 2
      AlphaWalletTests/Factories/FakeTokenGroupIdentifier.swift
  4. 16
      AlphaWalletTests/Tokens/TokenGroupIdentifierTest.swift
  5. 15
      modules/AlphaWalletFoundation/AlphaWalletFoundation/Tokens/JSON/TokenGroupIdentifier.swift

@ -514,6 +514,7 @@
8728FFEA2743CAA7008E5524 /* NFTAssetSelectionSectionHeaderView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8728FFE92743CAA7008E5524 /* NFTAssetSelectionSectionHeaderView.swift */; };
8729389029A6074500705EFF /* AVPlayerViewModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8729388F29A6074500705EFF /* AVPlayerViewModel.swift */; };
8729389229A612EC00705EFF /* WebImageViewModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8729389129A612EC00705EFF /* WebImageViewModel.swift */; };
8729D3BC29F7D79700117F2F /* tokens.json in Resources */ = {isa = PBXBuildFile; fileRef = 87B1ACDA28C0D55D0072A5E2 /* tokens.json */; };
872A98662862EC0C00196EA3 /* base64_image_example.txt in Resources */ = {isa = PBXBuildFile; fileRef = 872A98652862EC0C00196EA3 /* base64_image_example.txt */; };
872A987128645EF100196EA3 /* SectionProtocol.swift in Sources */ = {isa = PBXBuildFile; fileRef = 872A987028645EF100196EA3 /* SectionProtocol.swift */; };
872A987328645F1D00196EA3 /* SendFungiblesTransactionViewModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 872A987228645F1D00196EA3 /* SendFungiblesTransactionViewModel.swift */; };
@ -4734,6 +4735,7 @@
isa = PBXResourcesBuildPhase;
buildActionMask = 2147483647;
files = (
8729D3BC29F7D79700117F2F /* tokens.json in Resources */,
872A98662862EC0C00196EA3 /* base64_image_example.txt in Resources */,
87831C842849E4E0002E856E /* tikersForTest.json in Resources */,
);

@ -232,7 +232,7 @@ class AppCoordinator: NSObject, Coordinator {
tokenGroupIdentifier: tokenGroupIdentifier,
spamImage: R.image.spamSmall()!)
private let tokenGroupIdentifier: TokenGroupIdentifierProtocol = TokenGroupIdentifier.identifier(fromFileName: "tokens")!
private let tokenGroupIdentifier: TokenGroupIdentifierProtocol = TokenGroupIdentifier.identifier(tokenJsonUrl: R.file.tokensJson()!)!
//Unfortunate to have to have a factory method and not be able to use an initializer (because we can't override `init()` to throw)
static func create() throws -> AppCoordinator {

@ -10,7 +10,7 @@ import Foundation
import AlphaWalletFoundation
final class FakeTokenGroupIdentifier: TokenGroupIdentifierProtocol {
static func identifier(fromFileName: String) -> TokenGroupIdentifierProtocol? {
static func identifier(tokenJsonUrl: URL) -> TokenGroupIdentifierProtocol? {
return nil
}

@ -18,22 +18,22 @@ class TokenGroupIdentifierTest: XCTestCase {
}
func testReadingExistingFile() throws {
let reader: TokenGroupIdentifierProtocol? = TokenGroupIdentifier.identifier(fromFileName: "tokens")
let reader: TokenGroupIdentifierProtocol? = TokenGroupIdentifier.identifier(tokenJsonUrl: R.file.tokensJson()!)
XCTAssertNotNil(reader)
}
func testReadingExistingNonJsonFile() throws {
let reader: TokenGroupIdentifierProtocol? = TokenGroupIdentifier.identifier(fromFileName: "schema")
let reader: TokenGroupIdentifierProtocol? = TokenGroupIdentifier.identifier(tokenJsonUrl: R.file.tokensJson()!)
XCTAssertNil(reader)
}
func testReadingNonExistingFile() throws {
let reader: TokenGroupIdentifierProtocol? = TokenGroupIdentifier.identifier(fromFileName: "Not present at all")
let reader: TokenGroupIdentifierProtocol? = TokenGroupIdentifier.identifier(tokenJsonUrl: URL(string: "no-file-url")!)
XCTAssertNil(reader)
}
func testDetectDefi() throws {
let tokenGroupIdentifier: TokenGroupIdentifierProtocol? = TokenGroupIdentifier.identifier(fromFileName: "tokens")
let tokenGroupIdentifier: TokenGroupIdentifierProtocol? = TokenGroupIdentifier.identifier(tokenJsonUrl: R.file.tokensJson()!)
XCTAssert(tokenGroupIdentifier != nil)
// match address and id in contract
let t1 = Token(contract: AlphaWallet.Address(string: "0xF0A5717Ec0883eE56438932b0fe4A20822735fBa")!, server: RPCServer.custom(chainId: 42161))
@ -50,7 +50,7 @@ class TokenGroupIdentifierTest: XCTestCase {
}
func testDetectGoverance() throws {
let tokenGroupIdentifier: TokenGroupIdentifierProtocol? = TokenGroupIdentifier.identifier(fromFileName: "tokens")
let tokenGroupIdentifier: TokenGroupIdentifierProtocol? = TokenGroupIdentifier.identifier(tokenJsonUrl: R.file.tokensJson()!)
XCTAssert(tokenGroupIdentifier != nil)
// match address and id in contract
let t1 = Token(contract: AlphaWallet.Address(string: "0x596fa47043f99a4e0f122243b841e55375cde0d2")!, server: RPCServer.custom(chainId: 43114))
@ -67,7 +67,7 @@ class TokenGroupIdentifierTest: XCTestCase {
}
func testDetectAssets() throws {
let tokenGroupIdentifier: TokenGroupIdentifierProtocol? = TokenGroupIdentifier.identifier(fromFileName: "tokens")
let tokenGroupIdentifier: TokenGroupIdentifierProtocol? = TokenGroupIdentifier.identifier(tokenJsonUrl: R.file.tokensJson()!)
XCTAssert(tokenGroupIdentifier != nil)
let t1 = Token(contract: AlphaWallet.Address(string: "0xfc82bb4ba86045af6f327323a46e80412b91b27d")!, server: RPCServer.custom(chainId: 1))
XCTAssertEqual(tokenGroupIdentifier!.identify(token: t1), TokenGroup.assets)
@ -80,7 +80,7 @@ class TokenGroupIdentifierTest: XCTestCase {
}
func testDetectCollectible() throws {
let tokenGroupIdentifier: TokenGroupIdentifierProtocol? = TokenGroupIdentifier.identifier(fromFileName: "tokens")
let tokenGroupIdentifier: TokenGroupIdentifierProtocol? = TokenGroupIdentifier.identifier(tokenJsonUrl: R.file.tokensJson()!)
XCTAssert(tokenGroupIdentifier != nil)
let t1 = Token(type: .erc721)
XCTAssertEqual(tokenGroupIdentifier!.identify(token: t1), TokenGroup.collectibles)
@ -91,7 +91,7 @@ class TokenGroupIdentifierTest: XCTestCase {
}
func testDetectSpam() throws {
let tokenGroupIdentifier: TokenGroupIdentifierProtocol? = TokenGroupIdentifier.identifier(fromFileName: "tokens")
let tokenGroupIdentifier: TokenGroupIdentifierProtocol? = TokenGroupIdentifier.identifier(tokenJsonUrl: R.file.tokensJson()!)
XCTAssert(tokenGroupIdentifier != nil)
XCTAssertFalse(tokenGroupIdentifier!.hasContract(address: "0xD7f1d4F5A1B44D827a7C3cC5dd46a80fADe55558", chainID: 137))
XCTAssertTrue(tokenGroupIdentifier!.hasContract(address: "0x596fa47043f99a4e0f122243b841e55375cde0d2", chainID: 43114))

@ -42,17 +42,16 @@ public class TokenJsonReader {
private var decodedTokenEntries: [TokenEntry] = [TokenEntry]()
public init?(fromLocalFileNameWithoutSuffix fileName: String) {
public init?(tokenJsonUrl: URL) {
do {
try readAndDecodeData(fileName: fileName)
try readAndDecodeData(tokenJsonUrl: tokenJsonUrl)
} catch {
return nil
}
}
private func readAndDecodeData(fileName: String) throws {
guard let bundlePath = Bundle.main.path(forResource: fileName, ofType: "json") else { throw TokenJsonReader.error.fileDoesNotExist }
guard let jsonData = try String(contentsOfFile: bundlePath).data(using: .utf8) else { throw TokenJsonReader.error.fileIsNotUtf8 }
private func readAndDecodeData(tokenJsonUrl: URL) throws {
let jsonData = try Data(contentsOf: tokenJsonUrl)
do {
decodedTokenEntries = try JSONDecoder().decode([TokenEntry].self, from: jsonData)
} catch DecodingError.dataCorrupted {
@ -124,7 +123,7 @@ public protocol TokenGroupIdentifiable {
}
public protocol TokenGroupIdentifierProtocol {
static func identifier(fromFileName: String) -> TokenGroupIdentifierProtocol?
static func identifier(tokenJsonUrl: URL) -> TokenGroupIdentifierProtocol?
func identify(token: TokenGroupIdentifiable) -> TokenGroup
func hasContract(address: String, chainID: Int) -> Bool
func isSpam(address: String, chainID: Int) -> Bool
@ -135,8 +134,8 @@ public class TokenGroupIdentifier: TokenGroupIdentifierProtocol {
private var decodedTokenEntries: TokenGroupDictionary = TokenGroupDictionary()
private var spamTokenEntries: Set<String> = Set<String>()
public static func identifier(fromFileName fileName: String) -> TokenGroupIdentifierProtocol? {
guard let reader = TokenJsonReader(fromLocalFileNameWithoutSuffix: fileName) else { return nil }
public static func identifier(tokenJsonUrl: URL) -> TokenGroupIdentifierProtocol? {
guard let reader = TokenJsonReader(tokenJsonUrl: tokenJsonUrl) else { return nil }
do {
let decodedTokenEntries = try reader.tokenGroupDictionary()
var spamTokenEntries: Set<String> = Set<String>()

Loading…
Cancel
Save