Skip to content

Commit

Permalink
Fix auto-enable tokens for EVM accounts with lots of tokens
Browse files Browse the repository at this point in the history
  • Loading branch information
ealymbaev committed Oct 4, 2023
1 parent 907ff01 commit cabccf0
Showing 1 changed file with 31 additions and 34 deletions.
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import BigInt
import Combine
import Eip20Kit
import EvmKit
import Foundation
import RxSwift
import MarketKit
import HsExtensions
import HsToolKit
import EvmKit
import Eip20Kit
import UniswapKit
import MarketKit
import OneInchKit
import HsExtensions
import Combine
import BigInt
import RxSwift
import UniswapKit

class EvmAccountManager {
private let blockchainType: BlockchainType
Expand Down Expand Up @@ -82,28 +82,28 @@ class EvmAccountManager {

case let decoration as SwapDecoration:
switch decoration.tokenOut {
case .eip20Coin(let address, _): foundTokens.insert(FoundToken(tokenType: .eip20(address: address.hex), tokenInfo: decoration.tokenOut.tokenInfo))
case let .eip20Coin(address, _): foundTokens.insert(FoundToken(tokenType: .eip20(address: address.hex), tokenInfo: decoration.tokenOut.tokenInfo))
default: ()
}

case let decoration as OneInchSwapDecoration:
switch decoration.tokenOut {
case .eip20Coin(let address, _): foundTokens.insert(FoundToken(tokenType: .eip20(address: address.hex), tokenInfo: decoration.tokenOut.tokenInfo))
case let .eip20Coin(address, _): foundTokens.insert(FoundToken(tokenType: .eip20(address: address.hex), tokenInfo: decoration.tokenOut.tokenInfo))
default: ()
}

case let decoration as OneInchUnoswapDecoration:
if let tokenOut = decoration.tokenOut {
switch tokenOut {
case .eip20Coin(let address, _): foundTokens.insert(FoundToken(tokenType: .eip20(address: address.hex), tokenInfo: tokenOut.tokenInfo))
case let .eip20Coin(address, _): foundTokens.insert(FoundToken(tokenType: .eip20(address: address.hex), tokenInfo: tokenOut.tokenInfo))
default: ()
}
}

case let decoration as OneInchUnknownSwapDecoration:
if let tokenOut = decoration.tokenAmountOut?.token {
switch tokenOut {
case .eip20Coin(let address, _): foundTokens.insert(FoundToken(tokenType: .eip20(address: address.hex), tokenInfo: tokenOut.tokenInfo))
case let .eip20Coin(address, _): foundTokens.insert(FoundToken(tokenType: .eip20(address: address.hex), tokenInfo: tokenOut.tokenInfo))
default: ()
}
}
Expand Down Expand Up @@ -145,26 +145,26 @@ class EvmAccountManager {

do {
let queries = (foundTokens.map { $0.tokenType } + suspiciousTokenTypes).map { TokenQuery(blockchainType: blockchainType, tokenType: $0) }
let tokens = try marketKit.tokens(queries: queries)
let tokens = try queries.chunks(500).map { try marketKit.tokens(queries: $0) }.flatMap { $0 }

var tokenInfos = [TokenInfo]()

for foundToken in foundTokens {
if let token = tokens.first(where: { $0.type == foundToken.tokenType }) {
let tokenInfo = TokenInfo(
type: foundToken.tokenType,
coinName: token.coin.name,
coinCode: token.coin.code,
tokenDecimals: token.decimals
type: foundToken.tokenType,
coinName: token.coin.name,
coinCode: token.coin.code,
tokenDecimals: token.decimals
)

tokenInfos.append(tokenInfo)
} else if let tokenInfo = foundToken.tokenInfo {
let tokenInfo = TokenInfo(
type: foundToken.tokenType,
coinName: tokenInfo.tokenName,
coinCode: tokenInfo.tokenSymbol,
tokenDecimals: tokenInfo.tokenDecimal
type: foundToken.tokenType,
coinName: tokenInfo.tokenName,
coinCode: tokenInfo.tokenSymbol,
tokenDecimals: tokenInfo.tokenDecimal
)

tokenInfos.append(tokenInfo)
Expand All @@ -174,10 +174,10 @@ class EvmAccountManager {
for tokenType in suspiciousTokenTypes {
if let token = tokens.first(where: { $0.type == tokenType }) {
let tokenInfo = TokenInfo(
type: tokenType,
coinName: token.coin.name,
coinCode: token.coin.code,
tokenDecimals: token.decimals
type: tokenType,
coinName: token.coin.name,
coinCode: token.coin.code,
tokenDecimals: token.decimals
)

tokenInfos.append(tokenInfo)
Expand Down Expand Up @@ -247,21 +247,19 @@ class EvmAccountManager {

let enabledWallets = infos.map { info in
EnabledWallet(
tokenQueryId: TokenQuery(blockchainType: blockchainType, tokenType: info.type).id,
accountId: account.id,
coinName: info.coinName,
coinCode: info.coinCode,
tokenDecimals: info.tokenDecimals
tokenQueryId: TokenQuery(blockchainType: blockchainType, tokenType: info.type).id,
accountId: account.id,
coinName: info.coinName,
coinCode: info.coinCode,
tokenDecimals: info.tokenDecimals
)
}

walletManager.save(enabledWallets: enabledWallets)
}

}

extension EvmAccountManager {

struct TokenInfo {
let type: TokenType
let coinName: String
Expand All @@ -282,9 +280,8 @@ extension EvmAccountManager {
hasher.combine(tokenType)
}

static func ==(lhs: FoundToken, rhs: FoundToken) -> Bool {
static func == (lhs: FoundToken, rhs: FoundToken) -> Bool {
lhs.tokenType == rhs.tokenType
}
}

}

0 comments on commit cabccf0

Please sign in to comment.