Skip to content

Commit

Permalink
feat(pg): Support fully qualified names in foundry
Browse files Browse the repository at this point in the history
  • Loading branch information
RPate97 committed Jun 23, 2023
1 parent 8ca4c13 commit 4e6a91e
Show file tree
Hide file tree
Showing 7 changed files with 34 additions and 12 deletions.
5 changes: 5 additions & 0 deletions .changeset/wicked-coins-jog.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@chugsplash/plugins': patch
---

Support fully qualified names in foundry
2 changes: 1 addition & 1 deletion packages/plugins/chugsplash/Storage.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ const config: UserChugSplashConfig = {
},
contracts: {
MyStorage: {
contract: 'Storage',
contract: 'contracts/test/ContainsStorage.sol:Storage',
kind: 'proxy',
constructorArgs,
variables,
Expand Down
File renamed without changes.
2 changes: 1 addition & 1 deletion packages/plugins/contracts/test/SimpleStorage.sol
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.9;

import { Storage } from "./Storage.sol";
import { Storage } from "./ContainsStorage.sol";
import { Stateless } from "./Stateless.sol";

contract SimpleStorage {
Expand Down
2 changes: 1 addition & 1 deletion packages/plugins/contracts/test/Stateless.sol
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.9;

import { Storage } from "./Storage.sol";
import { Storage } from "./ContainsStorage.sol";
import { Version } from "@chugsplash/contracts/contracts/ChugSplashDataTypes.sol";

contract Stateless {
Expand Down
33 changes: 25 additions & 8 deletions packages/plugins/src/foundry/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,19 +45,36 @@ export const getContractArtifact = async (
name: string,
artifactFilder: string
): Promise<ContractArtifact> => {
// Try to find the artifact in the standard format
const folderName = `${name}.sol`
const fileName = `${name}.json`
const completeFilePath = join(artifactFilder, folderName, fileName)

if (!(await existsAsync(completeFilePath))) {
const standardFilePath = join(artifactFilder, folderName, fileName)

// Try to find the artifact in the qualified format
// Technically we don't need the full path to the file b/c foundry outputs a flat directory structure
// For clarity and consistency with other tools, we still handle the fully qualified format and recommend it
const qualifiedSections = name.split('/').pop()
const [file, contract] = qualifiedSections?.split(':') ?? ['', '']
const qualifiedFilePath = join(artifactFilder, file, `${contract}.json`)

if (await existsAsync(standardFilePath)) {
return parseFoundryArtifact(
JSON.parse(await readFileAsync(standardFilePath, 'utf8'))
)
} else if (await existsAsync(qualifiedFilePath)) {
return parseFoundryArtifact(
JSON.parse(await readFileAsync(qualifiedFilePath, 'utf8'))
)
} else {
// If we can't find the artifact, throw an error and recommend checking their options and using fully qualified format
throw new Error(
`Could not find artifact for: ${name}. Please make sure that this contract exists in either the src, script, or test directory that you've configured in your foundry.toml.`
`Could not find artifact for: ${name}.
- Please make sure that this contract exists in either the src, script, or test directory that you've configured in your foundry.toml.
- If you have multiple contracts in the same file or have files with different names from the contracts they contain, please use the fully qualified name for the contract.
For example: 'path/to/file/SomeFile.sol:MyContract'
`
)
}

const artifact = JSON.parse(await readFileAsync(completeFilePath, 'utf8'))

return parseFoundryArtifact(artifact)
}

/**
Expand Down
2 changes: 1 addition & 1 deletion packages/plugins/test/foundry/ChugSplash.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import "forge-std/Test.sol";
import "../../contracts/foundry/ChugSplash.sol";
import { ChugSplashUtils } from "../../contracts/foundry/ChugSplashUtils.sol";
import { SimpleStorage } from "../../contracts/test/SimpleStorage.sol";
import { Storage } from "../../contracts/test/Storage.sol";
import { Storage } from "../../contracts/test/ContainsStorage.sol";
import { ComplexConstructorArgs } from "../../contracts/test/ComplexConstructorArgs.sol";
import { Stateless } from "../../contracts/test/Stateless.sol";
import { ChugSplashRegistry } from "@chugsplash/contracts/contracts/ChugSplashRegistry.sol";
Expand Down

0 comments on commit 4e6a91e

Please sign in to comment.