Skip to content

Commit

Permalink
Merge develop into master (#49)
Browse files Browse the repository at this point in the history
* fix(hh/forge): typechain support (#45)

Typechain uses a private method on the artifact
with an unsafe `any` typecast to get artifact paths.
To work with typechain, this method must exist until
typechain is updated.

The issue is here: dethcrypto/TypeChain#544

* feat: `Artifacts.getBuildInfo` (#48)

Implement a naive `getBuildInfo` function. This is useful
for tooling that integrates more deeply with the hardhat ecosystem.
Note that this implementation is not 100% with the output
that the hardhat compiler toolchain outputs, but the output
is good enough to get more tooling to be compatible.
  • Loading branch information
tynes authored Jun 2, 2022
1 parent 6c800c2 commit d6f61aa
Show file tree
Hide file tree
Showing 6 changed files with 110 additions and 6 deletions.
5 changes: 5 additions & 0 deletions .changeset/many-rivers-jump.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@foundry-rs/hardhat-forge": patch
---

Add deprecated private method for typechain compat
5 changes: 5 additions & 0 deletions .changeset/tame-otters-sing.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@foundry-rs/hardhat-forge": patch
---

Implement `Artifacts.getBuildInfo`
80 changes: 78 additions & 2 deletions packages/hardhat-forge/src/forge/artifacts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,10 +99,86 @@ export class ForgeArtifacts implements IArtifacts {
return paths.map((p) => this._getFullyQualifiedNameFromPath(p)).sort();
}

/**
* Dynamically generate a build artifact for a contract.
* In practice, hardhat build artifacts are decorated
* with additional fields on top of the `BuildInfo`
* type. Fields that foundry cannot fill in are currently
* left blank. `extra_output` must be configured for some
* fields to be populated
*/
public async getBuildInfo(
_fullyQualifiedName: string
fullyQualifiedName: string
): Promise<BuildInfo | undefined> {
return undefined;
const artifactPath = await this._getArtifactPath(fullyQualifiedName);
try {
const forgeArtifact = (await fsExtra.readJson(
artifactPath
)) as ForgeArtifact;

if (!forgeArtifact.ast) {
throw new Error("Must compile with ast");
}

const relativePath = forgeArtifact.ast.absolutePath;

return {
_format: "hh-sol-build-info-1",
id: "",
solcVersion: "",
solcLongVersion: "",
input: {
language: "",
sources: {},
settings: {
optimizer: {},
metadata: { useLiteralContent: true },
outputSelection: {},
evmVersion: "",
libraries: {},
},
},
output: {
sources: {
[fullyQualifiedName]: {
id: 0,
ast: forgeArtifact.ast,
},
},
contracts: {
[relativePath]: {
[fullyQualifiedName]: {
abi: forgeArtifact.abi,
devdoc: forgeArtifact.devdoc,
metadata: forgeArtifact.metadata,
storageLayout: forgeArtifact.storageLayout,
userdoc: forgeArtifact.userdoc,
evm: {
bytecode: {
object: forgeArtifact.bytecode.object!,
opcodes: "",
sourceMap: forgeArtifact.bytecode.sourceMap!,
linkReferences: forgeArtifact.bytecode.linkReferences,
immutableReferences: {},
},
deployedBytecode: {
object: forgeArtifact.deployedBytecode.object!,
opcodes: "",
sourceMap: forgeArtifact.deployedBytecode.sourceMap!,
linkReferences:
forgeArtifact.deployedBytecode.linkReferences,
immutableReferences: {},
},
methodIdentifiers: forgeArtifact.methodIdentifiers,
},
},
},
},
},
} as any;
} catch (e) {
return undefined;
}
}

public async getArtifactPaths(): Promise<string[]> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,6 @@ src = 'src'
out = 'out'
libs = ['lib']

# See more config options https://github.com/foundry-rs/foundry/tree/master/config
extra_output = ['devdoc', 'userdoc', 'metadata', 'storageLayout']

# See more config options https://github.com/foundry-rs/foundry/tree/master/config
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
pragma solidity ^0.8.13;

contract Contract {

function example() public {}

uint256 public bar;
function example() public {}
}
17 changes: 17 additions & 0 deletions packages/hardhat-forge/test/project.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,5 +48,22 @@ describe("Integration tests", function () {
assert.equal(artifact.contractName, path.basename(name, ".json"));
}
});

it("Should return build info", async function () {
const info = await this.hre.artifacts.getBuildInfo("Contract");
assert.exists(info);
const contract = info?.output.contracts["src/Contract.sol"].Contract!;
assert.exists(contract);
assert.exists(contract.abi);
assert.exists((contract as any).devdoc);
assert.exists((contract as any).metadata);
assert.exists((contract as any).storageLayout);
assert.exists((contract as any).userdoc);
assert.exists(contract.evm);
assert.exists(contract.evm.bytecode);
assert.exists(contract.evm.bytecode.object);
assert.exists(contract.evm.deployedBytecode);
assert.exists(contract.evm.deployedBytecode.object);
});
});
});

0 comments on commit d6f61aa

Please sign in to comment.