Getting started with truffle (1)--[Blockchain]

1. Download and install nodejs

 You can download the nodejs version to version 16 through nvm

# 安装nvm
curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.33.2/install.sh | bash

# 若出现因网络问题导致长时间下载失败,可尝试以下命令
curl -o- https://gitee.com/mirrors/nvm/raw/v0.33.2/install.sh | bash

# 加载nvm配置
source ~/.$(basename $SHELL)rc
# 安装Node.js 8
nvm install 16

 If you have problems connecting to GitHub, you can read my other blogFailed to connect to github.com port 443: Refused to connect Could not resolve host: github. com_Evening breezes are all passers-by.’s blog-CSDN blog

 After the download is complete

#查询下载好的版本
node -v

npm -v

nvm --version

 

 

2. Install Truffle

2.1. Environmental dependence

  • NodeJS v8.9.4 or later

  • Installation command

npm install -g truffle

 2.2. Create project project

Truffle Most commands are run from the Truffle project directory. So the first step is to create a Truffle project. You can create an empty project template, but for students who are new to Truffle, it is recommended to use Truffle Boxes, which provides sample application code and project templates. We will use the MetaCoin box as an example, which creates a Token that can be transferred between accounts.

Create a new directory for the Truffle project:

mkdir MetaCoin
cd MetaCoin

 Download (“unbox”) MetaCoin box:

truffle unbox metacoin

 If the download is unsuccessful, use the following command

git clone https://github.com/truffle-box/metacoin-box.git

 If you want to create an empty project without a contract, you can use truffle init.

After the operation is completed, there will be a project structure like this:

2.3. Project structure

  1. contracts/MetaCoin.sol: This is a MetaCoin token smart contract written in Solidity . Note that he also references another contract file in the directory contracts/ConvertLib.sol .

  2. contracts/Migrations.sol: This is a separate Solidity file used tomanage and upgrade smart contracts. Each project has such a file, And there's usually no need to edit it.

  3. migrations/1_initial_migration.js: This is a deployment script used to deploy the Migrations contract, corresponding to the Migrations.sol file.

  4. migrations/2_deploy_contracts.js: This is a deployment script used to deploy MetaCoin contract. (The deployment script is run in order, scripts starting with 2 usually run after scripts starting with 1 )

  5. test/TestMetacoin.sol: This is a test case file written in Solidity to check whether the contract works as expected.

  6. test/metacoin.js : This is a test case script written in JavaScript, with the same purpose as above.

  7. truffle-config.js (previously truffle.js): Truffle  configuration file, used to set network information, and other items related settings. When we use the built-in default Truffle command, it is okay to leave this file empty.

 2.4. Usage test

Open a console terminal and run the Solidity test case:

truffle test ./test/TestMetacoin.sol

 The screenshot of the completed result is as follows

 Run JavaScript test cases

truffle test ./test/metacoin.js

 The completed result is as shown in the figure

 2.5. Compile contract

Compile the smart contract:

truffle compile

 I have compiled it and forgot to cut out the image ovo (the following is a reminder that it has been compiled)

 

3. Error handling

3.1. Error 1

/home/csdn/.nvm/versions/node/v8.17.0/lib/node_modules/truffle/build/3618.bundled.js:24504
            return _this.outputFormatter && res ? _this.outputFormatter(res, this?.hexFormat) : res;

SyntaxError: Unexpected token .
    at createScript (vm.js:80:10)
    at Object.runInThisContext (vm.js:139:10)
    at Module._compile (module.js:617:28)
    at Object.Module._extensions..js (module.js:664:10)
    at Module.load (module.js:566:32)
    at tryModuleLoad (module.js:506:12)
    at Function.Module._load (module.js:498:3)
    at Module.require (module.js:597:17)
    at require (internal/module.js:11:18)
    at Object.__webpack_require__.f.require (/home/csdn/.nvm/versions/node/v8.17.0/lib/node_modules/truffle/build/cli.bundled.js:608:28)

 This error is entirely because your node version is not enough. If you use nvm to download, you can use nvm list to check the nodejs version you have. Use nvm use 16 (corresponding to the version number you have) to switch the nodejs version. After the switch is completed, check the nodejs version. Can

