truffle开发一个简单的Dapp

1.安装Truffle:npm install -g truffle

2.建立项目目录并进入:mkdir pet-shop-tutorial     

                                      cd pet-shop-tutorial

3.使用truffle unbox创建项目,这种方式的前端直接就是写好的:truffle unbox pet-shop

4.在contracts目录下,添加合约文件Adoption.sol

pragma solidity ^0.4.17;

contract Adoption {

  address[16] public adopters;  // 保存领养者的地址

    // 领养宠物
  function adopt(uint petId) public returns (uint) {
    require(petId >= 0 && petId <= 15);  // 确保id在数组长度内

    adopters[petId] = msg.sender;        // 保存调用这地址 
    return petId;
  }

  // 返回领养者
  function getAdopters() public view returns (address[16]) {
    return adopters;
  }

}

5.编译合约:truffle compile

6.创建一个自己的部署脚本2_deploy_contracts.js

var Adoption = artifacts.require("Adoption");

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

7.执行部署之前,确保有一个区块链运行,使用Ganache开启一个私链进行开发测试,默认会在7545端口上。安装Ganache:执行wget https://github.com/trufflesuite/ganache/release/download/v1.0.1/ganache-1.0.1-x86_64.AppImage,再修改权限:chmod +x ganache-1.0.1-x86_64.AppImage,双击图标就可以运行Ganache。

8.部署合约:truffle mimgrate

9.测试,在test目录下新建一个TestAdoption.sol

pragma solidity ^0.4.17;

import "truffle/Assert.sol";   // 引入的断言
import "truffle/DeployedAddresses.sol";  // 用来获取被测试合约的地址
import "../contracts/Adoption.sol";      // 被测试合约

contract TestAdoption {
  Adoption adoption = Adoption(DeployedAddresses.Adoption());

  // 领养测试用例
  function testUserCanAdoptPet() public {
    uint returnedId = adoption.adopt(8);

    uint expected = 8;
    Assert.equal(returnedId, expected, "Adoption of pet ID 8 should be recorded.");
  }

  // 宠物所有者测试用例
  function testGetAdopterAddressByPetId() public {
    // 期望领养者的地址就是本合约地址,因为交易是由测试合约发起交易,
    address expected = this;
    address adopter = adoption.adopters(8);
    Assert.equal(adopter, expected, "Owner of pet ID 8 should be recorded.");
  }

    // 测试所有领养者
  function testGetAdopterAddressByPetIdInArray() public {
  // 领养者的地址就是本合约地址
    address expected = this;
    address[16] memory adopters = adoption.getAdopters();
    Assert.equal(adopters[8], expected, "Owner of pet ID 8 should be recorded.");
  }
}

10.运行测试用例:truffle test

11.Truffle Box的pet-shop中,已经包含了应用的前端代码,都在src/文件夹中,更改app.js来实现应用的功能。初始化web3,修改initWeb3(),删除注释,修改为

initWeb3: function() {
  // Is there an injected web3 instance?
  if (typeof web3 !== 'undefined') {
    App.web3Provider = web3.currentProvider;
  } else {
    // If no injected web3 instance is detected, fall back to Ganache
    App.web3Provider = new Web3.providers.HttpProvider('http://localhost:7545');
  }
  web3 = new Web3(App.web3Provider);

  return App.initContract();
}

12.实例化合约,修改initContract()函数

initContract: function() {
  // 加载Adoption.json,保存了Adoption的ABI(接口说明)信息及部署后的网络(地址)信息,它在编译合约的时候生成ABI,在部署的时候追加网络信息
  $.getJSON('Adoption.json', function(data) {
    // 用Adoption.json数据创建一个可交互的TruffleContract合约实例。
    var AdoptionArtifact = data;
    App.contracts.Adoption = TruffleContract(AdoptionArtifact);

    // Set the provider for our contract
    App.contracts.Adoption.setProvider(App.web3Provider);

    // Use our contract to retrieve and mark the adopted pets
    return App.markAdopted();
  });
  return App.bindEvents();
}

13.处理领养,修改markAdopted()

markAdopted: function(adopters, account) {
  var adoptionInstance;

  App.contracts.Adoption.deployed().then(function(instance) {
    adoptionInstance = instance;

    // 调用合约的getAdopters(), 用call读取信息不用消耗gas
    return adoptionInstance.getAdopters.call();
  }).then(function(adopters) {
    for (i = 0; i < adopters.length; i++) {
      if (adopters[i] !== '0x0000000000000000000000000000000000000000') {
        $('.panel-pet').eq(i).find('button').text('Success').attr('disabled', true);
      }
    }
  }).catch(function(err) {
    console.log(err.message);
  });
}

14.修改handleAdopt()

handleAdopt: function(event) {
  event.preventDefault();

  var petId = parseInt($(event.target).data('id'));

  var adoptionInstance;

  // 获取用户账号
  web3.eth.getAccounts(function(error, accounts) {
    if (error) {
      console.log(error);
    }
  
    var account = accounts[0];
  
    App.contracts.Adoption.deployed().then(function(instance) {
      adoptionInstance = instance;
  
      // 发送交易领养宠物
      return adoptionInstance.adopt(petId, {from: account});
    }).then(function(result) {
      return App.markAdopted();
    }).catch(function(err) {
      console.log(err.message);
    });
  });
}

15.安装MetaMask,点击链接https://metamask.io/,将插件安装到浏览器中,点击浏览器右上角的图标,首先我们选择一个测试网络(如粉红色的),进入界面,选择Accept,在登录页面中点击“Import Existing DEN”,还原Ganache创建的钱包,作为开发测试钱包,在输入框中填写candy maple cake sugar pudding cream honey rich smooth crumble sweet treat这几个词,输入自己的密码。

16.登录之后,点击Custom RPC,添加一个网络:http://127.0.0.1:7545,切换之后返回,看到第一个账户的状态

17.运行npm run dev启动服务

自己在完成这个项目中遇到的问题

问题一:执行truffle compile虚拟机卡顿,一直执行不下去

解决方法:将虚拟机的内存改大一点,可以改为3G

问题二:执行了npm run dev,但是不能直接启动到Firefox浏览器

解决办法:直接打开浏览器,在地址栏中输入localhost:3000

问题三:进入到项目后,网页上显示不出pet的信息

解决办法:显示不出信息是因为jQuery的加载问题,直接百度一个jQuery.1.11.1.min.js,替换新的链接

问题四:当点击完Adopt,并且submit之后,相应的按钮应该显示为success,通过打印输出发现是adopters数组有问题,地址全都为0x

解决办法:再执行一次truffle migrate

根据https://learnblockchain.cn/2018/01/12/first-dapp/#more博主的分享总结的。

原文地址:https://www.cnblogs.com/mxj333/p/8876865.html