Programming

(truffle 환경 설정)Truffle deploy 오류 해결 - Error: Could not find artifacts for Simple

IT오이시이 2023. 8. 3. 18:19
728x90

 

(truffle 환경 설정) 스마트 계약 배포 - truffle deploy 오류 해결
- Error: Could not find artifacts for SimpleStorage from any sources

 
간단한 오류의 원인은 truffle  환경의 contracts 소스의 위치를  확인하여야 하는 경우 입니다. 처음 truffle을 사용하다보니 환경 설정의 묵시적인 설정을 잘 이해하지 못한 경우 발생하는 오류 입니다.
 
Truffle 설치 와 스마트 계약(Smart Contract) 개발

1. Truffle 설치 - 스마트 계약 개발 환경 구성 (truffle, ganache-cli, solc )
    - npm install -g truffle
    - npm install -g ganache-cli
    - npm install -g solc

2. Truffle 사용 - 스마트 계약(Smart Contract)개발
   - truffle create contract YourContractName
   - truffle create test YourTestName 

3. [Truffle 환경 설정] 컴파일 오류 대처 - Error: Failed to fetch the Solidity compiler from the following locations:
     - Are you connected to the internet?

4.  [Truffle 환경 설정] Deploy 오류 대처 - Error: Could not find artifacts for SimpleStorage from any sources
  - throw new Error("Could not find artifacts for " + import_path + " from any sources");

 

ㅁ Truffle 명령을 이용한 스마트 계약 개발


# Truffle을 이용하여 스마트 컨트렉트를 이용하는 명령은 다음과 같습니다.

$ ganache-cli  -h 0.0.0.0 -p 8545 &  # 테스트용 블록체인 네트웍을 작동 합니다.

$ truffle init
->  smart contract  소스를 작성 합니다.
$ truffle compile
$ truffle migration ./migration/deploy_someContract.js
$ truffle deploy
$ truffle test ./test/someContrace_test.js

이번에는 truffle migration 에 대한 실행시 오류에 대하여 알아 보고자 합니다.
 


ㅁ 스마트 계약 배포 오류:  truffle migration

  - throw new Error("Could not find artifacts for " + import_path + " from any sources");
#  truffle migration 과 truffle deploy 는 스마트계약을 배포 하는 과정에서 오류가 발생하여 찾아 보았습니다. 인터넷에 찾아도  solidity 컴파일이 안되어 컴파일러를 설치하라고 하지만 컴파일러를 설치해도 해결이 안되었습니다.
 
결국은 truffle 환경 파일 truffle-config.js에  contracts 소스가 저장되는 위치인 "contracts_directory"설정 값에 맞게   smart contract 파일을 저장하지 않았기 때문에 소스의 컴파일이 안되었던 문제 입니다."
 
여러 샘플 소스로 작업을 하면서 스마트컨트렉트 소스의 위치와 환경파일을 설정하지 않고 컴파일하면서 소스가 컴파일 이  안되는 상황이었습니다.

아래와 같이  truffle deploy나 truffle migration을 수행하면   " throw new Error("Could not find artifacts for " + import_path + " from any sources"); " 오류가 발생합니다.
Compiing your contracts : 에는 모든 것이 최신화 되었다고 해서 컴파일이 된 것으로 알았는데 실제는 소스가 없어서 컴파일을 안했다는 내용입니다.
 

#  truffle migration 과 truffle deploy 는 스마트계약을 배포 하는 과정입니다.
# -------------------------------------
[ipman@vm1 BSC-Truffle-Starter-Box]$ truffle migration
[ipman@vm1 BSC-Truffle-Starter-Box]$ truffle deploy

Compiling your contracts...
===========================
> Everything is up to date, there is nothing to compile.


Starting migrations...
======================
> Network name:    'development'
> Network id:      1691056795366
> Block gas limit: 30000000 (0x1c9c380)


1_deploy_simple_storage.js
==========================

