Reputation: 709
I can access a Blockchain Service based on Quorum, and I'm using Nethereum Library to interact with Smart Contract.
When I deploy a new Smart Contract, Nethereum gives the address and the ABI of SmartContract.
But I can't access the ABI of SmartContract without deploying the process. How can I get ABI based on the address of Smart Contract?
Upvotes: 0
Views: 2137
Reputation: 2728
As stated by the other answers, there are plenty of ways to generate the ABI and the bytecode for a smart contract. But there is no way to get the ABI knowing the smartcontract address.
However, I recommend using this web3.js plugin to generate the ABI and the bytecode. You can compile the smart contract and save the ABI and the bytcode. Or you can even initializing a web3.js Smart Contract instance directly from the solidity code and start interacting with it.
Here is the npm package: https://www.npmjs.com/package/web3-plugin-craftsman that describe the features and includes sample codes that is mentioned in the README.md file.
And here is a step by step tutorial: https://medium.com/dapps-decentralized-apps-development/interacting-with-ethereum-smart-contracts-directly-from-solidity-source-code-9caf55457eb8
And here is a full example:
import { ExtendedWeb3 } from 'web3-plugin-craftsman';
const web3 = new Web3('http://localhost:8545'); // your Web3 object
// Using ExtendedWeb3 as a plugin
web3.registerPlugin(new ExtendedWeb3());
const contract = new web3.craftsman.ExtendedContract('./test/smart_contracts/simple-contract.sol');
// now the `contract` instance can be used like any web3.js contract
// Wait for the contract compilation and handle compilation errors if any
try {
const compilationResult = await contract.compilationResult;
// the compilationResult will consists of:
// {
// abi: ContractAbi,
// bytecodeString: string,
// contractName: string,
// }
} catch (e) {
console.log(e);
}
// Deploying and interacting with your Contract
// get the accounts provided by your Ethereum node (like Ganache).
const accounts = await web3.eth.getAccounts();
fromAccount = accounts[0];
// Deploy contract
const deployed = await contract
.deploy({ arguments: [1000] })
.send({ from: fromAccount });
// Call a method
const myNumber = await deployed.methods.myNumber().call();
// Send a transaction
await deployed.methods.setMyNumber(100).send({ from: fromAccount });
// If you are using TypeScript you need to ask similar to the following:
// await(deployed.methods.setMyNumber(100) as any)
// .send({ from: fromAccount });
// Call a method
const myNumberUpdated = await deployed.methods.myNumber().call();
Upvotes: 0
Reputation: 25
You could copy your smartcontract into remix and let remix compile it. Remix then shows you the Api and the bytecode of your contract.
Upvotes: 0
Reputation: 43491
You can get ABI JSON during compilation of the contract source code. The inputs for compilation are source code and few other values (such as optimizer settings)... The outputs include the ABI JSON and bytecode... So you don't really need to deploy the contract to get the json, just compile it (without deployment).
It's not possible to get ABI JSON purely from a bytecode (or an address that contains just the bytecode).
If the contract has source code published, you can compile the source code to get the ABI JSON.
If if doesn't have the source code published, it's also possible that the contract implements some standard (e.g. ERC-20). If you know whether and what standard it implements, you can use a general ABI JSON that reflects on this standard (e.g. this is ABI JSON of a ERC-20 standard). However, it does not reflect any functions that the contract might have used to extend the standard-defined minimum.
Upvotes: 1