In Sway, StorageVec and HeapVec are two data structures used to store mutable arrays.
storage_vec_examples
function, we demonstrate how to use these operations.heap_vec_examples
function, we demonstrate how to use these operations.In the provided code, we define a smart contract named MyContract
with two functions named storage_vec_examples
and heap_vec_examples
, each demonstrating how to use StorageVec and HeapVec.
nums
, which stores u64
type values.storage_vec_examples
function, we demonstrate how to use various StorageVec operations.heap_vec_examples
function, we demonstrate how to use various HeapVec operations.This simple example is intended to show you how to use StorageVec and HeapVec in Sway to store and manipulate mutable arrays. We hope this tutorial helps you better understand these two data structures in Sway.
All Examplescontract; use std::{ auth::{ msg_sender, }, call_frames::{ msg_asset_id, }, contract_id::ContractId, context::{ balance_of, msg_amount, }, asset::{ transfer, }, }; abi MyContract { #[payable] fn deposit(); fn withdraw(amount: u64); fn get_balance() -> u64; } impl MyContract for Contract { #[payable] fn deposit() { require(msg_asset_id() == AssetId::base(), "not base asset"); require(msg_amount() > 0, "amount = 0"); } fn withdraw(amount: u64) { transfer(msg_sender().unwrap(), AssetId::base(), amount); } fn get_balance() -> u64 { balance_of(ContractId::this(), AssetId::base()) } }