Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix auto-enable tokens for EVM accounts with lots of tokens #5296

Merged
merged 1 commit into from
Oct 4, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
}
}

}