Load contract artifacts
loadContractAbi() / loadContractBytecode() read plain ABI / .bin files or
Foundry/Hardhat artifacts:
/** Loads `artifacts/<name>/<name>.json` and `.bin`. */
export function loadArtifact(name: string): ContractArtifact {
return {
name,
abi: loadContractAbi(`${name}/${name}.json`, ARTIFACTS_DIR),
bytecode: loadContractBytecode(`${name}/${name}.bin`, ARTIFACTS_DIR),
};
}
Tested in examples/src/artifacts.ts:25
Relative paths resolve against <cwd>/test/artifacts or withArtifactsDir():
const abi = anvil.contractAbi("ExampleToken/ExampleToken.json");
const bytecode = anvil.contractBytecode("ExampleToken/ExampleToken.bin");
const [deployer] = await anvil.addresses();
const receipt = await anvil.deployContract(abi, bytecode, deployer, [
"Second",
"TWO",
]);
expect(
await anvil.testClient.readContract({
address: receipt.contractAddress!,
abi,
functionName: "symbol",
}),
).toBe("TWO");
Tested in examples/test/e2e/fixtures-and-isolation.e2e.test.ts:73
A deploy helper returning a checksummed address:
/**
* Deploys an artifact and returns the new contract's checksummed address
* (receipts carry it lowercase; contract reads return checksummed addresses).
*/
export async function deployContract(
client: SignerClient,
artifact: ContractArtifact,
args: readonly unknown[] = [],
): Promise<Address> {
const hash = await client.deployContract({
abi: artifact.abi,
bytecode: artifact.bytecode,
args,
});
const { contractAddress } = await client.confirmTransaction({ hash });
if (!contractAddress) throw new Error(`${hash} created no contract`);
return getAddress(contractAddress);
}
Tested in examples/src/deploy.ts:25
JSON ABIs are typed Abi, so reads return unknown: narrow them in helpers, or
use parseAbiItem (Query logs).
Concepts
- Reference:
loadContractAbi/loadContractBytecode