I know the question is old, but I had some problems with this so I will share what helped me.
To import the ABI from a JSON file, you can use the following code (assuming you already have your web3 Object):
var fs = require('fs');
var jsonFile = "pathToYourJSONFile/project.json";
var parsed= JSON.parse(fs.readFileSync(jsonFile));
var abi = parsed.abi;
var YourContract= new web3.eth.Contract(abi, 0x12345678912345678912345678912345678912);
You can read more about it here: https://web3js.readthedocs.io/en/1.0/web3-eth-contract.html
Update 01/2020:
Since Typescript 2.9, reading JSON Files is supported directly. So if you try to get the ABI or Bytecode from within a Typescript Project (Angular, React, ...) simply go to your tsconfig.json and set:
{
"compilerOptions": {
"resolveJsonModule": true,
"esModuleInterop": true
}
}
You can then use import ContractName from 'path/to/contract/ContractName.json'
to import your Contracts and get the Abi with ContractName.abi
.
Cheers
The page "Verify Contract" ask for "Constructor Arguments ABI-encoded".
In your case the constructor has no parameters, so you can leave such field empty.
In the general case when your constrctor has parameters you can proceed as follow:
You can use ethereumjs-abi to encode/decode the parameters for transaction calls.
Let's suppose your constructor is MyToken(address _to, uint256 _value, bool _enabled)
. The parameters types are ["address", "uint256", "bool"].
If you have created the contract MyToken("0x1234567812345678", 0x314159268, true). Then the parameters values are ["0x1234567812345678", "0x314159268", true].
Then you can encode the values with rawEncode(parameterTypes, parameterValues)
.
var abi = require('ethereumjs-abi')
var parameterTypes = ["address", "uint256", "bool"];
var parameterValues = ["0x1234567812345678", "0x314159268", true];
var encoded = abi.rawEncode(parameterTypes, parameterValues);
console.log(encoded.toString('hex'));
On execution it will output
000000000000000000000000000000000000000000000000123456781234567800000000000000000000000000000000000000000000000000000003141592680000000000000000000000000000000000000000000000000000000000000001
Best Answer
a) you know the source code of smart-contract
programmatically: see the link to gist from Ivan's answer
manually: with Remix
b) you don't know the source code but smart-contract was verified in one of the explorers
Then, use block explorer search to get ABI of smart-contract. For example, you can read the verified source code of smart-contracts in BlockScout, Etherscan, Etherchain
c) you don't know the source code && contract is not verified
You can use Panoramix smart-contracts decompiler to get a partial ABI of smart-contract