赞
踩
声明:本文只用于个人学习。在参考别人文章后结合自己试验所总结的内容,仅供参考。
本文环境:
Windows
Truffle v5.5.28 (core: 5.5.28)
Node v16.15.0
Ganache CLI v6.12.2 (ganache-core: 2.13.2)
编译环境:vscode
打开终端,输入
npm install -g ganache-cli
安装完成后命令行输入$ ganache-cli,查看是否安装成功。
注意:启动ganache-cli也是此指令。
PS E:\VSCODE\helloworld> ganache-cli Ganache CLI v6.12.2 (ganache-core: 2.13.2) Available Accounts ================== (0) 0x4Ef77E82D5eC8660Dc92e91C771af74e3476028a (100 ETH) (1) 0x79733c1C637486f1E37dF51979C9bAD767d5d6A1 (100 ETH) (2) 0xB013643232F3dC05DE3D1a6dB281aB2A3AAb379F (100 ETH) (3) 0x0Adf75FF3017CB05267F55C0d28C947d71f353C8 (100 ETH) (4) 0x154A33e652B41ff773398F8E71eb095F95556fe3 (100 ETH) (5) 0xb81Ccab240F70Cd1Ead20F03eD7676415E7cE3ac (100 ETH) (6) 0x29116B55aFDb2278a2C4A4E9abf48c052c3F1810 (100 ETH) (7) 0x03962DB7A11c862715815E029c8e703917c668aa (100 ETH) (8) 0x48E2021a19eC67e802bC3BB26e59537e7981716C (100 ETH) (9) 0x9dDBCa6bbbb095607CC7D52cEb0b6e15ee218e32 (100 ETH) Private Keys ================== (0) 0x01ba9c1fef0e6143af73547406d6197f25517acc1e116d6be5a2e9c34b65c29f (1) 0x7e36c1b0c4f6ecc793cb2230296e4ecf8e766380ed31da3b7840a55e2feb332d (2) 0x820648d31f094b67c88efa5368a17700b91cd08d56525dafb0297946113861e2 (3) 0x384f62cb50e92dd40a61b64569e4826f06e4b58bb641ac146e3c711bc6cb951c (4) 0xa0ab18b41192e27616b8928a6bd8180f12ed07024d5d93deb8809d8c60fccc81 (5) 0x29dd757fa884ecdb236f8f71b55004c34c6c93f7a04e39402302302aa0f7992c (6) 0x32ea3c8662f24780f6129d362550f6d17dfda96340554d66c7e0027998d64741 (7) 0x89a25c4983e17c10d3c5f8266b9423cf04db645a1296c02ec165e8a6e96fa573 (8) 0x400fc50e4200c10098ef2bbf54456fbf909da3aa04afcc49fe92354ee0df30f1 (9) 0x097a624e0a6d2e0d6cb963befdbf33b427e9bbf5bddee6446473a45a7fd8c823 HD Wallet ================== Mnemonic: fiber obvious coffee census mail pet appear amazing lunar turtle filter wish 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
创建文件夹并初始化项目
PS E:\VSCODE>mkdir HelloWorld
PS E:\VSCODE>cd HelloWorld
PS E:\VSCODE\helloworld> truffle init
Starting init...
================
> Copying project files to E:\VSCODE\helloworld
Try our scaffold commands to get started:
$ truffle create contract YourContractName # scaffold a contract
$ truffle create test YourTestName # scaffold a test
http://trufflesuite.com/docs
完成后的文件目录如下:
当使用Windows的命令行时,默认的配置文件名与truffle冲突。这种情况下,我们推荐使用Windows的power Shell或Git BASH。你也可
以将配置文件重命名为truffle-config.js来避免冲突。
pragma solidity ^0.4.17;
contract HelloWorld {
//say hello world
function say() public pure returns (string) {
return "Hello World";
}
//print name
function print(string name) public pure returns (string) {
return name;
}
}
var HelloWorld = artifacts.require("./HelloWorld.sol");
module.exports = function(deployer) {
deployer.deploy(HelloWorld);
};
module.exports = {
networks: {
development: {
host: "localhost",
port: 8545,
network_id: "*"
}
}
};
Ganache CLI默认运行在8545端口。
如果要部署到指定的网络,可以使用–network参数,例如:
truffle migrate --network live
多个网络的配置格式如下:
networks: { development: { host: "localhost", port: 8545, network_id: "*" // match any network }, live: { host: "178.25.19.88", // Random IP for example purposes (do not use) port: 80, network_id: 1, // Ethereum public network // optional config values: // gas Gas limit used for deploys. Default is 4712388 // gasPrice Gas price used for deploys. Default is 100000000000 (100 Shannon). // from - default address to use for any transaction Truffle makes during migrations // provider - web3 provider instance Truffle should use to talk to the Ethereum network. // - if specified, host and port are ignored. } }
稳妥起见,也可以加入指定编译器,内容如下:
module.exports = { compilers: { solc: { version: "0.4.17", // Fetch exact version from solc-bin (default: truffle's version) // docker: true, // Use "0.5.1" you've installed locally with docker (default: false) // settings: { // See the solidity docs for advice about optimization and evmVersion // optimizer: { // enabled: false, // runs: 200 // }, // evmVersion: "byzantium" // } } }, networks: { development: { host: "localhost", port: 8545, network_id: "*" // match any network }, live: { host: "178.25.19.88", // Random IP for example purposes (do not use) port: 80, network_id: 1, // Ethereum public network // optional config values: // gas Gas limit used for deploys. Default is 4712388 // gasPrice Gas price used for deploys. Default is 100000000000 (100 Shannon). // from - default address to use for any transaction Truffle makes during migrations // provider - web3 provider instance Truffle should use to talk to the Ethereum network. // - if specified, host and port are ignored. } } };
PS E:\VSCODE\helloworld> truffle compile
Compiling your contracts...
===========================
> Compiling .\contracts\HelloWorld.sol-bin. Attempt #1
> Artifacts written to E:\VSCODE\helloworld\build\contracts
> Compiled successfully using:
- solc: 0.4.17+commit.bdeb9e52.Emscripten.clang
首先启动Ganache CLI,新开一个命令窗口,输入命令ganache-cli,如果刚才已经启动过,就不需要再次启动了。
然后回到Truffle,输入truffle migrate开始部署
yuyangdeMacBook-Pro:TestTruffle yuyang$ truffle migrate
Using network 'development'.
Network up to date.
如果部署成功,会显示以下结果
PS E:\VSCODE\helloworld> truffle migrate --reset Compiling your contracts... =========================== > Compiling .\contracts\HelloWorld.sol > Artifacts written to E:\VSCODE\helloworld\build\contracts > Compiled successfully using: - solc: 0.4.17+commit.bdeb9e52.Emscripten.clang Starting migrations... ====================== > Network name: 'development' > Network id: 1662306457614 > Block gas limit: 6721975 (0x6691b7) 1_initial_migration.js ====================== Deploying 'HelloWorld' ---------------------- > transaction hash: 0xa90362bffda2beb9020351f9b88a95a7384c9e21cb1c14adc008f8b9e04a602d > Blocks: 0 Seconds: 0 > contract address: 0xce8C19769f9eC8EE5935108524e5377f990a1cC1 > block number: 1 > block timestamp: 1662306500 > account: 0x4Ef77E82D5eC8660Dc92e91C771af74e3476028a > gas used: 176982 (0x2b356) > gas price: 20 gwei > value sent: 0 ETH > total cost: 0.00353964 ETH > Saving artifacts ------------------------------------- > Total cost: 0.00353964 ETH Summary ======= > Total deployments: 1 > Final cost: 0.00353964 ETH
PS E:\VSCODE\helloworld> truffle console
truffle(development)> var contract
undefined
truffle(development)> HelloWorld.deployed().then(function(instance){contract = instance;});
undefined
truffle(development)> contract.say()
'Hello World'
truffle(development)> contract.print("hello world")
'hello world'
var contract和javascript语法一样,表示声明一个contract变量。
HelloWorld.deployed().then(function(instance){contract= instance;});
表示将HelloWorld合约主体,传递给contract变量。
后面我们就可以直接使用变量contract分别调用say()方法和print(‘’),得到我们想要的结果。
测试成功!
参考文件:https://www.jianshu.com/p/9233c214e205
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。