[TACT] 1. BASIC TACT

매일 꾸준하게·2024년 1월 11일
post-thumbnail

tact란 무엇일까?

  • 텔레그램에서 사용되는 TON 블록체인의 프로그래밍언어입니다.
  • ton network에서 smart contract를 구현할 수 있습니다.

Basic 문법

  • Tact언어는 TypeScript/JavaScript와 swift 문법과 유사하므로 관련 문법을 숙지한 사람은 쉽게 배울 수 있습니다.
  1. 기본요소 :

    • Int형 : Tact의 int는 257비트 +-정수

    • Bool형 : true, false(0,1) 다들 알죠 ?

    • Slice : 특정 부분 참조

    • Cell : 블록체인의 structure은 머클트리 형태인데 tree 속한 노드를 cell이라고 한다. 그 cell을 지칭할 때 사용

    • Builder : cell을 구축할때 사용(아래 예시 참조)

    • String : 문자열 선언

    • StringBuilder : string과 비슷하지만, 메모리 사용량 감소, 성능 개선을 위해 사용

  2. map<key(int, address), value(int, cell, address, bool)> : key,value값을 지정할 수 있음.

  1. structure & message : 구조체, message가 sender로 사용될 수 있다는 것을 제외하고 비슷함.
  1. Contracts and Traits : smart contract 구현할 때 사용
  1. address : tact상에서 사용되는 address(wallet)입니다.

example

using build, cell example

  build(): Cell {
    return new Cell(this.data);
  }
}

Contracts and Traits example:

struct Point {
    x: Int;
    y: Int;
}
 
message SetValue {
    key: Int;
    value: Int?; // Optional
}

contract example

  • contract는 TON 블록체인의 스마트 계약의 주요 항목입니다.
  • 주요 요소 : get fun() 및 receive()
contract HelloWorld {
  counter: Int;
 
  init() {
    self.counter = 0;
  }
 
  receive("increment") {
    self.counter = self.counter + 1;
  }
 
  get fun counter(): Int {
    return self.counter;
  }
}

Trait example

  • trait은 기능, 수신자 및 필수 필드를 정의합니다.(react의 component와 유사)
trait Ownable {
    owner: Address;
 
    fun requireOwner() {
        nativeThrowUnless(132, context().sender == self.owner);
    }
 
    get fun owner(): Address {
        return self.owner;
    }
}

and it will use it contract ↓

contract Treasure with Ownable {
  owner: Address; // Field from trait MUST be defined in contract itself
 
  // Here we init the way we need, trait can't specify how you must init owner field
  init(owner: Address) {
    self.owner = owner;
  }
}

참조 : Tact Documentation

profile
현직 BackEnd Dev with Spring

0개의 댓글