mac下搭建以太坊开发环境

2019-04-12  本文已影响0人  瓦莉拉拉

0 环境准备

nodejs npm

1 直接brew 的方式

brew install ethereum
...
geth -v

2 安装truffle

npm install -g truffle
安装完成后,

truffle -v
Truffle v5.0.12 - a development framework for Ethereum
...

3 安装Ganache

npm install -g ganache-cli
启动 测试服务,指定输入日志到当前执行木灵,允许外部访问

mkdir ethereum
cd ethereum
ganache-cli>>trace.log -h 0.0.0.0 -p 8545

4 创建一个合约

4.1 新建一个测试用例 helloworld

mkdir helloworld
cd helloworld

4.2 初始化一个truffle工程

真心好用哇

trufful init

4.3 修改 truffle-config.js 文件

compilers: {
solc: {
version: "^0.4.21",
指定版本 0.4.21及以上。默认是0.5
配置network环境
networks: {
...
development: {
host: "127.0.0.1", // Localhost (default: none)
port: 8545, // Standard Ethereum port (default: none)
network_id: "*", // Any network (default: none)
},

4.4 编写合约

在contracts目录下,新建Hello.sol

pragma solidity ^0.4.4;

contract Hello {
  function sayHello() public pure returns (string) {
    return ("Hello World");
  }
}

tip:可以先把代码贴到Remix可以做语法检查,注意调整右边的版本

image.png

4.5 编译合约

truffle compile

执行后会在工程目录下生成一个build的文件夹,编译好的合约会存放在build下面的contracts,Hello.json


image.png

4.6 发布合约

truffle migrate

4.7 发布成功后,进入truffle console控制台部署合约

truffle console
Hello.deployed().then(instance=>contract = instance)

把合约的实例赋值给contract,执行

contract.sayHello()
'Hello World'

参考

truffle官方文档
手把手教你搭建智能合约测试环境、开发、编译、部署以及如何通过JS调用合约方法

上一篇 下一篇

猜你喜欢

热点阅读