/app_svc/.nvm/versions/node/v18.17.0/lib/node_modules/truffle/build/4317.bundled.js:4469
        throw new Error("Could not find artifacts for " + import_path + " from any sources");
        ^

Error: Could not find artifacts for SimpleStorage from any sources
    at Resolver.require (/app_svc/.nvm/versions/node/v18.17.0/lib/node_modules/truffle/build/webpack:/packages/resolver/dist/lib/resolver.js:53:1)
    at Object.require (/app_svc/.nvm/versions/node/v18.17.0/lib/node_modules/truffle/build/webpack:/packages/migrate/dist/src/index.js:196:1)

 
 


(현황) Smart Contract 배포시 오류가 발생 합니다. 

스마트 컨트렉트를 배포 하려면 아래와 같이 truffle migration 또는 truffle deploy 명령을 실행 하면 "migrations/1_deploy_simple_storage.js" 을 이용하여 배포를 하게 됩니다.
 이때  smart contract가 컴파일된 파일 위치는 build/local-contracts/ 아래로 설정되어 있지만 "SimpleStorage" 가 컴파일 되어 저장 되고 있지 않습니다.
 
# ./migrations/1_deploy_simple_storage.js
이 코드 스니펫은 Truffle 프레임워크를 사용하여 SimpleStorage 스마트 계약을 배포하는 JavaScript 마이그레이션 스크립트입니다. Truffle은 이더리움을 위한 인기있는 개발 프레임워크로, 스마트 계약의 개발과 배포 과정을 간단하게 만들어줍니다.

# migrations/1_deploy_simple_storage.js
---------------------------------------
[ipman@vm1 BSC-Truffle-Starter-Box]$ cat migrations/1_deploy_simple_storage.js
const SimpleStorage = artifacts.require("SimpleStorage");

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

 


# truffle-config.js 파일의 smart contracts 의 컴파일된 파일이 저장되는 위치 설정을 다음과 같습니다.
(참고) 아래와 같이 contracts 파일의 위치 설정을 하지 않으면 기본 설정 값으로 ./contracts 위치에서 contract 파일을 찾습니다. 그리고  스마트컨트렉트가 컴파일되면  "build/contracts/SimpleStorage.json " 로 Artifacts 파일이 저장이 됩니다.

  # truffle-config.js
  # ----------------------------------
  // contracts_directory tells Truffle where the contracts you want to compile are located
  // 스마트 컨트렉트의 소스 위치를 지정 (contract 파일이 있는지 확인)
  contracts_directory: './contracts/local-dev',
  
  // contracts_build_directory tells Truffle where to store compiled contracts
  // 스마트 컨트렉트의 컴파일된 파일의 저장 위치 (컴파일 후 파일이 생성되는지 확인)
  contracts_build_directory: './build/local-contracts',

 
[truffle-config.js에서 다음 환경 변수를  확인합니다.]
다음 두개의 환경 변수 기본 설정 값은 다음과 같습니다.

  • contracts_directory : 스마트컨트렉트의 소스 파일 위치
  •  - default : ./contract ( 설정이 없을 경우 기본 위치)
  • contracts_build_directory : 스마트컨트렉트가  컴파일되어 저장되는 위치
  •  - default : ./build  ( 설정이 없을 경우 기본 위치)

 
o 아래와 같이 smart-contract 파일의 소스 위치(`contracts_directory`)와 컴파일 후 artifacts 파일의 저장 위치(`contracts_build_directory`)가 설정이 되어 있는지 확인합니다.
o 그리고 해당 위치에 스마트컨트렉트 소스 파일을 저장하면 됩니다.
  // contracts_directory tells Truffle where the contracts you want to compile are located
  // 스마트 컨트렉트의 소스 위치를 지정 (contract 파일이 있는지 확인)
  contracts_directory: './contracts/local-dev', 
// 스마트 컨트렉트의 컴파일된 파일의 저장 위치 (컴파일 후 파일이 생성되는지 확인)
  contracts_build_directory: './build/local-contracts',

위의 선언이 설정되어 있는지 꼭 확인합니다. 
 


