Package detail

abi-to-sol

gnidan31.5kMIT0.8.0

Compile ABI JSON to Solidity interface

readme

abi-to-sol

npm version gitpoap badge

Input an ABI JSON and get a Solidity interface compatible with whatever pragma solidity <version-range> you need.

abi-to-sol evaluates your input ABI and finds all the external functions, events, structs, and user-defined value types in order to produce a source file that's suitable for copying and pasting into your project. Import your external contract's interface and interact with it, almost as if you had copied the whole other project's sourcecode into a "vendor" directory (but without the potential Solidity version mismatch!)

It doesn't matter what version of Solidity generated the ABI in the first place (or if the contract wasn't even written in Solidity), abi-to-sol will give you *.sol output that's compatible with your existing project! (Some rare caveats may apply, see below.)

Try online!

Skip the terminal and just use the hosted Web UI.

CLI instructions

Install globally via:

$ npm install -g abi-to-sol

Installing locally should work fine as well, but you may have to jump through hoops to get the abi-to-sol script available on your PATH.

Usage

Pipe ABI JSON to stdin, get Solidity on stdout.

abi-to-sol [--solidity-version=<solidityVersion>] [--license=<license>] [--validate] [<name>]
abi-to-sol -h | --help
abi-to-sol --version

Options:

<name>
  Name of generated interface. Default: MyInterface

--validate
  Validate JSON before starting

-V --solidity-version
  Version of Solidity (for pragma). Default: >=0.7.0 <0.9.0

-L --license
  SPDX license identifier. default: UNLICENSED

-h --help     Show this screen.
--version     Show version.

Example

Run the following command:

$ echo '[{"constant":true,"inputs":[{"name":"node","type":"bytes32"}],"name":"resolver","outputs":[{"name":"","type":"address"}],"payable":false,"type":"function"},{"constant":true,"inputs":[{"name":"node","type":"bytes32"}],"name":"owner","outputs":[{"name":"","type":"address"}],"payable":false,"type":"function"},{"constant":false,"inputs":[{"name":"node","type":"bytes32"},{"name":"label","type":"bytes32"},{"name":"owner","type":"address"}],"name":"setSubnodeOwner","outputs":[],"payable":false,"type":"function"},{"constant":false,"inputs":[{"name":"node","type":"bytes32"},{"name":"ttl","type":"uint64"}],"name":"setTTL","outputs":[],"payable":false,"type":"function"},{"constant":true,"inputs":[{"name":"node","type":"bytes32"}],"name":"ttl","outputs":[{"name":"","type":"uint64"}],"payable":false,"type":"function"},{"constant":false,"inputs":[{"name":"node","type":"bytes32"},{"name":"resolver","type":"address"}],"name":"setResolver","outputs":[],"payable":false,"type":"function"},{"constant":false,"inputs":[{"name":"node","type":"bytes32"},{"name":"owner","type":"address"}],"name":"setOwner","outputs":[],"payable":false,"type":"function"},{"anonymous":false,"inputs":[{"indexed":true,"name":"node","type":"bytes32"},{"indexed":false,"name":"owner","type":"address"}],"name":"Transfer","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"name":"node","type":"bytes32"},{"indexed":true,"name":"label","type":"bytes32"},{"indexed":false,"name":"owner","type":"address"}],"name":"NewOwner","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"name":"node","type":"bytes32"},{"indexed":false,"name":"resolver","type":"address"}],"name":"NewResolver","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"name":"node","type":"bytes32"},{"indexed":false,"name":"ttl","type":"uint64"}],"name":"NewTTL","type":"event"}]' \
  | npx abi-to-sol ENS

Get this output:

// SPDX-License-Identifier: UNLICENSED
// !! THIS FILE WAS AUTOGENERATED BY abi-to-sol. SEE BELOW FOR SOURCE. !!
pragma solidity ^0.7.0;
pragma experimental ABIEncoderV2;

interface ENS {
  function resolver(bytes32 node) external view returns (address);

  function owner(bytes32 node) external view returns (address);

  function setSubnodeOwner(
    bytes32 node,
    bytes32 label,
    address owner
  ) external;

  function setTTL(bytes32 node, uint64 ttl) external;

  function ttl(bytes32 node) external view returns (uint64);

  function setResolver(bytes32 node, address resolver) external;

  function setOwner(bytes32 node, address owner) external;

  event Transfer(bytes32 indexed node, address owner);
  event NewOwner(bytes32 indexed node, bytes32 indexed label, address owner);
  event NewResolver(bytes32 indexed node, address resolver);
  event NewTTL(bytes32 indexed node, uint64 ttl);
}

// THIS FILE WAS AUTOGENERATED FROM THE FOLLOWING ABI JSON:
/* ... */