3.2. Error 2

SyntaxError: Unexpected token '.'
    at wrapSafe (internal/modules/cjs/loader.js:915:16)
    at Module._compile (internal/modules/cjs/loader.js:963:27)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1027:10)
    at Module.load (internal/modules/cjs/loader.js:863:32)
    at Function.Module._load (internal/modules/cjs/loader.js:708:14)
    at Module.require (internal/modules/cjs/loader.js:887:19)
    at require (internal/modules/cjs/helpers.js:74:18)
    at Object.__webpack_require__.f.require (/home/csdn/.nvm/versions/node/v12.22.12/lib/node_modules/truffle/build/cli.bundled.js:608:28)
    at /home/csdn/.nvm/versions/node/v12.22.12/lib/node_modules/truffle/build/cli.bundled.js:538:40
    at Array.reduce (<anonymous>)

    at postinstallObtain (/home/csdn/.nvm/versions/node/v12.22.12/lib/node_modules/truffle/scripts/postinstall.js:13:11)
    at Object.<anonymous> (/home/csdn/.nvm/versions/node/v12.22.12/lib/node_modules/truffle/scripts/postinstall.js:20:3)
    at Module._compile (internal/modules/cjs/loader.js:999:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1027:10)
    at Module.load (internal/modules/cjs/loader.js:863:32)
    at Function.Module._load (internal/modules/cjs/loader.js:708:14)
    at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:60:12)
    at internal/main/run_main_module.js:17:47

This problem is because the downloaded truffle has too many caches. Just clear the cache and download again.

npm uninstall -g truffle
npm install -g truffle

 3.3, Error 3

✖ Downloading
Unbox failed!
RequestError: getaddrinfo ENOTFOUND codeload.github.com
    at ClientRequest.<anonymous> (/home/csdn/.nvm/versions/node/v16.20.1/lib/node_modules/truffle/build/webpack:/node_modules/download/node_modules/got/index.js:182:1)
    at Object.onceWrapper (node:events:628:26)
    at ClientRequest.emit (node:events:513:28)
    at TLSSocket.socketErrorListener (node:_http_client:494:9)
    at TLSSocket.emit (node:events:513:28)
    at emitErrorNT (node:internal/streams/destroy:157:8)
    at emitErrorCloseNT (node:internal/streams/destroy:122:3)
    at processTicksAndRejections (node:internal/process/task_queues:83:21)
Truffle v5.11.0 (core: 5.11.0)
Node v16.20.1

 This is because the computer connection to GitHub is unsuccessful and the code cannot be pulled down. You can try changing the command. 

git clone https://github.com/truffle-box/metacoin-box.git

3.4, Error 4

Error: Could not find artifacts for /home/csdn/fisco/MetaCoin/metacoin-box/test/TestMetacoin.sol from any sources
    at Resolver.require (/home/csdn/.nvm/versions/node/v16.20.1/lib/node_modules/truffle/build/webpack:/packages/resolver/dist/lib/resolver.js:53:1)
    at /home/csdn/.nvm/versions/node/v16.20.1/lib/node_modules/truffle/build/webpack:/packages/test/dist/src/Test.js:139:1
    at Array.map (<anonymous>)
    at Object.<anonymous> (/home/csdn/.nvm/versions/node/v16.20.1/lib/node_modules/truffle/build/webpack:/packages/test/dist/src/Test.js:138:1)
    at Generator.next (<anonymous>)
    at fulfilled (/home/csdn/.nvm/versions/node/v16.20.1/lib/node_modules/truffle/build/webpack:/packages/test/dist/src/Test.js:28:43)
Truffle v5.11.0 (core: 5.11.0)
Node v16.20.1

 This is because the name is wrong. If you look carefully, you can see that the name of the TestMetaCoin.sol file under the test directory does not correspond to the sol file in the test file. When executing the command, just change it to the sol file in the test file.

Guess you like

Origin blog.csdn.net/m0_72435337/article/details/131910362