[오류 원인]  스마트계약 소스 파일을 찾지 못해서 컴파일이 안된 상태 입니다.

 
o 스마트 컨트렉트 파일의 저장 위치와 환경 변수를 확인을 해보아야 합니다.
truffle compile을 실행하면 컴파일이 오류없이 실행 된 것 같지만 실제 스마트계약이 생성되지 않은 상태 를 확인 할 수 있습니다.
위의 truffle 환경 파일에  contracts_directory: './contracts/local-dev' 로 지정되어 있지만 스마트 컨트렉트 소스 파일은 contracts/SimpleStorage.sol 에 저장되어 있었습니다.
때문에 컴파일 명령은 이상 없어 보이지만 컨트렉트 파일은 "build/local-contracts/"에 컴파일이 되지 않았습니다.

 

# 스마트 컨트렉트의 파일: contracts/SimpleStorage.sol
[ipman@vm1 BSC-Truffle-Starter-Box]$ ls -al contracts/SimpleStorage.sol
-rw-r--r--. 1 ipman ipman 234  8월  4 09:32 contracts/SimpleStorage.sol

# 컴파일을 하면 정상으로 된듯 하지만 컴파일 된것이 없습니다.
[ipman@vm1 BSC-Truffle-Starter-Box]$ truffle compile

Compiling your contracts...
===========================
> Everything is up to date, there is nothing to compile.
[ipman@vm1 BSC-Truffle-Starter-Box]$ ls -al build/local-contracts/
합계 0
drwxr-xr-x. 2 ipman ipman  6  8월  4 08:02 .
drwxr-xr-x. 3 ipman ipman 29  8월  4 08:02 ..

 
>>   contracts/SimpleStorage.sol  파일을 contracts/local-dev/SimpleStorage.sol 로 이동을해서 컴파일을 하면됩니다.


[Solution]  스마트계약 파일의 위치를 확인하여 컴파일을 합니다.

 

1. 스마트 컨트렉트 소스 파일을 "contracts_directory" 에 저장합니다.

  - "contracts_directory" 설정에 맞게 스마트컨트렉트 소스 파일을 저장 합니다.
  >>  truffle-config.js 에 선언된 " contracts_directory: './contracts/local-dev'," 라고 선언이 되어 있으면
     ./contracts/local-dev/ 아래 컨트렉트 소스 파일을 저장합니다.

[ipman@vm1 BSC-Truffle-Starter-Box]$ mkdir ./contracts/local-dev

[ipman@vm1 BSC-Truffle-Starter-Box]$ cp contracts/SimpleStorage.sol contracts/local-dev/

[ipman@vm1 BSC-Truffle-Starter-Box]$ ls -al contracts/local-dev/
-rw-r--r--. 1 ipman ipman 234  8월  4 09:35 SimpleStorage.sol

 


2. 다시 스마트 컨트렉트를 컴파일 합니다 :  truffle compile 

 다시 컴파일 실행하면 다음과 같이 파일이 생성됨을 확인 할 수 있습니다.

  • contracts_directory : 스마트컨트렉트의 소스 파일 위치
  •  - 설정값 : './contracts/local-dev'
  • contracts_build_directory : 스마트컨트렉트가  컴파일되어 저장되는 위치
  •  - 설정값 : './build/local-contracts'

다음과 같이 컴파일이 이루어 지면 컴파일 되는 파일명과 저장 위치를 출력합니다.

[ipman@vm1 BSC-Truffle-Starter-Box]$ truffle compile

Compiling your contracts...
===========================
> Compiling ./contracts/local-dev/SimpleStorage.sol
> Artifacts written to /app_svc/contr_example/BSC-Truffle-Starter-Box/build/local-contracts
> Compiled successfully using:
   - solc: 0.8.4+commit.c7e474f2.Emscripten.clang

# 컴파일 후 파일이 생성되었습니다.
[ipman@vm1 BSC-Truffle-Starter-Box]$ ls -al build/local-contracts/
-rw-r--r--. 1 ipman ipman 34664  8월  4 09:37 SimpleStorage.json

 


