以太坊教程3-编写第一个区块链应用

linux版本:U麒麟16.04

nodejs环境版本v8.11.0

truffle版本v4.1.8

第一步:安装npm和nodejs环境。

第二步:安装truffle:

npm install -g truffle

第三步:安装webpack示例项目。

找个权限开放的地方,例如桌面路径或者home下的本人用户文件夹下,新建一个文件夹例如“demo”存放truffle项目,以后的终端启动都是在“demo”目录下运行命令:

//1. 先开启私有链,在你创建的私有链文件夹内运行
geth  --datadir "./chain" --nodiscover console 2>>eth_output.log
 
//2.重新打开一个终端,创建truffle测试文件夹,并在truffle测试文件夹内运行以下命令,安装webpack示例项目。
truffle unbox webpack
 
//3.重新打开一个终端,在truffle测试文件夹中运行testrpc
testrpc

1.开始私有链

 
Welcome to the Geth JavaScript console!
 
instance: Geth/v1.8.12-stable-37685930/linux-amd64/go1.10
coinbase: 0xfd5db5967bb4600840adde5035b8de4d56176ad5
at block: 1526 (Mon, 16 Jul 2018 18:26:06 CST)
 datadir: /home/hao/workspace/blockchain/siyoulian/chain
 modules: admin:1.0 debug:1.0 eth:1.0 miner:1.0 net:1.0 personal:1.0 rpc:1.0 txpool:1.0 web3:1.0
 
> 
> 
> 
> miner.start()

2.安装示例项目

root@hao:/home/hao/workspace/blockchain/truffle_test#  truffle unbox webpack
Downloading...
Unpacking...
Setting up...
Unbox successful. Sweet!
 
Commands:
 
  Compile:              truffle compile
  Migrate:              truffle migrate
  Test contracts:       truffle test
  Run linter:           npm run lint
  Run dev server:       npm run dev
  Build for production: npm run build

3.开rpc会显示

test     testrpc  
root@hao:/home/hao/workspace/blockchain/truffle_test# testrpc 
EthereumJS TestRPC v6.0.3 (ganache-core: 2.0.2)
 
Available Accounts
==================
(0) 0xdc375bb5e2954298ca77efe82c12e164efac042b
(1) 0x3b7245b483af4b407adec1c5ec510363e6e3116d
(2) 0x7a29b9b238eaf6a953f0ce097137d06f731c6a36
(3) 0x98748fc68feba1fed4bfaaa6e0e22c42a69801e7
(4) 0x1ccc897436726d83fbb5ef6614a73adfd4229471
(5) 0x7a47e2270dc4cb590736f0cc14150640dfc68ad7
(6) 0x68d3ab7672928ff34670224966d4627315e5a2a7
(7) 0x13586dc5c3939f531e1205c3d13005ed7d119c7c
(8) 0x73fb6e8d813780ed8d7ddd24acedfdac4f9ebaae
(9) 0x07e264d5ad5bf9ac1e3941c658ac8a4f1a6b2455
 
Private Keys
==================
(0) ece6f98979693e8588958ab2dc59b91deb728fa9438b6de5fde70353991c715f
(1) add2a65782c20768d6b5aa2c74bb4db303e46baa5b4256b8d7db5cd4460cafb5
(2) 94ce4753cd26e73edca6422d8b248d28c99f6d8905c37be933af9dc3791c1b94
(3) c7324de899563795eb2b148acb47b66afdccf12ebcf8f181a9e750da95c2a76e
(4) 7c1258f992035201793b5bd87335311e56820ccf51cbbe46c528ca63e0c8a031
(5) c0660daca940ad6cc7a0e4e1906af56539e318eee6382bed86815c888896c5bb
(6) 4283cadfb8bc48045fadead455c395478c81abb8cd225586137575ba385a5196
(7) 06989c5740cdbe7989d4f378a7d5f476c5ceaa62a867887e30fda216ed40e863
(8) 2be69e6d2069b3d91a5489726237684edd7ca930c2f4a030ed75056e25f59629
(9) 84184e2bf9057b945351c00f509a5b3cf0eaf650d45e0bb4a1fca2d9192e1fcf
 
HD Wallet
==================
Mnemonic:      now rare ask benefit desert blame stamp marble wool gown crowd jewel
Base HD Path:  m/44'/60'/0'/0/{account_index}
Listening on localhost:8545

