blockchain - !!! truffle的用法!看这个 - 在infura上,使用truffle 创建和发币contract
访问量: 1083
refer to:
https://docs.infura.io/infura/tutorials/ethereum/deploy-a-contract-using-truffle
1. 安装 truffle
npm install -g truffle --verbose(我在这里卡住了, 用proxy也不行,只能使用 cnpm 的方式来安装了 )
2024.3.27
使用npm 代理:
https://stackoverflow.com/a/10304317/445908
npm config set https-proxy http://proxy.company.com:8080
使用yarn :
yarn config set http-proxy http://192.168.0.104:8072
yarn config set https-proxy http://192.168.0.104:8072
好吧,用了cnpm, 900 seconds
npm timing build:run:postinstall:node_modules/truffle/node_modules/es5-ext Completed in 105ms
npm info run truffle@5.5.18 postinstall { code: 0, signal: null }
npm timing build:run:postinstall:node_modules/truffle Completed in 827343ms
npm timing build:run:postinstall Completed in 827343ms
npm timing build:deps Completed in 853234ms
npm timing build Completed in 853234ms
npm timing reify:build Completed in 853238ms
npm timing reify:trash Completed in 705ms
npm timing reify Completed in 908472ms
added 577 packages in 15m
npm timing command:install Completed in 908495ms
npm verb exit 0
npm timing npm Completed in 908605ms
npm info ok
2. 在当前文件夹下,$ npm install dotenv
3. $ truffle init
$ truffle init Starting init... ================ > Copying project files to /mnt/d/workspace/test_truffle Init successful, sweet! Try our scaffold commands to get started: $ truffle create contract YourContractName # scaffold a contract $ truffle create test YourTestName # scaffold a test
可以看到,truffle 生成了这样几个文件夹:
$ find contracts/ migrations/ test/ contracts/ 智能合约 contracts/Migrations.sol 已经存在了一个文件了 migrations/ 迁移 migrations/1_initial_migration.js 已经存在了一个了 test/ 测试文件夹
4. 安装 hdwallet-provider
npm install @truffle/hdwallet-provider
5. 创建.env文件
INFURA_API_URL = "https://goerli.infura.io/v3/427a8f460d514ebfadf0d6c60b0?????" # infura的 API URL MNEMONIC = "margin blouse tenant chat hub chef addict club rather ??? ??? ??? " # metamask 助记词
metamask的助记词可以在 https://metamask.zendesk.com/hc/en-us/articles/360015290032-How-to-Reveal-Your-Seed-Phrase 这里查询到 ( 右上角 LOGO -> settings -> 隐私与安全性 -> ... )
6. 在contracts 目录下,创建 Demo.sol , 内容如下:
$ cat contracts/Demo.sol
pragma solidity >= 0.5.8;
contract Demo {
event Echo(string message);
function echo(string calldata message) external {
emit Echo(message);
}
}
7. 修改 truffle-config.js ,内容如下:
require('dotenv').config()
const HDWalletProvider = require('@truffle/hdwallet-provider')
const { INFURA_API_URL, MNEMONIC } = process.env
module.exports = {
networks: {
development: {
host: "127.0.0.1",
port: 8545,
network_id: '*'
},
goerli: {
provider: () => new HDWalletProvider(MNEMONIC, INFURA_API_URL),
network_id: 5,
gas: 5500000
}
}
};
8. 运行 truffle compile
$ truffle compile Compiling your contracts... =========================== > Compiling ./contracts/Demo.sol > Compiling ./contracts/Migrations.sol > Artifacts written to /mnt/d/workspace/test_truffle/build/contracts > Compiled successfully using: - solc: 0.5.16+commit.9c3226ce.Emscripten.clang
9. 我们回顾一下contracts 目录下
在migration 目录下,创建新文件, 内容如下:2_deploy_contract.js
const DemoContract = artifacts.require('Demo')
module.exports = function(deployer) {
deployer.deploy(DemoContract)
}
10. 部署到网络上
查看命令详情: truffle help deploy
运行: truffle deploy --network goerli --verbose-rpc --interactive
我这里没能成功,一开始是报错:
Compiling your contracts...
===========================
> Everything is up to date, there is nothing to compile.
Starting migrations...
======================
> Network name: 'goerli'
> Network id: 5
> Block gas limit: 29970705 (0x1c95111)
1_initial_migration.js
======================
Deploying 'Migrations'
----------------------
> transaction hash: 0xfea291f3c52b3862654a9ea0d21edf9a7c99f380808c3f723950f9842e71eb0b
⠧ Blocks: 0 Seconds: 37
/mnt/d/workspace/test_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_truffle/node_modules/eth-block-tracker/src/polling.js:51:24)
at runMicrotasks (<anonymous>)
at processTicksAndRejections (node:internal/process/task_queues:96:5)
后来搜索了一下,要么是.env 变量写错了,要么是从https 切换到wss的 rpc server, 我试了不好用,后来是修改 truffle-config.js , 内容为:
require('dotenv').config();
const HDWalletProvider = require('@truffle/hdwallet-provider');
const { INFURA_API_URL, MNEMONIC } = process.env;
module.exports = {
networks: {
development: {
host: "127.0.0.1",
port: 8545,
network_id: "*"
},
goerli: {
provider: () => new HDWalletProvider(MNEMONIC, INFURA_API_URL),
network_id: '5',
gas: 5500000,
networkCheckTimeout: 1000000, // 这里设置超时时间1000秒
timeoutBlocks: 200,
addressIndex: 2
}
}
};
然后就卡住了, 看了一下,可以在goerli 网络上找到对应的 pending contract address:
https://goerli.etherscan.io/tx/0x458646b9bfc73fe4bc856a310eeeaee9e9d7888e812acdf5820056ce36031352

