首先启动Ganache
开发环境搭建
首先建立一个文件夹(HelloWorld),然后进入文件
然后 npm init 命令之后一直回车
然后 安装web3等包
最后 code . 打开vscode编辑器。
获取私有链上的信息
新建一个 index.js 文件,添加如下代码。
获取Ganache中给予我们的十个账户地址:
var localhost = "http://127.0.0.1:7545"
var Web3 = require("web3")
var web3 = new Web3(new Web3.providers.HttpProvider(localhost))
web3.eth.getAccounts(function (error, result) {
console.log("账户列表地址:");
console.log(result);
});
在vs中新建终端,输入node index.js,便可以看到十个账户的地址已经被打印出来。
部署合约
https://remix.ethereum.org/
浏览器打开remix的网址,由于我们是测试,可以选一个较为简单的合约部署,如他上面的1_Storage.sol(一个简单存数字的合约)
然后点击左侧的按钮并编译
编译后我们可以看到编译成功,点击最下方的Compilation Details可以查看编译的详情有各种的信息,我们这里需要的是WEB3DEPLOY,如下可示:
将WEB3DEPLOY里面的代码复制到我们代码的最后方,把里面的web3.eth.accounts[0]替换为我们自己的地址account_1(在Ganache给我们的十个账户中随便选一个就行),替换后的代码如下:
var localhost = "http://127.0.0.1:7545" // 设置网络,这里是我们本机ganache
var account_1 = '0x8038F0BF1CE32A31325BC4e166fcaCCFB171d1d6'; // 账户地址
var Web3 = require("web3")
var web3 = new Web3(new Web3.providers.HttpProvider(localhost))
web3.eth.getAccounts(function (error, result) { //打印出所有账户
console.log("账户列表地址:");
console.log(result);
});
var storageContract = new web3.eth.Contract([{"inputs":[],"name":"retrieve","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"uint256","name":"num","type":"uint256"}],"name":"store","outputs":[],"stateMutability":"nonpayable","type":"function"}]);
var storage = storageContract.deploy({
data: '0x608060405234801561001057600080fd5b50610150806100206000396000f3fe608060405234801561001057600080fd5b50600436106100365760003560e01c80632e64cec11461003b5780636057361d14610059575b600080fd5b610043610075565b60405161005091906100a1565b60405180910390f35b610073600480360381019061006e91906100ed565b61007e565b005b60008054905090565b8060008190555050565b6000819050919050565b61009b81610088565b82525050565b60006020820190506100b66000830184610092565b92915050565b600080fd5b6100ca81610088565b81146100d557600080fd5b50565b6000813590506100e7816100c1565b92915050565b600060208284031215610103576101026100bc565b5b6000610111848285016100d8565b9150509291505056fea264697066735822122005d160d7f76cf393033d59a64019e4eac4fd1bfc66036fb96874f3343f112b5364736f6c634300080f0033',
arguments: []
}).send({
from: account_1, // 创建合约的账户地址
gas: '4700000'
}, function (e, contract){
console.log(e, contract);
if (typeof contract.address !== 'undefined') {
console.log('Contract mined! address: ' + contract.address + ' transactionHash: ' + contract.transactionHash);
}
})
此时再在vs的终端中执行node index.js可以看到应该已经部署成功
创建完之后,回到ganache查看:花费的燃气费由创建合约的地址付。
此时合约已经在ganache上部署成功!
合约调用
首先将我们刚刚部署号的合约地址,data (从WEB3DEPLOY中复制出来的代码里面) 以及ABI加到我们的代码里
var contractAddress = '0x624c67b1E7487DDed360AFC3283093ee57Fd0486'; // 合约地址
var data = '0x608060405234801561001057600080fd5b50610150806100206000396000f3fe608060405234801561001057600080fd5b50600436106100365760003560e01c80632e64cec11461003b5780636057361d14610059575b600080fd5b610043610075565b60405161005091906100a1565b60405180910390f35b610073600480360381019061006e91906100ed565b61007e565b005b60008054905090565b8060008190555050565b6000819050919050565b61009b81610088565b82525050565b60006020820190506100b66000830184610092565b92915050565b600080fd5b6100ca81610088565b81146100d557600080fd5b50565b6000813590506100e7816100c1565b92915050565b600060208284031215610103576101026100bc565b5b6000610111848285016100d8565b9150509291505056fea264697066735822122005d160d7f76cf393033d59a64019e4eac4fd1bfc66036fb96874f3343f112b5364736f6c634300080f0033';
var contractABI = [
{
"inputs": [],
"name": "retrieve",
"outputs": [
{
"internalType": "uint256",
"name": "",
"type": "uint256"
}
],
"stateMutability": "view",
"type": "function"
},
{
"inputs": [
{
"internalType": "uint256",
"name": "num",
"type": "uint256"
}
],
"name": "store",
"outputs": [],
"stateMutability": "nonpayable",
"type": "function"
}
]
合约地址是我们部署到Ganache里合约的地址,上图有标注,ABI可以在
remix中编译详情下方复制(后续自己写合约构造合约实例来获取abi)。
然后就可以获取到合约实例并完成调用了!
var Web3 = require("web3")
var web3 = new Web3(new Web3.providers.HttpProvider(localhost))
var Storage_Contract = new web3.eth.Contract(contractABI, contractAddress)
Storage_Contract.methods.store(10000000000).send({ from : account_1}, function (error, result) {
console.log("结果_store:" + result); // acount_1调用合约的store方法存值
})
Storage_Contract.methods.retrieve().call({ from : account_1}, function(error, result) {
console.log("结果_retrieve: " + result);
})
可在终端 和 ganache 查看调用结果!
调用store方法:
在ganache中:
调用retrieve方法:
此方法仅查看,未发送交易。