Node.js, Truffle 설치 후
truffle init
ls
cd contracts
ls
cd ..
pwd
ls
truffle create contract A
cd contracts
ls
cd ..
pwd
cd contracts
cat A.sol
vim A.sol
pragma solidity >=0.4.17 <0.9.0;
contract A {
function add(uint _a, uint _b) public pure returns(uint) {
return _a+_b;
}
}
cd contracts
truffle compile
host: "127.0.0.1", // Localhost (default: none)
port: 8545, // Standard Ethereum port (default: none)
network_id: "*", // Any network (default: none)
},
compilers: {
solc: {
version: "0.8.21",
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.19;
contract A {
function add(uint _a, uint _b) public pure returns(uint) {
return _a+_b;
}
}
const A_con = artifacts.require("A");
module.exports = function(deployer) {
deployer.deploy(A_con);
}
contract A와 require의 "A"
const A_con과 deploy의 (A_con)
일치하는지 확인
이후
1. `ganache 앱` → port 7545 설정
2. vscode > truffle-config.js > port 8545 설정
3. terminal > `ganache-cli` 실행 > 8545 확인
4. 새로운 터미널에서 contracts > truffle migrate
npm install -g ganache-cli
ganache-cli --version
truffle migrate
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.19;
contract second_contract {
uint public A;
function add(uint _a, uint _b) public pure returns(uint) {
return _a+_b;
}
function mul(uint _a, uint _b) public pure returns(uint) {
return _a*_b;
}
function setA(uint _a) public {
A = _a;
}
function getA() public view returns(uint) {
return A;
}
}
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.19;
contract number3 {
uint[] public numbers;
function pushNumber(uint _n) public {
numbers.push(_n);
}
function getNumber(uint _n) public view returns(uint) {
return numbers[_n-1];
}
function getNumbers() public view returns(uint[] memory) {
return numbers;
}
}
const BBB = artifacts.require("second_contract");
module.exports = function (deployer) {
deployer.deploy(BBB);
};
const CC = artifacts.require("number3");
module.exports = function (deployer) {
deployer.deploy(CC);
};
truffle compile 입력 후 json 뜨는지 확인
cd contracts
truffle migrate 입력 후 ganache에 transaction 여러개 뜨는지 확인
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.19;
contract second_contract {
uint public A;
function add(uint _a, uint _b) public pure returns(uint) {
return _a+_b;
}
function mul(uint _a, uint _b) public pure returns(uint) {
return _a*_b;
}
function setA(uint _a) public {
A = _a;
}
function getA() public view returns(uint) {
return A;
}
}
contract last_contract {
string public name;
function setName(string calldata _name) public {
name = _name;
}
}
truffle compile 후 last_contract.json 뜨는지 확인
migrations에 추가한 contract 포함하지 않으면 compile 안 됨
톱니바퀴 설정모양 클릭 - Add project 클릭 - truffle-config.js file 업로드 - Save & Restart 클릭 - Contract에 4개 뜨는지 확인
const BBB = artifacts.require("second_contract");
const BBB2 = artifacts.require("last_contract");
module.exports = function (deployer) {
deployer.deploy(BBB);
deployer.deploy(BBB2);
};
이후 terminal에서 truffle migrate 후 ganache contracts-last contract 뜨는지 확인
24 = 10+14
앞에 있는 10은 10진수, 뒤에 있는 14는 16진수
-> 10 (10진수) + 14(16진수) = 24