咸鱼

咸鱼是以盐腌渍后,晒干的鱼

0%

8.2、基于truffle发布Token到Ropsten测试网络

我们利用 truffle 的集成环境发布一个名称为 VToken 的代币到Ropsten测试网络。

开发环境

本示例在Ubuntu下开发(原因下面会解释)

1
2
node:v10.16.0
npm:v6.9.0

truffle常用命令

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
samwen@samwen-ubuntu:~/VToken$ truffle -v
Truffle v5.0.24 - a development framework for Ethereum

Usage: truffle <command> [options]

Commands:
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

初始化项目

由于不需要webapp,所以直接初始化一个truffle项目即可。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
samwen@samwen-ubuntu:~$ mkdir VToken
samwen@samwen-ubuntu:~$ cd VToken/
samwen@samwen-ubuntu:~/VToken$ truffle init

✔ Preparing to download
✔ Downloading
✔ Cleaning up temporary files
✔ Setting up box

Unbox successful. Sweet!

Commands:

Compile: truffle compile
Migrate: truffle migrate
Test contracts: truffle test
samwen@samwen-ubuntu:~/VToken$ tree
.
├── contracts
│   └── Migrations.sol
├── migrations
│   └── 1_initial_migration.js
├── test
└── truffle-config.js

3 directories, 3 files

编写ERC20标准智能合约

为了方便测试,我们继承 zeppelin-solidity 的ERC20合约,EIP-20标准文档

1
2
3
4
5
6
7
8
9
samwen@samwen-ubuntu:~/VToken$ vim package.json
{
"name": "VToken",
"version": "1.0.0",
"dependencies": {
}
}
samwen@samwen-ubuntu:~/VToken$ sudo npm install zeppelin-solidity
samwen@samwen-ubuntu:~/VToken$ vim /contracts/VToken.sol

VToken合约代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
pragma solidity ^0.4.24;

import 'zeppelin-solidity/contracts/token/ERC20/StandardToken.sol';

contract VToken is StandardToken {

string public name = "VToken";
string public symbol = "VTK";
uint8 public decimals = 18;
uint public INITIAL_SUPPLY = 1000000000;//10亿


constructor()public{

totalSupply_ = INITIAL_SUPPLY;//此处发行量错误,看:修复!
balances[msg.sender] = INITIAL_SUPPLY;
}
}
** 修复!修复!修复!** ** 修复!修复!修复!** ** 修复!修复!修复!**

发布之后发现账号的VToken余额都是 0 ,才发现:

这里发行量 INITIAL_SUPPLY 搞错了,小数点是18位,应该还需要加18个零才是10亿。当然也可以设置 decimals=0 ,那么 VToken 就没有小数,最小单位就是 1VTk
可以将构造函数修改为以下(decimals=18):

1
2
3
//补“小数位”个数的零,这里是18。
totalSupply_ = INITIAL_SUPPLY * 10 ** uint256(decimals);
balances[msg.sender] = totalSupply_;

编译&本地测试部署

truffle-config.js 重命名为 truffle.js ,并修改内容为:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
module.exports = {

networks: {
development: {
host: "127.0.0.1",
port: 8545,
network_id: "*",
gas: 6721975,
gasPrice: 20000000000
},
},
compilers: {
solc: {
version: "0.4.24"
}
}
}

在migrations目录下新建 2_deploy_contracts.js ,配置部署我们的合约。

1
2
3
4
5
6
const VToken = artifacts.require("VToken");

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

开始编译

1
2
3
4
5
6
7
8
samwen@samwen-ubuntu:~/VToken$ truffle compile

Compiling your contracts...
===========================
> Compiling ./contracts/VToken.sol
> Artifacts written to /home/samwen/VToken/build/contracts
> Compiled successfully using:
- solc: 0.4.24+commit.e67f0147.Emscripten.clang

编译通过,用一个新的终端启动本地 testrpc

