PetShopTutorial

nuts3745,blog

Truffle の PetShopTutorial をやる

PetShopTutorial (opens in a new tab)

準備

ディレクトリ構造

スマートコントラクトを書こう

pragma solidity >=0.4.22 <0.6.2;

contract Adoption {

}

変数を用意しよう

pragma solidity >=0.4.22 <0.6.2;

contract Adoption {
    address[16] public adopters;
}

最初の関数:ペットを飼う

contract内のadopters変数を定義したあとに次の関数を書いていく。

//Adopting pet
    function adopt(uint256 petId) public returns (uint256) {
        require(petId >= 0 && petId <= 15);

        adopters[petId] = msg.sender;

        return petId;
    }

2 つ目の関数:飼い主の取得

上で書いたとおり、配列の取得は与えられたキーに対応したひとつの値しか返さない。 なので配列全体を取得する関数を用意する。

    //Retrieving the adopters
    function getAdopters() public view returns (address[16] memory) {
        return adopters;
    }

スマートコントラクトをコンパイルしてマイグレートしよう

pragma solidity >=0.4.22 <0.6.2;

contract Adoption {
    address[16] public adopters;
    //Adopting pet
    function adopt(uint256 petId) public returns (uint256) {
        require(petId >= 0 && petId <= 15);

        adopters[petId] = msg.sender;

        return petId;
    }

    //Retrieving the adopters
    function getAdopters() public view returns (address[16] memory) {
        return adopters;
    }
}

マイグレートしよう

無事コンパイルできたら、それらをブロックチェーンにマイグレートする。

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

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

スマートコントラクトをテストしよう

pragma solidity >=0.4.22 <0.6.2;

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);

}

adopt()関数をテストしよう

// 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");
  }

テストしよう

スマートコントラクトにアクセスするための UI を作成しよう

web3 をインスタンス化しよう

    //Modern dapp browsers...
    if (window.ethereum) {
      App.web3Provider = window.ethereum;
      try {
        //Request account access
        await window.ethereum.enable();
      } catch (error) {
        console.error("User denied account access")
      }
    }
    //Legacy dapp browsers...
    else if (window.web3) {
      App.web3Provider = window.web3.currentProvider;
    }
    //If no injected web3 instance is detected, fall back to Ganache
    else {
      App.web3Provider = new Web3.providers.HttpProvider('http://localhost:7545');
    }
    web3 = new Web3(App.web3Provider);

コントラクトのインスタンス化

$.getJSON('Adoption.json', function (data) {
      // Get the necessary contract artifact file and instantiate it with truffle-contract
      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();
    });
© nuts3745.RSS