Hide some methods under internal level #5255

pull/5264/head
Krypto Pank 2 years ago
parent 3d228a4dbd
commit 296aca4b38
  1. 50
      modules/AlphaWalletFoundation/AlphaWalletFoundation/Core/Tokens/Types/EventActivity.swift
  2. 2
      modules/AlphaWalletFoundation/AlphaWalletFoundation/Core/Tokens/Types/EventInstance.swift
  3. 27
      modules/AlphaWalletFoundation/AlphaWalletFoundation/Core/Tokens/Types/EventInstanceValue.swift
  4. 12
      modules/AlphaWalletFoundation/AlphaWalletFoundation/Core/Tokens/Types/Token.swift
  5. 11
      modules/AlphaWalletFoundation/AlphaWalletFoundation/Core/Tokens/Types/TokenBalance.swift
  6. 8
      modules/AlphaWalletFoundation/AlphaWalletFoundation/Core/Tokens/Types/TokenObject.swift
  7. 4
      modules/AlphaWalletFoundation/AlphaWalletFoundation/Core/Tokens/Types/TokensDataStore.swift
  8. 49
      modules/AlphaWalletFoundation/AlphaWalletFoundation/Core/Transactions/Storage/Transaction.swift

@ -4,8 +4,8 @@ import Foundation
import BigInt
import RealmSwift
public class EventActivity: Object {
public static func generatePrimaryKey(fromContract contract: AlphaWallet.Address, tokenContract: AlphaWallet.Address, server: RPCServer, eventName: String, blockNumber: Int, transactionId: String, logIndex: Int, filter: String) -> String {
class EventActivity: Object {
static func generatePrimaryKey(fromContract contract: AlphaWallet.Address, tokenContract: AlphaWallet.Address, server: RPCServer, eventName: String, blockNumber: Int, transactionId: String, logIndex: Int, filter: String) -> String {
"\(contract.eip55String)-\(tokenContract.eip55String)-\(server.chainID)-\(eventName)-\(blockNumber)-\(transactionId)-\(logIndex)-\(filter)"
}
@ -46,7 +46,7 @@ public class EventActivity: Object {
.init(chainID: chainId)
}
public convenience init(contract: AlphaWallet.Address, tokenContract: AlphaWallet.Address, server: RPCServer, date: Date, eventName: String, blockNumber: Int, transactionId: String, transactionIndex: Int, logIndex: Int, filter: String, json: String) {
convenience init(contract: AlphaWallet.Address, tokenContract: AlphaWallet.Address, server: RPCServer, date: Date, eventName: String, blockNumber: Int, transactionId: String, transactionIndex: Int, logIndex: Int, filter: String, json: String) {
self.init()
self.primaryKey = EventActivity.generatePrimaryKey(fromContract: contract, tokenContract: tokenContract, server: server, eventName: eventName, blockNumber: blockNumber, transactionId: transactionId, logIndex: logIndex, filter: filter)
self.contract = contract.eip55String
@ -63,7 +63,7 @@ public class EventActivity: Object {
self._data = EventActivity.convertJsonToDictionary(json)
}
public convenience init(value: EventActivityInstance) {
convenience init(value: EventActivityInstance) {
self.init()
self.primaryKey = value.primaryKey
self.contract = value.contract.eip55String
@ -80,15 +80,15 @@ public class EventActivity: Object {
self._data = value.data
}
public override static func primaryKey() -> String? {
override static func primaryKey() -> String? {
return "primaryKey"
}
public override static func ignoredProperties() -> [String] {
override static func ignoredProperties() -> [String] {
return ["_data", "data"]
}
private static func convertJsonToDictionary(_ json: String) -> [String: AssetInternalValue] {
static func convertJsonToDictionary(_ json: String) -> [String: AssetInternalValue] {
let dict = json.data(using: .utf8).flatMap({ (try? JSONSerialization.jsonObject(with: $0, options: [])) as? [String: Any] }) ?? .init()
return Dictionary(uniqueKeysWithValues: dict.compactMap { key, value -> (String, AssetInternalValue)? in
switch value {
@ -124,23 +124,6 @@ public struct EventActivityInstance {
//Needed because Realm objects' properties (`json`) don't fire didSet after the object has been written to the database
public var data: [String: AssetInternalValue]
public init(event: EventActivity) {
self.primaryKey = event.primaryKey
self.contract = AlphaWallet.Address(uncheckedAgainstNullAddress: event.contract)!
self.tokenContract = AlphaWallet.Address(uncheckedAgainstNullAddress: event.tokenContract)!
self.server = RPCServer(chainID: event.chainId)
self.date = event.date
self.eventName = event.eventName
self.blockNumber = event.blockNumber
self.transactionId = event.transactionId
self.transactionIndex = event.transactionIndex
self.logIndex = event.logIndex
self.filter = event.filter
self.json = event.json
self.data = event.data
}
public init(contract: AlphaWallet.Address, tokenContract: AlphaWallet.Address, server: RPCServer, date: Date, eventName: String, blockNumber: Int, transactionId: String, transactionIndex: Int, logIndex: Int, filter: String, json: String) {
self.primaryKey = EventActivity.generatePrimaryKey(fromContract: contract, tokenContract: tokenContract, server: server, eventName: eventName, blockNumber: blockNumber, transactionId: transactionId, logIndex: logIndex, filter: filter)
self.contract = contract
@ -171,3 +154,22 @@ public struct EventActivityInstance {
})
}
}
extension EventActivityInstance {
init(event: EventActivity) {
self.primaryKey = event.primaryKey
self.contract = AlphaWallet.Address(uncheckedAgainstNullAddress: event.contract)!
self.tokenContract = AlphaWallet.Address(uncheckedAgainstNullAddress: event.tokenContract)!
self.server = RPCServer(chainID: event.chainId)
self.date = event.date
self.eventName = event.eventName
self.blockNumber = event.blockNumber
self.transactionId = event.transactionId
self.transactionIndex = event.transactionIndex
self.logIndex = event.logIndex
self.filter = event.filter
self.json = event.json
self.data = event.data
}
}

@ -3,7 +3,7 @@
import Foundation
import RealmSwift
public class EventInstance: Object {
class EventInstance: Object {
static func generatePrimaryKey(fromContract contract: AlphaWallet.Address, tokenContract: AlphaWallet.Address, server: RPCServer, eventName: String, blockNumber: Int, logIndex: Int, filter: String) -> String {
"\(contract.eip55String)-\(tokenContract.eip55String)-\(server.chainID)-\(eventName)-\(blockNumber)-\(logIndex)-\(filter)"
}

@ -32,19 +32,6 @@ public struct EventInstanceValue {
self._data = EventInstanceValue.convertJsonToDictionary(json)
}
public init(event: EventInstance) {
self.primaryKey = event.primaryKey
self.contract = event.contract
self.tokenContract = event.tokenContract
self.chainId = event.chainId
self.eventName = event.eventName
self.blockNumber = event.blockNumber
self.logIndex = event.logIndex
self.filter = event.filter
self.json = event.json
self._data = event._data
}
private static func convertJsonToDictionary(_ json: String) -> [String: AssetInternalValue] {
let dict = json.data(using: .utf8).flatMap({ (try? JSONSerialization.jsonObject(with: $0, options: [])) as? [String: Any] }) ?? .init()
return Dictionary(uniqueKeysWithValues: dict.compactMap { key, value -> (String, AssetInternalValue)? in
@ -60,3 +47,17 @@ public struct EventInstanceValue {
}
}
extension EventInstanceValue {
init(event: EventInstance) {
self.primaryKey = event.primaryKey
self.contract = event.contract
self.tokenContract = event.tokenContract
self.chainId = event.chainId
self.eventName = event.eventName
self.blockNumber = event.blockNumber
self.logIndex = event.logIndex
self.filter = event.filter
self.json = event.json
self._data = event._data
}
}

@ -75,7 +75,13 @@ public struct Token: Equatable, Hashable {
self.info = .init(uid: self.primaryKey)
}
public init(tokenObject: TokenObject) {
public static func == (lhs: Token, rhs: Token) -> Bool {
return lhs.primaryKey == rhs.primaryKey
}
}
extension Token {
init(tokenObject: TokenObject) {
name = tokenObject.name
primaryKey = tokenObject.primaryKey
server = tokenObject.server
@ -88,10 +94,6 @@ public struct Token: Equatable, Hashable {
balance = Array(tokenObject.balance.map { TokenBalanceValue(balance: $0) })
info = .init(tokenInfoObject: tokenObject.info)
}
public static func == (lhs: Token, rhs: Token) -> Bool {
return lhs.primaryKey == rhs.primaryKey
}
}
extension Token: TokenScriptSupportable {

@ -8,15 +8,14 @@ public struct TokenBalanceValue {
let json: String
let balance: String
let nonFungibleBalance: NonFungibleFromJson?
}
public init(balance: TokenBalance) {
extension TokenBalanceValue: Hashable {
init(balance: TokenBalance) {
self.json = balance.json
self.balance = balance.balance
self.nonFungibleBalance = balance.nonFungibleBalance
}
}
extension TokenBalanceValue: Hashable {
public func hash(into hasher: inout Hasher) {
hasher.combine(json)
@ -24,7 +23,7 @@ extension TokenBalanceValue: Hashable {
}
}
public class TokenBalance: Object {
class TokenBalance: Object {
@objc dynamic var balance = "0" {
didSet {
_nonFungibleBalance = balance.data(using: .utf8).flatMap { nonFungible(fromJsonData: $0) }
@ -52,7 +51,7 @@ public class TokenBalance: Object {
}
}
public override static func ignoredProperties() -> [String] {
override static func ignoredProperties() -> [String] {
return ["openSeaNonFungible", "_openSeaNonFungible"]
}
}

@ -4,7 +4,7 @@ import Foundation
import RealmSwift
import BigInt
public class TokenObject: Object {
class TokenObject: Object {
static func generatePrimaryKey(fromContract contract: AlphaWallet.Address, server: RPCServer) -> String {
return "\(contract.eip55String)-\(server.chainID)"
}
@ -91,15 +91,15 @@ public class TokenObject: Object {
return BigInt(value) ?? BigInt()
}
public override static func primaryKey() -> String? {
override static func primaryKey() -> String? {
return "primaryKey"
}
public override static func ignoredProperties() -> [String] {
override static func ignoredProperties() -> [String] {
return ["type", "info"]
}
public override func isEqual(_ object: Any?) -> Bool {
override func isEqual(_ object: Any?) -> Bool {
guard let object = object as? TokenObject else { return false }
//NOTE: to improve perfomance seems like we can use check for primary key instead of checking contracts
return object.contractAddress.sameContract(as: contractAddress)

@ -723,7 +723,7 @@ extension MultipleChainsTokensDataStore.functional {
])
}
public static func etherTokenObject(forServer server: RPCServer) -> TokenObject {
static func etherTokenObject(forServer server: RPCServer) -> TokenObject {
return TokenObject(
contract: Constants.nativeCryptoAddressInDatabase,
server: server,
@ -763,7 +763,7 @@ extension MultipleChainsTokensDataStore.functional {
}
//TODO: Rename tokenObject(ercToken with createTokenObject(ercToken, more clear name
public static func createTokenObject(ercToken token: ERCToken, shouldUpdateBalance: Bool) -> TokenObject {
static func createTokenObject(ercToken token: ERCToken, shouldUpdateBalance: Bool) -> TokenObject {
let newToken = TokenObject(
contract: token.contract,
server: token.server,

@ -4,7 +4,7 @@ import Foundation
import BigInt
import RealmSwift
public class Transaction: Object {
class Transaction: Object {
@objc dynamic var primaryKey: String = ""
@objc dynamic var chainId: Int = 0
@objc dynamic var id: String = ""
@ -92,16 +92,14 @@ public class Transaction: Object {
self.localizedOperations = list
}
public override static func primaryKey() -> String? {
override static func primaryKey() -> String? {
return "primaryKey"
}
var state: TransactionState {
return TransactionState(int: internalState)
}
}
extension Transaction {
var operation: LocalizedOperationObject? {
return localizedOperations.first
}
@ -211,27 +209,6 @@ public struct TransactionInstance: Equatable {
self.localizedOperations = localizedOperations
}
public init(transaction: Transaction) {
self.primaryKey = transaction.primaryKey
self.id = transaction.id
self.chainId = transaction.server.chainID
self.blockNumber = transaction.blockNumber
self.transactionIndex = transaction.transactionIndex
self.from = transaction.from
self.to = transaction.to
self.value = transaction.value
self.gas = transaction.gas
self.gasPrice = transaction.gasPrice
self.gasUsed = transaction.gasUsed
self.nonce = transaction.nonce
self.date = transaction.date
self.internalState = transaction.state.rawValue
self.isERC20Interaction = transaction.isERC20Interaction
self.localizedOperations = transaction.localizedOperations.map {
LocalizedOperationObjectInstance(object: $0)
}
}
public var state: TransactionState {
return TransactionState(int: internalState)
}
@ -265,3 +242,25 @@ public struct TransactionInstance: Equatable {
}
extension TransactionInstance {
init(transaction: Transaction) {
self.primaryKey = transaction.primaryKey
self.id = transaction.id
self.chainId = transaction.server.chainID
self.blockNumber = transaction.blockNumber
self.transactionIndex = transaction.transactionIndex
self.from = transaction.from
self.to = transaction.to
self.value = transaction.value
self.gas = transaction.gas
self.gasPrice = transaction.gasPrice
self.gasUsed = transaction.gasUsed
self.nonce = transaction.nonce
self.date = transaction.date
self.internalState = transaction.state.rawValue
self.isERC20Interaction = transaction.isERC20Interaction
self.localizedOperations = transaction.localizedOperations.map {
LocalizedOperationObjectInstance(object: $0)
}
}
}

Loading…
Cancel
Save