Truffle-Petshop

Truffle-Petshop项目搭建

关于本项目,你需要的一些准备工作。

  • 安装vscode(推荐)
  • 安装 ganache(也可以不安装,不过推荐安装,可以用于本地私有链测试)
  • 安装metamask(虚拟币钱包插件,可在chrome应用商店下载)
  • 安装Nodejs:下载地址https://nodejs.org/en/download/
  • 当然对于nodejs你可能需要一些配置,可以在网上找到教程。
  • 安装 Truffle :npm install -g truffle
  • 安装ganache-cli:npm install -g ganache-cli(脚手架,用于创建区块链,所以ganache在此项目中是一个可选项而不是必选,但是本人选择直接用了本地的ganache)

安装过程可以参考:区块链DAPP-基于Truffle框架的宠物商店_黄豆2019的博客-CSDN博客

这里主要讲一下这个项目的大概目录与架构。

如何引入项目?

在项目目录运行:
truffle unbox pet-shop

关于truffle目录结构的介绍:

contract:solidity智能合约所在的目录
----这里有一个重要的合约Migrations.sol(在后面介绍)

migrations:Truffle使用迁移系统来处理智能合约部署。迁移是一个附加的特殊智能合约,用于跟踪更改。

test:测试文件所在的目录,智能合约的JavaScript和Solidity测试

truffle.js:truffle项目的配置文件

在本项目中使用到的truffle命令:

truffle compile
  ---用于将Solidity编译为字节代码,以供以太坊虚拟机(EVM)执行

添加合约代码:

contracts > Adoption.sol

pragma solidity >=0.4.22 <0.8.0;

contract Adoption{

  address[16] public adopters;

  function adopt(uint petId) public returns (uint){
    require(petId<=15 && petId>=0);
    adopters[petId] = msg.sender;//记录领养者地址
    return petId;
  }

  /*此处该数组若未声明为memory变量会报错
  因为memory表示值变量,即只是复制副本;而默认为storage是引用变量,所有的引用都实际只想一处
  如果这里是storage变量,则外部可以修改该变量,这显然不符合区块链的安全性原则

  view只读 pure不读写
  */
  function getAdopters() public view returns (address[16] memory){
    return adopters;
  }
}

添加完成后编译合约:

truffle compile

预期输入:
Compiling .\contracts\Adoption.sol...

Compiling .\contracts\Migrations.sol...

Writing artifacts to .\build\contracts

创建迁移脚本

migrations目录中创建迁移脚本,告诉Truffle如何部署智能合约,在该目录中创建文件2_deploy_contracts.js

注意,在migrations目录中所有文件都有编号,作用是让Truffle知道执行它们的顺序。

2_deploy_contracts.js文件内容如下:

migrations > 2_deploy_contracts.js

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

module.exports = function(deployer) {

  deployer.deploy(Adoption);

};

上面的代码中:

  • 首先,require了创建的合约,并将其分配给一个名为“Adoption”的变量。
  • 接着,将合约加入部署清单,运行迁移命令时合约将被部署。

修改配置文件:

修改dapp-guide-pet-shop配置文件truffle-config.js,连接本地地址和端口。

在这里插入图片描述

这个develop配置也不知道是哪里来的,,但是对我这个项目没有用

如果你已经下载了ganache,可以直接进行迁移(记得先把gannache打开),如果你没有下载gannache,请输入命令:

我们将使用ganache-cli,来开启一个私链来进行开发测试,可用于部署合同,开发应用程序和运行测试,设置端口:8545。

ganache-cli >> D:\xxx\你想存放的路径\ganachetrace.log

日志输出至指定的文件,默认启动的10个钱包账户和对应的私钥地址,账户余额均为100ETH。

执行迁移命令:

truffle migrate

matamask联动使用

区块链DAPP-基于Truffle框架的宠物商店_黄豆2019的博客-CSDN博客

添加的代码

test > TestAdoption.sol

pragma solidity ^0.5.0;

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

contract TestAdoption {
  // The address of the adoption contract to be tested
  Adoption adoption = Adoption(DeployedAddresses.Adoption());

  // The id of the pet that will be used for testing
  uint expectedPetId = 8;

  //The expected owner of adopted pet is this contract
  address expectedAdopter = address(this);

  // Testing the adopt() function
  function testUserCanAdoptPet() public {
    uint returnedId = adoption.adopt(expectedPetId);

    Assert.equal(returnedId, expectedPetId, "Adoption of the expected pet should match what is returned.");
  }

  // Testing retrieval of a single pet's owner
  function testGetAdopterAddressByPetId() public {
   address adopter = adoption.adopters(expectedPetId);

   Assert.equal(adopter, expectedAdopter, "Owner of the expected pet should be this contract");
  }

  // Testing retrieval of all pet owners
  function testGetAdopterAddressByPetIdInArray() public {
   // Store adopters in memory rather than contract's storage
   address[16] memory adopters = adoption.getAdopters();

   Assert.equal(adopters[expectedPetId], expectedAdopter, "Owner of the expected pet should be this contract");
  }
}

输入命令:

>truffle test

Using network 'development'.

Compiling .\test\TestAdoption.sol...

添加代码的案例:一些对链,合约的初始化

区块链DApp从零开始学 (二) | 超详细 DApp创建 | 发行代币token | 宠物领养_111辄的博客-CSDN博客

并重新初始化petshop’

运行:npm run dev

如果你的ganache cli出现问题的话,可以考虑直接使用ganache。

以太坊开发------使用Ganache CLI在私有链上搭建智能合约_bk1171676983的博客-CSDN博客

如果你对metamask与私有链的连接使用有一些疑惑的话:

1.打开gannache

2.执行迁移命令

3.npm run dev

4.将ganache中第一个账户的私钥导入到钱包里成为一个新的账户

关于ganache cli报错:版本过低。

Error: Callback was already called.
at F:\Nodejs\node_global\node_modules\ganache-cli\build\ganache-core.node.cli.js:25:273
at f. (F:\Nodejs\node_global\node_modules\ganache-cli\build\ganache-core.node.cli.js:25:2116)
at f.emit (node:events:513:28)
at f.destroy (F:\Nodejs\node_global\node_modules\ganache-cli\build\ganache-core.node.cli.js:37:906702)
at finish (node:internal/streams/writable:769:14)
at processTicksAndRejections (node:internal/process/task_queues:83:21)

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Samblue_M

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值