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"}];

 

定义地址: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

 

 

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
要开发一个基于Node.js的DApp(去中心化应用程序),你需要遵循以下步骤: 1. 初始化一个Node.js项目。进入项目的根目录,打开命令行窗口,并运行以下命令: ``` npm init ``` 2. 安装所需的依赖项。在命令行窗口中运行以下命令来安装Web3和其他相关库: ``` npm install web3 ``` 3. 在你的项目中创建一个JavaScript文件,用于编写DApp的逻辑。你可以使用Web3库来与以太坊区块链进行交互,例如发送交易、读取合约状态等。以下是一个简单的示例代码: ```javascript // 引入Web3库 var Web3 = require('web3'); // 创建Web3实例,并指定以太坊节点的URL var web3 = new Web3(new Web3.providers.HttpProvider('http://localhost:8545')); // 在这里编写你的DApp逻辑 // 例如,连接到以太坊节点、创建合约、发送交易等 ``` 4. 根据需要,你可能还需要安装其他库或工具来简化开发过程,例如Truffle框架用于合约开发、Ganache用于本地测试等。你可以根据具体需求来选择合适的工具和库。 5. 在你的项目中,创建一个入口文件,用于启动DApp。例如,在routes目录下创建一个index.js文件,并使用Express框架来创建HTTP服务器。以下是一个简单的示例代码: ```javascript // 引入Express库 var express = require('express'); var app = express(); // 设置路由 app.get('/', function(req, res) { // 在这里处理请求,并返回响应 }); // 启动HTTP服务器 app.listen(3000, function() { console.log('DApp已启动,访问 http://localhost:3000'); }); ``` 6. 启动你的DApp。在命令行窗口中,进入你的项目根目录,并运行以下命令: ``` node index.js ``` 这样,你就可以通过访问 http://localhost:3000 来访问你的DApp了。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值