메인 콘텐츠로 건너뛰기

SAVITRI-1155: 멀티 에셋 토큰 표준

SAVITRI-1155 (SMA -- Savitri Multi Asset)는 Savitri Network의 멀티 에셋 토큰 표준으로, Ethereum의 ERC-1155에 해당합니다. 효율적인 배치 작업을 갖춘 단일 컨트랙트에서 대체 가능 토큰과 대체 불가능 토큰을 모두 지원합니다.

인터페이스

함수매개변수반환설명
balance_ofowner, idu128소유자의 특정 토큰 잔액
balance_of_batchowners[], ids[]u128[]배치 잔액 조회
safe_transfer_fromfrom, to, id, amount, dataResult토큰 전송
safe_batch_transfer_fromfrom, to, ids[], amounts[], dataResult배치 전송
set_approval_for_alloperator, approvedResult모든 토큰에 대한 운영자 승인
is_approved_for_allowner, operatorbool운영자 승인 확인

스토리지 레이아웃

잔액 스토리지 (슬롯 100+)

중첩 매핑: balances[owner][id]

hash1 = keccak256(owner || 100)
hash2 = keccak256(id || hash1)
slot = first 8 bytes of hash2 as u64

운영자 승인 스토리지 (슬롯 200+)

중첩 매핑: operator_approvals[owner][operator]

hash1 = keccak256(owner || 200)
hash2 = keccak256(operator || hash1)
slot = first 8 bytes of hash2 as u64

이 레이아웃은 다음을 제공합니다:

  • 균일한 분포: Keccak256이 균일한 슬롯 분포를 보장
  • 충돌 없음: 무시할 수 있는 충돌 확률
  • 효율적인 배치 쿼리: 각 슬롯이 독립적으로 계산됨
  • 캐시 친화적: ContractStorage가 읽기를 캐싱함

사용법

단일 전송

use savitri_contracts::contracts::standards::savitri1155::SAVITRI1155;

SAVITRI1155::safe_transfer_from(
&mut contract_storage,
&storage,
&from, // [u8; 32]
&to, // [u8; 32]
1, // token id
100, // amount
&[], // data
&mut event_system,
Some(&mut gas_meter),
)?;

배치 전송

SAVITRI1155::safe_batch_transfer_from(
&mut contract_storage,
&storage,
&from,
&to,
&[1, 2, 3], // token ids
&[100, 50, 1], // amounts
&[], // data
&mut event_system,
Some(&mut gas_meter),
)?;

배치 잔액 조회

let balances = SAVITRI1155::balance_of_batch(
&contract_storage,
&storage,
&[owner1, owner2, owner3],
&[token_id_1, token_id_2, token_id_3],
)?;

운영자 승인

// 모든 토큰에 대한 운영자 승인
SAVITRI1155::set_approval_for_all(
&mut contract_storage,
&storage,
&owner,
&operator,
true, // approved
&mut event_system,
Some(&mut gas_meter),
)?;

// 승인 확인
let approved = SAVITRI1155::is_approved_for_all(
&contract_storage,
&storage,
&owner,
&operator,
)?;

이벤트

이벤트필드설명
TransferSingleoperator, from, to, id, amount단일 토큰 전송
TransferBatchoperator, from, to, ids[], amounts[]배치 전송
ApprovalForAllowner, operator, approved운영자 승인 변경

사용 사례

시나리오토큰 유형예시
게임 내 통화대체 가능 (id=1)골드 코인 1000개
고유한 아이템대체 불가능 (amount=1)전설의 검 #42
반대체 가능한정 공급희귀 카드 50장
보상대체 가능PoU 보상 토큰
인증서대체 불가능검증자 인증

배치 최적화

SAVITRI-1155 구현은 다음을 통해 배치 작업을 최적화합니다:

  1. 스토리지 읽기 전 모든 슬롯 사전 계산
  2. 반복적인 DB 읽기 방지를 위한 ContractStorage 캐시 활용
  3. 향후 병렬 읽기를 가능하게 하는 독립적인 슬롯 계산
  4. 작업 간 디코딩된 주소 재사용으로 주소 디코딩 최소화