如果没有安装,运行: npm install -g ganache-cli

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
samwen@samwen-ubuntu:~$ ganache-cli
Ganache CLI v6.4.4 (ganache-core: 2.5.6)
.....
.....
.....
HD Wallet
==================
Mnemonic: copy speak kitten fiscal wrestle stool unfair alcohol source siren chicken patient
Base HD Path: m/44'/60'/0'/0/{account_index}

Gas Price
==================
20000000000

Gas Limit
==================
6721975

Listening on 127.0.0.1:8545

开始部署

deploy 和 migrate 一样

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
samwen@samwen-ubuntu:~/VToken$ truffle deploy

Compiling your contracts...
===========================
> Everything is up to date, there is nothing to compile.


Starting migrations...
======================
> Network name: 'development'
> Network id: 1561694497418
> Block gas limit: 0x6691b7


1_initial_migration.js
======================

Deploying 'Migrations'
----------------------
> transaction hash: 0x327b6ca1a13f4d95206953a18b7228f6258db111ddfc94c7e19a2e2368189975
> Blocks: 0 Seconds: 0
> contract address: 0xbf744a6b346e8c4f1335077F3Fbcb94624E80F86
> block number: 5
> block timestamp: 1561695101
> account: 0x43df1995a777C2b14D011D565beE7411109DaeF3
> balance: 99.95728774
> gas used: 277462
> gas price: 20 gwei
> value sent: 0 ETH
> total cost: 0.00554924 ETH


> Saving migration to chain.
> Saving artifacts
-------------------------------------
> Total cost: 0.00554924 ETH


2_deploy_contracts.js
=====================

Deploying 'VToken'
------------------
> transaction hash: 0x21c591204930427919cdcbbd2f6b98895174e2798c0bed8499e7d7b8198c3403
> Blocks: 0 Seconds: 0
> contract address: 0x8E4FBa7256b92837723688AFFFc7DC6679d3d538
> block number: 7
> block timestamp: 1561695102
> account: 0x43df1995a777C2b14D011D565beE7411109DaeF3
> balance: 99.92621412
> gas used: 1511673
> gas price: 20 gwei
> value sent: 0 ETH
> total cost: 0.03023346 ETH


> Saving migration to chain.
> Saving artifacts
-------------------------------------
> Total cost: 0.03023346 ETH


Summary
=======
> Total deployments: 2
> Final cost: 0.0357827 ETH

samwen@samwen-ubuntu:~/VToken$

到此证明我的项目的合约是没有问题的了,那么接下来就可以尝试部署到公网Ropsten网络。

注册infura.io

infura提供公开以太坊和测试节点,在 https://infura.io/login 用邮箱注册一个账号和新建project,得到一个带token的链接地址。

infura的使用

