1

When I compile two contracts in the same folder I get duplicates. The following contracts, Store.sol

pragma solidity ^0.8.0;

contract Store {
    event ItemSet(bytes32 key, bytes32 value);
    mapping (bytes32 => bytes32) public items;

    constructor() public {}

    function setItem(bytes32 key, bytes32 value) external {
        items[key] = value;
        emit ItemSet(key, value);
    }
}

And Store2.sol

pragma solidity ^0.8.0;

contract Store2 {
    event ItemSet(bytes32 key, bytes32 value);
    mapping (bytes32 => bytes32) public items;

    constructor() public {}

    function setItem(bytes32 key, bytes32 value) external {
        items[key] = value;
        emit ItemSet(key, value);
    }
}

When I run

docker run -v /mypath/contracts:/sources ethereum/solc:0.8.0 -o /sources/abi --optimize --overwrite --abi --bin /sources/solc/Store2.sol
docker run -v /mypath/contracts:/sources ethereum/solc:0.8.0 -o /sources/abi --optimize --overwrite --abi --bin /sources/solc/Store.sol
abigen --abi=../abi/Store2.abi --bin=../abi/Store2.bin --pkg=api --out=../api/Store2.go
abigen --abi=../abi/Store.abi --bin=../abi/Store.bin --pkg=api --out=../api/Store.go

I get duplicate variables and functions in the compiled Store.go & Store2.go, both files contain:

var ApiMetaData = &bind.MetaData{...}

var ApiABI = ApiMetaData.ABI
func DeployApi(auth *bind.TransactOpts, backend bind.ContractBackend){...}

These are just some examples of many duplicates.

How can I compile multiple contracts in the same folder?

1 Answer 1

3
+50

You can use the type parameter of the abigen command to define a custom prefix, for example:

abigen --abi=../abi/Store2.abi --bin=../abi/Store2.bin --pkg=api --type=Store2 --out=../api/Store2.go
abigen --abi=../abi/Store.abi --bin=../abi/Store.bin --pkg=api --type=Store --out=../api/Store.go

The above commands generate variables and functions like the following:

var Store2ABI = Store2MetaData.ABI
var Store2Bin = Store2MetaData.Bin
func DeployStore2(auth *bind.TransactOpts, backend bind.ContractBackend) (common.Address, *types.Transaction, *Store2, error) {...}

...

var StoreABI = StoreMetaData.ABI
var StoreBin = StoreMetaData.Bin
func DeployStore(auth *bind.TransactOpts, backend bind.ContractBackend) (common.Address, *types.Transaction, *Store, error) {...}

Why your commands generate the same prefix? The reason is the the default value for type is the name of the package. In your case, your commands set the package as api (with --pkg=api) and so your variables and functions start with the prefix Api (ApiMetaData, ApiABI, etc.).

See abigen --help for other interesting parameters (i.e., alias):

❯ abigen --help

NAME:
   abigen - Ethereum ABI wrapper code generator

USAGE:
   abigen [global options] command [command options] [arguments...]

VERSION:
   1.11.6-stable-ea9e62ca

COMMANDS:
   help, h  Shows a list of commands or help for one command

GLOBAL OPTIONS:
    --abi value                   
          Path to the Ethereum contract ABI json to bind, - for STDIN

    --alias value                 
          Comma separated aliases for function and event renaming, e.g. original1=alias1,original2=alias2

    --bin value                   
          Path to the Ethereum contract bytecode (generate deploy method)

    --combined-json value         
          Path to the combined-json file generated by compiler, - for STDIN

    --exc value                   
          Comma separated types to exclude from binding

    --lang value                   (default: "go")
          Destination language for the bindings (go)

    --out value                   
          Output file for the generated binding (default = stdout)

    --pkg value                   
          Package name to generate the binding into

    --type value                  
          Struct name for the binding (default = package name)

   MISC
    --help, -h                     (default: false)
          show help

    --version, -v                  (default: false)
          print the version

COPYRIGHT:
   Copyright 2013-2023 The go-ethereum Authors

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.