Skip to content

Commit

Permalink
chore: bump safe-deployments (#936)
Browse files Browse the repository at this point in the history
improve check scripts
  • Loading branch information
dasanra authored Aug 9, 2024
1 parent fbc4957 commit 83ac125
Show file tree
Hide file tree
Showing 8 changed files with 220 additions and 61 deletions.
8 changes: 5 additions & 3 deletions packages/protocol-kit/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@
"SDK"
],
"scripts": {
"safe-deployments": "ts-node scripts/checkSafeDeployments.ts",
"check-safe-deployments": "ts-node scripts/safe-deployments/checkSafeDeployments.ts",
"update-safe-deployments": "ts-node scripts/safe-deployments/updateLocalNetworks.ts",
"check-short-name-integrity": "ts-node scripts/safe-deployments/checkShortNameIntegrity.ts",
"test": "mocha -r ts-node/register -r tsconfig-paths/register tests/unit/**/*.ts",
"test:hardhat:web3:v1.0.0": "export TEST_NETWORK=hardhat && export ETH_LIB=web3 && export SAFE_VERSION=1.0.0 && hardhat deploy && nyc hardhat test",
"test:hardhat:web3:v1.1.1": "export TEST_NETWORK=hardhat && export ETH_LIB=web3 && export SAFE_VERSION=1.1.1 && hardhat deploy && nyc hardhat test",
Expand All @@ -33,7 +35,7 @@
"format:check": "prettier --check \"*/**/*.{js,json,md,ts}\"",
"format": "prettier --write \"*/**/*.{js,json,md,ts}\"",
"unbuild": "rimraf dist artifacts deployments cache .nyc_output *.tsbuildinfo",
"build": "yarn unbuild && hardhat compile && yarn safe-deployments && tsc -p tsconfig.build.json && tsc-alias -p tsconfig.build.json"
"build": "yarn unbuild && hardhat compile && yarn check-safe-deployments && tsc -p tsconfig.build.json && tsc-alias -p tsconfig.build.json"
},
"repository": {
"type": "git",
Expand Down Expand Up @@ -74,7 +76,7 @@
"dependencies": {
"@noble/hashes": "^1.3.3",
"@safe-global/safe-core-sdk-types": "^5.0.3",
"@safe-global/safe-deployments": "^1.37.1",
"@safe-global/safe-deployments": "^1.37.3",
"abitype": "^1.0.2",
"ethereumjs-util": "^7.1.5",
"ethers": "^6.13.1",
Expand Down
48 changes: 0 additions & 48 deletions packages/protocol-kit/scripts/checkSafeDeployments.ts

This file was deleted.

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()
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()
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()
59 changes: 59 additions & 0 deletions packages/protocol-kit/scripts/safe-deployments/utils.ts
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
}
Loading

0 comments on commit 83ac125

Please sign in to comment.