windows安装truffle solidity智能合约开发框架

1 准备node环境

    版本无要求,越新越好,具体步骤自行百度。

2 truffle安装

准备一个用来存放 truffle 环境的空文件夹也就是开发合约文件所放位置

新建完成后,在此文件夹 运行powershell(我使用cmd报错),输入 npm -g install truffle 全局安装 truffle

然后输入 truffle init 会在此文件夹生成3个文件夹(contracts,migrations,test)和两个js

contracts: 合约文件所在的文件夹  文件后缀名为.sol    

migrations: 存放部署文件

test: 文件夹用来存放测试文件 

--------------------------------------------------------------------------------------------------------------------------------------------------------

3 安装Ganache客户端

本地测试合约需要本地搭建一个eth私有链,这里选择 具有图形界面的Ganache 客户端  不用同步区块,传送门:https://github.com/trufflesuite/ganache/releases  选择最新版的exe(windows)界面如下图

4 用智能合约say helloword

(1)打开contracts文件夹  新增文件 TestMyCoin.sol (命名随意  驼峰)

pragma solidity ^0.4.4;
 contract TestMyCoin {
  function sayHello() returns (string) {
   return ("Hello World");
 }
}

(2)打开migrations文件夹,新建文件,3_xx_xx.js  这里的文件名要以数字开头,而且这个数字最好是逐渐增长的,比如现在文件夹里有1_xx_xx,2_xx_xx,你新建的时候就新建3_xx_xx 

var hello = 
artifacts.require("TestMyCoin");
module.exports = function(deployer){
  deployer.deploy(hello);
}

(3)修改truffle.js 初始这个里面内容很少,下例只是简单修改目的是连上本地ganache客户端

module.exports = {
  // See <http://truffleframework.com/docs/advanced/configuration>
  // to customize your Truffle configuration!
  networks:{
   development:{
    host : "127.0.0.1",
    port : "7545",
    network_id:"*"
  }
 }
};

(4)进入那个cmd  执行 truffle compile (编译)

(5)执行 truffle migrate  (发布到区块)

(6)输入truffle console 进入交互界面 输入TestMyCoin.deployed().then(instance => contract = instance)  然后输入contract.sayHello.call()  控制台打印 hello world ,成功。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章