Truffle以太坊合约部署实战

详细介绍如何利用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

本文参与区块链开发网写作激励计划 ,好文好收益,欢迎正在阅读的你也加入。

  • 发表于 2020-07-11 11:32
  • 阅读 ( 2935 )
  • 学分 ( 145 )
  • 分类:以太坊
© 版权声明
THE END
喜欢就支持一下吧
点赞0
分享
评论 抢沙发
区块链技术的头像-区块链开发网

昵称

取消
昵称表情代码图片