node.js本地安装和Truffle安装

node.js安装

1. NodeJS 的下载地址: http://nodejs.cn/download/     win64.zip
2. path 环境变量中 , 添加 nodejs 路径       
3. node ­h 获取各种参数信息
4. node test.js 可以执行当前目录的 test.js 文件
 

使用淘宝 NPM 镜像安装cnpm

$ npm install -g cnpm --registry=https://registry.npm.taobao.org
加快安装truffle

Truffle介绍与开发环境搭建

  1. 内置智能合约编译、链接、开发和二进制管理
  2. 快速开发的自动化合约测试
  3. 脚本、可扩展性部署和迁移框架
  4. 为合约通讯提供交互式平台
  5. Truffle的环境中执行外部的运行脚本

// -g为全局变脸安装,会主动安装到node.js文件夹下面

$cnpm install -g truffle

truffle的使用文档链接http://truffleframework.com/docs

开启truffle

进入cmd,truffle init 时候要在一个空白文件夹

//在profiles里面创建demo

下载使用Atom https://atom.io/

下载插件 进入到Setting 里面的install

1: autocomplete-solidity 代码自动补全

2:language-ethereum 支持高亮

3:linter-solium

4.linter-solidity 代码错误检查

有些可能安装不了,可以进入github下载zip文件再解压到C:/User/huanggang/.atom/package.

然后再进入cmd,使用cnpm install 就可以了。

Powershell的使用

Powershell vista 就出现了 , 用来代替 CMD . Powershell cmd 的超集,换句话说,
cmd 能做的事情, Powershell 都能做。
 
win+r;将cmd换成powershell。。。。但是我的win10好像不能够进入开发者模式
 

Truffle 命令

首先进入到项目的目录,

//进入到开发者模式

>truffle develop 

//执行完上面命令,会出现9个账户和私钥

>compile      //编译,atom会出现buildwenjian

>deploy       //部署

//常用命令

 build     Execute build pipeline (if configuration present)
  compile   Compile contract source files
  config    Set user-level configuration options
  console   Run a console with contract abstractions and commands
            available
  create    Helper to create new contracts, migrations and tests
  debug     Interactively debug any transaction on the blockchain
            (experimental)
  deploy    (alias for migrate)
  develop   Open a console with a local development blockchain
  exec      Execute a JS module within this Truffle environment
  help      List all commands or provide information about a specific
            command
  init      Initialize new and empty Ethereum project
  install   Install a package from the Ethereum Package Registry
  migrate   Run migrations to deploy contracts
  networks  Show addresses for deployed contracts on each network
  obtain    Fetch and cache a specified compiler
  opcode    Print the compiled opcodes for a given contract
  publish   Publish a package to the Ethereum Package Registry
  run       Run a third-party command
  test      Run JavaScript and Solidity tests
  unbox     Download a Truffle Box, a pre-built Truffle project
  version   Show version number and exit
  watch     Watch filesystem for changes and rebuild the project
            automatically

See more at http://truffleframework.com/docs

Web3.js

//web3中文文档,使用方式都在文档当中

http://cw.hubwiz.com/card/c/web3.js-1.0/1/2/9/    //新文档

https://web3.tryblockchain.org/Web3.js-api-refrence.html   //旧文档

 web3.eth.getAccounts()
[
  '0xF403C41247461bAa730d01A137bF162f4a5017a7',
  '0x39be9593b363be46cDa1fE5aD1D2D75266fC2407',
  '0x2A3132C7B50c93cdaD819c144781ed26E2778923',
  '0xDa9feF73d080F4aEb29592e8716393e33a4EF266',
  '0x4fdEa3A6Cd070475e53d6bD7B3d4CC7CF3C9f260',
  '0x32cf15CAd668321B564a4BFe267D34c8a0179694',
  '0x8eB5f3be5ACAF382a94f6070455Fb9c73E47606b',
  '0xad15971e404a92C5923C2a880ea66EaF35524Db0',
  '0xf5d0a4FA670F0F8a3ed942795231bf203D140b3d',
  '0xa393d733E8b7087cA737003410cf87225C904D49'
]
 web3.eth.sendTransaction({from:'0xF403C41247461bAa730d01A137bF162f4a5017a7',to:'0x39be9593b363be46cDa1fE5aD1D2D75266fC2407',value:'1000000'})
