Skip to content

Commit

Permalink
Improve EnabledWallet migration
Browse files Browse the repository at this point in the history
  • Loading branch information
esen committed Aug 11, 2023
1 parent d815d1e commit b5911da
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 28 deletions.
6 changes: 6 additions & 0 deletions UnstoppableWallet/UnstoppableWallet.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -2308,6 +2308,8 @@
D02A67D5272A7460009B2C1C /* CoinTweetsService.swift in Sources */ = {isa = PBXBuildFile; fileRef = D02A67BA272A7460009B2C1C /* CoinTweetsService.swift */; };
D02A67D7272A7460009B2C1C /* CoinTweetsModule.swift in Sources */ = {isa = PBXBuildFile; fileRef = D02A67BB272A7460009B2C1C /* CoinTweetsModule.swift */; };
D02A67D8272A7460009B2C1C /* CoinTweetsModule.swift in Sources */ = {isa = PBXBuildFile; fileRef = D02A67BB272A7460009B2C1C /* CoinTweetsModule.swift */; };
D046016E2A8504F400194648 /* EnabledWallet_v_0_34.swift in Sources */ = {isa = PBXBuildFile; fileRef = D046016D2A8504F400194648 /* EnabledWallet_v_0_34.swift */; };
D046016F2A8504F400194648 /* EnabledWallet_v_0_34.swift in Sources */ = {isa = PBXBuildFile; fileRef = D046016D2A8504F400194648 /* EnabledWallet_v_0_34.swift */; };
D04D98EA268055A2001A3135 /* TransactionRecord.swift in Sources */ = {isa = PBXBuildFile; fileRef = D04D98E9268055A2001A3135 /* TransactionRecord.swift */; };
D04D98EB268055A2001A3135 /* TransactionRecord.swift in Sources */ = {isa = PBXBuildFile; fileRef = D04D98E9268055A2001A3135 /* TransactionRecord.swift */; };
D05E968D2A25D6C6002CCD71 /* Trc20Adapter.swift in Sources */ = {isa = PBXBuildFile; fileRef = D05E968C2A25D6C6002CCD71 /* Trc20Adapter.swift */; };
Expand Down Expand Up @@ -3793,6 +3795,7 @@
D02A67B9272A7460009B2C1C /* TweetsPageResponse.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = TweetsPageResponse.swift; sourceTree = "<group>"; };
D02A67BA272A7460009B2C1C /* CoinTweetsService.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = CoinTweetsService.swift; sourceTree = "<group>"; };
D02A67BB272A7460009B2C1C /* CoinTweetsModule.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = CoinTweetsModule.swift; sourceTree = "<group>"; };
D046016D2A8504F400194648 /* EnabledWallet_v_0_34.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = EnabledWallet_v_0_34.swift; sourceTree = "<group>"; };
D04D98E9268055A2001A3135 /* TransactionRecord.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = TransactionRecord.swift; sourceTree = "<group>"; };
D05E968C2A25D6C6002CCD71 /* Trc20Adapter.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Trc20Adapter.swift; sourceTree = "<group>"; };
D05E968F2A261D82002CCD71 /* TronTransactionAdapter.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TronTransactionAdapter.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -4238,6 +4241,7 @@
11B35DFA83DA24A00D73EA7D /* RestoreSettingRecord_v_0_25.swift */,
11B35EBD933DD3C9E72F1CA8 /* EnabledWallet_v_0_25.swift */,
11B357B2D07C69579BAEC997 /* CoinType.swift */,
D046016D2A8504F400194648 /* EnabledWallet_v_0_34.swift */,
);
path = Deprecated;
sourceTree = "<group>";
Expand Down Expand Up @@ -8412,6 +8416,7 @@
11B359C198AA7A141522E5E9 /* EvmAccountManagerFactory.swift in Sources */,
11B350918797E615D4FF6677 /* BlockchainSettingRecordStorage.swift in Sources */,
11B35B7132B99D12DC745064 /* BtcBlockchainSettingsModule.swift in Sources */,
D046016F2A8504F400194648 /* EnabledWallet_v_0_34.swift in Sources */,
11B35F6B92C2FB142E522828 /* BtcBlockchainSettingsViewModel.swift in Sources */,
11B3564C09AB663A2F146BE8 /* BtcBlockchainSettingsViewController.swift in Sources */,
11B35CC0D8AC06CE594F84DA /* BtcBlockchainSettingsService.swift in Sources */,
Expand Down Expand Up @@ -9661,6 +9666,7 @@
ABC9ABF99296DEA24FC5BFF0 /* SendAmountCautionService.swift in Sources */,
ABC9AB1E703AE57DF856ECD9 /* SendAmountCautionViewModel.swift in Sources */,
1A564E2897197EB14584A62E /* MarketOverviewViewModel.swift in Sources */,
D046016E2A8504F400194648 /* EnabledWallet_v_0_34.swift in Sources */,
11B3538CDAA5FE6682A661D0 /* EvmAccountManagerFactory.swift in Sources */,
11B35696E9CD808522BEFCD6 /* BlockchainSettingRecordStorage.swift in Sources */,
11B3581F4D975FC21B9A25F2 /* BtcBlockchainSettingsModule.swift in Sources */,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -658,15 +658,15 @@ class StorageMigrator {

try db.drop(table: EnabledWallet_v_0_25.databaseTableName)

try db.create(table: EnabledWallet.databaseTableName) { t in
t.column(EnabledWallet.Columns.tokenQueryId.name, .text).notNull()
t.column("coinSettingsId", .text).notNull()
t.column(EnabledWallet.Columns.accountId.name, .text).notNull()
t.column(EnabledWallet.Columns.coinName.name, .text)
t.column(EnabledWallet.Columns.coinCode.name, .text)
t.column(EnabledWallet.Columns.tokenDecimals.name, .integer)
try db.create(table: EnabledWallet_v_0_34.databaseTableName) { t in
t.column(EnabledWallet_v_0_34.Columns.tokenQueryId.name, .text).notNull()
t.column(EnabledWallet_v_0_34.Columns.coinSettingsId.name, .text).notNull()
t.column(EnabledWallet_v_0_34.Columns.accountId.name, .text).notNull()
t.column(EnabledWallet_v_0_34.Columns.coinName.name, .text)
t.column(EnabledWallet_v_0_34.Columns.coinCode.name, .text)
t.column(EnabledWallet_v_0_34.Columns.tokenDecimals.name, .integer)

t.primaryKey([EnabledWallet.Columns.tokenQueryId.name, EnabledWallet.Columns.accountId.name], onConflict: .replace)
t.primaryKey([EnabledWallet_v_0_34.Columns.tokenQueryId.name, EnabledWallet_v_0_34.Columns.coinSettingsId.name, EnabledWallet_v_0_34.Columns.accountId.name], onConflict: .replace)
}

for old in oldWallets {
Expand Down Expand Up @@ -753,23 +753,23 @@ class StorageMigrator {
}

var enabledWallets: [EnabledWallet] = []
let rows = try Row.fetchCursor(db, sql: "SELECT * FROM \(EnabledWallet.databaseTableName)")
let rows = try Row.fetchCursor(db, sql: "SELECT * FROM \(EnabledWallet_v_0_34.databaseTableName)")

while let row = try rows.next() {
guard let tokenQuery = tokenQuery(tokenQueryId: row["tokenQueryId"], coinSettingsId: row["coinSettingsId"]) else {
guard let tokenQueryId = tokenQueryId(tokenQueryId: row["tokenQueryId"], coinSettingsId: row["coinSettingsId"]) else {
continue
}

let updatedWallet = EnabledWallet(
tokenQueryId: tokenQuery.id,
tokenQueryId: tokenQueryId,
accountId: row["accountId"], coinName: row["coinName"],
coinCode: row["coinCode"], tokenDecimals: row["tokenDecimals"]
)

enabledWallets.append(updatedWallet)
}

try db.drop(table: EnabledWallet.databaseTableName)
try db.drop(table: EnabledWallet_v_0_34.databaseTableName)
try db.create(table: EnabledWallet.databaseTableName) { t in
t.column(EnabledWallet.Columns.tokenQueryId.name, .text).notNull()
t.column(EnabledWallet.Columns.accountId.name, .text).notNull()
Expand Down Expand Up @@ -812,17 +812,23 @@ class StorageMigrator {
}
}

private static func tokenQuery(tokenQueryId: String, coinSettingsId: String) -> TokenQuery? {
guard let tokenQuery = TokenQuery(id: tokenQueryId) else {
private static func tokenQueryId(tokenQueryId: String, coinSettingsId: String) -> String? {
let chunks = tokenQueryId.split(separator: "|").map { String($0) }

guard chunks.count == 2 else {
return nil
}

guard tokenQuery.tokenType == .native else {
return tokenQuery
let blockchainUid = chunks[0]
let tokenType = chunks[1]

let tokenTypeChunks = tokenType.split(separator: ":").map { String($0) }
guard tokenTypeChunks.count == 1 && tokenTypeChunks[0] == "native" else {
return tokenQueryId
}

switch tokenQuery.blockchainType {
case .bitcoin, .litecoin:
switch blockchainUid {
case "bitcoin", "litecoin":
let chunks = coinSettingsId.split(separator: "|")

for chunk in chunks {
Expand All @@ -832,16 +838,17 @@ class StorageMigrator {
continue
}

guard String(subChunks[0]) == "derivation",
let derivation = TokenType.Derivation(rawValue: String(subChunks[1])) else {
let settingsName = String(subChunks[0])
let derivation = String(subChunks[1])
guard settingsName == "derivation", ["bip44", "bip49", "bip84", "bip86"].contains(derivation) else {
continue
}

let tokenType = TokenType.derived(derivation: derivation)
return TokenQuery(blockchainType: tokenQuery.blockchainType, tokenType: tokenType)
return "\(blockchainUid)|derived:\(derivation)"
}

case .bitcoinCash:

case "bitcoin-cash":
let chunks = coinSettingsId.split(separator: "|")

for chunk in chunks {
Expand All @@ -851,17 +858,17 @@ class StorageMigrator {
continue
}

guard String(subChunks[0]) == "bitcoinCashCoinType",
let addressType = TokenType.AddressType(rawValue: String(subChunks[1])) else {
let settingsName = String(subChunks[0])
let addressType = String(subChunks[1])
guard settingsName == "bitcoinCashCoinType", ["type0", "type145"].contains(addressType) else {
continue
}

let tokenType = TokenType.addressType(type: addressType)
return TokenQuery(blockchainType: .bitcoinCash, tokenType: tokenType)
return "\(blockchainUid)|address_type:\(addressType)"
}

default:
return TokenQuery(blockchainType: tokenQuery.blockchainType, tokenType: .native)
return tokenQueryId
}

return nil
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import GRDB
import MarketKit

class EnabledWallet_v_0_34: Record {
let tokenQueryId: String
let coinSettingsId: String
let accountId: String

let coinName: String?
let coinCode: String?
let tokenDecimals: Int?

init(tokenQueryId: String, coinSettingsId: String, accountId: String, coinName: String? = nil, coinCode: String? = nil, tokenDecimals: Int? = nil) {
self.tokenQueryId = tokenQueryId
self.coinSettingsId = coinSettingsId
self.accountId = accountId
self.coinName = coinName
self.coinCode = coinCode
self.tokenDecimals = tokenDecimals
super.init()
}
override class var databaseTableName: String {
"enabled_wallets"
}

enum Columns: String, ColumnExpression {
case tokenQueryId, coinSettingsId, accountId, coinName, coinCode, tokenDecimals
}

required init(row: Row) {
tokenQueryId = row[Columns.tokenQueryId]
coinSettingsId = row[Columns.coinSettingsId]
accountId = row[Columns.accountId]
coinName = row[Columns.coinName]
coinCode = row[Columns.coinCode]
tokenDecimals = row[Columns.tokenDecimals]
super.init(row: row)
}

override func encode(to container: inout PersistenceContainer) {
container[Columns.tokenQueryId] = tokenQueryId
container[Columns.coinSettingsId] = coinSettingsId
container[Columns.accountId] = accountId
container[Columns.coinName] = coinName
container[Columns.coinCode] = coinCode
container[Columns.tokenDecimals] = tokenDecimals
}
}

0 comments on commit b5911da

Please sign in to comment.