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

Vendor Safe Deployments at Build Time #61

Merged
merged 2 commits into from
Sep 22, 2024
Merged

Conversation

bh2smith
Copy link
Collaborator

@bh2smith bh2smith commented Sep 22, 2024

User description

This reduces the number of reads to safe repos on every adapter initialization, by fetching and writing the contract deployment data directly into the project build. These artifacts are now a constant in the project.


PR Type

enhancement, configuration changes, tests


Description

  • Added new scripts to fetch and process Safe and module deployments, and to write deployment data to a file.
  • Refactored SafeContractSuite to use pre-fetched deployment data instead of fetching it dynamically.
  • Updated NearSafe class and tests to use the new SafeContractSuite initialization.
  • Added types for Safe deployments and deployment data.
  • Updated build scripts and dependencies in package.json.
  • Updated TypeScript configuration to include new script.

Changes walkthrough 📝

Relevant files
Enhancement
6 files
fetch-deployments.ts
Add script to fetch and process Safe and module deployments

scripts/fetch-deployments.ts

  • Added script to fetch and process Safe and module deployments.
  • Defined deployment versions and chain ID.
  • Implemented functions to get and fetch deployments.
  • +82/-0   
    safe-deployments.ts
    Add script to fetch and write deployment data                       

    scripts/safe-deployments.ts

  • Added script to fetch and write deployment data to a file.
  • Implemented main function to generate TypeScript constants.
  • +78/-0   
    deployments.ts
    Add auto-generated deployment data file                                   

    src/_gen/deployments.ts

  • Added auto-generated file containing deployment data as TypeScript
    constants.
  • +1889/-0
    safe.ts
    Refactor SafeContractSuite to use pre-fetched deployment data

    src/lib/safe.ts

  • Refactored SafeContractSuite to use pre-fetched deployment data.
  • Removed dynamic fetching of deployment data.
  • +16/-102
    near-safe.ts
    Update NearSafe to use new SafeContractSuite initialization

    src/near-safe.ts

  • Updated NearSafe class to use the new SafeContractSuite
    initialization.
  • +2/-4     
    types.ts
    Add types for Safe deployments and deployment data             

    src/types.ts

    • Added types for Safe deployments and deployment data.
    +14/-1   
    Tests
    1 files
    safe.spec.ts
    Update tests for new ViemPack initialization                         

    tests/lib/safe.spec.ts

    • Updated tests to use the new ViemPack initialization.
    +1/-1     
    Configuration changes
    2 files
    package.json
    Update build scripts and dependencies                                       

    package.json

  • Added new build script for fetching and generating deployment data.
  • Moved Safe dependencies to devDependencies.
  • +4/-3     
    tsconfig.json
    Update TypeScript configuration to include new script       

    tsconfig.json

    • Included new script file in TypeScript configuration.
    +1/-1     

    💡 PR-Agent usage:
    Comment /help on the PR to get a list of all available PR-Agent tools and their descriptions

    @mintbase-codium-pr-agent
    Copy link

    PR Reviewer Guide 🔍

    ⏱️ Estimated effort to review: 3 🔵🔵🔵⚪⚪
    🧪 No relevant tests
    🔒 No security concerns identified
    ⚡ Key issues to review

    Error Handling
    The function fetchDeployments lacks comprehensive error handling for asynchronous operations within the Promise.all. Consider adding more specific error handling for each individual promise to provide clearer debugging and fault isolation.

    Hardcoded Values
    The chain ID '11155111' is hardcoded in multiple places. Consider defining it as a constant or making it configurable to enhance flexibility and maintainability.

    Error Handling
    The error handling in fetchAndWriteDeployments function could be improved by providing more detailed error messages or handling specific types of errors differently to aid in troubleshooting.

    @bh2smith bh2smith merged commit 34dad57 into main Sep 22, 2024
    1 check passed
    @bh2smith bh2smith deleted the vendor-safe-deployments branch September 22, 2024 13:58
    @mintbase-codium-pr-agent
    Copy link

    PR Code Suggestions ✨

    CategorySuggestion                                                                                                                                    Score
    Error handling
    Add error handling to Promise.all for better error management

    Add error handling for the Promise.all call to manage individual promise rejections
    and provide more detailed error information.

    scripts/fetch-deployments.ts [54-58]

     const [singleton, proxyFactory, moduleSetup, m4337] = await Promise.all([
    -  safeDeployment(getSafeL2SingletonDeployment),
    -  safeDeployment(getProxyFactoryDeployment),
    -  m4337Deployment(getSafeModuleSetupDeployment),
    -  m4337Deployment(getSafe4337ModuleDeployment),
    +  safeDeployment(getSafeL2SingletonDeployment).catch(e => { throw new Error(`Failed to fetch singleton: ${e}`); }),
    +  safeDeployment(getProxyFactoryDeployment).catch(e => { throw new Error(`Failed to fetch proxyFactory: ${e}`); }),
    +  m4337Deployment(getSafeModuleSetupDeployment).catch(e => { throw new Error(`Failed to fetch moduleSetup: ${e}`); }),
    +  m4337Deployment(getSafe4337ModuleDeployment).catch(e => { throw new Error(`Failed to fetch m4337: ${e}`); }),
     ]);
     
    Suggestion importance[1-10]: 9

    Why: Adding error handling for individual promises within Promise.all enhances robustness and provides more detailed error information, which is crucial for debugging and reliability.

    9
    Best practice
    Enhance the immutability of ABI definitions by using the readonly modifier

    To ensure that the ABI definitions are immutable and cannot be altered at runtime,
    consider marking the abi array as readonly. This change will enforce immutability at
    the type level.

    src/_gen/deployments.ts [7-20]

    -abi: [
    +readonly abi: [
       {
         anonymous: false,
         inputs: [
           {
             indexed: true,
             internalType: "address",
             name: "owner",
             type: "address",
           },
         ],
         name: "AddedOwner",
         type: "event",
       },
     
    Suggestion importance[1-10]: 8

    Why: Marking the abi array as readonly enforces immutability, which is a good practice for ensuring that the ABI definitions cannot be altered at runtime. This is a significant improvement for maintaining the integrity of the ABI.

    8
    Improve type safety and readability by using a more specific type for event input names

    Consider using a more specific type for the name field in the inputs array of the
    event definitions. Using a string type like "address" or "bytes32" directly can help
    avoid potential type mismatches and improve code readability.

    src/_gen/deployments.ts [13-14]

    -name: "owner",
    +name: "owner" as const,
     type: "address",
     
    Suggestion importance[1-10]: 7

    Why: Using as const for the name field improves type safety and readability, but it is a minor improvement and not critical for functionality.

    7
    Initialize class properties directly in the constructor for safer usage

    Consider initializing SAFE_DEPLOYMENTS directly in the constructor to ensure it is
    always defined before use, which can prevent potential runtime errors if
    SAFE_DEPLOYMENTS is accessed before the constructor runs.

    src/lib/safe.ts [43]

    -const deployments = SAFE_DEPLOYMENTS;
    +this.deployments = SAFE_DEPLOYMENTS;
     
    Suggestion importance[1-10]: 7

    Why: Initializing SAFE_DEPLOYMENTS directly in the constructor can prevent potential runtime errors, but the current code already initializes it immediately within the constructor, so the improvement is minor.

    7
    Maintainability
    Use a constant or configurable option for chain ID to enhance code flexibility

    Replace the hardcoded chain ID in getClient with a configurable option or constant
    to enhance flexibility and maintainability of the code.

    src/lib/safe.ts [42]

    -this.dummyClient = getClient(11155111);
    +this.dummyClient = getClient(DEFAULT_CHAIN_ID);
     
    Suggestion importance[1-10]: 8

    Why: Replacing the hardcoded chain ID with a configurable option improves maintainability and flexibility, making the code easier to adapt to different environments.

    8
    Reduce redundancy and improve manageability by extracting common event input patterns into a separate type

    For better code organization and potential reuse, consider extracting the repeated
    event input patterns into a separate type or interface. This approach can reduce
    redundancy and make the ABI easier to manage and update.

    src/_gen/deployments.ts [11-16]

    -inputs: [
    -  {
    -    indexed: true,
    -    internalType: "address",
    -    name: "owner",
    -    type: "address",
    -  },
    -],
    +type EventInputAddress = {
    +  indexed: true,
    +  internalType: "address",
    +  name: "owner",
    +  type: "address",
    +};
     
    +inputs: [EventInputAddress],
    +
    Suggestion importance[1-10]: 6

    Why: Extracting repeated event input patterns into a separate type reduces redundancy and improves code maintainability. However, it is a minor refactor and does not address any critical issues.

    6
    Possible issue
    Prevent runtime errors by adding explicit type assertions in the ABI definitions

    To avoid potential runtime errors and ensure that the ABI matches expected types,
    consider adding explicit type assertions or checks where the ABI is consumed. This
    can prevent issues when interacting with the blockchain.

    src/_gen/deployments.ts [20]

    -type: "event",
    +type: "event" as const,
     
    Suggestion importance[1-10]: 7

    Why: Adding as const for the type field in the ABI definitions helps prevent potential runtime errors by ensuring type correctness. This is a useful improvement for type safety.

    7
    Readability
    Improve variable naming for clarity and maintainability

    Use a more descriptive variable name than tsContent for the TypeScript content
    string to improve code readability.

    scripts/safe-deployments.ts [35]

    -const tsContent = `
    +const deploymentConstantsScript = `
     
    Suggestion importance[1-10]: 6

    Why: Using a more descriptive variable name improves readability and maintainability, but the impact on the overall code quality is relatively minor.

    6

    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Projects
    None yet
    Development

    Successfully merging this pull request may close these issues.

    1 participant