playground

The world is a playground.

Truffle でコントラクトの ABI を取得

Truffle でビルド済みのコントラクトの ABI を取得する方法を紹介します。

一番簡単なのは、./build/contracts ディレクトリにビルド済みのコントラクト ABI や bytecode が記録されている JSON ファイルがあるので、そこから取得する方法です。

たとえば、ExampleSmartContract というクラスをコンパイルすると、./build/contracts/ExampleSmartContract.json というファイルができているので、そのなかの abi の値をみます。

サンプルコード

  • JavaScript (Node.js) によるサンプルコードです。
  • ExampleSmartContract というクラスをコンパイルした場合のサンプルです。
const fs = require('fs');
const contract = JSON.parse(fs.readFileSync('./build/contracts/ExampleSmartContract.json', 'utf8'));
console.log(JSON.stringify(contract.abi));