# Solidity 

- 동작방식

    - 트렌젝션별 각각의 스마트 컨트렉은 각자다른 state, logic 있다. binary 형태로 존재하며 EVM 의해 실행된다. 

    - tx smart contract 코드가 있으며 EVM 의해 실행된다. 해당 인자는 해당 트렌젝션에 보관된다.

    - 라인단위로 **gas** 소모 된다.

- 저장된 스마트 컨트렉은 트렌젝션을 조회 하면 모두 있다.

   - 민감정보를 쓰기에는 어려워 보인다. (개인정보, 계급별 수수료 비율 )

- EVM (Ethereum Virtual Machine)

  - 저장영역

    - storage

      - 모든 컨트렉에 있는 영역, 함수호출에 변하지 않지만 비용이 많이듬

      - 구조체, 변수 매핑타입 지역변수가 저장됨 (별도 선언 없을시)

    - memory

      - 일시적인 값을 저장, 외부함수 호출시 지워짐, 비용이 적게듬

      - 함수 인자가 저장됨 

      - 함수 내부에서만 사용할 있음 (밖에서 사용시 컴파일 에러)

    - stack

      - 지역변수 저장, 비용이 거의 안듬, 저장갯수가 한정적

      


## truffle 환경 셋팅

### truffle ? 

> 서양 송로 버섯 solidity 컴파일/배포를 쉽게 할수 있게 지원해주는 Framework

- 설치 (npm 설치 선행, mac 기준 데모)

    - https://truffleframework.com/docs/truffle/getting-started/installation

    - #npm install -g truffle


- 명령어 순서 (폴더/프로젝트 이동후)

  1. #truffle unbox metacoin

  2. #vi truffle.js [테스트넷 설정]

     - 테스트넷에 붙을 host, port, network_id 작성(수정) 한다.

     - [folder]/contracts/[some].sol solidity 파일을 작성한다.     

  3. #truffle compile  

     - ~/build/ 폴더에 컴파일된 파일이 생성된다.

  4. #truffle migrate




### **소수점을 지원하지 않는다! ** (..아직)


```

The main difference between floating point (float and double in many languages, more precisely IEEE 754 numbers) and fixed point numbers is that the number of bits used for the integer and the fractional part (the part after the decimal dot) is flexible in the former, while it is strictly defined in the latter. Generally, in floating point almost the entire space is used to represent the number, while only a small number of bits define where the decimal point is.

[파파고]

부동 소수점(많은 언어의 플로트와 더블, IEEE 754 숫자) 고정점 번호의 주요 차이점은 정수와 부분 부분 부분(십진수 뒤의 부분) 사용되는 비트의 수가 전자에서 유연하지만 후자에서 엄격히 정의된다는 것이다. 일반적으로 부동 소수점에서는 소수점 위치를 정의하는 반면, 거의 전체 공간은 숫자를 나타내기 위해 사용된다.

```




## solidity 작성


- ERC20 토큰 방식을 따른다.

  - https://theethereum.wiki/w/index.php/ERC20_Token_Standard

- 언어 특징 : 완전한 언어를 꿈꾸며 보수적으로 시작하였으나 현재 (0.5.~) 버전에서는 이것저것 붙이고 있다.

  - 처음에는 for 문도, string 자료형도 없었다.

- 작성법 

  - public private

  - function event 

  - uint uint8 uint256 address string bool byte bytes32

  - struct - 구조체 , 선언 위치에 따라서 storage, memory 할당

  - for while - 반복문 

  - require - 예외처리 ex. assert

  - mapping - key, value 이루어진 해쉬 테이블 

  - enum

  - 전송 - address

    - address 확장자로 선언된 인자에 transfer 콜해서 사용한다.

    - transfer - (send > deprecated)

    



## 사용할 있는 블록 트렌젝션  

Block and Transaction Properties

block.blockhash(uint blockNumber) returns (bytes32): hash of the given block - only works for 256 most recent blocks excluding current

block.coinbase (address): current block miner’s address

block.difficulty (uint): current block difficulty

block.gaslimit (uint): current block gaslimit

block.number (uint): current block number

block.timestamp (uint): current block timestamp as seconds since unix epoch

gasleft() returns (uint256): remaining gas

msg.data (bytes): complete calldata

msg.gas (uint): remaining gas - deprecated in version 0.4.21 and to be replaced by gasleft()

msg.sender (address): sender of the message (current call)

msg.sig (bytes4): first four bytes of the calldata (i.e. function identifier)

msg.value (uint): number of wei sent with the message

now (uint): current block timestamp (alias for block.timestamp)

tx.gasprice (uint): gas price of the transaction

tx.origin (address): sender of the transaction (full call chain)




+ Recent posts