
truffle을 이용하여 기본 폴더 구조를 생성한다.
$ truffle init
Starting init...
================
> Copying project files to /home/user/block-chain/practice-1
Init successful, sweet!
Try our scaffold commands to get started:
$ truffle create contract YourContractName # scaffold a contract
$ truffle create test YourTestName # scaffold a test
http://trufflesuite.com/docs
다음과 같이 폴더 및 파일이 생성된다.
$ tree
.
├── contracts
├── migrations
├── test
└── truffle-config.js
3 directories, 1 file
contracts 폴더에 소스 파일(HelloWorld.sol)을 생성한 뒤 다음과 같이 작성한다.
상단에는 라이선스를 기입하고 solidity 버전을 명시한다.
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.17;
contract HelloWorld {
string public Message;
constructor() {
Message = "Hello World";
}
function sayHello() public view returns (string memory) {
return Message;
}
}
해당 파일을 컴파일한다.
compile이 완료되면 build/contracts 폴더가 생성되며 contract이름의 json파일이 생성된다.
$ truffle compile
Compiling your contracts...
===========================
> Compiling ./contracts/HelloWorld.sol-bin. Attempt #1
> Artifacts written to /home/user/block-chain/practice-1/build/contracts
> Compiled successfully using:
- solc: 0.8.17+commit.8df45f5f.Emscripten.clang
migrations폴더에 1_deploy_contracts.js라는 파일 생성 후 다음과 같이 작성한다.
var contract = artifacts.require("HelloWorld");
module.exports = function(deployer) {
deployer.deploy(contract);
};
이후, 해당 contract를 배포하기 위해 설정 파일(truffle-config.js)의 networks부분을 수정한다.
...
networks: {
// Useful for testing. The `development` name is special - truffle uses it by default
// if it's defined here and no other network is specified at the command line.
// You should run a client (like ganache, geth, or parity) in a separate terminal
// tab if you use this network and you must also set the `host`, `port` and `network_id`
// options below to some value.
//
development: {
host: "127.0.0.1", // Localhost (default: none)
port: 8545, // Standard Ethereum port (default: none)
network_id: "*", // Any network (default: none)
},
...
}
truffle-config.js의 networks와 구성하려는 네트워크와 IP와 포트를 맞춰야한다.
-h와 -p를 이용하여 IP와 포트번호를 정할 수 있으며 기본 값은 127.0.0.1:8545이다.
$ ganache-cli -h 0.0.0.0 -d -m Hello
test 폴더에 test.js파일을 만들고 다음과 같이 작성한다.
const mycontract = artifacts.require("HelloWorld");
contract("HelloWorld", function([deployer]) {
let helloworld;
beforeEach(async () => {
helloworld = await mycontract.new();
});
it("Test1", async () => {
let ret = await helloworld.sayHello();
assert.equal(ret, "Hello World");
console.log(`return value : ${ret}`);
});
});
생성한 파일을 실행해본다.
$ truffle test test/test.js
Using network 'development'.
Compiling your contracts...
===========================
> Everything is up to date, there is nothing to compile.
Contract: HelloWorld
return value : Hello World
✔ Test1
1 passing (141ms)
truffle-config.js의 networks와 구성하려는 네트워크와 IP와 포트를 맞춰야한다.
-h와 -p를 이용하여 IP와 포트번호를 정할 수 있으며 기본 값은 127.0.0.1:8545이다.
스마트 컨트랙트를 배포할 때는 컨트랙트 주소를 기억하는 것이 좋다.
다시 배포할 때는 --reset 옵션을 주는 것이 좋다.
$ ganache-cli -h 0.0.0.0 -d -m Hello
다른 쉘을 열고 truffle을 이용하여 배포한다.
$ truffle migrate
truffle migrate
Compiling your contracts...
===========================
> Compiling ./contracts/HelloWorld.sol
> Artifacts written to /home/user/block-chain/practice-1/build/contracts
> Compiled successfully using:
- solc: 0.8.17+commit.8df45f5f.Emscripten.clang
...
1_deploy_contracts.js
=====================
Deploying 'HelloWorld'
----------------------
> transaction hash: 0xff67d66675b418a7b012d248a020a3b0f95aec6957b124e5146883b088d7865d
> Blocks: 0 Seconds: 0
> contract address: 0x905D51F388d59e2eDb94651EBf9e5A334eBC189c
> block number: 1
> block timestamp: 1669808410
> account: 0x3fc5e11a36857F0cc27B8D1e311af7a1D9d3dC84
...
truffle console을 이용하여 sayHello를 호출하여 Hello World를 출력해본다.
$ truffle console
truffle(development)> HelloWorld.deployed().then(function(instance){inst = instance})
truffle(development)> inst.sayHello()
'Hello World'
truffle(development)> .exit