我可以访问基于Quorum的区块链服务,并且我正在使用Nethereum Library与智能合约进行交互。
当我部署新的智能合约时,Nethereum 会给出智能合约的地址和 ABI。
但是如果不部署流程,我就无法访问 SmartContract 的 ABI。如何根据智能合约地址获取ABI?
您可以在合约源码编译过程中获取ABI JSON。编译的输入是源代码和一些其他值(例如优化器设置)...输出包括 ABI JSON 和字节码...因此您实际上不需要部署合约来获取 json,只需编译它(无需部署)。
不可能纯粹从字节码(或仅包含字节码的地址)获取 ABI JSON。
如果合约有源码发布,您可以编译源码得到ABI JSON。
如果没有发布源代码,也有可能合约实现了某些标准(例如 ERC-20)。如果您知道它是否实现以及实现什么标准,则可以使用反映该标准的通用 ABI JSON(例如,this 是 ERC-20 标准的 ABI JSON)。但是,它并不反映合约可能用于扩展标准定义的最小值的任何功能。
您可以将智能合约复制到 remix 中,然后让 remix 对其进行编译。然后,Remix 会向您显示合约的 Api 和字节码。
正如其他答案所述,有很多方法可以生成智能合约的 ABI 和字节码。但没有办法让 ABI 知道智能合约地址。
但是,我建议使用这个 web3.js 插件来生成 ABI 和字节码。您可以编译智能合约并保存ABI和字节码。或者,您甚至可以直接从 Solidity 代码初始化 web3.js 智能合约实例并开始与其交互。
这是 npm 包: https://www.npmjs.com/package/web3-plugin-craftsman 描述了功能并包含 README.md 文件中提到的示例代码。
这是一个完整的示例:
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();