Skip to content

feat: add @metamask/foundryup package #5810

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

Draft
wants to merge 11 commits into
base: main
Choose a base branch
from
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,7 @@ scripts/coverage
!.yarn/versions

# typescript
packages/*/*.tsbuildinfo
packages/*/*.tsbuildinfo

# foundry cache
.metamask/cache
20 changes: 19 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,12 @@ Each package in this repository has its own README where you can find installati
- [`@metamask/chain-agnostic-permission`](packages/chain-agnostic-permission)
- [`@metamask/composable-controller`](packages/composable-controller)
- [`@metamask/controller-utils`](packages/controller-utils)
- [`@metamask/delegation-controller`](packages/delegation-controller)
- [`@metamask/earn-controller`](packages/earn-controller)
- [`@metamask/eip1193-permission-middleware`](packages/eip1193-permission-middleware)
- [`@metamask/ens-controller`](packages/ens-controller)
- [`@metamask/eth-json-rpc-provider`](packages/eth-json-rpc-provider)
- [`@metamask/foundryup`](packages/foundryup)
- [`@metamask/gas-fee-controller`](packages/gas-fee-controller)
- [`@metamask/json-rpc-engine`](packages/json-rpc-engine)
- [`@metamask/json-rpc-middleware-stream`](packages/json-rpc-middleware-stream)
Expand Down Expand Up @@ -87,10 +89,12 @@ linkStyle default opacity:0.5
chain_agnostic_permission(["@metamask/chain-agnostic-permission"]);
composable_controller(["@metamask/composable-controller"]);
controller_utils(["@metamask/controller-utils"]);
delegation_controller(["@metamask/delegation-controller"]);
earn_controller(["@metamask/earn-controller"]);
eip1193_permission_middleware(["@metamask/eip1193-permission-middleware"]);
ens_controller(["@metamask/ens-controller"]);
eth_json_rpc_provider(["@metamask/eth-json-rpc-provider"]);
foundryup(["@metamask/foundryup"]);
gas_fee_controller(["@metamask/gas-fee-controller"]);
json_rpc_engine(["@metamask/json-rpc-engine"]);
json_rpc_middleware_stream(["@metamask/json-rpc-middleware-stream"]);
Expand Down Expand Up @@ -136,30 +140,41 @@ linkStyle default opacity:0.5
assets_controllers --> network_controller;
assets_controllers --> permission_controller;
assets_controllers --> preferences_controller;
assets_controllers --> transaction_controller;
base_controller --> json_rpc_engine;
bridge_controller --> base_controller;
bridge_controller --> controller_utils;
bridge_controller --> gas_fee_controller;
bridge_controller --> multichain_network_controller;
bridge_controller --> polling_controller;
bridge_controller --> accounts_controller;
bridge_controller --> assets_controllers;
bridge_controller --> eth_json_rpc_provider;
bridge_controller --> network_controller;
bridge_controller --> remote_feature_flag_controller;
bridge_controller --> transaction_controller;
bridge_status_controller --> base_controller;
bridge_status_controller --> bridge_controller;
bridge_status_controller --> controller_utils;
bridge_status_controller --> polling_controller;
bridge_status_controller --> user_operation_controller;
bridge_status_controller --> accounts_controller;
bridge_status_controller --> bridge_controller;
bridge_status_controller --> gas_fee_controller;
bridge_status_controller --> network_controller;
bridge_status_controller --> transaction_controller;
chain_agnostic_permission --> controller_utils;
chain_agnostic_permission --> network_controller;
chain_agnostic_permission --> permission_controller;
composable_controller --> base_controller;
composable_controller --> json_rpc_engine;
delegation_controller --> base_controller;
delegation_controller --> accounts_controller;
delegation_controller --> keyring_controller;
earn_controller --> base_controller;
earn_controller --> controller_utils;
earn_controller --> accounts_controller;
earn_controller --> network_controller;
earn_controller --> transaction_controller;
eip1193_permission_middleware --> chain_agnostic_permission;
eip1193_permission_middleware --> controller_utils;
eip1193_permission_middleware --> json_rpc_engine;
Expand All @@ -183,10 +198,13 @@ linkStyle default opacity:0.5
multichain --> network_controller;
multichain --> permission_controller;
multichain_api_middleware --> chain_agnostic_permission;
multichain_api_middleware --> controller_utils;
multichain_api_middleware --> json_rpc_engine;
multichain_api_middleware --> network_controller;
multichain_api_middleware --> permission_controller;
multichain_api_middleware --> multichain_transactions_controller;
multichain_network_controller --> base_controller;
multichain_network_controller --> controller_utils;
multichain_network_controller --> accounts_controller;
multichain_network_controller --> keyring_controller;
multichain_network_controller --> network_controller;
Expand Down
1 change: 1 addition & 0 deletions packages/foundryup/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.metamask
1 change: 1 addition & 0 deletions packages/foundryup/.yarnrc.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
enableGlobalCache: false
10 changes: 10 additions & 0 deletions packages/foundryup/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# Changelog

