web3入门-web3.js通过Ganache连接truffle智能合约

//环境
Truffle v5.5.21 (core: 5.5.21)
Ganache v7.2.0
Solidity v0.5.16 (solc-js)
Node v14.17.3
Web3.js v1.7.4

一、用truffle框架在本地部署智能合约

1.建个文件夹 truffle_demo

mkdir truffle_demo

2.进入文件夹内,执行init命令,会搭建基本框架出来

truffle init

3.在contracts下新建一个文件,比如DataDemo.sol

// SPDX-License-Identifier: MIT
pragma solidity >=0.4.22 <0.9.0;
contract DataDemo{
    string public returnTest;
    //set方法
    function setData(string memory str ) public payable {
        returnTest = str;
    }
    //get方法
    function getData() public view returns (string memory) {
        return returnTest;
    }
}

4.修改migrations/1_initial_migration.js

const Migrations = artifacts.require("Migrations");
var DataDemo = artifacts.require("./DataDemo.sol");

module.exports = function(deployer) {
  deployer.deploy(Migrations);
  d
  • 2
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Truffle框架是一个用于以太坊智能合约开发的开发环境和工具集,Ganache则是一个用于本地测试以太坊智能合约的工具。下面是使用Truffle框架和Ganache网络进行智能合约部署的具体流程和代码。 1. 安装Truffle框架和Ganache网络 首先需要在本地安装Truffle框架和Ganache网络。可以使用npm命令进行安装: ``` npm install -g truffle npm install -g ganache-cli ``` 2. 创建Truffle项目 使用Truffle框架创建一个新的项目: ``` truffle init ``` 这将会在当前目录下创建一个名为`truffle-config.js`的配置文件和一个名为`contracts`的合约目录。 3. 编写智能合约代码 在`contracts`目录下创建一个名为`MyContract.sol`的智能合约文件,并编写合约代码。例如,创建一个简单的存储合约: ``` pragma solidity ^0.8.0; contract MyContract { uint256 private value; function setValue(uint256 newValue) public { value = newValue; } function getValue() public view returns (uint256) { return value; } } ``` 4. 配置Truffle项目 在`truffle-config.js`文件中配置Truffle项目。首先需要指定要使用的网络,这里使用Ganache网络: ``` module.exports = { networks: { development: { host: "localhost", port: 8545, network_id: "*" } } }; ``` 5. 编译智能合约 使用Truffle框架编译智能合约: ``` truffle compile ``` 6. 部署智能合约 使用Truffle框架部署智能合约: ``` truffle migrate ``` 这将会将智能合约部署到Ganache网络上。 7. 与智能合约交互 现在可以使用Web3.js或其他以太坊客户端库与智能合约进行交互。以下是一个使用Web3.js与上一步中部署的存储合约进行交互的示例代码: ``` const Web3 = require('web3'); const web3 = new Web3('http://localhost:8545'); const myContract = new web3.eth.Contract([{"inputs":[],"stateMutability":"nonpayable","type":"constructor"},{"inputs":[{"internalType":"uint256","name":"newValue","type":"uint256"}],"name":"setValue","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[],"name":"getValue","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"}], '0x1234567890123456789012345678901234567890'); myContract.methods.setValue(42).send({from: '0x1234567890123456789012345678901234567890'}) .then(() => myContract.methods.getValue().call()) .then(value => console.log(value)); ``` 这段代码首先创建了一个Web3实例,并连接Ganache网络。然后创建了一个`myContract`实例,它表示上一步中部署的存储合约。最后使用`myContract`实例调用`setValue`方法将值设置为42,并使用`getValue`方法获取当前值并输出到控制台。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值