truffle(develop)> web3.eth.getBalance( '0xf403c41247461baa730d01a137bf162f4a5017a7');
'99999915999998999990'
truffle(develop)> web3.eth.getBalance('0x39be9593b363be46cda1fe5ad1d2d75266fc2407')
'100000000000001000010'

web3调用智能合约

1.进入项目目录

>truffle develop

2.编译,部署

>compile

>deploy        //得使用合约的地址contract address

 Deploying 'Mycontract'
   ----------------------
   > transaction hash:    0x133a140cc4166fffb1360a89712182283513332c6a37d56c3db732a946b829ea
   > Blocks: 0            Seconds: 0
   > contract address:    0xCDAc9842125f7399bE0492d9f4B40AC69a62b34E
   > block number:        5
   > block timestamp:     1573567067
   > account:             0xF403C41247461bAa730d01A137bF162f4a5017a7
   > balance:             99.99172593999899999
   > gas used:            103739
   > gas price:           20 gwei
   > value sent:          0 ETH
   > total cost:          0.00207478 ETH

 

3.将atom中的json压缩成一行 

https://www.sojson.com/yasuoyihang.html  //使用压缩网址

 var abi=[{"constant":false,"inputs":[{"internalType":"uint256","name":"x","type":"uint256"},{"internalType":"uint256","name":"y","type":"uint256"}],"name":"Cal","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"payable":false,"stateMutability":"nonpayable","type":"function"}];

node.js本地安装和Truffle安装_第1张图片

 

定义地址:truffle(develop)> var address="0x6E8253010690046EBc9F48F574Bf83E0d1b53A66"
合约部署:truffle(develop)>var myContract=new web3.eth.Contract(abi,address);

调用方法:truffle(develop)> myContract.methods.myFunction().call();
'2'

// web3.js
var MyContract = new web3.eth.contract(abi, address);
MyContract.methods.myFunction().call()
.then(console.log);
> "Hello!%"

unbox项目演示与改造

官方提供了很多基于 truffle react 整合的 dmoe, 我们只需要在官方 Demo 基础上搭
建自己的项目即可 , 官方地址: http://truffleframework.com/boxes/
 

从官方下载unbox项目    //这不是一个完整的DAPP只是一个开始

//在node。js中,每个项目都有一个package。json,此类文件类似maven的pom.xml此文件定义了项目所需要的各种配置,和当前项目依赖的资源
//name:定义项目名称,version:定义当前项目的版本
//main:定义当前加载的入口,truffle。js就是truffle框架的核心配置文件
//scripts:制定了当前脚本命令的缩写,npm run dev --》nom run lite-serve
//devDependencies:定义当前项目开发时需要依赖模块(都存储在node_modules)


{
  "name": "tutorialtoken",
  "version": "1.0.0",
  "description": "",
  "main": "truffle.js",
  "directories": {
    "test": "test"
  },
  "scripts": {
    "dev": "lite-server",
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "",
  "license": "ISC",
  "devDependencies": {
    "lite-server": "^2.3.0"
  }
}

Zeppelin­solidity搭建标准代币

下载此文件

[C:\Program Files\solidity\demo03]$ cnpm install zeppelin-solidity
鈭?Installed 1 packages
鈭?Linked 0 latest versions
鈭?Run 0 scripts
deprecate zeppelin-solidity@* This package has been renamed to openzeppelin-solidity. Please update your dependency, or you will no longer receive updates.
鈭?All packages installed (1 packages installed from npm registry, used 2s(network 2s), speed 41.34kB/s, json 1(2.89kB), tarball 61.43kB)

pragma solidity >=0.4.21 <0.6.0;

/* import "zeppelin‐solidity/contracts/token/ERC20/StandardToken.sol"; */
import 'zeppelin-solidity/contracts/token/ERC20/StandardToken.sol';
contract TutorialToken is StandardToken {
  //代币名称
  string public name = "TutorialToken";
  //代笔标志
  string public symbol = "TT";
  /* uint8 public decimals = 2; */ //代笔单位
  //指定代币发布数量
  uint public INITIAL_SUPPLY = 12000;

  //构造函数
  constructor() public {
    // 继承于父类 BasicToken的属性
    totalSupply_ = INITIAL_SUPPLY;
    balances[msg.sender] = INITIAL_SUPPLY;
}
}

//编译
>compile
//部署
>deploy

//重新进入cmd 项目目录下面
>npm run dev

 

建立Package.json配置文件

https://www.trufflesuite.com/tutorials/robust-smart-contracts-with-openzeppelin

 

 

你可能感兴趣的:(智能合约)