All notable changes to this project will be documented in this file.

The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [Unreleased]

[Unreleased]: https://github.com/MetaMask/core/
20 changes: 20 additions & 0 deletions packages/foundryup/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
MIT License

Copyright (c) 2025 MetaMask

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
47 changes: 47 additions & 0 deletions packages/foundryup/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
# `@metamask/foundryup`

foundryup

## Installation

`yarn add @metamask/foundryup`

or

`npm install @metamask/foundryup`

## Usage

Once installed into a package you can do `yarn bin mm-foundryup`.

This will install the latest version of Foundry things by default.

Try `yarn bin mm-foundryup --help` for more options.

Once you have the binaries installed, you have to figure out how to get to them.

Probably best to just add each as a `package.json` script:

```json
"scripts": {
"anvil": "node_modules/.bin/anvil",
}
```

Kind of weird, but it seems to work okay. You can probably use `npx anvil` in place of `node_modules/.bin/anvil`, but
getting it to work in all scenarios (cross platform and in CI) wasn't straightforward. `yarn bin anvil` doesn't work
in yarn v4 because it isn't a bin of `@metamask/foundryup`, so yarn pretends it doesn't exist.

This all needs to work.

---

You can try it here in the monorepo by running `yarn workspace @metamask/foundryup anvil`.



## Contributing

