以太坊智能合约课
Posted wangguanyi
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了以太坊智能合约课相关的知识,希望对你有一定的参考价值。
第03课-部署智能合约
第03课--部署智能合约
1.部署到Truffle
Truffle中文文档地址:
1.安装 Truffle:
npm install -g truffle2.新建项目文件夹
mkdir myProject cd myProject truffle init
3.安装Openzeppelin
npm install @openzeppelin/contracts@2.5.04.创建Token.sol
vim contracts/Token.solpragma solidity ^0.5.0; import "@openzeppelin/contracts/token/ERC20/ERC20.sol"; import "@openzeppelin/contracts/token/ERC20/ERC20Detailed.sol"; contract ExampleToken is ERC20, ERC20Detailed { constructor () public ERC20Detailed("CuiToken", "CUI", 18){ _mint(msg.sender,10000000000 * (10 ** uint256(decimals()))); } }
5.编译
truffle compile6.创建文件
vim migrations/2_deploy_contracts.jsconst ExampleToken = artifacts.require("ExampleToken"); module.exports = function(deployer) { deployer.deploy(ExampleToken); };
7.部署到Truffle develop
truffle developmigrate8.合约调用
var myCoin ExampleToken.deployed().then(function(instance){myCoin=instance})
2.部署到Ganache
1.修改truffle-config.js文件
vim truffle-config.jsmodule.exports = { networks: { development: { host: "192.168.1.30", // Localhost (default: none) port: 7545, // Standard Ethereum port (default: none) network_id: "*", // Any network (default: none) }, } };
2.部署到Ganache
truffle consolemigrate3.合约调用
var myCoin ExampleToken.deployed().then(function(instance){myCoin=instance})
3.部署到Ropsten
1.安装HDWalletProvider
npm install truffle-hdwallet-provider2.获取Ropsten测试币
获取地址:
3.获取MetaMask助记词
4.注册Infura,获取测试网或主网的KEY
地址:
5.修改truffle-config.js文件
vim truffle-config.jsvar HDWalletProvider = require("truffle-hdwallet-provider"); // 导入模块 var mnemonic = "oppose say prevent raven mystery fiber program pupil poverty else pill enact"; //MetaMask的助记词。 module.exports = { networks: { ropsten: { provider: function() { // mnemonic表示MetaMask的助记词。 "ropsten.infura.io/v3/33..."表示Infura上的项目id return new HDWalletProvider(mnemonic, "https://ropsten.infura.io/v3/e1bb25c2b20b4b5383517028056c89a3", 1); // 0表示第二个账户(从0开始) }, network_id: "*", // match any network gas: 3012388, gasPrice: 20000000000, confirmations: 2, // # of confs to wait between deployments. (default: 0) timeoutBlocks: 200, // # of blocks before a deployment times out (minimum/default: 50) skipDryRun: true // Skip dry run before migrations? (default: false for public nets ) }, } };
6.部署
truffle migrate --network ropsten7.合约调用
truffle console --network ropstenvar myCoin ExampleToken.deployed().then(function(instance){myCoin=instance})
4.部署到主网
1.修改truffle-config.js文件
vim truffle-config.jsvar HDWalletProvider = require("truffle-hdwallet-provider"); // 导入模块 var mnemonic_mainnet = "主网助记词"; //MetaMask的助记词。 module.exports = { networks: { mainnet: { provider: new HDWalletProvider(mnemonic_mainnet, "https://mainnet.infura.io/e1bb25c2b20b4b5383517028056c89a3"), network_id: 1, gas: 3012388, gasPrice: 20000000000, confirmations: 2, // # of confs to wait between deployments. (default: 0) timeoutBlocks: 200, // # of blocks before a deployment times out (minimum/default: 50) skipDryRun: true // Skip dry run before migrations? (default: false for public nets ) } } };
2.部署
truffle migrate --network mainnet3.合约调用
truffle console --network mainnetvar myCoin ExampleToken.deployed().then(function(instance){myCoin=instance})
以上是关于以太坊智能合约课的主要内容,如果未能解决你的问题,请参考以下文章