soliditysmartcontractsabifoundry-forge

How can I generate the ABI of my smart contract locally with foundry/forge?


I have a project with multiple smart contracts locally and I want to generate the ABI of my sc.sol smart contract. I do wish to perform this locally using forge or foundry. I know it is possible to do it on Remix or to use solc but I do not have these and wishes to use foundry/forge only.


Solution

  • forge build generates the contract artifacts in the out folder (by default).

    You can parse the JSON artifact and read the abi section.

    For example using the jq bash command:

    forge build --silent && jq '.abi' ./out/MyContract.sol/MyContract.json
    

    src/MyContract.sol:

    pragma solidity ^0.8.21;
    
    contract MyContract {
        function foo() external {}
    }
    

    Output of the above command:

    [
      {
        "inputs": [],
        "name": "foo",
        "outputs": [],
        "stateMutability": "nonpayable",
        "type": "function"
      }
    ]