不过pending的也太久了吧。。。10分钟过去了还没消息。
This txn hash was found in our secondary node and should be picked up by our indexer in a short while.
好的,750秒之后, 有消息了
Compiling your contracts...
===========================
> Everything is up to date, there is nothing to compile.
Starting migrations...
======================
> Network name: 'goerli'
> Network id: 5
> Block gas limit: 29970705 (0x1c95111)
1_initial_migration.js
======================
Deploying 'Migrations'
----------------------
> transaction hash: 0x458646b9bfc73fe4bc856a310eeeaee9e9d7888e812acdf5820056ce36031352
⠋ Blocks: 0 Seconds: 76
⠼ Blocks: 0 Seconds: 76
⠇ Blocks: 0 Seconds: 76
⠙ Blocks: 0 Seconds: 76
⠴ Blocks: 0 Seconds: 76
⠧ Blocks: 0 Seconds: 76
⠋ Blocks: 0 Seconds: 80
⠙ Blocks: 0 Seconds: 480
> {locks: 0 Seconds: 752
> "jsonrpc": "2.0",
> "method": "eth_call",
> "params": [
> {
> "from": "0xc0dd5021e298db57bef361c735cd1c04cef2e48a",
> "gas": "0xe4a888",
> "data": "0x6080604052336000806101000a81548173ffffffffffffffffffffffffffffffffffffffff021916908373ffffffffffffffffffffffffffffffffffffffff16021790555034801561005057600080fd5b5061021e806100606000396000f3fe608060405234801561001057600080fd5b50600436106100415760003560e01c8063445df0ac146100465780638da5cb5b14610064578063fdacd576146100ae575b600080fd5b61004e6100dc565b6040518082815260200191505060405180910390f35b61006c6100e2565b604051808273ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200191505060405180910390f35b6100da600480360360208110156100c457600080fd5b8101908080359060200190929190505050610107565b005b60015481565b6000809054906101000a900473ffffffffffffffffffffffffffffffffffffffff1681565b6000809054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff163373ffffffffffffffffffffffffffffffffffffffff16146101ac576040517f08c379a00000000000000000000000000000000000000000000000000000000081526004018080602001828103825260338152602001806101b76033913960400191505060405180910390fd5b806001819055505056fe546869732066756e6374696f6e206973207265737472696374656420746f2074686520636f6e74726163742773206f776e6572a265627a7a7231582007302f208a10686769509b529e1878bda1859883778d70dedd1844fe790c9bde64736f6c63430005100032",
> "maxPriorityFeePerGas": "0x9502F900",
> "maxFeePerGas": "0x9502f910"
> },
> "latest"
> ],
> "id": 1655422484419
> }
< {locks: 0 Seconds: 752
< "id": 1655422484419,
< "jsonrpc": "2.0",
< "result": "0x608060405234801561001057600080fd5b50600436106100415760003560e01c8063445df0ac146100465780638da5cb5b14610064578063fdacd576146100ae575b600080fd5b61004e6100dc565b6040518082815260200191505060405180910390f35b61006c6100e2565b604051808273ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200191505060405180910390f35b6100da600480360360208110156100c457600080fd5b8101908080359060200190929190505050610107565b005b60015481565b6000809054906101000a900473ffffffffffffffffffffffffffffffffffffffff1681565b6000809054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff163373ffffffffffffffffffffffffffffffffffffffff16146101ac576040517f08c379a00000000000000000000000000000000000000000000000000000000081526004018080602001828103825260338152602001806101b76033913960400191505060405180910390fd5b806001819055505056fe546869732066756e6374696f6e206973207265737472696374656420746f2074686520636f6e74726163742773206f776e6572a265627a7a7231582007302f208a10686769509b529e1878bda1859883778d70dedd1844fe790c9bde64736f6c63430005100032"
< }
✖ Blocks: 0 Seconds: 752
*** Deployment Failed ***
"Migrations" -- Transaction was not mined within 750 seconds, please make sure your transaction was properly sent. Be aware that it might still be mined! -- Reason given: Custom error (could not decode)..
Exiting: Review successful transactions manually by checking the transaction hashes above on Etherscan.
Error: *** Deployment Failed ***
"Migrations" -- Transaction was not mined within 750 seconds, please make sure your transaction was properly sent. Be aware that it might still be mined! -- Reason given: Custom error (could not decode)..
at /home/siwei/.asdf/installs/nodejs/16.15.1/.npm/lib/node_modules/truffle/build/webpack:/packages/deployer/src/deployment.js:379:1
at runMicrotasks ()
at processTicksAndRejections (node:internal/process/task_queues:96:5)
at Migration._deploy (/home/siwei/.asdf/installs/nodejs/16.15.1/.npm/lib/node_modules/truffle/build/webpack:/packages/migrate/Migration.js:68:1)
at Migration._load (/home/siwei/.asdf/installs/nodejs/16.15.1/.npm/lib/node_modules/truffle/build/webpack:/packages/migrate/Migration.js:54:1)
at Migration.run (/home/siwei/.asdf/installs/nodejs/16.15.1/.npm/lib/node_modules/truffle/build/webpack:/packages/migrate/Migration.js:202:1)
at Object.runMigrations (/home/siwei/.asdf/installs/nodejs/16.15.1/.npm/lib/node_modules/truffle/build/webpack:/packages/migrate/index.js:152:1)
at Object.runFrom (/home/siwei/.asdf/installs/nodejs/16.15.1/.npm/lib/node_modules/truffle/build/webpack:/packages/migrate/index.js:117:1)
at Object.run (/home/siwei/.asdf/installs/nodejs/16.15.1/.npm/lib/node_modules/truffle/build/webpack:/packages/migrate/index.js:94:1)
at module.exports (/home/siwei/.asdf/installs/nodejs/16.15.1/.npm/lib/node_modules/truffle/build/webpack:/packages/core/lib/commands/migrate/runMigrations.js:10:1)
at Object.module.exports [as run] (/home/siwei/.asdf/installs/nodejs/16.15.1/.npm/lib/node_modules/truffle/build/webpack:/packages/core/lib/commands/migrate/run.js:41:1)
at runCommand (/home/siwei/.asdf/installs/nodejs/16.15.1/.npm/lib/node_modules/truffle/build/webpack:/packages/core/lib/command-utils.js:184:1)
Truffle v5.5.18 (core: 5.5.18)
Node v16.15.1
看起来是 750秒之后出的问题 , 迟迟没有被确认 . 不过刷新了下浏览器,这个contract已经被部署成功了(估计是 truffle没有收到 response 或者event )

