無知

갈 길이 먼 공부 일기

기술 공부/블록체인

스마트 컨트랙트 (8-2) | Truffle 프레임워크

moozii 2022. 5. 12. 19:31

 

1. 트러플 프레임워크란

- 스마트 컨트랙트의 개발, 테스트, 배포 작업을 돕는 프레임워크

- 가나슈, Ganache는 개발용 이더리움 네트워크 생성 프로그램

- 드리즐, Drizzle은 디앱 프론트엔드 작성 지원 라이브러리

 

 

A world class development environment, testing framework and asset pipeline for blockchains using the Ethereum Virtual Machine (EVM), aiming to make life as a developer easier.
-truffle docs-

 

 

The Truffle Suite gets developers from idea to dapp as comfortably as possible. https://trufflesuite.com/

 

 

 

 

2. Truffle Tutorial

(Source : https://trufflesuite.com/docs/truffle/quickstart/)

 

Truffle Quickstart - Truffle Suite

Truffle Quickstart This page will take you through the basics of creating a Truffle project and deploying a smart contract to a blockchain. Note: Before you begin, make sure that you read our Ethereum Overview page. Installing Truffle Before you can use Tr

trufflesuite.com

 

 

Installing Truffle

npm install -g truffle
// Before you can use Truffle, you will have to install it using npm

// first step is to create a Truffle project.
// You can create a bare project template, but for those just getting started
// you can use Truffle Boxes, which are example applications and project templates
// To create a bare Truffle project with no smart contracts included, use truffle init.

// Create a new directory for your Truffle project:
mkdir MetaCoin
cd MetaCoin

// Download ("unbox") the MetaCoin box:
truffle unbox metacoin

// Source: https://trufflesuite.com/docs/truffle/quickstart/

Once this operation is completed, you'll now have a project structure with the following items:
contracts/: Directory for Solidity contracts
migrations/: Directory for scriptable deployment files
test/: Directory for test files for testing your application and contracts
truffle.js: Truffle configuration file

 

 

Commands

// Testing
truffle test ./test/TestMetaCoin.sol
// In a terminal, run the Solidity test
truffle test ./test/metacoin.js
// Run the JavaScript test


// Compiling
truffle compile
// truffle console에서 compile 명령을 하면 이 파일들이 컴파일되어
// build 폴더 밑에 JSON format으로 저장됩니다.

// Migrating, Deploying
// To deploy our smart contracts, we are going to need to connect to a blockchain. 
// Truffle has a built-in personal blockchain that can be used for testing.
// This blockchain is local to your system and does not interact with the main Ethereum network.

// You can create this blockchain and interact with it using Truffle Develop.
// Run Truffle Develop:
truffle develop

// The command to deploy your compiled contracts to the blockchain is truffle migrate
truffle migrate
// This shows the transaction IDs and addresses of your deployed contracts
// Migration 파일은 크게 import statement와 deployment statement로 구성됩니다.
// Import statement에서는 artifacts.require 메소드를 통해서
// contract abstraction을 읽게 됩니다.
// 이때, 파라미터로 contract의 이름을 사용합니다.

// Source: https://trufflesuite.com/docs/truffle/quickstart/

 

 

Contract Interaction

방법 (1) Transaction : 컨트랙트 상태를 변경시키는 트랜잭션으로 인터랙션

방법 (2) Call : 컨트랙트 상태를 변경시키지 않고 값만 읽는 방식으로 인터랙션

 

The Ethereum network makes a distinction between writing data to the network and reading data from it, and this distinction plays a significant part in how you write your application. In general, writing data is called a transaction whereas reading data is called a call. Transactions and calls are treated very differently, and have the following characteristics.

Interacting with your contracts
https://trufflesuite.com/docs/truffle/getting-started/interacting-with-your-contracts/ 

 

Transactions

Transactions fundamentally change the state of the network. A transaction can be as simple as sending Ether to another account, or as complicated as executing a contract function or adding a new contract to the network. The defining characteristic of a transaction is that it writes (or changes) data. Transactions cost Ether to run, known as "gas", and transactions take time to process. When you execute a contract's function via a transaction, you cannot receive that function's return value because the transaction isn't processed immediately. In general, functions meant to be executed via a transaction will not return a value; they will return a transaction id instead.

So in summary, transactions:
Cost gas (Ether)
Change the state of the network
Aren't processed immediately
Won't expose a return value (only a transaction id).

https://trufflesuite.com/docs/truffle/getting-started/interacting-with-your-contracts/ 
Calls

Calls, on the other hand, are very different. Calls can be used to execute code on the network, though no data will be permanently changed. Calls are free to run, and their defining characteristic is that they read data. When you execute a contract function via a call you will receive the return value immediately.

In summary, calls:
Are free (do not cost gas)
Do not change the state of the network
Are processed immediately
Will expose a return value (hooray!)

Choosing between a transaction and a call is as simple as deciding whether you want to read data, or write it.

https://trufflesuite.com/docs/truffle/getting-started/interacting-with-your-contracts/ 

 

 

 

 

 

3. Truffle Boxes


트러플에서는 트러플 박스라고 하는, 쉽게 DApp을 작성할 수 있는 template을 제공한다. 각 box들은 실행 가능한 예제들과 라이브러리들을 포함해 DApp 개발을 용이하게 한다.

 

// Create a new directory for your Truffle project:
mkdir MetaCoin
cd MetaCoin

// Download ("unbox") the React box:
truffle unbox react

// Compile and Deploy
truffle develop
truffle(develop) > compile
truffle(develop) > migrate

// Run UI
cd client
// client directory includes UI examples
// client/src becomes the root of react, possessing html, css, js
npm run start
// run react, show UI in web browser
// acces by port and account from truffle develop mode
// show App.js

 

client 폴더가 추가되어 UI 예제가 포함되어, contract code를 컴파일하는 것뿐만 아니라 DApp을 테스트해 볼 수도 있다.