Caveats

  • This tool works best with ABIs from contracts written in Solidity, thanks to the useful internalType hints that Solidity provides. This is non-standard, so abi-to-sol still works without those. You should be able to use this tool to import someone else's Vyper contract interface into your Solidity project.

  • User-defined value types are supported, but if these UDVTs require special constructors, abi-to-sol won't give you any implementations. Take extra care to make sure you know how to interact with an external contract that has UDVTs as part of its interface.

  • You might run into problems if you need this tool to output interfaces that are compatible with sufficiently old versions of Solidity (<0.5.0), due to certain missing features (structs/arrays couldn't be external function parameters back then).

    ... but probably you should definitely just don't use solc that old.

  • Similarly, there might be problems if you need this tool to output interfaces that are compatible with a particularly large range of solc versions (e.g. ^0.6.0 || ^0.7.0). This is because the data location changed across versions (from address[] calldata to address[] memory, e.g.), and there's no single syntax that abi-to-sol can output that would satisfy everything. (This only matters for input ABIs where it's relevant, so you may still be alright.)

  • This project does not output Vyper code... but you don't need a project like this one for Vyper because Vyper already lets you import *.abi.json files directly! Maybe this isn't a caveat.

Is this project useful to you?

Feel free to donate to gnidan.eth ❤️

changelog

abi-to-sol changelog

vNext

New features

  • Rearchitect declaration internals to support UDVTs (#114 by @gnidan)

Project updates

v0.7.1

See release notes.

Bug fixes

Dependency updates

v0.7.0

See release notes.

New features

  • Allow producing embeddable output, e.g. no pragma (#91 by @gnidan)

Project updates

Dependency updates

v0.6.6

See release notes.

Internal improvements

Dependency updates

  • Bump @truffle/abi-utils to ^0.3.0, drop dependency @truffle/codec (#92 by @benjamincburns)

v0.6.5

See release notes.

Project updates

  • Update README to remove npx support (#83 by @gnidan)

Dependency updates

v0.6.4

See release notes.

Dependency updates

  • Upgrade prettier-plugin-solidity and prettier to latest (#80 by @gnidan)

v0.6.3

See release notes.

Bug fixes

  • Produce valid syntax for function type params (#77 by @gnidan)

Dependency updates

v0.6.2

See release notes.

Dependency updates

  • Bump @truffle/codec from ^0.11.24 to ^0.13.2 (#72 by @cds-amal)

v0.6.1

See release notes.

New features

  • Add --no-attribution and --no-source flags (#69 by @gnidan)

Internal improvements

  • Update to Node.js LTS (#63 by @gnidan)

  • Update copyright notice (#64 by @gnidan)

  • Add version feature knowledge for user-defined value types (#65 by @gnidan)

Dependency updates

v0.5.3

See release notes.

Dependency updates

v0.5.2

See release notes.

Dependency updates

v0.5.1

See release notes.

Internal improvements

  • Add script for upgrading dependencies (#37 by @gnidan)

  • Upgrade typescript to ^4.4.4 (#38 by @gnidan)

v0.5.0

See release notes.

New features

Fixes

  • Properly error when generating structs on unsupported Solidity versions (#32 by @gnidan)
  • Fix version-feature checks to succeed only when entire range has feature (#35 by @gnidan)

Internal improvements

  • Enforce CHANGELOG entries in pull requests (#30 by @gnidan)

Dependency updates

  • Update dependency: @truffle/abi-utils@^0.2.2 (#29 by @gnidan)

web-ui changes

  • Switch to react-simple-code-editor (#31 by @gnidan)

v0.4.0

See release notes.

New features

  • Error upon Solidity version syntax ambiguity (#21 by @gnidan)

Enhancements

  • Include package version in autogen notice (#25 by @gnidan)
  • Only emit pragma experimental ABIEncoderV2 when necessary (#22 by @gnidan)

Bug fixes

  • Put structs in the right places (#24 by @gnidan)

Housekeeping

  • Note lack of support for ABI errors (#26 by @gnidan)
  • Change default version to >=0.7.0 <0.9.0 (#20 by @gnidan)

Internal improvements

web-ui changes

  • Show errors from generateSolidity (#19 by @gnidan)

v0.3.0

See release notes.

New features

  • Export GenerateSolidityOptions interface type (#13 by @gnidan)
  • Allow disabling prettier (#14 by [@gnidan])
  • Add web UI (#16 by [@gnidan])
  • Begin to differ output based on Solidity version / features used by ABI (#17 by [@gnidan])

Bug fixes

  • Replace invalid characters in internalTypes (#15 by [@gnidan])

v0.2.1

See release notes.

Dependency updates

  • Remove unused @drizzle/store dependency (#12 by @gnidan)

v0.2.0

See release notes.

Dependency updates

  • Switch to using @truffle/abi-utils (#11 by @gnidan)

v0.1.6

See release notes.

Enhancements

Dependency updates

  • Upgrade prettier-plugin-solidity to v1.0.0-alpha.59 (#6 by @gnidan)

Internal improvements

v0.1.5

See release notes.

Enhancements

  • Broaden default Solidity version to >=0.5.0 <0.8.0 (and centralize defaulting logic) (#1 by @gnidan)
  • Add runtime source map support (#4 by @gnidan)

Internal improvements