Let me explain the ABI's role. In the context of decentralized applications (dApps), the ABI is essential for interacting with a smart contract. The ABI acts as the bridge between two binary program modules, in this case, your front-end application and the deployed smart contract on the blockchain.
让我解释一下ABI的作用。在去中心化应用程序(Dapp)的环境中,ABI对于与智能合同交互是必不可少的。ABI充当两个二进制程序模块之间的桥梁,在这种情况下,您的前端应用程序和区块链上部署的智能合同。
Without the ABI, your front-end wouldn't know how to structure calls to the contract's functions, what data types to use, or how to interpret the data returned from function calls.
如果没有ABI,您的前端将不知道如何构造对约定函数的调用、使用什么数据类型或如何解释从函数调用返回的数据。
So, if you've deployed a contract without saving its ABI, you'll generally have trouble integrating it into a dApp. That said, the ABI is generated when you compile the smart contract, not when you deploy it. If you still have access to the original source code of the smart contract, you can re-compile it to obtain the ABI, without having to redeploy the contract. Many development tools and environments, like Truffle or Remix, make it easy to extract the ABI upon compilation.
因此,如果您部署了一个合同而没有保存它的ABI,您通常会在将其集成到DAPP中时遇到困难。也就是说,ABI是在编译智能合同时生成的,而不是在部署时生成的。如果您仍然可以访问智能合同的原始源代码,您可以重新编译它以获得ABI,而不必重新部署合同。许多开发工具和环境,如Truffle或Remix,使得在编译时提取ABI变得很容易。
Let's simulate an ABI save.
让我们来模拟一次ABI保存。
I am assuming that you have smart contract like this:
我假设您有这样的智能合同:
pragma solidity ^0.8.0;
contract MyContract {
uint256 public myVariable;
function setVariable(uint256 _myVariable) public {
myVariable = _myVariable;
}
function getVariable() public view returns (uint256) {
return myVariable;
}
}
Compiling the smart contract:
编制智能合同:
truffle compile
When you run this command, Truffle will create a JSON file in the build/contracts directory that includes the ABI, among other things.
当您运行此命令时,Truffle将在构建/合同目录中创建一个JSON文件,其中包括ABI等。
Deploying the smart contract:
部署智能合同:
truffle migrate --network name_of_your_network
You can deploy your smart contract to the network of your choice or local blockchain for testing.
您可以将您的智能合同部署到您选择的网络或本地区块链进行测试。
Saving the ABI:
保存ABI:
const myContractJSON = require("./build/contracts/MyContract.json");
const myContractABI = myContractJSON.abi;
const myContractAddress = "0x..."; // Replace with your deployed address
const myContract = new web3.eth.Contract(myContractABI, myContractAddress);
Now you can use this ABI (myContractABI) to interact with your deployed smart contract using libraries like Web3.js or Ethers.js.
现在,您可以使用此ABI(MyContractABI)通过Web3.js或Ethers.js等库与部署的智能合同进行交互。
Initializing Web3 on Frontend
正在前端初始化Web3
const Web3 = require("web3");
const web3 = new Web3(new Web3.providers.HttpProvider("http://localhost:8545"));
This is the place you will provide the URL pointing to an Ethereum node that can read from the live blockchain or whichever network you're targeting. I have put http://localhost:8545. This can be used for local development while running a local Ethereum node. You can use alternatives:
在此位置,您将提供指向可从实时区块链或任何目标网络读取的以太节点的URL。我已经把http://localhost:8545.这可以在运行本地以太节点的同时用于本地开发。您可以使用其他选项:
// const web3 = new Web3(new Web3.providers.HttpProvider("https://mainnet.infura.io/v3/YOUR_INFURA_API_KEY"));
// const web3 = new Web3(new Web3.providers.HttpProvider("http://your-ethereum-node.org:8545"));
Reading Data From the Contract
从合同中读取数据
myContract.methods.getVariable().call().then(console.log);
Writing Data to the Contract
将数据写入合同
const accounts = await web3.eth.getAccounts();
myContract.methods.setVariable(5).send({ from: accounts[0] })
.then(receipt => console.log(receipt));
Note 1: If you're deploying manually, say using Remix IDE, you will find the ABI under the 'Compile' tab after you've compiled your smart contract. You can then copy this ABI and use it in your frontend code.
注1:如果您手动部署,例如使用Remix IDE,则在编译智能合约后,您将在“编译”选项卡下找到ABI。然后,您可以复制此ABI并在前端代码中使用它。
Note 2: Remember, you need to keep track of both the ABI and the contract's deployed address to interact with it from your dApp. I have put the example on the "Saving the ABI" step.
注2:请记住,您需要同时跟踪ABI和合同的部署地址,以便从您的DAPP与其交互。我已经把这个例子放在了“拯救ABI”的步骤上。
更多回答
FYI the OP asked no programming question. You wrote 100% of any code and any other detail. Please don't do this. There is literally no programming question, and no need to write a blog post for a generic question seeking opinions and general information.
仅供参考,操作员没有提出编程问题。你100%地编写了任何代码和任何其他细节。请不要这样做。从字面上讲,没有编程问题,也不需要为寻求意见和一般信息的一般性问题写博客文章。
Note that there is already a comment that was auto-generated several days ago, explaining to the OP that they needed to edit to be specific. At this point, there is nothing to answer.
请注意,已经有一条几天前自动生成的评论,向操作员解释说他们需要编辑才能具体。在这一点上,没有什么可回答的。
Thank you @DavidMakogon As I said on the other comment, I mostly write answers for the problems that I struggled before. I will be more careful.
谢谢你@DavidMakogon正如我在另一条评论中所说的,我主要是为我以前遇到的问题写答案。我会更小心的
right - the issue is that a ton of broad, general questions get posted, and they are not really helpful to Stack Overflow (and usually get closed fairly quickly). Plus, this site is not intended as a free coding resource. It's for specific programming problems.
对-问题是发布了一大堆广泛的一般性问题,它们对堆栈溢出并没有真正的帮助(通常很快就会关闭)。另外,这个站点并不是一个免费的编码资源。它是针对特定的编程问题的。
我是一名优秀的程序员,十分优秀!