blockchain - ganache的使用 ethereum truffle
访问量: 970
refer to: https://trufflesuite.com/docs/
refer to: https://ethereum.stackexchange.com/questions/129633/rpc-error-32603-when-deploy-new-contract/130532#130532
ganache跟 hardhat 一样,是运行在本地的ethereum网络。不一样的是,ganache 带有友好的操作界面。具备explorer,这个太给力了。
注意事项
1. 务必安装好一个nodejs , 例如16.x 这样ganache才可以查看truffle项目中的contract
2. 关掉防火墙(如果你是在虚拟机,wsl 或者docker中使用的话) 否则无法访问ganache rpc server
3. 特别特别注意:关掉Linux 下的代理(例如http_proxy, https_proxy ) 否则无法访问ganache rpc server
windows下的安装方式
1. 到官网,下载exe, 安装
2. 运行后,编辑它。
3. 填写个名字
4. 看一下其他配置
5. 其他选项: 记录日志,选择日志文件夹等
6. 主界面的说明
使用之前:
使用curl 命令确认是否可以访问:
curl -X POST -H "Content-Type: application/json" --data '{"jsonrpc":"2.0","method":"eth_getBalance","params": ["0xDEC781c67a86570c004c7840BE1AddAF51C39487", "latest"],"id":1}' http://192.168.10.54:3355 -vvv
Note: Unnecessary use of -X or --request, POST is already inferred. * Trying 192.168.10.54:3355... * TCP_NODELAY set * Connected to 192.168.10.54 (192.168.10.54) port 3355 (#0) > POST / HTTP/1.1 > Host: 192.168.10.54:3355 > User-Agent: curl/7.68.0 > Accept: */* > Content-Type: application/json > Content-Length: 117 > * upload completely sent off: 117 out of 117 bytes * Mark bundle as not supporting multiuse < HTTP/1.1 200 OK < Content-Type: application/json < Date: Tue, 21 Jun 2022 00:22:24 GMT < Connection: keep-alive < Transfer-Encoding: chunked < * Connection #0 to host 192.168.10.54 left intact {"id":1,"jsonrpc":"2.0","result":"0x56266a7b7dbf06000"}
如果是上面这样,就没问题。
使用 浏览器打开 该地址: 如果能看到 400 Bad Request, 就说明 该server已经在运行了
7. 在truffle中使用: 修改truffle-config.js 增加对应的network:
require('dotenv').config(); const HDWalletProvider = require('@truffle/hdwallet-provider'); module.exports = { networks: {
// 这个就是需要我们增加的 ganache: { provider: () => new HDWalletProvider("gym host tag slow tent leopard woman emerge pear ??? ??? ???", "http://192.168.10.54:3355"), network_id: "5777", networkCheckTimeout: 30000, timeoutBlocks: 200, addressIndex: 2 }, }, compilers: { solc: { // version: "^0.8.0", version: "0.8.15", } }, plugins: ['truffle-plugin-verify'], api_keys: { etherscan: '4ANBPF8KQQHJ6J9BBU7WU53BNPDMXHYJMW' } };
8. 运行前,确保你没有使用 http_proxy , 例如
9. 部署:truffle deploy --network ganache --verbose-rpc --interactive --reset
Compiling your contracts... =========================== > Everything is up to date, there is nothing to compile. Starting migrations... ====================== > Network name: 'ganache' > Network id: 5777 > Block gas limit: 6721975 (0x6691b7) 1_initial_migration.js ====================== Replacing 'Migrations' ---------------------- > transaction hash: 0xc901d726176f23ccfb243ddd788e02d60dc9a550c31995d2aa0c9171f9e67527 > Blocks: 0 Seconds: 0 > contract address: 0x155ac1Fdb6fAFe0F411adaaE4717cA569cD298bd > block number: 25 > block timestamp: 1655770133 > account: 0xDEC781c67a86570c004c7840BE1AddAF51C39487 > balance: 99.4882175 > gas used: 248854 (0x3cc16) > gas price: 20 gwei > value sent: 0 ETH > total cost: 0.00497708 ETH > Saving migration to chain. > Saving artifacts ------------------------------------- > Total cost: 0.00497708 ETH 2_deploy_contracts.js ===================== Replacing 'MyTestNft' --------------------- > transaction hash: 0x4ca4fe4f388e9b124554975c0bae0e02a1ad9f49181356b8c66dbb2ee9dbbe9d > Blocks: 0 Seconds: 0 > contract address: 0xBA19dF9aA5cd67780E6a83DaA73d03a2B129BD23 > block number: 27 > block timestamp: 1655770135 > account: 0xDEC781c67a86570c004c7840BE1AddAF51C39487 > balance: 99.43390834 > gas used: 2672945 (0x28c931) > gas price: 20 gwei > value sent: 0 ETH > total cost: 0.0534589 ETH > Saving migration to chain. > Saving artifacts ------------------------------------- > Total cost: 0.0534589 ETH 3_deploy_MyTestNftB.js ====================== Replacing 'MyTestNftB' ---------------------- > transaction hash: 0xef23e81d51c704699b3eb73cd846db363efbcdcbc57a41b6527429221fbc7ff8 > Blocks: 0 Seconds: 0 > contract address: 0xb8196BfABcCdE94EbeFB02896cFB8D42a9dA99CC > block number: 29 > block timestamp: 1655770136 > account: 0xDEC781c67a86570c004c7840BE1AddAF51C39487 > balance: 99.37989846 > gas used: 2672981 (0x28c955) > gas price: 20 gwei > value sent: 0 ETH > total cost: 0.05345962 ETH > Saving migration to chain. > Saving artifacts ------------------------------------- > Total cost: 0.05345962 ETH 4_deploy_MyTestNftC.js ====================== Replacing 'MyTestNftC' ---------------------- > transaction hash: 0xf5d1849fd5356881f33b4ce696e318d47a87a8204a657bc3c1009ceafc19caa2 > Blocks: 0 Seconds: 0 > contract address: 0xc6eD3B8ebf7E644655C743EE1DFf884B22a8EeD4 > block number: 31 > block timestamp: 1655770138 > account: 0xDEC781c67a86570c004c7840BE1AddAF51C39487 > balance: 99.3248097 > gas used: 2726925 (0x299c0d) > gas price: 20 gwei > value sent: 0 ETH > total cost: 0.0545385 ETH > Saving migration to chain. > Saving artifacts ------------------------------------- > Total cost: 0.0545385 ETH Summary ======= > Total deployments: 4 > Final cost: 0.1664341 ETH
可以说是非常的爽歪歪了。在本地运行速度奇快,瞬间结束(goerli等网络需要一个30秒左右)
10. 查看Ganache Dashboard
可以看出,上面出现的3个Contract Creation tx, 都依次出现在了这里(时间倒叙排序)
(创建的时间顺序: 从上到下)
MyTestNft 0x4ca4fe4f388e9b124554975c0bae0e02a1ad9f49181356b8c66dbb2ee9dbbe9d
MyTestNftB 0xef23e81d51c704699b3eb73cd846db363efbcdcbc57a41b6527429221fbc7ff8
MyTestNftC 0xf5d1849fd5356881f33b4ce696e318d47a87a8204a657bc3c1009ceafc19caa2
在Ganache 中的Transactions :越在上面的,就是越新的 tx
点击可以查看详情:
可以看到目前还没有event
点击对应的account, 发现balance在变少
每个tx对应一个block
可以看到都已经部署了
点击 Contract的详情,就可以看到下面的页面:
点击查看log:
最常见的问题
1. code -32603 , socket hang up
truffle deploy --network ganache Compiling your contracts... =========================== > Everything is up to date, there is nothing to compile. /mnt/d/workspace/test_erc_721_in_truffle/node_modules/eth-block-tracker/src/polling.js:51 const newErr = new Error(`PollingBlockTracker - encountered an error while attempting to update latest block:\n${err.stack}`) ^ Error: PollingBlockTracker - encountered an error while attempting to update latest block: undefined at PollingBlockTracker._performSync (/mnt/d/workspace/test_erc_721_in_truffle/node_modules/eth-block-tracker/src/polling.js:51:24) at processTicksAndRejections (node:internal/process/task_queues:96:5) UnhandledRejections detected Promise { { code: -32603, message: 'socket hang up', data: { originalError: [Object] } } } { code: -32603, message: 'socket hang up', data: { originalError: { code: 'ECONNRESET' } } }
跟ganache 没有连通。 解决办法:
1. 重启ganache
2. 重启terminal 不要使用代理 (这里最关键, 我被这里卡了大半个下午+2小时上午+一晚上琢磨这个事儿)
3. 关闭防火墙
4. 检查truffle-config.js 中的network是否正确