0

I have some Truffle and Solidity produced ABI files that I am bundling with a JavaScript NPM package.

221.7kB esm5/lib/abi/IERC777.json   

Currently, the ABI files include full details, like bytecode, as the files are used for unit testing, deployment and so on by Truffle itself. But the JavaScript based user interface package only needs abi function definitions themselves, nothing else.

How can I strip ABI files out from unnessary data and reduce the download size?

2

Option #1 - compile your code directly with solc executable, for example:

solc --bin --abi --optimize --optimize-runs=200 --output-dir=artifacts MyContract.sol

Option #2 - extract the artifacts from the output of truffle or solc.js:

const fs = require("fs");

const INPUT_DIR  = "YourJsonFilesDirectory";
const OUTPUT_DIR = "YourArtifactsDirectory";

for (const fileName of fs.readdirSync(INPUT_DIR)) {
    const data = JSON.parse(fs.readFileSync(INPUT_DIR + fileName, {encoding: "utf8"}));
    fs.writeFileSync(OUTPUT_DIR + fileName.replace(".json", ".abi"), JSON.stringify(data.abi)  , {encoding: "utf8"});
    fs.writeFileSync(OUTPUT_DIR + fileName.replace(".json", ".bin"), data.bytecode.substring(2), {encoding: "utf8"});
}
| improve this answer | |
0

Thanks for @goodvibration for pointing out the Option #2 approach.

I made this shell script that uses jq command for processing the ABI files.

I also refined the key list what is removed a bit.

#!/bin/bash
#
# Remove unnecessary cruft from ABI files
#
# Uses JQ: https://stedolan.github.io/jq/
#
# https://ethereum.stackexchange.com/questions/83299/decreasing-the-size-of-abi-files
#
# https://stackoverflow.com/questions/48529016/remove-a-keyvalue-from-an-json-object-using-jq
#

set -e

for abi in src/lib/abi/*.json ; do
  echo "Shrinking $abi"

  # Truffle compile generates this data by default
  # These are only necessary when deploying the contract or verifying it on EtherScan
  # cat trick: https://stackoverflow.com/a/60744617/315168
  cat <<< $(jq 'delpaths([["ast"], ["metadata"], ["source"], ["legacyAST"], ["devdoc"]])' $abi) > $abi
done
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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