美文网首页
行云流水地实现Ganache+Node+Web3js合约部署、通

行云流水地实现Ganache+Node+Web3js合约部署、通

作者: 祥龙翔天 | 来源:发表于2021-03-27 17:18 被阅读0次

    君不见一个简单的东西没有一个好的流程去说明一下,折腾起人来那是相当的费劲

    本文旨在让感兴趣的客官,能够快速体验到区块链dapp开发的流程,不对步骤做任何详细的解释

    流程跑通后,再去学习,相信你会感觉更加良好

    启动 Ganache

    Ganache启动后会生成10个账号地址,如红色框便是第一个地址,记为账户地址A
    同时请记住RPC SERVER地址,如红色箭头所示,记为服务器地址B

    Ganache启动

    切记暂时不要关闭Ganache

    环境创造

    请确保安装了 node 和 npm (命令node -v以及命令npm -v查看 )

    并创建webjs3Test目录(命令mkdir webjs3Test以及命令cd webjs3Test

    在该目录下安装web3模块(命令npm --registry http://registry.npm.taobao.org install web3

    环境安装

    执行命令npm init后一路回车

    用vscode打开当前目录(命令code .

    其代买结构如下


    代买结构

    账户信息获取

    没错,就是这么直接,我们现在就要获取账户信息了,创建index.js

    创建index.js

    并赋予其如下代码

    var Web3 = require("web3")
    var web3 = new Web3(new Web3.providers.HttpProvider("http://localhost:7545"))
    web3.eth.getAccounts()
        .then(function (accounts) {
            console.log("打印账户地址列表:");
            console.log(accounts);
        })
    

    看代码中的打印行,我们就这么直接的获取Ganache中的账户并打印出来,是的,你没看错,就是这么直接,简单地一批

    执行命令node index.js后,得到如下结果

    账户信息

    恭喜,成果获取到Ganache上的10个账户信息

    合约部署

    浏览器访问地址 http://remix.ethereum.org/ 并,定位到一个简单的例子上,如1_Storage.sol

    1_Storage.sol

    然后按照箭头3所示切入到编译功能区

    编译区

    点击Compile 1_Stroage.so按钮,编译成功后,会显示出编译详情等信息,此刻左侧的编译区按钮回有一个绿色的勾

    点击编译详情后出现下面的界面

    编译详情

    滚动到WEB3DEPLOY,然后点击箭头出的复杂图标,将其拷贝到前面的代码后面

    var Web3 = require('web3')
    
    var web3 = new Web3(new Web3.providers.HttpProvider('http://localhost:7545'))
    
    web3.eth.getAccounts().then(function (account) {
        console.log("打印账户地址列表:");
        console.log(account);
    })
    
    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: '0x608060405234801561001057600080fd5b5061012f806100206000396000f3fe6080604052348015600f57600080fd5b506004361060325760003560e01c80632e64cec11460375780636057361d146051575b600080fd5b603d6069565b6040516048919060c2565b60405180910390f35b6067600480360381019060639190608f565b6072565b005b60008054905090565b8060008190555050565b60008135905060898160e5565b92915050565b60006020828403121560a057600080fd5b600060ac84828501607c565b91505092915050565b60bc8160db565b82525050565b600060208201905060d5600083018460b5565b92915050565b6000819050919050565b60ec8160db565b811460f657600080fd5b5056fea264697066735822122062db17618d746a1967495ede611efc2c1e881cb29cbd6b40b23bd35a720c134c64736f6c63430008010033', 
         arguments: [
         ]
    }).send({
         from: web3.eth.accounts[0], 
         gas: '4700000'
       }, function (e, contract){
        console.log(e, contract);
        if (typeof contract.address !== 'undefined') {
             console.log('Contract mined! address: ' + contract.address + ' transactionHash: ' + contract.transactionHash);
        }
     })
    

    执行命令node index.js后,得到如下结果

    运行出错

    好吧,直接拷贝过来的代码看来不能正常运行,根据提示(红色标注),我们将14行的代码替换成账户地址A

    var Web3 = require("web3")
    
    var accound0Address = "0x6F5830e61F4ca16844261c118EE285CFa1006f5e"
    
    var web3 = new Web3(new Web3.providers.HttpProvider("http://localhost:7545"))
    web3.eth.getAccounts()
        .then(function (accounts) {
            console.log("打印账户地址列表:");
            console.log(accounts);
        })
    
    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: '0x608060405234801561001057600080fd5b5061012f806100206000396000f3fe6080604052348015600f57600080fd5b506004361060325760003560e01c80632e64cec11460375780636057361d146051575b600080fd5b603d6069565b6040516048919060c2565b60405180910390f35b6067600480360381019060639190608f565b6072565b005b60008054905090565b8060008190555050565b60008135905060898160e5565b92915050565b60006020828403121560a057600080fd5b600060ac84828501607c565b91505092915050565b60bc8160db565b82525050565b600060208201905060d5600083018460b5565b92915050565b6000819050919050565b60ec8160db565b811460f657600080fd5b5056fea264697066735822122062db17618d746a1967495ede611efc2c1e881cb29cbd6b40b23bd35a720c134c64736f6c63430008010033',
        arguments: [
        ]
    }).send({
        from: accound0Address,
        gas: '4700000'
    }, function (e, contract) {
        console.log(e, contract);
        if (typeof contract.address !== 'undefined') {
            console.log('Contract mined! address: ' + contract.address + ' transactionHash: ' + contract.transactionHash);
        }
    })
    

    执行命令node index.js后,得到如下结果

    部署成功

    看上去是部署成功了,并打印出了交易的hash值,我们切换到Ganache里面去确认对比

    Ganache部署详情1 Ganache部署详情2

    没错,一样的,说明部署成功了,同时,在红色框的右下方,为我们新生成了一个合约地址,记为合约地址C

    拿着合约地址无法无天

    与之前获取合约的方式不同,我们需要根据合约地址C重新获取合约

    var Web3 = require('web3')
    
    var accound0Address = "0x6F5830e61F4ca16844261c118EE285CFa1006f5e"
    var constractAddress = "0x3bCE924200544a39e085F6219e1750B00294e629"
    
    var web3 = new Web3(new Web3.providers.HttpProvider("http://localhost:7545"))
    web3.eth.getAccounts()
        .then(function (accounts) {
            console.log("打印账户地址列表:");
            console.log(accounts);
        })
    
    var abi = [{"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 data = "608060405234801561001057600080fd5b5060016000819055506101dd806100286000396000f3fe608060405234801561001057600080fd5b50600436106100365760003560e01c80638ada066e1461003b578063978033d514610059575b600080fd5b610043610075565b60405161005091906100e6565b60405180910390f35b610073600480360381019061006e91906100ae565b61007e565b005b60008054905090565b8060008082825461008f9190610101565b9250508190555050565b6000813590506100a881610190565b92915050565b6000602082840312156100c057600080fd5b60006100ce84828501610099565b91505092915050565b6100e081610157565b82525050565b60006020820190506100fb60008301846100d7565b92915050565b600061010c82610157565b915061011783610157565b9250827fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff0382111561014c5761014b610161565b5b828201905092915050565b6000819050919050565b7f4e487b7100000000000000000000000000000000000000000000000000000000600052601160045260246000fd5b61019981610157565b81146101a457600080fd5b5056fea2646970667358221220775d75a36f63dd518f87860a69f2615634b9e7934291e93d625e5136868e8bdb64736f6c63430008010033";
    
    var myContract = new web3.eth.Contract(abi, constractAddress);
    
    myContract.options.data = data;
    
    myContract.methods.retrieve().call({ from: accound0Address }, function (error, result) {
        console.log("结果:" + result);
    });
    

    我们把 abi以及data从之前的代码中提取出来,可以清晰的发现,新的合约获取,直接传入了constractAddress参数

    之后,我们调用了合约中的retrieve方法(在合约部署小节中定义),并将结果打印

    执行命令node index.js后,得到如下结果

    方法调用结果1

    可见,能够正常获取到 值

    我们再调用合约中的store方法(在合约部署小节中定义),并将结果打印

    var Web3 = require('web3')
    var accound0Address = "0x6F5830e61F4ca16844261c118EE285CFa1006f5e"
    var constractAddress = "0x3bCE924200544a39e085F6219e1750B00294e629"
    
    var web3 = new Web3(new Web3.providers.HttpProvider("http://localhost:7545"))
    web3.eth.getAccounts()
        .then(function (accounts) {
            console.log("打印账户地址列表:");
            console.log(accounts);
        })
    
    var abi = [{"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 data = "608060405234801561001057600080fd5b5060016000819055506101dd806100286000396000f3fe608060405234801561001057600080fd5b50600436106100365760003560e01c80638ada066e1461003b578063978033d514610059575b600080fd5b610043610075565b60405161005091906100e6565b60405180910390f35b610073600480360381019061006e91906100ae565b61007e565b005b60008054905090565b8060008082825461008f9190610101565b9250508190555050565b6000813590506100a881610190565b92915050565b6000602082840312156100c057600080fd5b60006100ce84828501610099565b91505092915050565b6100e081610157565b82525050565b60006020820190506100fb60008301846100d7565b92915050565b600061010c82610157565b915061011783610157565b9250827fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff0382111561014c5761014b610161565b5b828201905092915050565b6000819050919050565b7f4e487b7100000000000000000000000000000000000000000000000000000000600052601160045260246000fd5b61019981610157565b81146101a457600080fd5b5056fea2646970667358221220775d75a36f63dd518f87860a69f2615634b9e7934291e93d625e5136868e8bdb64736f6c63430008010033";
    
    var myContract = new web3.eth.Contract(abi, constractAddress);
    
    myContract.options.data = data;
    
    myContract.methods.store(8).send({ from: accound0Address }, function (error, result) {
        console.log("结果2s:" + result);
    });
    
    myContract.methods.retrieve().call({ from: accound0Address }, function (error, result) {
        console.log("结果:" + result);
    });
    

    执行命令node index.js后,得到如下结果

    方法调用结果2

    结果还是0,这看起来不科学,应该是8呀。客官别急,我们执行的store和retrieve方法是异步的,结果不同步是应该的

    再来一遍,执行命令node index.js后,得到如下结果

    方法调用结果3

    结果是8,那么就对了,我们再去核对一下交易hash

    交易信息对比

    果然能对上,又一次成功了

    完!

    相关文章

      网友评论

          本文标题:行云流水地实现Ganache+Node+Web3js合约部署、通

          本文链接:https://www.haomeiwen.com/subject/salyhltx.html