申请ETH账号

  1. 用MetaMask申请一个账号(我申请的是: 0xAFEbF61AF27866a27D839f47CfBef7dd415bAB65
  2. 选择“Ropsten测试网络”
  3. 点击“存入”,从“测试水管”领取一个ETH
  4. 导出“助记词”

如果安装MetaMask插件遇到网络问题,可以临时用网页钱包创建账号。

Ropsten网络配置

修改 truffle.js

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
var HDWalletProvider = require("truffle-hdwallet-provider");

var mnemonic = "MetaMask导出的助记词";
var infura = "https://ropsten.infura.io/v3/{你的token}}";

module.exports = {

networks: {
development: {
host: "127.0.0.1", // Localhost (default: none)
port: 8545, // Standard Ethereum port (default: none)
network_id: "*", // Any network (default: none)
},

ropsten: {
provider: () => new HDWalletProvider(mnemonic, infura),
network_id: 3, // Ropsten's id
gas: 6721975,
gasPrice: 20000000000
},
},
compilers: {
solc: {
version: "0.4.24"
}
}
}

安装依赖

1
samwen@samwen-ubuntu:~/VToken$ npm install truffle-hdwallet-provider --unsafe-perm=true

此处坑比较多,特别是Win10下,文章后面描述在Ubuntu下的解决办法 ,《hdwallet安装问题》

部署到Ropsten网络

本次部署运气比较好,只遇到一次网络错误就部署成功了,有时候运行半天都部署不了。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
samwen@samwen-ubuntu:~/VToken$ truffle deploy --reset --network ropsten
Compiling your contracts...
===========================
> Everything is up to date, there is nothing to compile.


Migrations dry-run (simulation)
===============================
> Network name: 'ropsten-fork'
> Network id: 3
> Block gas limit: 0x7a1200


1_initial_migration.js
======================
Error: Invalid JSON RPC response: ""
at Object.InvalidResponse (/home/samwen/VToken/node_modules/truffle-hdwallet-provider/dist/webpack:/truffle-hdwallet-provider/Users/tyler/projects/truffle/node_modules/web3-providers-http/node_modules/web3-core-helpers/src/errors.js:42:1)
at e.InvalidResponse [as onreadystatechange] (/home/samwen/VToken/node_modules/truffle-hdwallet-provider/dist/webpack:/truffle-hdwallet-provider/Users/tyler/projects/truffle/node_modules/web3-providers-http/src/index.js:92:1)
at e._a [as dispatchEvent] (/home/samwen/VToken/node_modules/truffle-hdwallet-provider/dist/webpack:/truffle-hdwallet-provider/Users/tyler/projects/truffle/node_modules/xhr2-cookies/dist/xml-http-request-event-target.js:27:61)
at e.dispatchEvent [as _setReadyState] (/home/samwen/VToken/node_modules/truffle-hdwallet-provider/dist/webpack:/truffle-hdwallet-provider/Users/tyler/projects/truffle/node_modules/xhr2-cookies/dist/xml-http-request.js:208:1)
at e._setReadyState [as _onHttpRequestError] (/home/samwen/VToken/node_modules/truffle-hdwallet-provider/dist/webpack:/truffle-hdwallet-provider/Users/tyler/projects/truffle/node_modules/xhr2-cookies/dist/xml-http-request.js:349:1)
at ClientRequest._onHttpRequestError (/home/samwen/VToken/node_modules/truffle-hdwallet-provider/dist/webpack:/truffle-hdwallet-provider/Users/tyler/projects/truffle/node_modules/xhr2-cookies/dist/xml-http-request.js:252:47)
at ClientRequest.emit (events.js:198:13)
at TLSSocket.socketErrorListener (_http_client.js:392:9)
at TLSSocket.emit (events.js:198:13)
at emitErrorNT (internal/streams/destroy.js:91:8)
at emitErrorAndCloseNT (internal/streams/destroy.js:59:3)
at process._tickCallback (internal/process/next_tick.js:63:19)

Deploying 'Migrations'
----------------------
> block number: 5878867
> block timestamp: 1561706914
> account: 0xAFEbF61AF27866a27D839f47CfBef7dd415bAB65
> balance: 1.99475076
> gas used: 262462
> gas price: 20 gwei
> value sent: 0 ETH
> total cost: 0.00524924 ETH

-------------------------------------
> Total cost: 0.00524924 ETH


2_deploy_contracts.js
=====================

Deploying 'VToken'
------------------
> block number: 5878869
> block timestamp: 1561706947
> account: 0xAFEbF61AF27866a27D839f47CfBef7dd415bAB65
> balance: 1.96577714
> gas used: 1421673
> gas price: 20 gwei
> value sent: 0 ETH
> total cost: 0.02843346 ETH

-------------------------------------
> Total cost: 0.02843346 ETH


Summary
=======
> Total deployments: 2
> Final cost: 0.0336827 ETH


Starting migrations...
======================
> Network name: 'ropsten'
> Network id: 3
> Block gas limit: 0x7a1200


1_initial_migration.js
======================

Deploying 'Migrations'
----------------------
> transaction hash: 0xcb0b58dc9e78627a5c703c605372d95a21a38d477223a34cb50674e67a035f95
> Blocks: 1 Seconds: 12
> contract address: 0x43A01b6F8f49906f29F12af26572c341b9720719
> block number: 5878879
> block timestamp: 1561706995
> account: 0xAFEbF61AF27866a27D839f47CfBef7dd415bAB65
> balance: 1.99445076
> gas used: 277462
> gas price: 20 gwei
> value sent: 0 ETH
> total cost: 0.00554924 ETH


> Saving migration to chain.
> Saving artifacts
-------------------------------------
> Total cost: 0.00554924 ETH


2_deploy_contracts.js
=====================

Deploying 'VToken'
------------------
> transaction hash: 0x877fd23aeec91a37c076bee389e13563ffa67398a6c264ae9760033f897d080f
> Blocks: 1 Seconds: 22
> contract address: 0xe834bB279e791f3Bb6b9c20C04Ee7cdEb4b79308
> block number: 5878886
> block timestamp: 1561707091
> account: 0xAFEbF61AF27866a27D839f47CfBef7dd415bAB65
> balance: 1.96337714
> gas used: 1511673
> gas price: 20 gwei
> value sent: 0 ETH
> total cost: 0.03023346 ETH


> Saving migration to chain.
> Saving artifacts
-------------------------------------
> Total cost: 0.03023346 ETH


Summary
=======
> Total deployments: 2
> Final cost: 0.0357827 ETH

samwen@samwen-ubuntu:~/VToken$

查看部署成果

先看下账号下发生了什么交易,通过 https://ropsten.etherscan.io/address/账号地址 来访问 。
这是本次部署: 0xAFEbF61AF27866a27D839f47CfBef7dd415bAB65

一共有四笔交易,两份合约,就是我们执行的。

合约地址: 0xe834bB279e791f3Bb6b9c20C04Ee7cdEb4b79308

通过网页可以看到还需要我们上传源代码,此处由于依赖到 OpenZeppelin ,结果上传几次都编译失败(Remix编译没问题),先不管了。

常见错误问题

  1. 部署的账号没有ETH余额
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    samwen@samwen-ubuntu:~/VToken$ truffle deploy --reset --network ropsten

    Compiling your contracts...
    ===========================
    > Everything is up to date, there is nothing to compile.


    Starting migrations...
    ======================
    > Network name: 'ropsten'
    > Network id: 3
    > Block gas limit: 0x7a1200


    1_initial_migration.js
    ======================

    Deploying 'Migrations'
    ----------------------
    Error: Error: Error: *** Deployment Failed ***

    "Migrations" could not deploy due to insufficient funds
    * Account: 0xA495032Bf8c02cC4594a40F53AC5A38555728107
    * Balance: 0 wei
    * Message: insufficient funds for gas * price + value
    * Message: sender doesn't have enough funds to send tx. The upfront cost is: 134439500000000000 and the sender's account only has: 0
    * Try:
    + Using an adequately funded account
    + If you are using a local Geth node, verify that your node is synced.

    at Object.run (/usr/local/lib/node_modules/truffle/build/webpack:/packages/truffle-migrate/index.js:92:1)
    at process._tickCallback (internal/process/next_tick.js:68:7)
    Truffle v5.0.24 (core: 5.0.24)
    Node v10.16.0

could not deploy due to insufficient funds : 因资金不足无法部署

  1. gas设置太高
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    ===========================
    > Everything is up to date, there is nothing to compile.


    Migrations dry-run (simulation)
    ===============================
    > Network name: 'ropsten-fork'
    > Network id: 3
    > Block gas limit: 0x7a1200


    1_initial_migration.js
    ======================

    Deploying 'Migrations'
    ----------------------
    Error: Error: Error: *** Deployment Failed ***

    "Migrations" exceeded the block limit (with a gas value you set).
    * Block limit: 0x203c3f0
    * Gas sent: 11118500000
    * Try:
    + Sending less gas.
    + Setting a higher network block limit if you are on a
    private network or test client (like ganache).

    at Object.run (/usr/local/lib/node_modules/truffle/build/webpack:/packages/truffle-migrate/index.js:92:1)
    at process._tickCallback (internal/process/next_tick.js:68:7)
    Truffle v5.0.24 (core: 5.0.24)
    Node v10.16.0

超过block限制(使用您设置的 gas 值),把gas的值改小。

hdwallet安装问题

**最新解决办法20200114记录(Ubuntu)** **最新解决办法20200114记录(Ubuntu)** **最新解决办法20200114记录(Ubuntu)**
  1. 这是npm一个普遍的权限问题,尽量不要以root账号身份安装此依赖(普通账号也不要加sudo)
  2. 如果一定要用root身份,在命令后加参数 --allow-root
  3. 最好是普通账号执行 npm install truffle-hdwallet-provider --unsafe-perm=true
  4. root账号执行 npm install truffle-hdwallet-provider --unsafe-perm=true --allow-root
**早期手动修改的办法,已不建议使用**
在Windows10系统中安装hdwallet一样会提示缺少C++组件: `MSBUILD : error MSB3428: 未能加载 Visual C++ 组件“VCBuild.exe”。要解决此问题,1) 安装 .NET Framework 2.0 SDK;` 一般通过管理员启动一个终端,执行`$ npm install -g windows-build-tools` 会安装好C++依赖,但是我的Windows10安装不了这个软件,手动下载MSBuild.exe也是无法安装,打开就闪退。

所以,这里只记录一下在Ubuntu下的解决日志。

如果想尽快使用,可以下载我在Ubuntu下打包的 node_modules: https://share.weiyun.com/5fscwl8

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
# 先确认已安装C++编译环境
samwen@samwen-ubuntu:~/VToken$ sudo apt-get install build-essential
Reading package lists... Done
Building dependency tree
Reading state information... Done
build-essential is already the newest version (12.4ubuntu1).
0 upgraded, 0 newly installed, 0 to remove and 0 not upgraded.
samwen@samwen-ubuntu:~/VToken$
samwen@samwen-ubuntu:~/VToken$ sudo npm install truffle-hdwallet-provider
npm WARN deprecated tar.gz@1.0.7: ⚠️ WARNING ⚠️ tar.gz module has been deprecated and your application is vulnerable. Please use tar module instead: https://npmjs.com/tar

> scrypt@6.0.3 preinstall /home/samwen/VToken/node_modules/scrypt
> node node-scrypt-preinstall.js


> scrypt@6.0.3 install /home/samwen/VToken/node_modules/scrypt
> node-gyp rebuild

gyp ERR! configure error
gyp ERR! stack Error: EACCES: permission denied, mkdir '/home/samwen/VToken/node_modules/scrypt/build'
gyp ERR! System Linux 4.15.0-20-generic
gyp ERR! command "/usr/local/bin/node" "/usr/local/lib/node_modules/npm/node_modules/node-gyp/bin/node-gyp.js" "rebuild"
gyp ERR! cwd /home/samwen/VToken/node_modules/scrypt
gyp ERR! node -v v10.16.0
gyp ERR! node-gyp -v v3.8.0
gyp ERR! not ok
npm WARN VToken@1.0.0 No repository field.
npm WARN VToken@1.0.0 No license field.

npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! scrypt@6.0.3 install: `node-gyp rebuild`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the scrypt@6.0.3 install script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

npm ERR! A complete log of this run can be found in:
npm ERR! /home/samwen/.npm/_logs/2019-06-28T05_50_58_642Z-debug.log

# 错误提示没有权限创建目录,手动创建目录
samwen@samwen-ubuntu:~/VToken$ sudo mkdir -p /home/samwen/VToken/node_modules/scrypt/build
samwen@samwen-ubuntu:~/VToken$ sudo chmod -R 777 /home/samwen/VToken/node_modules/scrypt/

# 重新运行安装一次
samwen@samwen-ubuntu:~/VToken$ sudo npm install truffle-hdwallet-provider
npm WARN deprecated fs-promise@2.0.3: Use mz or fs-extra^3.0 with Promise Support
npm WARN deprecated tar.gz@1.0.7: ⚠️ WARNING ⚠️ tar.gz module has been deprecated and your application is vulnerable. Please use tar module instead: https://npmjs.com/tar
npm ERR! path /home/samwen/VToken/node_modules/web3-providers-ws/node_modules/websocket
npm ERR! code EISGIT
npm ERR! git /home/samwen/VToken/node_modules/web3-providers-ws/node_modules/websocket: Appears to be a git repo or submodule.
npm ERR! git /home/samwen/VToken/node_modules/web3-providers-ws/node_modules/websocket
npm ERR! git Refusing to remove it. Update manually,
npm ERR! git or move it out of the way first.

npm ERR! A complete log of this run can be found in:
npm ERR! /home/samwen/.npm/_logs/2019-06-28T05_58_55_864Z-debug.log
samwen@samwen-ubuntu:~/VToken$
# 提示websocket下有 'git repo or submodule',手动删除 '.git'
samwen@samwen-ubuntu:~/VToken$ sudo rm -rf node_modules/web3-providers-ws/node_modules/websocket/.git
samwen@samwen-ubuntu:~/VToken$ sudo npm install truffle-hdwallet-provider
npm WARN deprecated fs-promise@2.0.3: Use mz or fs-extra^3.0 with Promise Support
npm WARN deprecated tar.gz@1.0.7: ⚠️ WARNING ⚠️ tar.gz module has been deprecated and your application is vulnerable. Please use tar module instead: https://npmjs.com/tar

> scrypt@6.0.3 preinstall /home/samwen/VToken/node_modules/scrypt
> node node-scrypt-preinstall.js


> scrypt@6.0.3 install /home/samwen/VToken/node_modules/scrypt
> node-gyp rebuild

gyp ERR! configure error
gyp ERR! stack Error: EACCES: permission denied, mkdir '/home/samwen/VToken/node_modules/scrypt/build'
gyp ERR! System Linux 4.15.0-20-generic
gyp ERR! command "/usr/local/bin/node" "/usr/local/lib/node_modules/npm/node_modules/node-gyp/bin/node-gyp.js" "rebuild"
gyp ERR! cwd /home/samwen/VToken/node_modules/scrypt
gyp ERR! node -v v10.16.0
gyp ERR! node-gyp -v v3.8.0
gyp ERR! not ok
npm WARN VToken@1.0.0 No repository field.
npm WARN VToken@1.0.0 No license field.

npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! scrypt@6.0.3 install: `node-gyp rebuild`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the scrypt@6.0.3 install script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

npm ERR! A complete log of this run can be found in:
npm ERR! /home/samwen/.npm/_logs/2019-06-28T06_10_26_806Z-debug.log
samwen@samwen-ubuntu:~/VToken$
samwen@samwen-ubuntu:~/VToken$ node-gyp rebuild
internal/modules/cjs/loader.js:638
throw err;
^

Error: Cannot find module 'graceful-fs'
at Function.Module._resolveFilename (internal/modules/cjs/loader.js:636:15)
at Function.Module._load (internal/modules/cjs/loader.js:562:25)
at Module.require (internal/modules/cjs/loader.js:690:17)
at require (internal/modules/cjs/helpers.js:25:18)
at Object.<anonymous> (/usr/share/node-gyp/lib/node-gyp.js:12:10)
at Module._compile (internal/modules/cjs/loader.js:776:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:787:10)
at Module.load (internal/modules/cjs/loader.js:653:32)
at tryModuleLoad (internal/modules/cjs/loader.js:593:12)
at Function.Module._load (internal/modules/cjs/loader.js:585:3)
samwen@samwen-ubuntu:~/VToken$
# 切换到root账号执行,就没有权限问题了。
samwen@samwen-ubuntu:~/VToken$ sudo su root
root@samwen-ubuntu:/home/samwen/VToken# sudo npm install truffle-hdwallet-provider
npm WARN deprecated fs-promise@2.0.3: Use mz or fs-extra^3.0 with Promise Support
npm WARN deprecated tar.gz@1.0.7: ⚠️ WARNING ⚠️ tar.gz module has been deprecated and your application is vulnerable. Please use tar module instead: https://npmjs.com/tar

> scrypt@6.0.3 preinstall /home/samwen/VToken/node_modules/scrypt
> node node-scrypt-preinstall.js


> scrypt@6.0.3 install /home/samwen/VToken/node_modules/scrypt
> node-gyp rebuild

make: Entering directory '/home/samwen/VToken/node_modules/scrypt/build'
SOLINK_MODULE(target) Release/obj.target/copied_files.node
COPY Release/copied_files.node
CC(target) Release/obj.target/scrypt_wrapper/src/util/memlimit.o
CC(target) Release/obj.target/scrypt_wrapper/src/scryptwrapper/keyderivation.o
CC(target) Release/obj.target/scrypt_wrapper/src/scryptwrapper/pickparams.o
CC(target) Release/obj.target/scrypt_wrapper/src/scryptwrapper/hash.o
AR(target) Release/obj.target/scrypt_wrapper.a
COPY Release/scrypt_wrapper.a
CC(target) Release/obj.target/scrypt_lib/scrypt/scrypt-1.2.0/lib/crypto/crypto_scrypt.o
CC(target) Release/obj.target/scrypt_lib/scrypt/scrypt-1.2.0/lib/crypto/crypto_scrypt_smix.o
CC(target) Release/obj.target/scrypt_lib/scrypt/scrypt-1.2.0/libcperciva/util/warnp.o
CC(target) Release/obj.target/scrypt_lib/scrypt/scrypt-1.2.0/libcperciva/alg/sha256.o
CC(target) Release/obj.target/scrypt_lib/scrypt/scrypt-1.2.0/libcperciva/util/insecure_memzero.o
CC(target) Release/obj.target/scrypt_lib/scrypt/scrypt-1.2.0/lib/scryptenc/scryptenc_cpuperf.o
AR(target) Release/obj.target/scrypt_lib.a
COPY Release/scrypt_lib.a
CXX(target) Release/obj.target/scrypt/src/node-boilerplate/scrypt_common.o
CXX(target) Release/obj.target/scrypt/src/node-boilerplate/scrypt_params_async.o
In file included from ../src/node-boilerplate/inc/scrypt_async.h:28:0,
from ../src/node-boilerplate/inc/scrypt_params_async.h:28,
from ../src/node-boilerplate/scrypt_params_async.cc:4:
../src/node-boilerplate/inc/scrypt_common.h: In constructor ‘NodeScrypt::Params::Params(const v8::Local<v8::Object>&)’:
../src/node-boilerplate/inc/scrypt_common.h:39:63: warning: ‘uint32_t v8::Value::Uint32Value() const’ is deprecated: Use maybe version [-Wdeprecated-declarations]
N(obj->Get(Nan::New("N").ToLocalChecked())->Uint32Value()),
^
In file included from /root/.node-gyp/10.16.0/include/node/v8.h:26:0,
from /root/.node-gyp/10.16.0/include/node/node.h:63,
from ../../nan/nan.h:53,
from ../src/node-boilerplate/scrypt_params_async.cc:1:
/root/.node-gyp/10.16.0/include/node/v8.h:2477:47: note: declared here
V8_DEPRECATED("Use maybe version", uint32_t Uint32Value() const);
^
/root/.node-gyp/10.16.0/include/node/v8config.h:324:3: note: in definition of macro ‘V8_DEPRECATED’
declarator __attribute__((deprecated(message)))
^~~~~~~~~~
很多node-gyp日志
很多node-gyp日志
很多node-gyp日志

CXX(target) Release/obj.target/scrypt/scrypt_node.o
SOLINK_MODULE(target) Release/obj.target/scrypt.node
COPY Release/scrypt.node
make: Leaving directory '/home/samwen/VToken/node_modules/scrypt/build'
npm WARN VToken@1.0.0 No repository field.
npm WARN VToken@1.0.0 No license field.

+ truffle-hdwallet-provider@1.0.11
added 8 packages from 8 contributors and audited 112619 packages in 60.524s
found 0 vulnerabilities

root@samwen-ubuntu:/home/samwen/VToken#
# 终于没报错了
root@samwen-ubuntu:/home/samwen/VToken# exit
exit
samwen@samwen-ubuntu:~/VToken$ cat package.json
{
"name": "VToken",
"version": "1.0.0",
"dependencies": {
"truffle-hdwallet-provider": "^1.0.11",
"zeppelin-solidity": "^1.12.0"
}
}
# truffle-hdwallet-provider 已安装成功