3. 스마트 컨트렉트를 배포 합니다 :  truffle migration or truffle deploy

 
소스 컴파일후에  truffle deploy를 실행하여 성공 하였습니다.  아우~~ 축하드립니다. 이제 개발만 잘 하면 되겠습니다.

[ipman@vm1 BSC-Truffle-Starter-Box]$ truffle deploy

Compiling your contracts...
===========================
> Everything is up to date, there is nothing to compile.


Starting migrations...
======================
> Network name:    'development'
> Network id:      1691056795366
> Block gas limit: 30000000 (0x1c9c380)


1_deploy_simple_storage.js
==========================

   Deploying 'SimpleStorage'
   -------------------------
   > transaction hash:    0xbf70cbd769bb06b34455c1434b70a804165b022e1e9d5792d88537f5cd882938
   > Blocks: 0            Seconds: 0
   > contract address:    0x5327dF79795174bd41460B994AF056c5308fa1E3
   > block number:        5352
   > block timestamp:     1691110409
   > account:             0x0566c542dc24c6fEE43b9373aD5937Fa4E57eC5F
   > balance:             999.996314993419206158
   > gas used:            118829 (0x1d02d)
   > gas price:           2.500000007 gwei
   > value sent:          0 ETH
   > total cost:          0.000297072500831803 ETH

   > Saving artifacts
   -------------------------------------
   > Total cost:     0.000297072500831803 ETH

Summary
=======
> Total deployments:   1
> Final cost:          0.000297072500831803 ETH

 
 
한참 찾았지만 원인은 스마트 컨트렉트의 환경 설정을 확인하지 않고 예제를 만들다 보니 컴파일도 안되었던 것입니다.


다음과 같은 오류가 발생한다면 스마트 컨트렉트의 저장 위치에 관련된 환경 변수를 확인하여 스마트컨트렉트 파일을 만들어 실행해 보세요

[ipman@vm1 truffle]$ truffle develop
Truffle Develop started at http://127.0.0.1:9545/

truffle(develop)> compile

Compiling your contracts...
===========================
> Everything is up to date, there is nothing to compile.
truffle(develop)> migrate

Compiling your contracts...
===========================
> Everything is up to date, there is nothing to compile.


Starting migrations...
======================
> Network name:    'develop'
> Network id:      5777
> Block gas limit: 6721975 (0x6691b7)


1_deploy_contracts.js
=====================

/app_svc/.nvm/versions/node/v18.17.0/lib/node_modules/truffle/build/4317.bundled.js:4469
        throw new Error("Could not find artifacts for " + import_path + " from any sources");
        ^

Error: Could not find artifacts for SimpleStorage from any sources
    at Resolver.require (/app_svc/.nvm/versions/node/v18.17.0/lib/node_modules/truffle/build/webpack:/packages/resolver/dist/lib/resolver.js:53:1)
    at Object.require (/app_svc/.nvm/versions/node/v18.17.0/lib/node_modules/truffle/build/webpack:/packages/migrate/dist/src/index.js:196:1)
    at ResolverIntercept.require (/app_svc/.nvm/versions/node/v18.17.0/lib/node_modules/truffle/build/webpack:/packages/migrate/dist/src/ResolverIntercept.js:23:1)
    at /app_svc/mySmartContract/migrations/1_deploy_contracts.js:3:31

 
 
[참고]
[truffle-config.js]

module.exports = {
  // contracts_build_directory tells Truffle where to store compiled contracts
  contracts_build_directory: './build/local-contracts',

  // contracts_directory tells Truffle where the contracts you want to compile are located
  contracts_directory: './contracts/local-dev',


   networks: {
     development: {
       host: "127.0.0.1",     // Localhost (default: none)
       port: 8545,            // Standard Ethereum port (default: none)
       network_id: "*",       // Any network (default: none)
     }
   },
   
     // Configure your compilers
  compilers: {
    solc: {
       version: "0.8.21",
    }
  },
  };

 

728x90
반응형