所以开始踩坑: 看了下区块链浏览器, 在我发布contract的30分钟内是没有任何block产生的。

11. 所以调高 gas价格,我们重新发布:
修改 truffle-config.js
truffle deploy --network goerli --verbose-rpc --interactive --skip-dry-run --reset
这次就可以了。

require('dotenv').config();
const HDWalletProvider = require('@truffle/hdwallet-provider');
const { INFURA_API_URL, MNEMONIC } = process.env;
module.exports = {
networks: {
development: {
host: "127.0.0.1",
port: 8545,
network_id: "*"
},
goerli: {
provider: () => new HDWalletProvider(MNEMONIC, INFURA_API_URL),
network_id: '5',
gas: 5500000,
gasPrice: 50000000000, // 50 gwei 这里特别重要
networkCheckTimeout: 1000000,
timeoutBlocks: 200,
addressIndex: 2
}
}
};
一个正常的log是这样的:
Compiling your contracts... =========================== > Everything is up to date, there is nothing to compile. Starting migrations... ====================== > Network name: 'goerli' > Network id: 5 > Block gas limit: 29971671 (0x1c954d7) 1_initial_migration.js ====================== Replacing 'Migrations' ---------------------- > transaction hash: 0x4ac4d051c622e4ea918149c0e7c162c835f5ecdd51682e853cea8dc368e5dc8e > Blocks: 2 Seconds: 28 > contract address: 0x47165943De7b3d274E36694eef8364e3Df8f3Be5 > block number: 7071808 > block timestamp: 1655423360 > account: 0xc0dD5021e298dB57bEF361C735cd1C04cef2E48A > balance: 3.980539809985891948 > gas used: 193243 (0x2f2db) > gas price: 50 gwei > value sent: 0 ETH > total cost: 0.00966215 ETH > Saving migration to chain. > Saving artifacts ------------------------------------- > Total cost: 0.00966215 ETH 2_deploy_contract.js ==================== Replacing 'Demo' ---------------- > transaction hash: 0xb722d8a45e6d308edeaeee66a394a39e7727f2bc4b004ab8a713ef4fa4db9fe8 > Blocks: 0 Seconds: 12 > contract address: 0xE770243b04094757852b6B06e023F914cA59B522 > block number: 7071810 > block timestamp: 1655423390 > account: 0xc0dD5021e298dB57bEF361C735cd1C04cef2E48A > balance: 3.972081259985891948 > gas used: 123433 (0x1e229) > gas price: 50 gwei > value sent: 0 ETH > total cost: 0.00617165 ETH > Saving migration to chain. > Saving artifacts ------------------------------------- > Total cost: 0.00617165 ETH Summary ======= > Total deployments: 2 > Final cost: 0.0158338 ETH
总结一下调试 需要用到的工具
1. google web browser
2. goerli.etherscan.io (查看部署后的结果)
3. 修改本地的代码 (看配置)
4. 看本地的log 输出