第四步:运行开发控制台:

truffle develop
root@hao:/home/hao/workspace/blockchain/truffle_test# truffle develop
Truffle Develop started at http://127.0.0.1:9545/
 
Accounts:
(0) 0x627306090abab3a6e1400e9345bc60c78a8bef57
(1) 0xf17f52151ebef6c7334fad080c5704d77216b732
(2) 0xc5fdf4076b8f3a5357c5e395ab970b5b54098fef
(3) 0x821aea9a577a9b44299b9c15c88cf3087f3b5544
(4) 0x0d1d4e623d10f9fba5db95830f7d3839406c6af2
(5) 0x2932b7a2355d6fecc4b5c0b6bd44cc31df247a2e
(6) 0x2191ef87e392377ec08e7c08eb105ef5448eced5
(7) 0x0f4f2ac550a1b4e2280d04c21cea7ebd822934b5
(8) 0x6330a553fc93768f612722bb8c2ec78ac90b3bbc
(9) 0x5aeda56215b167893e80b4fe645ba6d5bab767de
 
Private Keys:
(0) c87509a1c067bbde78beb793e6fa76530b6382a4c0241e5e4a9ec0a0f44dc0d3
(1) ae6ae8e5ccbfb04590405997ee2d52d2b330726137b875053c36d94e974d162f
(2) 0dbbe8e4ae425a6d2687f1a7e3ba17bc98c673636790f1b8ad91193c05875ef1
(3) c88b703fb08cbea894b6aeff5a544fb92e78a18e19814cd85da83b71f772aa6c
(4) 388c684f0ba1ef5017716adb5d21a053ea8e90277d0868337519f97bede61418
(5) 659cbb0e2411a44db63778987b1e22153c086a95eb6b18bdf89de078917abc63
(6) 82d052c865f5763aad42add438569276c00d3d88a2d062d36b2bae914d58b8c8
(7) aa3680d5d48a8283413f7a108367c7299ca73f553735860a87b08f39395618b7
(8) 0f62d96d6675f32685bbdb8ac13cda7c23436f63efbb9d07700d8669ff12b7c4
(9) 8d5366123cb560bb606379f90a0bfd4769eecc0557f1b362dcae9012b548b1e5
 
Mnemonic: candy maple cake sugar pudding cream honey rich smooth crumble sweet treat
 
⚠️  Important ⚠️  : This mnemonic was created for you by Truffle. It is not secure.
Ensure you do not use it on production blockchains, or else you risk losing funds.
 
truffle(develop)> 

第五步:编译和移植:

这里有两种方法,第一种是在步骤4中的开发终端直接输入

 compile migrate

或者在新的终端中在上面加上truffle

  1. truffle compile

  2. truffle migrate


truffle(develop)>  compile
Compiling ./contracts/ConvertLib.sol...
Compiling ./contracts/MetaCoin.sol...
Compiling ./contracts/Migrations.sol...
 
Compilation warnings encountered:
 
