朋友希望我总结一下本地进行以太坊开发所必须的内容,既然总结了,就想着发出来吧,希望对大家也有用。
ps. 官方文档是比较新和全的的信息来源
Solidity 开发文档:https://solidity.readthedocs.io/en/v0.6.3/
VSCODE,用于写以太坊智能合约solidity,及js代码
下载后,安装插件solidity, JavaScript (ES6) code snippets
官网:
https://www.trufflesuite.com/truffle
文档:
https://www.trufflesuite.com/docs/truffle/overview
brew update
brew install node
node -v
输出:类似v0.10.31
npm -v
输出:类似1.4.27
修改源为淘宝镜像地址
npm config set registry http://registry.npm.taobao.org/
如果需要改回来,怎用
npm config set registry https://registry.npmjs.org/
npm install truffle -g
truffle unbox metacoin
用上面的命令就可以直接下载metacoin的示例代码,测试一下环境是不是正常,也可以看一下代码的用法
contracts/ Solidity
合约代码
Migrations.sol
记录部署情况,一般不能编辑MetaCoin.sol
示例代码,包含积分ConvertLib.sol
示例代码,被MetaCoin.sol 引用migrations/
合约部署脚本
1_initial_migration.js
部署脚本,部署Migrations.sol2_deploy_contracts.js
可编辑的部署脚本,2代表顺序test/
测试代码
metacoin.js
用JS写得测试脚本truffle-config.js
是truffle部署配置文件solidity版本
truffle test ./test/TestMetaCoin.sol
TestMetacoin √ testInitialBalanceUsingDeployedContract (71ms) √ testInitialBalanceWithNewMetaCoin (59ms) 2 passing (794ms)
js版本
truffle test ./test/metacoin.js
Contract: MetaCoin √ should put 10000 MetaCoin in the first account √ should call a function that depends on a linked library (40ms) √ should send coin correctly (129ms) 3 passing (255ms)
truffle compile
Compiling .\contracts\ConvertLib.sol...Compiling .\contracts\MetaCoin.sol...Compiling .\contracts\Migrations.sol...Writing artifacts to .\build\contracts
编译完之后,会在build
目录下生成编译完成的json
文件,里面包含调用合约需要用到的abi
,bytecode
部署合约的时候,合约部署的区块链,可以用Truffle Develop
, 也可以用ganache
, 也可以用我们的私链(私链已经部署完,直接填地址就可以了)详见本文 - 部署本地区块链
部署命令:
truffle migrate
部署配置文件:
部署的时候,有两个文件需要注意.
truffle-config.js
配置部署合约的区块链地址,和部署账号migrations/
里面的部署文件,默认存在的是1_initial_migration.js
这个不要删除和修改,我们可以新增一个对应的部署文件,例如2_initial_metacoin.js
其中这边的2
代表执行顺序。最简单的使用方式如下,我们本地使用ganache
或者Truffle Develop
的时候就可以采用这种方式:
module.exports = {
networks: {
development: {
host: "127.0.0.1", //区块链服务地址
port: 8545, //区块链服务端口号
from: "0xCA529A03166d997709d295Ab1D6FC65295Fa3031", //部署合约的地址
network_id: "*" //匹配任意network_id
},
}
};
在某些情况下,我们需要使用指定的账号来部署,所以我们需要在部署时,配置好部署合约的账户私钥,配置如下:
前提:
需要安装truffle-hdwallet-provider
,
方式一、我们可以临时安装
npm install truffle-hdwallet-provider
方式二、也可以添加到package.json
中去
npm install truffle-hdwallet-provider --save
方式三(推荐方式),我们这边使用的版本如下,可以在package.json
直接增加如下内容
"devDependencies": {
"truffle-hdwallet-provider": "^1.0.17"
},
然后执行
npm install
即可完成安装。
truffle-config.js 配置
const PrivateKeyProvider = require("truffle-hdwallet-provider"); //插件
const privateKey = "8f2a55949038a9610f5aaaa3b5883af3b4ecb3c3bb792cbcefbd1542c692be63"; //账户私钥
const privateKeyProvider = new PrivateKeyProvider(privateKey, "http://127.0.0.1:8545"); //区块链地址
module.exports = {
networks: {
//以太坊环境地址
besuWallet: {
provider: privateKeyProvider,
network_id: "*",
gasPrice: 0,
gas: "0x1ffffffffffffe"
},
}
};
为了便于开发,truffle-config.js 支持同时配置多个环境,在部署的时候,指定对应的网络配置即可。例如,我们可以把上述两个配置文件合并:
const PrivateKeyProvider = require("truffle-hdwallet-provider"); //插件
const privateKey = "8f2a55949038a9610f5aaaa3b5883af3b4ecb3c3bb792cbcefbd1542c692be63"; //账户私钥
const privateKeyProvider = new PrivateKeyProvider(privateKey, "http://192.128.110.1:8545"); //区块链地址
module.exports = {
networks: {
//本地环境
development: {
host: "127.0.0.1", //区块链服务地址
port: 8545, //区块链服务端口号
from: "0xCA529A03166d997709d295Ab1D6FC65295Fa3031", //部署合约的地址
network_id: "*" //匹配任意network_id
},
//以太坊环境地址
besuWallet: {
provider: privateKeyProvider,
network_id: "*",
gasPrice: 0,
gas: "0x1ffffffffffffe"
},
}
};
若要采用指定的配置, 在部署的时候,指定网络名称即可,举例如下:
truffle migrate --network development
//同理,若要使用besu环境,则指定besuWallet
truffle migrate --network besuWallet
以我的文件举例
若部署的时候不需要参数,则不用加参数,例如:
const Metacoin = artifacts.require("Evidence");
module.exports = function(deployer) {
deployer.deploy(Metacoin);
};
若部署的时候,需要使用参数,则增加参数,例如:
const Metacoin = artifacts.require("Metacoin");
module.exports = function(deployer) {
//这里0xCA529A03166d997709d295Ab1D6FC65295Fa3031是部署时,需要用到的参数,如果有多个,逗号隔开就可以了
deployer.deploy(Metacoin,"0xCA529A03166d997709d295Ab1D6FC65295Fa3031");
};
Truffle Develop ,是Truffle自带以太坊系统,我们在自己的项目目录下启动就可以了。启动命令:
truffle develop
输出:
则区块链地址为:http://127.0.0.1:9545/
, 并且自定义了一些账号
Truffle Develop started at http://127.0.0.1:9545/
Accounts:
(0) 0xb71db8e96f015112465379499104f2ef9c875bb5
(1) 0xa05ff8a0603a25d8c52f78701414f8e5bcb71d35
(2) 0xf0ead3941482debd1c8413481cec6cf8a4e6ab88
(3) 0x854a9e3cd0e43d26c7dff67c5be89a0012931de7
(4) 0x6794bb9a5106a2f375218f28b68fe5153bf18a30
(5) 0x56246d82ff3391ddb01eceb59014230452a2b400
(6) 0xd183b7582f5282b8783d75ea416f7d922b389015
(7) 0x2bb3cbbf11ef86efecd993861029fb00eb270bb8
(8) 0xfa4bbe0f00c289d79c81c7aa0cbb80a0bb23c014
(9) 0x14b239bfabd4c83dc0e903a031fb7231702ec87a
Private Keys:
(0) 11f51b605f4e77f146e7a2b5eeda47d3dac0e28e45003b004853eaf61cb12265
(1) f88b5a8e58f01cdc619fe3f748e460cb55e9b684048442cbff9f8d4f823c9260
(2) 48a3ef0821ddd65338fb9203730fbd9e7e632afae82ba7fc85c4c4bb6e47c95d
(3) 2ec28218e0e5e014c8b6b6c19af88655742224af1d98049a209d0edc6238fd33
(4) c2413118646914f1f337dfe72674c17a34374b1b7022fe7e262f9054a36bf7b0
(5) c9a4e2130ee87fcecd8afe98509730581baa81b32620dca7b6d9073d6c188999
(6) 7c6bf123b980874b2aab2098902493059c96ca0a709b59ac0da6c85043bbf94c
(7) 5aa578dba8c5e030ffbac155bafc86b9b0c6a6610a685bf9d3b00d9d6702c104
(8) 0e3321fb038be7d2ddfe63353d5c32bab4c28f942f13f0d8ed1088bbb99da83c
(9) 1d34dd9b93e3d39cc8e2dfda585a8dc70b17351265f1a95479497b1256e2e3fd
Mnemonic: letter usual token track warfare toward frown rude present disease raise pizza
⚠️ 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)>
注意要在带有
truffle-config.js
的目录下面启动,否则会报错➜ contract truffle developCould not find suitable configuration file.Truffle v5.1.14 (core: 5.1.14)Node v13.8.0
官网地址:https://www.trufflesuite.com/ganache
安装:
npm install -g ganache-cli
启动:
ganache-cli
输出:
➜ metacoin ganache-cli
Ganache CLI v6.9.1 (ganache-core: 2.10.2)
Available Accounts
==================
(0) 0x2400E7Bee7046d26bfE0AAF2d158005FcDB132dD (100 ETH)
(1) 0xC1420069304b555afE8aaCF054F863069F103BCC (100 ETH)
(2) 0x02b8C9848E93EC26b71f4C2DABf8210aBFd3475e (100 ETH)
(3) 0x297C448333e36F22FD48aCEDC9E566992cD9d451 (100 ETH)
(4) 0x0faEd52b3D79655Ee447ceF675B60CfA69DDE3eb (100 ETH)
(5) 0xBa6750d92E5a2404BA2AB7ded6555e4E862d0F07 (100 ETH)
(6) 0x1869cc6E76fE118870BB1dBF7f76770943c6519C (100 ETH)
(7) 0xA959Ee41591f5F1e8A93557b14f05310075DB4b9 (100 ETH)
(8) 0xaFf95cb39d3D0Ef52C2c8e441eEBc2bFf7eDeDdD (100 ETH)
(9) 0x703B6Ec0D4763877265F867c3cf22544c167Da16 (100 ETH)
Private Keys
==================
(0) 0xfb7643d0b4f6c773c787d75d45e23355957bcc3db586b57c805eeb26632ea3fd
(1) 0x34a8ca5fce9b6d18b5a7b0fed8e1f7de168b028f0fffc94b506260a5e01cfddb
(2) 0xc976318d4152a90d58c2ef4b48fe443ec8f5717c934b48072114f9dd4e9cea34
(3) 0x5e2c123281e825bb4be1cf2bd0f42d25978ba9482e53a1e1c3298a3a17427b69
(4) 0xc71fc9fb8c04d8669b25a9ed18319b6f1857b1891870b2b7b717cd31ca113454
(5) 0xa1b3fb9c8222198da7051334d063f7317da5ed77ae9a1476d88eb2ec87a652aa
(6) 0x4de2c37d2de82a957fdbd5c85a32dcc854862c01ebe6ddddf7f02a786daafc3c
(7) 0xc518272cafd8d4a732df0c7515c26cf66f6f5c1cb7b072e377a1f4526a6c249a
(8) 0x91818dbfd636f51593b7a8084bec5e1e66d0bea16cf8594a52333367b49ee84a
(9) 0x5a4b465ba6dc300b8a8ad904a8af24cee2f2b01e5723d6f204f6751edb82ff4f
HD Wallet
==================
Mnemonic: arrive fashion glory attend skin solution carbon tribe proud monster slow senior
Base HD Path: m/44'/60'/0'/0/{account_index}
Gas Price
==================
20000000000
Gas Limit
==================
6721975
Call Gas Limit
==================
9007199254740991
Listening on 127.0.0.1:8545
用ganache图形界面版,也可以很方便的看到交易,如果想要用命令行查,可以用geth客户端,也可以方便地查一些链上的交易状态,余额等,可以按需安装
安装命令:
$ brew update
$ brew upgrade
$ brew tap ethereum/ethereum
$ brew install ethereum
常用命令:
连接区块链
geth attach http://192.122.100.248:8545
查询余额
//account改为自己需要查询的账户 web3.fromWei(eth.getBalance("0xaaaa6e9bd9f6b57da3ecfd149dfc3b4ca9cdc4"),'ETHER')
3、查询交易
eth.getTransaction("0x49386fc72299c5d78306ae87c7ec517ab2bc466f3ac418e15d9d78945d77f32a")
3、查询交易是否成功
eth.getTransactionReceipt("0x49386fc72299c5d78306ae87c7ec517ab2bc466f3ac418e15d9d78945d77f32a")
eth.getBlockNumber()