truffle 框架简单运用

truffle 框架HelloWorld简单运用

安装truffle:npm install -g truffle

contracts --soloidity 的源码
migration --是用来编译solidity的js代码
test 用来测试
config 配置文件

安装ganache:https://www.trufflesuite.com/ganache

安装完后打开选择quickstart。

ganache 一个已经封装好的 以太坊测试网

truffle init 创建好后

简单的HelloWorld.sol复制到contracts文件夹

// SPDX-License-Identifier: MIT
pragma solidity ^0.5.16;
contract HelloWorld{
 
    string  _name;

    constructor() public {
      _name = "liuminghui";
    }
    function getName() public view returns(string memory ) {
        return _name;
    }
    function changeName(string memory name) public {
     _name = name;      
    }
}

在migrations下创建2_deploy_HelloWorld.js文件

const HelloWorld = artifacts.require("HelloWorld");

module.exports = function (deployer) {
  deployer.deploy(HelloWorld);
};

写好代码后在文件夹下打开终端

truffle migrate进行编译,并部署在本地测试网

truffle console 来进入console

HelloWorld.deployed().then(instance=>{app = instance});

然后调用方法:app.getName();等其他方法

还可以调用web3库等

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值