[TIL] 2023-05-23 DAY45
GIT 주소 : https://github.com/dowoo303/SOLIDITY
오늘 배운것들
- TEST6 리뷰
1) 시간기능 추가하기- TEST7
1) TEST7에서 '시 분 초'를 합쳐서 output 출력해보기- Import
1) 로컬 내 import
2) 라이브러리 import(230523_3.sol)- TEST8
각종 팁
abi.encodePacked()는 bytes형이다. -> string()으로 씌우면 당연히 문자형으로 변환가능.- new: 객체화
회고
- 전체적인 틀
EVM에서 Solidity로 로컬내 코딩 및 라이브러리 이용 후 web3.js를 통하여 frontend와 서로 소통
- Go-ethereum 안에 EVM 위에서 Solidity 언어 작동
- TEST8 문제 이해조차 못했따 ... 진짜 이번주는 이사+사업 때매 너무 정신없어서 수업집중 진짜 힘들다.
어떻게든 짬내서 공부는 해야할듯(특히 솔리디티뽀개기)
function finishPoll(string memory _title) external isItUser {
require(block.timestamp > polls[_title].time+100); /*100이 아닌 다른 숫자로도 가능(input 값), 자신이 제안한 안건만 마무리할 수 있게*/
if((polls[_title].pros + polls[_title].cons) > userCount*7/10 && (polls[_title].pros) *100 / (polls[_title].pros + polls[_title].cons) > 66 ) {
polls[_title].status = pollStatus.passed;
} else {
polls[_title].status = pollStatus.rejected;
}
}
string(abi.encodePacked())으로 병합해준다.contract Q7 {
function setTime(uint _n) public pure returns(uint, uint, uint) {
uint hour = _n/3600 ;
uint minute = (_n%3600)/60 ; // min = (_n/60)%60;
uint second = _n%60 ;
return(hour, minute, second);
}
}
contract Q5 {
function divideNumber(uint _n) public pure returns(uint[] memory) {
uint[] memory b = new uint[](getLength(_n));
uint i=getLength(_n);
while(_n !=0) {
b[--i] = _n%10;
_n = _n/10;
}
return (b);
}
function getLength(uint _n) public pure returns(uint) {
if(_n==0) {
return 1;
}
uint a;
while(_n >= 10**a) {
a++;
}
return a;
}
}
contract CONCAT2 {
Q5 q5 = new Q5();
Q7 q7 = new Q7();
function concat(uint a) public view returns(string memory) {
uint b = q5.getLength(a);
uint[] memory c = new uint[](b);
c = q5.divideNumber(a);
for(uint i=0; i<c.length; i++) {
c[i] += 48;
}
return string(abi.encodePacked(c));
}
function getConcat(uint _n) public view returns(string memory) {
return string(abi.encodePacked(concat(_n), "hours"));
}
function concat6(uint _n) public view returns(uint, uint, uint /*string memory*/) {
(uint a, uint b, uint c) = q7.setTime(_n);
return (a,b,c);
}
function concat7(uint _n) public view returns(string memory) {
(uint a, uint b, uint c) = q7.setTime(_n);
return string(abi.encodePacked(concat(a),"hours ", concat(b), "minutes ", concat(c), "seconds"));
}
}
다른파일 import해보기
형태: import './위치/파일명';
최종목표: 다른 컨트랙트에 접근하여 상태 변수 바꾸기 + 다른 파일에 있는 컨트랙트에 접근하는 법이 같은 파일내 컨트랙트 접근 방식과 어떻게 다른지 주시하기
같은 파일 내 컨트랙트를 쓰려면 인스턴스화를 시켜야 하듯 다른 파일에서 들고 올 때도 import를 위에 적어 준 후 그 파일 내 컨트랙트를 인스턴스화 시켜서 사용한다.
주소: https://github.com/OpenZeppelin/openzeppelin-contracts/blob/master/contracts/utils/Strings.sol
라이브러리는 인스턴스화를 하지않는다.

// SPDX-License-Identifier: GPL-3.0
pragma solidity >=0.8.2 <0.9.0;
import "@openzeppelin/contracts/utils/Strings.sol";
contract C {
function UtoS(uint _n) public pure returns(string memory) { // 라이브러리는 pure 사용 가능
return Strings.toString(_n);
}
}
Read Contract에는 정보를 보는것만(가스비 x)
Write Contract에는 입력값을 넣어서 정보를 쓰는 동작(가스비 o)

위 빨간점 connect to Web3눌러서 메마 연결 -> Write가서 이름이랑 주소 써넣기

지갑주소 나오고 -> 0000 -> 글자수+이름(아스키코드)
