-
Notifications
You must be signed in to change notification settings - Fork 228
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
improve check scripts
- Loading branch information
Showing
8 changed files
with
220 additions
and
61 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 was deleted.
Oops, something went wrong.
26 changes: 26 additions & 0 deletions
26
packages/protocol-kit/scripts/safe-deployments/checkSafeDeployments.ts
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 @@ | ||
import { getLocalNetworksConfig, getSafeDeploymentNetworks } from './utils' | ||
|
||
/** | ||
* Checks if there are any differences between the local EIP-3770 network configurations and the safe deployment networks. | ||
* | ||
* @function checkConfigDiff | ||
* @returns {void} - nothing, just throws an error if there are any discrepancies | ||
*/ | ||
function checkConfigDiff() { | ||
const safeDeployments = getSafeDeploymentNetworks() | ||
const localNetworks = getLocalNetworksConfig() | ||
|
||
const chainIdsMissing = safeDeployments.filter((chainId) => !localNetworks.includes(chainId)) | ||
if (chainIdsMissing.length > 0) { | ||
const errorMessage = `EIP-3770 local config is missing chainIds: ${chainIdsMissing}\nPlease run 'yarn workspace @safe-global/protocol-kit update-safe-deployments` | ||
throw new Error(errorMessage) | ||
} | ||
|
||
const chainIdsExtra = localNetworks.filter((chainId) => !safeDeployments.includes(chainId)) | ||
if (chainIdsExtra.length > 0) { | ||
const errorMessage = `EIP-3770 local config has not required chainIds: ${chainIdsExtra}` | ||
throw new Error(errorMessage) | ||
} | ||
} | ||
|
||
checkConfigDiff() |
27 changes: 27 additions & 0 deletions
27
packages/protocol-kit/scripts/safe-deployments/checkShortNameIntegrity.ts
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,27 @@ | ||
import { getChainShortName } from './utils' | ||
import { networks } from '../../src/utils/eip-3770/config' | ||
|
||
/** | ||
* Checks the short names of local EIP-3770 networks to match their corresponding chain short names. | ||
* | ||
* @async | ||
* @function checkShortNameIntegrity | ||
* @returns {void} - nothing, just prints those chains where the name is not the same. | ||
*/ | ||
async function checkShortNameIntegrity() { | ||
for (const network of networks) { | ||
try { | ||
const shortName = await getChainShortName(network.chainId.toString()) | ||
if (network.shortName !== shortName) { | ||
// It just prints the chain that is not aligned so we can check if manual action is necessary. | ||
console.log(`Update ${network.chainId} from '${network.shortName}' to '${shortName}'`) | ||
} | ||
} catch (error) { | ||
console.log(`EIP-3770 Failed to retrieve chain name for ${network.chainId}`) | ||
} | ||
} | ||
|
||
console.log('Local network configuration checked successfully!') | ||
} | ||
|
||
checkShortNameIntegrity() |
77 changes: 77 additions & 0 deletions
77
packages/protocol-kit/scripts/safe-deployments/updateLocalNetworks.ts
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,77 @@ | ||
import fs from 'fs' | ||
import { getChainShortName, getLocalNetworksConfig, getSafeDeploymentNetworks } from './utils' | ||
import { networks } from '../../src/utils/eip-3770/config' | ||
|
||
interface NetworkShortName { | ||
shortName: string | ||
chainId: bigint | ||
} | ||
|
||
/** | ||
* Updates the local networks array in the configuration file. | ||
* | ||
* @param {NetworkShortName[]} networks - The full list of networks. | ||
*/ | ||
|
||
function updateLocalNetworks(networks: NetworkShortName[]) { | ||
const path = 'src/utils/eip-3770/config.ts' | ||
|
||
fs.readFile(path, (err, data) => { | ||
if (err) { | ||
console.error(err) | ||
return | ||
} | ||
|
||
const content = data.toString() | ||
const startIndex = | ||
content.indexOf('export const networks: NetworkShortName[] = [') + | ||
'export const networks: NetworkShortName[] = ['.length | ||
const endIndex = content.indexOf(']\n\nif (process.env.TEST_NETWORK ===') | ||
|
||
const sortedNetworks = networks | ||
.sort((a, b) => Number(a.chainId - b.chainId)) | ||
.map( | ||
(network, index) => | ||
` { chainId: ${network.chainId}n, shortName: '${network.shortName}' }${index === networks.length - 1 ? '' : ','}` | ||
) | ||
.join('\n') | ||
|
||
fs.writeFile( | ||
path, | ||
`${content.substring(0, startIndex)}\n${sortedNetworks}\n${content.substring(endIndex)}`, | ||
(err) => { | ||
if (err) { | ||
console.error(err) | ||
} else { | ||
console.log('Networks array updated successfully!') | ||
} | ||
} | ||
) | ||
}) | ||
} | ||
|
||
/** | ||
* Checks and updates the local networks configuration file. | ||
*/ | ||
async function checkAndUpdate() { | ||
const safeDeployments = getSafeDeploymentNetworks() | ||
const localNetworks = getLocalNetworksConfig() | ||
|
||
const chainIdsMissing = safeDeployments.filter((chainId) => !localNetworks.includes(chainId)) | ||
if (chainIdsMissing.length > 0) { | ||
const updateNetworks = [...networks] | ||
for (const chainId of chainIdsMissing) { | ||
try { | ||
const shortName = await getChainShortName(chainId) | ||
console.log(`Adding ${chainId} with shortName ${shortName}`) | ||
updateNetworks.push({ chainId: BigInt(chainId), shortName: shortName }) | ||
} catch (error) { | ||
throw new Error(`EIP-3770 Failed to retrieve chain name for ${chainId}`) | ||
} | ||
} | ||
|
||
updateLocalNetworks(updateNetworks) | ||
} | ||
} | ||
|
||
checkAndUpdate() |
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,59 @@ | ||
import axios from 'axios' | ||
import { SafeVersion } from '@safe-global/safe-core-sdk-types' | ||
import { DeploymentFilter, getSafeSingletonDeployment } from '@safe-global/safe-deployments' | ||
import { networks } from '../../src/utils/eip-3770/config' | ||
|
||
/** | ||
* Array of compatible Safe versions. | ||
*/ | ||
const compatibleSafeVersions: SafeVersion[] = ['1.0.0', '1.1.1', '1.2.0', '1.3.0', '1.4.1'] | ||
|
||
/** | ||
* Gets the network IDs where the Safe smart contracts were deployed and added to @safe-global/safe-deployments. | ||
* | ||
* @returns {string[]} Array of network IDs | ||
*/ | ||
export function getSafeDeploymentNetworks(): string[] { | ||
const allDeployedNetworks = compatibleSafeVersions.reduce((acc: string[], safeVersion) => { | ||
const filters: DeploymentFilter = { version: safeVersion, released: true } | ||
const singletons = getSafeSingletonDeployment(filters) | ||
|
||
if (!singletons) { | ||
return acc | ||
} | ||
|
||
return acc.concat(Object.keys(singletons.networkAddresses)) | ||
}, []) | ||
|
||
const uniqueDeployedNetworks = [...new Set(allDeployedNetworks)] | ||
|
||
if (!uniqueDeployedNetworks) { | ||
throw new Error('Empty Safe Deployments') | ||
} | ||
return uniqueDeployedNetworks | ||
} | ||
|
||
/** | ||
* Gets the local EIP-3770 network configurations. | ||
* | ||
* @returns {string[]} Array of network IDs | ||
*/ | ||
export function getLocalNetworksConfig(): string[] { | ||
return networks.map((network) => network.chainId.toString()) | ||
} | ||
|
||
/** | ||
* Retrieves the chain shortName for a given chain ID from the Ethereum Lists repo. | ||
* | ||
* @param {string} chainId The chain ID to retrieve the name for | ||
* @returns {Promise<string>} A promise that resolves with the chain shortName | ||
*/ | ||
export async function getChainShortName(chainId: string): Promise<string> { | ||
const response = await axios.get( | ||
`https://raw.githubusercontent.com/ethereum-lists/chains/master/_data/chains/eip155-${chainId.toString()}.json` | ||
) | ||
if (!response.data.shortName) { | ||
throw new Error('Failed to retrieve chain shortName') | ||
} | ||
return response.data.shortName | ||
} |
Oops, something went wrong.