Truffle以太坊合约部署实战

  • potaxie
  • 更新于 2020-07-11 11:32
  • 阅读 4483

详细介绍如何利用truffle,完成以太坊合约的编译部署测试

概述

truffle 是世界级的以太坊开发框架
  • 内置智能合约编译、连接、开发和二进制管理
  • 快速开发的自动化合约测试
  • 脚本、可扩展性部署和迁移框架
  • 用于部署到任意数量的公网和私网的网络管理
  • 为合约通信提供交互式控制台

创建项目

truffle init

目录结构

  • contracts: 存放合约
  • migrations: 存放部署脚本
  • test: 测试文件
  • truffle-config.js: 配置文件,配置不同网络

创建合约

pragma solidity ^0.4.24;

contract SimpleStorage{
    uint storedData;

    function set(uint x) public{
        storedData =x;
    }

    function get() public view returns (uint){
        return storedData;
    }
}

编译合约

生成build/contract 编译文件

truffle compile

执行编译之后,会生成build文件夹,里面会有abi、bytecode、network

image.png

部署脚本

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

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

部署网络

    //你所要部署的网络的名字
    ganacheNet: {
     host: "127.0.0.1",     // Localhost (default: none)
     port: 7545,            // Standard Ethereum port (default: none)
    network_id: "*",       // Any network (default: none)
    },

结果展示

truffle migrate --network ganacheNet

image.png

此时交易已经产生到ganache

image.png

通过remix测试

at address 用 ganache 里面的 create address

image.png

Git 地址 https://github.com/potaxie/truffle-init

点赞 2
收藏 2
分享
本文参与登链社区写作激励计划 ,好文好收益,欢迎正在阅读的你也加入。

0 条评论

请先 登录 后评论
potaxie
potaxie
坐标上海 四年大数据+两年区块链工作经验 git地址:https://github.com/potaxie 微信:potaxie