这里讲使用 truffle
部署合约。
BSC 测试网
在此之前请先查看
这里我们部署一个 ERC20
的合约。
首先,这里认为已经创建好了 truffle
solidity
项目。
npm install @openzeppelin/contracts@3.4.0
npm install @truffle/hdwallet-provider // 部署脚本
整个项目如下
--contracts
----ERC20FixedSupply
--migrations
----2_deploy_ERC20FixedSupply
--.secret
--truffle-config.js
其中,各文件内容如下
ERC20FixedSupply
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
| pragma solidity >=0.4.22 <0.8.0;
import "@openzeppelin/contracts/token/ERC20/ERC20.sol";
contract ERC20FixedSupply is ERC20 { string private message;
constructor( string memory name, string memory symbol, uint256 totalSupply ) public ERC20(name, symbol) { message = "OK"; _mint(msg.sender, totalSupply * (10 ** decimals())); }
function getMessage() public view returns(string memory) { return message; }
function setMessage(string memory newMessage) public { message = newMessage; } }
|
2_deploy_ERC20FixedSupply
1 2 3 4 5 6 7 8
| const ERC20FixedSupply = artifacts.require("ERC20FixedSupply");
module.exports = function (deployer) { deployer.deploy(ERC20FixedSupply, "My Golden Coin", "MGC", 1000000000); };
|
这里要特别注意的是
ERC20FixedSupply
的文件名、合约名都得是一样的,才能部署。
truffle-config.js
这里的内容直接 copy
的内容。
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
| const HDWalletProvider = require('@truffle/hdwallet-provider'); const fs = require('fs'); const mnemonic = fs.readFileSync(".secret").toString().trim();
module.exports = { networks: { development: { host: "127.0.0.1", port: 8545, network_id: "*", }, testnet: { provider: () => new HDWalletProvider(mnemonic, `https://data-seed-prebsc-1-s1.binance.org:8545`), network_id: 97, confirmations: 10, timeoutBlocks: 200, skipDryRun: true }, bsc: { provider: () => new HDWalletProvider(mnemonic, `https://bsc-dataseed1.binance.org`), network_id: 56, confirmations: 10, timeoutBlocks: 200, skipDryRun: true }, },
mocha: { },
compilers: { solc: { version: "^0.7.0", } } }
|
.secret
内容是一个地址的助记词。「助记词可以通过 metamask
获得」
这里提一下如何获得 bsc
的测试币
编译
truffle compile
部署
truffle migrate --network testnet -f 2
或者
truffle migrate --network testnet -f 2 --reset --compile-none
这里的 2
参考下面的命令行参数。
关于上面的命令参数可以参考
但是,如果使用上面的部署文件,失败率非常高,经常会遇到下面的错误
PollingBlockTracker - encountered an error while attempting to update latest block:\n${err.stack
data: { originalError: { code: 'ETIMEDOUT', connect: true } }
即便是按照网上的内容,增加连接时间之类的还是会出现各种问题。
究其本质原因,就是网络不稳定的缘故,所以,这里推荐一下别的公开节点
部署好之后,输出内容如下
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
| Compiling your contracts... =========================== > Everything is up to date, there is nothing to compile.
Starting migrations... ====================== > Network name: 'testnet' > Network id: 97 > Block gas limit: 29999542 (0x1c9c1b6)
2_deploy_ERC20FixedSupply.js ============================
Replacing 'ERC20FixedSupply' ---------------------------- > transaction hash: 0xedec917512d005c25dd0a6233dbab88fe80e3907d35913c529b4f5b95ddf8b8c > Blocks: 1 Seconds: 5 > contract address: 0x444b74F93b1bCab3220b9C03bDeBA364F7EAa3B2 > block number: 15513720 > block timestamp: 1641134972 > account: 0xD1B260aba3bA6f3D3099725b1b1C7cdD12581974 > balance: 0.60656578 > gas used: 1287771 (0x13a65b) > gas price: 20 gwei > value sent: 0 ETH > total cost: 0.02575542 ETH
Pausing for 10 confirmations... ------------------------------- > confirmation number: 2 (block: 15513723) > confirmation number: 3 (block: 15513724) > confirmation number: 4 (block: 15513725) > confirmation number: 6 (block: 15513727) > confirmation number: 7 (block: 15513728) > confirmation number: 8 (block: 15513729) > confirmation number: 10 (block: 15513731) > Saving artifacts ------------------------------------- > Total cost: 0.02575542 ETH
Summary ======= > Total deployments: 1 > Final cost: 0.02575542 ETH
|
OK
,这里假定你已经部署好合约了。
交互
truffle console --network testnet
输入上面的命令,我们就进入了交互界面。
ERC20FixedSupply.abi
ERC20FixedSupply.address
交互,调用合约的方法。
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
| truffle(testnet)> ERC20FixedSupply.deployed().then(function(instance){return instance.getMessage();}) 'OK' truffle(testnet)> ERC20FixedSupply.deployed().then(function(instance){return instance.setMessage("Hello, truffle");}) { tx: '0xc47d9966da41527ecd0d50916ff3c02abc5e36a80a67c67e2fe935021bd47294', receipt: { blockHash: '0xa5ef68a88978d01e686ae987f33111c3ebcd389836b64b26258509c057c5445c', blockNumber: 15514314, contractAddress: null, cumulativeGasUsed: 1190605, from: '0xd1b260aba3ba6f3d3099725b1b1c7cdd12581974', gasUsed: 29293, logs: [], logsBloom: '0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000', status: true, to: '0x444b74f93b1bcab3220b9c03bdeba364f7eaa3b2', transactionHash: '0xc47d9966da41527ecd0d50916ff3c02abc5e36a80a67c67e2fe935021bd47294', transactionIndex: 3, type: '0x0', rawLogs: [] }, logs: [] } truffle(testnet)> ERC20FixedSupply.deployed().then(function(instance){return instance.getMessage();}) 'Hello, truffle'
|