-
Notifications
You must be signed in to change notification settings - Fork 150
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
move migrations to separate files, use TS (#1482)
- Loading branch information
Showing
13 changed files
with
327 additions
and
49 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import log from 'electron-log' | ||
|
||
import legacyMigrations from './migrations/legacy' | ||
import migration35 from './migrations/35' | ||
import migration36 from './migrations/36' | ||
|
||
import type { Migration } from './types' | ||
|
||
const migrations: Record<number, Migration> = { | ||
...legacyMigrations, | ||
...Object.fromEntries([ | ||
[35, migration35], | ||
[36, migration36] | ||
]) | ||
} | ||
|
||
// Version number of latest known migration | ||
const latest = Math.max(...Object.keys(migrations).map((n) => parseInt(n))) | ||
|
||
module.exports = { | ||
// Apply migrations to current state | ||
apply: (state: State, migrateToVersion = latest) => { | ||
state.main._version = state.main._version || 0 | ||
Object.keys(migrations) | ||
.sort((a, b) => parseInt(a) - parseInt(b)) | ||
.forEach((v) => { | ||
const version = parseInt(v) | ||
if (state.main._version < version && version <= migrateToVersion) { | ||
log.info(`Applying state migration: ${version}`) | ||
state = migrations[version](state) | ||
state.main._version = version | ||
} | ||
}) | ||
|
||
return state | ||
}, | ||
latest | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
function removeRpcConnection(connection: Connection, replaceWithPylon = true) { | ||
const isServiceRpc = connection.current === 'infura' || connection.current === 'alchemy' | ||
|
||
return { | ||
...connection, | ||
// turn off existing connections to Infura or Alchemy if they're not being replaced by Pylon | ||
on: connection.on && (!isServiceRpc || replaceWithPylon), | ||
current: isServiceRpc ? (replaceWithPylon ? 'pylon' : 'custom') : connection.current | ||
} | ||
} | ||
|
||
function updateChain(chain: Network, replaceWithPylon = true) { | ||
const { primary, secondary } = chain.connection | ||
|
||
const updatedChain = { | ||
...chain, | ||
connection: { | ||
...chain.connection, | ||
primary: removeRpcConnection(primary, replaceWithPylon), | ||
secondary: removeRpcConnection(secondary, replaceWithPylon) | ||
} | ||
} | ||
|
||
return updatedChain | ||
} | ||
|
||
export default function (initial: State) { | ||
const chains = Object.entries(initial.main.networks.ethereum) | ||
|
||
// migrate existing Infura and Alchemy connections to use Pylon where applicable | ||
const pylonChains = chains | ||
.filter(([id]) => ['1', '5', '10', '137', '42161', '11155111'].includes(id)) | ||
.map(([id, chain]) => [id, updateChain(chain)]) | ||
|
||
// these connections previously used Infura and Alchemy and are not supported by Pylon | ||
const retiredChains = chains | ||
.filter(([id]) => ['3', '4', '42'].includes(id)) | ||
.map(([id, chain]) => [id, updateChain(chain, false)]) | ||
|
||
initial.main.networks.ethereum = Object.fromEntries([...chains, ...pylonChains, ...retiredChains]) | ||
|
||
return initial | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
export default function (initial: State) { | ||
// remove Gnosis chain preset | ||
const removePoaConnection = (connection: Connection) => { | ||
const isPoa = connection.current === 'poa' | ||
|
||
return { | ||
...connection, | ||
current: isPoa ? 'custom' : connection.current, | ||
custom: isPoa ? 'https://rpc.gnosischain.com' : connection.custom | ||
} | ||
} | ||
|
||
const gnosis = initial.main.networks.ethereum[100] | ||
|
||
if (gnosis) { | ||
initial.main.networks.ethereum[100] = { | ||
...gnosis, | ||
connection: { | ||
primary: removePoaConnection(gnosis.connection.primary), | ||
secondary: removePoaConnection(gnosis.connection.secondary) | ||
} | ||
} | ||
} | ||
|
||
return initial | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export type Migration = (initialState: State) => State |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.