Commit 0daed587 authored by smartcontracts's avatar smartcontracts Committed by GitHub

Add plugins package (#6)

* Add plugins package

* chore(plugins): make consistent with tsconfigs

* chore: remove unnecessary deps
Co-authored-by: default avatarGeorgios Konstantopoulos <me@gakonst.com>
parent 197fd746
(The MIT License)
Copyright 2020 Optimism
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 SOFTWARE.
# @eth-optimism/plugins
## What is this?
A collection of useful JavaScript/TypeScript tooling plugins that you might need when building on top of Optimistic Ethereum!
## What kind of plugins are we talking about?
### Plugins for [`hardhat`](https://hardhat.org)
#### `@eth-optimism/plugins/hardhat/compiler`
Automatically compiles your Solidity contracts with the OVM compiler.
Defaults to Solidity version 0.7.6, but also has support for 0.5.16 and 0.6.12.
[Full README available here](./src/hardhat/compiler).
{
"name": "@eth-optimism/plugins",
"version": "1.0.0-alpha.2",
"main": "dist/index",
"types": "dist/index",
"files": [
"hardhat/",
"*.d.ts",
"*.d.ts.map",
"*.js",
"*.js.map",
"LICENSE.txt",
"README.md"
],
"license": "MIT",
"scripts": {
"clean": "rimraf hardhat *.d.ts *.map *.js tsconfig.tsbuildinfo",
"build": "tsc -p tsconfig.build.json",
"lint": "yarn run lint:fix && yarn run lint:check",
"lint:check": "tslint --format stylish --project .",
"lint:fix": "prettier --config prettier-config.json --write \"{src,test}/**/*.ts\""
},
"dependencies": {
"node-fetch": "^2.6.1"
},
"devDependencies": {
"@types/mocha": "^8.2.2",
"hardhat": "^2.1.2"
}
}
../../prettier-config.json
\ No newline at end of file
# @eth-optimism/plugins/hardhat/compiler
A plugin that brings OVM compiler support to Hardhat projects.
## Installation
Installation is super simple.
First, grab the package.
Via `npm`:
```
npm install @eth-optimism/plugins
```
Via `yarn`:
```
yarn add @eth-optimism/plugins
```
Next, import the plugin inside your `hardhat.config.js`:
```js
// hardhat.config.js
require("@eth-optimism/plugins/hardhat/compiler")
```
Or if using TypeScript:
```ts
// hardhat.config.ts
import "@eth-optimism/plugins/hardhat/compiler"
```
## Configuration
**By default, this plugin will use OVM compiler version 0.7.6**.
Configure this plugin by adding an `ovm` field to your Hardhat config:
```js
// hardhat.config.js
require("@eth-optimism/plugins/hardhat/compiler")
module.exports = {
ovm: {
solcVersion: 'X.Y.Z' // Your version goes here.
}
}
```
Has typings so it won't break your Hardhat config if you're using TypeScript.
/* Imports: External */
import * as fs from 'fs'
import * as path from 'path'
import fetch from 'node-fetch'
import { subtask, extendEnvironment } from 'hardhat/config'
import { getCompilersDir } from 'hardhat/internal/util/global-dir'
import { Artifacts } from 'hardhat/internal/artifacts'
import {
TASK_COMPILE_SOLIDITY_RUN_SOLCJS,
TASK_COMPILE_SOLIDITY_RUN_SOLC,
} from 'hardhat/builtin-tasks/task-names'
/* Imports: Internal */
import './type-extensions'
const OPTIMISM_SOLC_BIN_URL =
'https://raw.githubusercontent.com/ethereum-optimism/solc-bin/gh-pages/bin'
// I figured this was a reasonably modern default, but not sure if this is too new. Maybe we can
// default to 0.6.X instead?
const DEFAULT_OVM_SOLC_VERSION = '0.7.6'
/**
* Find or generate an OVM soljson.js compiler file and return the path of this file.
* We pass the path to this file into hardhat.
* @param version Solidity compiler version to get a path for in the format `X.Y.Z`.
* @return Path to the downloaded soljson.js file.
*/
const getOvmSolcPath = async (version: string): Promise<string> => {
// If __DANGEROUS_OVM_IGNORE_ERRORS__ env var is not undefined we append the -no-errors suffix to the solc version.
if (process.env.__DANGEROUS_OVM_IGNORE_ERRORS__) {
console.log('\n\n__DANGEROUS_OVM_IGNORE_ERRORS__ IS ENABLED!\n\n')
version += '-no_errors'
}
// First, check to see if we've already downloaded this file. Hardhat gives us a folder to use as
// a compiler cache, so we'll just be nice and use an `ovm` subfolder.
const ovmCompilersCache = path.join(await getCompilersDir(), 'ovm')
// Need to create the OVM compiler cache folder if it doesn't already exist.
if (!fs.existsSync(ovmCompilersCache))
[fs.mkdirSync(ovmCompilersCache, { recursive: true })]
// Check to see if we already have this compiler version downloaded. We store the cached files at
// `X.Y.Z.js`. If it already exists, just return that instead of downloading a new one.
const cachedCompilerPath = path.join(ovmCompilersCache, `${version}.js`)
if (fs.existsSync(cachedCompilerPath)) {
return cachedCompilerPath
}
console.log(`Downloading OVM compiler version ${version}`)
// We don't have a cache, so we'll download this file from GitHub. Currently stored at
// ethereum-optimism/solc-bin.
const compilerContentResponse = await fetch(
OPTIMISM_SOLC_BIN_URL + `/soljson-v${version}.js`
)
// Throw if this request failed, e.g., 404 because of an invalid version.
if (!compilerContentResponse.ok) {
throw new Error(
`Unable to download OVM compiler version ${version}. Are you sure that version exists?`
)
}
// Otherwise, write the content to the cache. We probably want to do some sort of hash
// verification against these files but it's OK for now. The real "TODO" here is to instead
// figure out how to properly extend and/or hack Hardat's CompilerDownloader class.
const compilerContent = await compilerContentResponse.text()
fs.writeFileSync(cachedCompilerPath, compilerContent)
return cachedCompilerPath
}
subtask(
TASK_COMPILE_SOLIDITY_RUN_SOLC,
async (args: { input: any; solcPath: string }, hre, runSuper) => {
if ((hre.network as any).ovm !== true) {
return runSuper(args)
}
// Just some silly sanity checks, make sure we have a solc version to download. Our format is
// `X.Y.Z` (for now).
let ovmSolcVersion = DEFAULT_OVM_SOLC_VERSION
if (hre.config?.ovm?.solcVersion) {
ovmSolcVersion = hre.config.ovm.solcVersion
}
// Get a path to a soljson file.
const ovmSolcPath = await getOvmSolcPath(ovmSolcVersion)
// These objects get fed into the compiler. We're creating two of these because we need to
// throw one into the OVM compiler and another into the EVM compiler. Users are able to prevent
// certain files from being compiled by the OVM compiler by adding "// @unsupported: ovm"
// somewhere near the top of their file.
const ovmInput = {
language: 'Solidity',
sources: {},
settings: args.input.settings,
}
// Separate the EVM and OVM inputs.
for (const file of Object.keys(args.input.sources)) {
// Ignore any contract that has this tag.
if (!args.input.sources[file].content.includes('// @unsupported: ovm')) {
ovmInput.sources[file] = args.input.sources[file]
}
}
// Build both inputs separately.
const ovmOutput = await hre.run(TASK_COMPILE_SOLIDITY_RUN_SOLCJS, {
input: ovmInput,
solcJsPath: ovmSolcPath,
})
// Just doing this to add some extra useful information to any errors in the OVM compiler output.
ovmOutput.errors = (ovmOutput.errors || []).map((error: any) => {
if (error.severity === 'error') {
error.formattedMessage = `OVM Compiler Error (insert "// @unsupported: ovm" if you don't want this file to be compiled for the OVM):\n ${error.formattedMessage}`
}
return error
})
return ovmOutput
}
)
extendEnvironment((hre) => {
if (process.env.TARGET === 'ovm') {
;(hre.network as any).ovm = true
// Quick check to make sure we don't accidentally perform this transform multiple times.
let artifactsPath = hre.config.paths.artifacts
if (!artifactsPath.endsWith('-ovm')) {
artifactsPath = artifactsPath + '-ovm'
}
let cachePath = hre.config.paths.cache
if (!cachePath.endsWith('-ovm')) {
cachePath = cachePath + '-ovm'
}
// Forcibly update the artifacts object.
hre.config.paths.artifacts = artifactsPath
hre.config.paths.cache = cachePath
;(hre as any).artifacts = new Artifacts(artifactsPath)
;(hre.network as any).ovm = true
}
})
import 'hardhat/types/config'
declare module 'hardhat/types/config' {
interface HardhatUserConfig {
ovm?: {
solcVersion?: string
}
}
interface HardhatConfig {
ovm?: {
solcVersion?: string
}
}
}
export * from './hardhat'
{
"extends": "../tsconfig.json",
"compilerOptions": {
"outDir": "../",
"rootDirs": ["."],
"composite": true
},
"include": [
"./**/*.ts"
],
"exclude": []
}
{
"extends": "../../tsconfig.build.json",
"compilerOptions": {
"outDir": "./dist"
},
"include": [
"src/**/*"
]
}
{
"extends": "../../tsconfig.json"
}
{
"extends": "../../tslint.base.json"
}
......@@ -29,7 +29,6 @@
},
"devDependencies": {
"@nomiclabs/ethereumjs-vm": "4.2.2",
"@eth-optimism/dev": "^1.1.1",
"@nomiclabs/hardhat-ethers": "^2.0.2",
"@nomiclabs/hardhat-waffle": "^2.0.1",
"@types/lodash": "^4.14.161",
......
......@@ -39,27 +39,6 @@
resolved "https://registry.yarnpkg.com/@ensdomains/resolver/-/resolver-0.2.4.tgz#c10fe28bf5efbf49bff4666d909aed0265efbc89"
integrity sha512-bvaTH34PMCbv6anRa9I/0zjLJgY4EuznbEMgbV77JBCQ9KNC46rzi0avuxpOfu+xDjPEtSFGqVEOr5GlUSGudA==
"@eth-optimism/dev@^1.1.1":
version "1.1.1"
resolved "https://registry.yarnpkg.com/@eth-optimism/dev/-/dev-1.1.1.tgz#7bae95b975c1d6641b4ae550cb3ec631c667a56b"
integrity sha512-BiKvjL8VoS2OsPHobyTe533XoZkYYBKUKhw9tkHskylD+s+/UwcgkkimrxQ67aJgLE22GW9YCdF2eeB3UcNwgA==
dependencies:
"@types/chai" "^4.2.15"
"@types/chai-as-promised" "^7.1.3"
"@types/mocha" "^8.2.0"
"@types/node" "^14.14.27"
chai "^4.3.0"
chai-as-promised "^7.1.1"
mocha "^8.3.0"
prettier "^2.2.1"
rimraf "^3.0.2"
ts-node "^9.1.1"
tslint "^6.1.3"
tslint-config-prettier "^1.18.0"
tslint-no-focused-test "^0.5.0"
tslint-plugin-prettier "^2.3.0"
typescript "^4.1.5"
"@ethereum-waffle/chai@^3.3.0":
version "3.3.1"
resolved "https://registry.yarnpkg.com/@ethereum-waffle/chai/-/chai-3.3.1.tgz#3f20b810d0fa516f19af93c50c3be1091333fa8e"
......@@ -1505,14 +1484,7 @@
dependencies:
"@types/node" "*"
"@types/chai-as-promised@^7.1.3":
version "7.1.3"
resolved "https://registry.yarnpkg.com/@types/chai-as-promised/-/chai-as-promised-7.1.3.tgz#779166b90fda611963a3adbfd00b339d03b747bd"
integrity sha512-FQnh1ohPXJELpKhzjuDkPLR2BZCAqed+a6xV4MI/T3XzHfd2FlarfUGUdZYgqYe8oxkYn0fchHEeHfHqdZ96sg==
dependencies:
"@types/chai" "*"
"@types/chai@*", "@types/chai@^4.2.15":
"@types/chai@*":
version "4.2.15"
resolved "https://registry.yarnpkg.com/@types/chai/-/chai-4.2.15.tgz#b7a6d263c2cecf44b6de9a051cf496249b154553"
integrity sha512-rYff6FI+ZTKAPkJUoyz7Udq3GaoDZnxYDEvdEdFZASiA7PoErltHezDishqQiSDWrGxvxmplH304jyzQmjp0AQ==
......@@ -1549,7 +1521,7 @@
dependencies:
"@types/node" "*"
"@types/mocha@^8.2.0":
"@types/mocha@^8.2.2":
version "8.2.2"
resolved "https://registry.yarnpkg.com/@types/mocha/-/mocha-8.2.2.tgz#91daa226eb8c2ff261e6a8cbf8c7304641e095e0"
integrity sha512-Lwh0lzzqT5Pqh6z61P3c3P5nm6fzQK/MMHl9UKeneAeInVflBSz1O2EkX6gM6xfJd7FBXBY5purtLx7fUiZ7Hw==
......@@ -1572,11 +1544,6 @@
resolved "https://registry.yarnpkg.com/@types/node/-/node-12.20.6.tgz#7b73cce37352936e628c5ba40326193443cfba25"
integrity sha512-sRVq8d+ApGslmkE9e3i+D3gFGk7aZHAT+G4cIpIEdLJYPsWiSPwcAnJEjddLQQDqV3Ra2jOclX/Sv6YrvGYiWA==
"@types/node@^14.14.27":
version "14.14.36"
resolved "https://registry.yarnpkg.com/@types/node/-/node-14.14.36.tgz#5637905dbb15c30a33a3c65b9ef7c20e3c85ebad"
integrity sha512-kjivUwDJfIjngzbhooRnOLhGYz6oRFi+L+EpMjxroDYXwDw9lHrJJ43E+dJ6KAd3V3WxWAJ/qZE9XKYHhjPOFQ==
"@types/normalize-package-data@^2.4.0":
version "2.4.0"
resolved "https://registry.yarnpkg.com/@types/normalize-package-data/-/normalize-package-data-2.4.0.tgz#e486d0d97396d79beedd0a6e33f4534ff6b4973e"
......@@ -1871,11 +1838,6 @@ are-we-there-yet@~1.1.2:
delegates "^1.0.0"
readable-stream "^2.0.6"
arg@^4.1.0:
version "4.1.3"
resolved "https://registry.yarnpkg.com/arg/-/arg-4.1.3.tgz#269fc7ad5b8e42cb63c896d5666017261c144089"
integrity sha512-58S9QDqG0Xx27YwPSt9fJxivjYl432YCwfDMfZ+71RAqUrZef7LrKQZ3LHLOwCS4FLNBplP533Zx895SeOCHvA==
argparse@^1.0.7:
version "1.0.10"
resolved "https://registry.yarnpkg.com/argparse/-/argparse-1.0.10.tgz#bcd6791ea5ae09725e17e5ad988134cd40b3d911"
......@@ -3005,13 +2967,6 @@ caseless@~0.12.0:
resolved "https://registry.yarnpkg.com/caseless/-/caseless-0.12.0.tgz#1b681c21ff84033c826543090689420d187151dc"
integrity sha1-G2gcIf+EAzyCZUMJBolCDRhxUdw=
chai-as-promised@^7.1.1:
version "7.1.1"
resolved "https://registry.yarnpkg.com/chai-as-promised/-/chai-as-promised-7.1.1.tgz#08645d825deb8696ee61725dbf590c012eb00ca0"
integrity sha512-azL6xMoi+uxu6z4rhWQ1jbdUhOMhis2PvscD/xjLqNMkv3BPPp2JyyuTHOrf9BOosGpNQ11v6BKv/g57RXbiaA==
dependencies:
check-error "^1.0.2"
chai@^4.3.0:
version "4.3.4"
resolved "https://registry.yarnpkg.com/chai/-/chai-4.3.4.tgz#b55e655b31e1eac7099be4c08c21964fce2e6c49"
......@@ -3555,11 +3510,6 @@ create-hmac@^1.1.0, create-hmac@^1.1.4, create-hmac@^1.1.7:
safe-buffer "^5.0.1"
sha.js "^2.4.8"
create-require@^1.1.0:
version "1.1.1"
resolved "https://registry.yarnpkg.com/create-require/-/create-require-1.1.1.tgz#c1d7e8f1e5f6cfc9ff65f9cd352d37348756c333"
integrity sha512-dcKFX3jn0MpIaXjisoRvexIJVEKzaq7z2rZKxf+MSr9TkdmHmsU4m2lcLojrj/FHl8mk5VxMmYA+ftRkP/3oKQ==
cross-fetch@^2.1.0, cross-fetch@^2.1.1:
version "2.2.3"
resolved "https://registry.yarnpkg.com/cross-fetch/-/cross-fetch-2.2.3.tgz#e8a0b3c54598136e037f8650f8e823ccdfac198e"
......@@ -5354,7 +5304,7 @@ hard-rejection@^2.1.0:
resolved "https://registry.yarnpkg.com/hard-rejection/-/hard-rejection-2.1.0.tgz#1c6eda5c1685c63942766d79bb40ae773cecd883"
integrity sha512-VIZB+ibDhx7ObhAe7OVtoEbuP4h/MuOTHJ+J8h/eBXotJYl0fBgR72xDFCKgIh22OJZIOVNxBMWuhAr10r8HdA==
hardhat@^2.1.1:
hardhat@^2.1.1, hardhat@^2.1.2:
version "2.1.2"
resolved "https://registry.yarnpkg.com/hardhat/-/hardhat-2.1.2.tgz#a2128b71b0fb216ffc978c85a2030835b4e306ea"
integrity sha512-42iOheDsDl6Gr7sBfpA0S+bQUIcXSDEUrrqmnFEcBHx9qBoQad3s212y2ODmmkdLt+PqqTM+Mq8N3bZDTdjoLg==
......@@ -9309,7 +9259,7 @@ source-map-support@^0.4.15:
dependencies:
source-map "^0.5.6"
source-map-support@^0.5.13, source-map-support@^0.5.17, source-map-support@^0.5.6:
source-map-support@^0.5.13, source-map-support@^0.5.6:
version "0.5.19"
resolved "https://registry.yarnpkg.com/source-map-support/-/source-map-support-0.5.19.tgz#a98b62f86dcaf4f67399648c085291ab9e8fed61"
integrity sha512-Wonm7zOCIJzBGQdB+thsPar0kYuCIzYvxZwlBa87yi/Mdjv7Tip2cyVbLj5o0cFPN4EVkuTwb3GDDyUx2DGnGw==
......@@ -9922,18 +9872,6 @@ ts-node@7.0.1:
source-map-support "^0.5.6"
yn "^2.0.0"
ts-node@^9.1.1:
version "9.1.1"
resolved "https://registry.yarnpkg.com/ts-node/-/ts-node-9.1.1.tgz#51a9a450a3e959401bda5f004a72d54b936d376d"
integrity sha512-hPlt7ZACERQGf03M253ytLY3dHbGNGrAq9qIHWUY9XHYl1z7wYngSr3OQ5xmui8o2AaxsONxIzjafLUiWBo1Fg==
dependencies:
arg "^4.1.0"
create-require "^1.1.0"
diff "^4.0.1"
make-error "^1.1.1"
source-map-support "^0.5.17"
yn "3.1.1"
tsconfig-paths@^3.5.0:
version "3.9.0"
resolved "https://registry.yarnpkg.com/tsconfig-paths/-/tsconfig-paths-3.9.0.tgz#098547a6c4448807e8fcb8eae081064ee9a3c90b"
......@@ -10099,7 +10037,7 @@ typedarray@^0.0.6:
resolved "https://registry.yarnpkg.com/typedarray/-/typedarray-0.0.6.tgz#867ac74e3864187b1d3d47d996a78ec5c8830777"
integrity sha1-hnrHTjhkGHsdPUfZlqeOxciDB3c=
typescript@^4.1.5, typescript@^4.2.3:
typescript@^4.2.3:
version "4.2.3"
resolved "https://registry.yarnpkg.com/typescript/-/typescript-4.2.3.tgz#39062d8019912d43726298f09493d598048c1ce3"
integrity sha512-qOcYwxaByStAWrBf4x0fibwZvMRG+r4cQoTjbPtUlrWjBHbmCAww1i448U0GJ+3cNNEtebDteo/cHOR3xJ4wEw==
......@@ -11029,11 +10967,6 @@ yargs@^4.7.1:
y18n "^3.2.1"
yargs-parser "^2.4.1"
yn@3.1.1:
version "3.1.1"
resolved "https://registry.yarnpkg.com/yn/-/yn-3.1.1.tgz#1e87401a09d767c1d5eab26a6e4c185182d2eb50"
integrity sha512-Ux4ygGWsu2c7isFWe8Yu1YluJmqVhxqK2cLXNQA5AcC3QfbGNpM7fu0Y8b/z16pXLnFxZYvWhd3fhBY9DLmC6Q==
yn@^2.0.0:
version "2.0.0"
resolved "https://registry.yarnpkg.com/yn/-/yn-2.0.0.tgz#e5adabc8acf408f6385fc76495684c88e6af689a"
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment