Skip to content

Commit

Permalink
✨ feat: Detect solc version in config (#359)
Browse files Browse the repository at this point in the history
## Description

Detect solc version from package.json instead.
This feels ripe for bugs so it's a WIP

## Testing

Explain the quality checks that have been done on the code changes

## Additional Information

- [ ] I read the [contributing docs](../docs/contributing.md) (if this
is your first contribution)

Your ENS/address:

Co-authored-by: Will Cory <[email protected]>
  • Loading branch information
roninjin10 and Will Cory authored Jul 24, 2023
1 parent 61482fa commit e24901a
Show file tree
Hide file tree
Showing 8 changed files with 120 additions and 11 deletions.
5 changes: 5 additions & 0 deletions .changeset/odd-ligers-search.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@evmts/config": minor
---

Added autodetection of solc version. solcVersion is no longer required
6 changes: 5 additions & 1 deletion bundlers/config/src/Config.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { getDefaultSolcVersion } from './getDefaultSolcVersion'
import { expandedString } from './zodUtils'
import { isAddress } from 'viem'
import { z } from 'zod'
Expand Down Expand Up @@ -188,9 +189,12 @@ export type ResolvedConfig = {
localContracts: Required<LocalContractsConfig>
externalContracts: Required<ExternalConfig>
}

export const defaultConfig: ResolvedConfig = {
compiler: {
solcVersion: '0.8.20',
get solcVersion() {
return getDefaultSolcVersion()
},
foundryProject: false,
remappings: {},
libs: [],
Expand Down
30 changes: 30 additions & 0 deletions bundlers/config/src/defineConfig.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,36 @@ describe(defineConfig.name, () => {
})
})

it('should default forge command to forge', () => {
const forgeCommandOutput = JSON.stringify({
solc_version: '0.8.4',
remappings: [],
libs: ['lib1', 'lib2'],
})
mockExecSync.mockReturnValueOnce(Buffer.from(forgeCommandOutput))

const configFactory = () =>
({
compiler: {
foundryProject: true,
},
}) as EvmtsConfig
const config = defineConfig(configFactory)
const resolvedConfig = config.configFn('./')

expect(mockExecSync).toHaveBeenCalledWith('forge config --json')
expect(resolvedConfig).toEqual({
compiler: {
solcVersion: '0.8.4',
remappings: defaultConfig.compiler.remappings,
foundryProject: true,
libs: ['lib1', 'lib2'],
},
localContracts: defaultConfig.localContracts,
externalContracts: defaultConfig.externalContracts,
})
})

it('should throw error when forge command fails', () => {
mockExecSync.mockImplementationOnce(() => {
throw new Error()
Expand Down
43 changes: 43 additions & 0 deletions bundlers/config/src/getDefaultSolcVersion.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import { getDefaultSolcVersion } from './getDefaultSolcVersion.js'
import { createRequire } from 'module'
import {
type MockedFunction,
beforeEach,
describe,
expect,
it,
vi,
} from 'vitest'

let version: string | undefined = '0.8.9'
vi.mock('module', () => ({
createRequire: vi.fn(),
}))

const mockCreateRequire = createRequire as MockedFunction<typeof createRequire>
const mockRequire = vi.fn()
const consoleErrorSpy = vi.fn()

beforeEach(() => {
vi.resetAllMocks()
mockRequire.mockReturnValue({ version })
mockCreateRequire.mockReturnValue(mockRequire as any)
vi.stubGlobal('console', {
error: consoleErrorSpy,
})
})

describe('getDefaultSolcVersion', () => {
it('should return solc version if available', async () => {
expect(getDefaultSolcVersion()).toBe(version)
})

it('should log error and return undefined if version is not available', async () => {
version = undefined
mockRequire.mockReturnValue({ version })
expect(getDefaultSolcVersion()).toBe(undefined)
expect(consoleErrorSpy).toBeCalledWith(
'Failed to get solc version! Please install it or specify a version in your config',
)
})
})
13 changes: 13 additions & 0 deletions bundlers/config/src/getDefaultSolcVersion.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { createRequire } from 'module'

export const getDefaultSolcVersion = () => {
const moduleRequire = createRequire(import.meta.url ?? __dirname)
const solc = moduleRequire('solc')
const version = solc?.version
if (!version) {
console.error(
'Failed to get solc version! Please install it or specify a version in your config',
)
}
return solc?.version
}
25 changes: 21 additions & 4 deletions bundlers/config/src/loadConfig.spec.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,15 @@
import { type EvmtsConfig, defaultConfig, loadConfig } from '.'
import * as cp from 'child_process'
import * as fs from 'fs'
import { beforeEach, describe, expect, it, vi } from 'vitest'
import { createRequire } from 'module'
import {
type MockedFunction,
beforeEach,
describe,
expect,
it,
vi,
} from 'vitest'

const mockTsConfig = () => {
return JSON.stringify(
Expand Down Expand Up @@ -32,6 +40,9 @@ const mockTsConfig = () => {
)
}

vi.mock('module', () => ({
createRequire: vi.fn(),
}))
vi.mock('fs', () => ({
readFileSync: vi.fn(),
existsSync: vi.fn(),
Expand All @@ -44,6 +55,12 @@ vi.mock('child_process', () => ({
describe(loadConfig.name, () => {
beforeEach(() => {
vi.resetAllMocks()
const mockCreateRequire = createRequire as MockedFunction<
typeof createRequire
>
const mockRequire = vi.fn()
mockCreateRequire.mockReturnValue(mockRequire as any)
mockRequire.mockReturnValue({ version: '0.8.42' })
vi.stubGlobal('process', {
...process,
env: { ...process.env, ETHERSCAN_KEY: 'MY_ETHERSCAN_KEY' },
Expand Down Expand Up @@ -262,7 +279,7 @@ describe(loadConfig.name, () => {
"foundryProject": false,
"libs": [],
"remappings": {},
"solcVersion": "0.8.20",
"solcVersion": "0.8.42",
},
"externalContracts": {
"apiKeys": {
Expand Down Expand Up @@ -298,7 +315,7 @@ describe(loadConfig.name, () => {
"foundryProject": false,
"libs": [],
"remappings": {},
"solcVersion": "0.8.20",
"solcVersion": "0.8.42",
},
"externalContracts": {
"apiKeys": {
Expand Down Expand Up @@ -427,7 +444,7 @@ describe(loadConfig.name, () => {
"foundryProject": false,
"libs": [],
"remappings": {},
"solcVersion": "0.8.20",
"solcVersion": "0.8.42",
},
"externalContracts": {
"apiKeys": {
Expand Down
6 changes: 3 additions & 3 deletions bundlers/config/vitest.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ export default defineConfig({
include: ['src/**/*.{test,spec}.{js,mjs,cjs,ts,mts,cts,jsx,tsx}'],
coverage: {
reporter: ['text', 'json-summary', 'json'],
lines: 99.78,
statements: 99.78,
lines: 100,
statements: 100,
functions: 100,
branches: 88.05,
branches: 89.18,
thresholdAutoUpdate: true,
},
},
Expand Down
3 changes: 0 additions & 3 deletions examples/next/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,6 @@
"plugins": [
{
"name": "@evmts/ts-plugin",
"compiler": {
"solcVersion": "0.8.19"
},
"localContracts": {
"contracts": [
{
Expand Down

0 comments on commit e24901a

Please sign in to comment.