This package is part of a monorepo. Instructions for contributing can be found in the [monorepo README](https://github.com/MetaMask/core#readme).


26 changes: 26 additions & 0 deletions packages/foundryup/jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*
* For a detailed explanation regarding each configuration property and type check, visit:
* https://jestjs.io/docs/configuration
*/

const merge = require('deepmerge');
const path = require('path');

const baseConfig = require('../../jest.config.packages');

const displayName = path.basename(__dirname);

module.exports = merge(baseConfig, {
// The display name when running multiple projects
displayName,

// An object that configures minimum threshold enforcement for coverage results
coverageThreshold: {
global: {
branches: 100,
functions: 100,
lines: 100,
statements: 100,
},
},
});
66 changes: 66 additions & 0 deletions packages/foundryup/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
{
"name": "@metamask/foundryup",
"version": "0.0.0",
"description": "foundryup",
"keywords": [
"MetaMask",
"Ethereum"
],
"homepage": "https://github.com/MetaMask/core/tree/main/packages/foundryup#readme",
"bugs": {
"url": "https://github.com/MetaMask/core/issues"
},
"repository": {
"type": "git",
"url": "https://github.com/MetaMask/core.git"
},
"license": "MIT",
"sideEffects": false,
"exports": {
"./package.json": "./package.json"
},
"bin": {
"mm-foundryup": "./dist/cli.mjs"
},
"files": [
"dist/"
],
"scripts": {
"anvil": "node_modules/.bin/anvil",
"build": "npx ts-bridge --project tsconfig.build.json --verbose --clean --no-references",
"build:docs": "typedoc",
"changelog:validate": "../../scripts/validate-changelog.sh @metamask/foundryup",
"publish:preview": "yarn npm publish --tag preview",
"test": "jest --reporters=jest-silent-reporter",
"test:clean": "jest --clearCache",
"test:verbose": "jest --verbose",
"test:watch": "jest --watch"
},
"devDependencies": {
"@metamask/auto-changelog": "^3.4.4",
"@types/jest": "^27.4.1",
"@types/unzipper": "^0.10.10",
"@types/yargs": "^17.0.32",
"@types/yargs-parser": "^21.0.3",
"deepmerge": "^4.2.2",
"jest": "^27.5.1",
"ts-jest": "^27.1.4",
"typedoc": "^0.24.8",
"typedoc-plugin-missing-exports": "^2.0.0",
"typescript": "~5.2.2"
},
"engines": {
"node": "^18.18 || >=20"
},
"publishConfig": {
"access": "public",
"registry": "https://registry.npmjs.org/"
},
"dependencies": {
"minipass": "^7.1.2",
"tar": "^7.4.3",
"unzipper": "^0.12.3",
"yargs": "17.7.2",
"yargs-parser": "^21.1.1"
}
}
9 changes: 9 additions & 0 deletions packages/foundryup/src/cli.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#!/usr/bin/env node

import { exit } from "node:process";
import { downloadAndInstallFoundryBinaries } from "./";

downloadAndInstallFoundryBinaries().catch((error) => {
console.error('Error:', error);
exit(1);
});
82 changes: 82 additions & 0 deletions packages/foundryup/src/download.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
import { request as httpRequest, type IncomingMessage } from 'node:http';
import { request as httpsRequest } from 'node:https';
import { Stream } from 'node:stream';
import { pipeline } from 'node:stream/promises';
import type { DownloadOptions } from './types';

class DownloadStream extends Stream.PassThrough {
async response(): Promise<IncomingMessage> {
return new Promise((resolve, reject) => {
this.once('response', resolve);
this.once('error', reject);
});
}
}

/**
* Starts a download from the given URL.
*
* @param url - The URL to download from
* @param options - The download options
* @param redirects - The number of redirects that have occurred
* @returns A stream of the download
*/

export function startDownload(
url: URL,
options: DownloadOptions = {},
redirects: number = 0,
) {
const MAX_REDIRECTS = options.maxRedirects ?? 5;
const request = url.protocol === 'http:' ? httpRequest : httpsRequest;
const stream = new DownloadStream();
request(url, options, async (response) => {
stream.once('close', () => {
response.destroy();
});

const { statusCode, statusMessage, headers } = response;
// handle redirects
if (
statusCode &&
statusCode >= 300 &&
statusCode < 400 &&
headers.location
) {
if (redirects >= MAX_REDIRECTS) {
stream.emit('error', new Error('Too many redirects'));
response.destroy();
} else {
// note: we don't emit a response until we're done redirecting, because
// handlers only expect it to be emitted once.
await pipeline(
startDownload(new URL(headers.location, url), options, redirects + 1)
// emit the response event to the stream
.once('response', stream.emit.bind(stream, 'response')),
stream,
).catch(stream.emit.bind(stream, 'error'));
response.destroy();
}
}

// check for HTTP errors
else if (!statusCode || statusCode < 200 || statusCode >= 300) {
stream.emit(
'error',
new Error(
`Request to ${url} failed. Status Code: ${statusCode} - ${statusMessage}`,
),
);
response.destroy();
} else {
// resolve with response stream
stream.emit('response', response);

response.once('error', stream.emit.bind(stream, 'error'));
await pipeline(response, stream).catch(stream.emit.bind(stream, 'error'));
}
})
.once('error', stream.emit.bind(stream, 'error'))
.end();
return stream;
}
Loading
Loading