/home/hao/workspace/blockchain/truffle_test/contracts/MetaCoin.sol:15:2: Warning: Defining constructors as functions with the same name as the contract is deprecated. Use "constructor(...) { ... }" instead.
	function MetaCoin() public {
 ^ (Relevant source part starts here and spans across multiple lines).
,/home/hao/workspace/blockchain/truffle_test/contracts/Migrations.sol:11:3: Warning: Defining constructors as functions with the same name as the contract is deprecated. Use "constructor(...) { ... }" instead.
  function Migrations() public {
  ^ (Relevant source part starts here and spans across multiple lines).
,/home/hao/workspace/blockchain/truffle_test/contracts/MetaCoin.sol:23:3: Warning: Invoking events without "emit" prefix is deprecated.
		Transfer(msg.sender, receiver, amount);
		^------------------------------------^
 
Writing artifacts to ./build/contracts
 
truffle(develop)>  migrate
Using network 'develop'.
 
Running migration: 1_initial_migration.js
  Deploying Migrations...
  ... 0x7da340f72d3202349d2fa6557d7b6dfc3159792a0b54864ff5928d25c27d28f2
  Migrations: 0x8cdaf0cd259887258bc13a92c0a6da92698644c0
Saving successful migration to network...
  ... 0xd7bc86d31bee32fa3988f1c1eabce403a1b5d570340a3a9cdba53a472ee8c956
Saving artifacts...
Running migration: 2_deploy_contracts.js
  Deploying ConvertLib...
  ... 0x7371fbae8517342aaa8b86ba137347b95512b7c9712f2af0af01deb11d055f62
  ConvertLib: 0x345ca3e014aaf5dca488057592ee47305d9b3e10
  Linking ConvertLib to MetaCoin
  Deploying MetaCoin...
  ... 0x93cb3b0e04fd951dc31b52eb54ab8ebf0f931bdf8c7fa33c34c16411083a5746
  MetaCoin: 0xf25186b5081ff5ce73482ad761db0eb0d25abfbf
Saving successful migration to network...
  ... 0x059cf1bbc372b9348ce487de910358801bbbd1c89182853439bec0afaee6c7db
Saving artifacts...

第六步:在你的项目文件夹中新启动一个终端,输入

 npm run dev

第七步:给账户充值:

test


truffle(develop)>  npm run dev
npm should be run outside of the node repl, in your normal shell.
(Press Control-D to exit.)
truffle(develop)> test
Using network 'develop'.
 
Compiling ./contracts/ConvertLib.sol...
Compiling ./contracts/MetaCoin.sol...
Compiling ./test/TestMetacoin.sol...
Compiling truffle/Assert.sol...
Compiling truffle/DeployedAddresses.sol...
 
Compilation warnings encountered:
 
/home/hao/workspace/blockchain/truffle_test/contracts/MetaCoin.sol:15:2: Warning: Defining constructors as functions with the same name as the contract is deprecated. Use "constructor(...) { ... }" instead.
	function MetaCoin() public {
 ^ (Relevant source part starts here and spans across multiple lines).
,/home/hao/workspace/blockchain/truffle_test/contracts/MetaCoin.sol:23:3: Warning: Invoking events without "emit" prefix is deprecated.
		Transfer(msg.sender, receiver, amount);
		^------------------------------------^
,/home/hao/workspace/blockchain/truffle_test/test/TestMetacoin.sol:9:3: Warning: No visibility specified. Defaulting to "public". 
  function testInitialBalanceUsingDeployedContract() {
  ^ (Relevant source part starts here and spans across multiple lines).
,/home/hao/workspace/blockchain/truffle_test/test/TestMetacoin.sol:17:3: Warning: No visibility specified. Defaulting to "public". 
  function testInitialBalanceWithNewMetaCoin() {
  ^ (Relevant source part starts here and spans across multiple lines).
 
 
 
  TestMetacoin
    ✓ testInitialBalanceUsingDeployedContract (55ms)
    ✓ testInitialBalanceWithNewMetaCoin (46ms)
 
  Contract: MetaCoin
    ✓ should put 10000 MetaCoin in the first account
    ✓ should call a function that depends on a linked library
    ✓ should send coin correctly (57ms)
 
 
  5 passing (764ms)
 
truffle(develop)>

运行Truffle4.1.8的Dapp demo时出现YOU have META没有显示余额的问题

修改 /app/javascripts 目录下 app.js 


window.addEventListener('load', function() {
  if (typeof web3 !== 'undefined') {
    console.warn("Using web3 detected from external source. If you find that your accounts don't appear or you have 0 MetaCoin, ensure you've configured that source properly. If using MetaMask, see the following link. Feel free to delete this warning. :) http://truffleframework.com/tutorials/truffle-and-metamask")
    window.web3 = new Web3(new Web3.providers.HttpProvider("http://localhost:8545")); //只需要修改这一行window.web3 = new Web3(web3.currentProvider);
  } else {
    console.warn("No web3 detected. Falling back to http://127.0.0.1:9545. You should remove this fallback when you deploy live, as it's inherently insecure. Consider switching to Metamask for development. More info here: http://truffleframework.com/tutorials/truffle-and-metamask");
    window.web3 = new Web3(new Web3.providers.HttpProvider("http://服务器ip地址:8545")); // 初始设置是http://127.0.0.1:9545,需要将ip地址换成服务器ip,端口号和testrpc端口一致
  };

猜你喜欢

转载自blog.csdn.net/u011164819/article/details/81557397