Closed11

truffle-config.jsの設定ミスでGanacheネットワークに接続できない

ikmzkroikmzkro

MIgrationsに必要なコードの理解

//継承の修飾子
  modifier Restricted_contracts_owner() {
    if(msg.sender == contracts_owner) _;
  }

  //migrationsの実行
  function SetCompletedMigrations(uint completed) public Restricted_contracts_owner{
    last_completed_migration = completed;
  }

  //migrationsの更新
  function UpgradeMigrations(address present_address) public Only_past_contracts_owner{
    //呪文かこれ・(^ω^)・・・
    Migrations Upgraded = Migrations(present_address);
    Upgraded.SetCompletedMigrations(last_completed_migration);
  }
ikmzkroikmzkro

truffleのコンパイル

$ truffle compile

Compiling your contracts...
===========================
✔ Fetching solc version list from solc-bin. Attempt #1
✔ Downloading compiler. Attempt #1.
> Compiling ./src/contracts/Migrations.sol
> Compiling ./src/contracts/Tether.sol
✔ Fetching solc version list from solc-bin. Attempt #1
ikmzkroikmzkro

エラー出て草、このような修飾子は受付ないという意味だったので修正

project:/src/contracts/Migrations.sol:27:62: DeclarationError: Undeclared identifier.
  function UpgradeMigrations(address present_address) public Only_past_contracts_owner{
                                                             ^-----------------------^

Compilation failed. See above.
ikmzkroikmzkro

ウェイ、成功した

Compiling your contracts...
===========================
✔ Fetching solc version list from solc-bin. Attempt #1
> Compiling ./src/contracts/Migrations.sol
> Compiling ./src/contracts/Tether.sol
✔ Fetching solc version list from solc-bin. Attempt #1
> Artifacts written to /home/sekai/defi-staking-app/defi-staking-app/src/truffle_abis
> Compiled successfully using:
``
ikmzkroikmzkro

truffle_abis/が生成されてる。
もっかい

Compiling your contracts...
===========================
✔ Fetching solc version list from solc-bin. Attempt #1
> Everything is up to date, there is nothing to compile.
ikmzkroikmzkro

migration実行

$ truffle migrate

エラー出た草

Compiling your contracts...
===========================
✔ Fetching solc version list from solc-bin. Attempt #1
> Everything is up to date, there is nothing to compile.

> Something went wrong while attempting to connect to the network at http://127.0.0.1:7545:7545. Check your network configuration.

Could not connect to your Ethereum client with the following parameters:
    - host       > 127.0.0.1:7545
    - port       > 7545
    - network_id > *
Please check that your Ethereum client:
    - is running
    - is accepting RPC connections (i.e., "--rpc" or "--http" option is used in geth)
    - is accessible over the network
    - is properly configured in your Truffle configuration file (truffle-config.js)

Truffle v5.4.16 (core: 5.4.16)
Node v14.18.0
ikmzkroikmzkro

どうやらネットワーク接続がうまくいっていない。
truffle-config.jsの設定を見直す必要がある。

https://qiita.com/romorimori/items/155bb1f5e4cced629ce8

修正実施↓↓↓

//Ganasheテストネットワーク接続の最小部品をここで構成
module.exports = {
  networks: {
    development: {
      host: "127.0.0.1",     // Localhost (default: none)
      port: 7545,            // Standard Ethereum port (default: none)
      network_id: "*",       // Any network (default: none)
    }
  },
ikmzkroikmzkro
$ truffle init
contracts already exists in this directory...

うん、前回のmigration時に下記のファイルが自動生成されたいたようですね。

contracts/Migrations.sol

// SPDX-License-Identifier: MIT
pragma solidity >=0.4.22 <0.9.0;

contract Migrations {
  address public owner = msg.sender;
  uint public last_completed_migration;

  modifier restricted() {
    require(
      msg.sender == owner,
      "This function is restricted to the contract's owner"
    );
    _;
  }

  function setCompleted(uint completed) public restricted {
    last_completed_migration = completed;
  }
}

削除して初期化しましたが、これでもうまくいきません。

ikmzkroikmzkro

root/migartions, root/contracts, truffle-config.jsを削除して初期化します。
うまくいったようです。

$ truffle init

Starting init...
================

> Copying project files to /home/sekai/defi-staking-app/defi-staking-app

Init successful, sweet!

Try our scaffold commands to get started:
  $ truffle create contract YourContractName # scaffold a contract
  $ truffle create test YourTestName         # scaffold a test
ikmzkroikmzkro

では、今まで書いていたコードで新規プロジェクトコードを書き換えます
これでも結果は同じくネットワーク接続エラーでした。

I/Oを理解せずに進めた結果だ、反省しろ

次はいつやるかな

このスクラップは2022/05/28にクローズされました