天地维杰网

人如秋鸿来有信,事若春梦了无痕


  • 首页

  • Redis

  • java

  • linux

  • 日常问题

  • Spring和Springboot

  • Mac相关

  • 中间件

  • 架构

  • python

  • 前端

  • jvm

  • c语言

  • web3

  • 归档

  • 关于

  • 搜索
close

truffle 创建测试合约并部署到测试网络

时间: 2023-02-11   |   分类: web3     |   原创   |   阅读: 684 字 ~2分钟

1、npm 安装truffle

npm install -g truffle

2、创建truffle项目

mkdir imooc-on-blockchain-truffle && cd imooc-on-blockchain-truffle

3、初始化truffle目录,会生成如下几个目录

  • contracts 存放.sol合约文件
  • migrations 部署脚本目录
  • test 测试文件目录
  • truffle-config.js truffle配置文件

    % truffle init
    % ls -l
    contracts               
    migrations              
    test                    
    truffle-config.js

4、编译合约

编译合约以后会生成build目录,存放编译好生成的合约的json文件

truffle compile

5、修改合约 如果要部署自己的合约文件,将合约文件赋值到contracts目录下重新编译即可。 如果只是需要运行truffle的demo,不用改变

6、修改部署脚本 使用 artifacts.require("CourseList")方法引入合约,名称为定义好的合约名称。 然后将deployer.deploy()方法测参数修改为相应的参数名称。

const CourseList = artifacts.require("CourseList");

module.exports = function(deployer) {
  deployer.deploy(CourseList);
};

7、启动dashboard 在项目的目录中执行truffle dashboard命令。会启动一个web服务并在默认浏览器中打开一个页面。

% truffle dashboard

8、执行部署 在另外一个命令行窗口中,执行以下命令,进行合约部署。

% truffle migrate --network dashboard

部署的过程中,在dashboard的页面中会有提示,是否发送,点击发送,会继续部署。

部署成功以后,在命令行窗口中会返回部署结果。

% truffle migrate --network dashboard
Compiling your contracts...
===========================
> Everything is up to date, there is nothing to compile.


Starting migrations...
======================
> Network name:    'dashboard'
> Network id:      11155111
> Block gas limit: 30000000 (0x1c9c380)


1_deploy_contracts.js
=====================

   Deploying 'CourseList'
   ----------------------
   > transaction hash:    0xc603ed8246528259fed877e62449fd35d3159e6c6dd07595a20a920353f5f3dassage.
   > Blocks: 5            Seconds: 68
   > contract address:    0x88888888888888888888888888888140937d29cD
   > block number:        2883963
   > block timestamp:     1676176488
   > account:             0x888888888888888888888888888889c16FcA382a
   > balance:             0.12207404998410734
   > gas used:            1696218 (0x19e1da)
   > gas price:           2.500000007 gwei
   > value sent:          0 ETH
   > total cost:          0.004240545011873526 ETH

   > Saving artifacts
   -------------------------------------
   > Total cost:     0.004240545011873526 ETH

Summary
=======
> Total deployments:   1
> Final cost:          0.004240545011873526 ETH

返回结果中会显示部署合约的耗费,以及部署到的地址> contract address: 0x88888888888888888888888888888140937d29cD 使用这个地址,在相应的etherscan可以查看相应信息。 https://sepolia.etherscan.io/tx/${contractAddress}

参考文档:

  • https://trufflesuite.com/docs/truffle/how-to/install/
  • https://trufflesuite.com/docs/truffle/quickstart/
  • https://trufflesuite.com/docs/truffle/how-to/use-the-truffle-dashboard/
#web3# #truffle# #contract#
Redis--COW(Copy On Write)
  • 文章目录
  • 站点概览
不与天斗Domino

不与天斗Domino

Programmer & Architect

183 日志
15 分类
224 标签
© 2013 - 2023 天地维杰网 京ICP备13019191号-1
Powered by - Hugo v0.63.2
Theme by - NexT
0%