SAVITRI-20: Standard Token Fungibile
SAVITRI-20 è lo standard per token fungibili della Savitri Network, equivalente all'ERC-20 di Ethereum. Consente la creazione e la gestione di token fungibili con meccanismi di trasferimento, approvazione e indennità.
Interfaccia
| Funzione | Parametri | Ritorna | Descrizione |
|---|---|---|---|
initialize | owner, name, symbol, initial_supply | - | Deploy e inizializzazione di un nuovo token |
total_supply | - | u128 | Fornitura totale di token |
balance_of | address | u128 | Saldo token di un indirizzo |
transfer | to, amount | Result | Trasferimento token al destinatario |
approve | spender, amount | Result | Approva l'indennità allo spender |
transfer_from | from, to, amount | Result | Trasferimento tramite indennità |
allowance | owner, spender | u128 | Verifica l'indennità approvata |
mint | to, amount | Result | Conia nuovi token (solo proprietario) |
burn | from, amount | Result | Brucia token |
Layout dello Storage
| Intervallo Slot | Scopo |
|---|---|
| 0-99 | BaseContract (riservato) |
| 100 | Fornitura totale |
| 101+ | Mapping saldi: keccak256(address || 101) |
| 200+ | Mapping indennità: keccak256(spender || keccak256(owner || 200)) |
| 300+ | Nome del token (storage stringhe) |
| 400+ | Simbolo del token (storage stringhe) |
Tutti i valori sono memorizzati come array di 32 byte con valori u128 in formato little-endian.
Deploy di un Token SAVITRI-20
Tramite Runtime Contract (Rust)
use savitri_contracts::contracts::standards::savitri20::SAVITRI20;
// Inizializza un nuovo token
SAVITRI20::initialize(
&mut contract_storage,
&storage,
&owner_address, // [u8; 32]
"My Token", // name
"MTK", // symbol
1_000_000_000_000_000_000_000_000, // 1M tokens (18 decimals)
Some(&mut gas_meter),
)?;
Tramite SDK
use savitri_sdk::{TransactionBuilder, Wallet};
let wallet = Wallet::from_private_key_hex("your_key")?;
// Deploy del contract token
let deploy_tx = TransactionBuilder::new()
.data(savitri20_bytecode)
.value(0)
.nonce(nonce)
.fee(5_000_000_000_000_000)
.build_and_sign(&wallet)?;
Operazioni sui Token
Trasferimento
// Trasferimento diretto
SAVITRI20::transfer(
&mut contract_storage,
&storage,
&sender, // [u8; 32]
&recipient, // [u8; 32]
1000_000_000_000_000_000, // 1000 tokens
&mut event_system,
Some(&mut gas_meter),
)?;
Approvazione e TransferFrom
// Approva uno spender
SAVITRI20::approve(
&mut contract_storage,
&storage,
&owner,
&spender,
5000_000_000_000_000_000, // 5000 token allowance
&mut event_system,
Some(&mut gas_meter),
)?;
// Trasferimento tramite indennità
SAVITRI20::transfer_from(
&mut contract_storage,
&storage,
&spender, // caller (deve avere indennità)
&owner, // from
&recipient, // to
1000_000_000_000_000_000,
&mut event_system,
Some(&mut gas_meter),
)?;
Interrogazione Saldi
let balance = SAVITRI20::balance_of(&contract_storage, &storage, &address)?;
let supply = SAVITRI20::total_supply(&contract_storage, &storage)?;
let allowed = SAVITRI20::allowance(&contract_storage, &storage, &owner, &spender)?;
Eventi
| Evento | Campi | Descrizione |
|---|---|---|
Transfer | from, to, amount | Trasferimento token |
Approval | owner, spender, amount | Approvazione indennità |
Mint | to, amount | Nuovi token coniati |
Burn | from, amount | Token bruciati |
Sicurezza
- Controllo indirizzo non nullo: I trasferimenti da/verso l'indirizzo zero vengono rifiutati
- Validazione del saldo: I trasferimenti falliscono se il mittente ha saldo insufficiente
- Verifica dell'indennità:
transfer_fromverifica e decrementa l'indennità in modo atomico - Protezione dall'overflow: Tutta l'aritmetica utilizza operazioni con controllo
- Hash slot Keccak256: Previene collisioni degli slot di storage tra mapping diverse