How to properly export a contract abi to reuse it in another contract?

how to properly export a contract abi to reuse it in another contract?

Hi guys, I have this question, lately I’ve been using dapp2dapp calls a lot (this is when one contract calls another)

In order to call contract A from contract B, I need to export the abi of contract A to the library and plug it into forc.toml of contract B.
At the moment I do it this way:


I have contracts A and B in the same folder and in the same folder I have the abi folder where the contract-a-abi library with a copy of contract A’s abi is located

The problem with this approach is that if I want to change something in the abi of contract A, I have to go to the abi folder in the contract-a-abi library and copy the abi from contract A there.

Could you please tell me how to do this without having to copy the abi?

Hey @fuel! You can structure your project by creating an interface folder that contains the ABIs for all necessary contracts. The directory layout would look something like this:

        ├── contracts/
        │   ├── AMM/
        │   │   └── main.sw
        │   └── Exchange/
        │       └── main.sw
        └── libraries/
            ├── interface.sw
            └── data_structures.sw

A prime example of this structure in action is the AMM project found in the sway applications repository. In this project, all ABIs are organized within a separate libraries folder and imported as required.

Specifically, in the AMM contract, which utilizes both interfaces, you can see this import process in action. The relevant code can be found here. This approach streamlines the workflow by ensuring that changes are centralized and don’t need to be duplicated across multiple locations.

This topic was automatically closed 20 days after the last reply. New replies are no longer allowed.