Aptos Move by Example
  • 🚀Getting Started
  • Set-Up
  • Why is Move Secure
    • Move prover
  • Move vs Solidity
    • Resources
    • Parallel Processing
    • Reentrancy attacks
    • Memory management
    • Smart contract verification
    • Compiled language
  • Basic Concepts
    • Move.toml
    • Primary data-types
    • Strings
    • Comments
    • Functions
    • Function Visibilities
    • Control flow and expressions
    • Loops
    • Error
    • Struct and its Abilities
    • Scripts
    • Operations
  • Intermediate Concepts
    • Local variables
    • Constants
    • Signer
    • Vector
    • Address
    • Uses and Aliases
    • Maps
    • Hash functions
    • References
    • Unit test
    • Generics
    • Type Arguments
    • Type Inference
  • Advanced Concepts
    • Global Storage Structure
    • Global Storage Operations
    • Phantom Type Parameters
    • Timestamps
    • Ownership
    • Move coding conventions
    • View functions
    • Aptos account
    • Aptos Coin
    • Aptos Token(Nft)
    • Object
    • Token V2
  • Applications
    • First App
    • ToDoList
    • Voting System
    • Basic Tokens
    • Storage using Generics
    • Company
    • Collection
    • Football Card
    • Staking Module
    • MultiSender Wallet
    • English Auction
    • Dutch Auction
    • Attendance Sheet
    • Polling Contract
    • Lottery Contract
  • Decentralized Finance
    • Simple Swap Protocol Contract
    • Code of Swapping Protocol
  • Hacks
    • Coming soon
  • Hands on tutorials
    • Indexer tutorials
Powered by GitBook
On this page
Edit on GitHub
  1. Applications

Collection

  • Basic contract is created for storing Item on Collection struct in vector form.

  • Some of the function like start_collection, exists_at, add_item, size and destroy are called publicly in the contract.

  • Scripts are also written for this contract.

module 0x12::Collection{
    use std::vector;
    use std::signer;

    struct Item has store, drop{}

    struct Collection has store, key{
        items:vector<Item>
    }

    public fun start_collection(account: &signer){
        move_to<Collection>(account,Collection{
            items: vector::empty<Item>()
        })
    }

    public fun exists_at(at: address): bool {
        exists<Collection>(at)
    }

    public fun add_item(account: &signer) acquires Collection{
        let addr = signer::address_of(account);
        let collection = borrow_global_mut<Collection>(addr);
        vector::push_back(&mut collection.items,Item{});
    }

    public fun size(account: &signer):u64 acquires Collection{
        let addr = signer::address_of(account);
        let collection = borrow_global_mut<Collection>(addr);
        vector::length(& collection.items)
    }

    public fun destroy(account: &signer) acquires Collection{
        let addr = signer::address_of(account);
        let collection = move_from<Collection>(addr);

        let Collection{items: _} = collection;
    }
}

Script of Collection contract:

script{
    use 0x12::collection as coll;
    use std::debug;
    use std::signer;

    fun main_resource(account: signer){
        let addr = signer::address_of(&account);
        // OR
        // let addr = @0x63;
        
        coll::destroy(&account);
        coll::start_collection(&account);
        let ea = coll::exists_at(addr);
        debug::print(&ea);
        coll::add_item(&account);
        coll::add_item(&account);
        coll::add_item(&account);
        let lsize = coll::size(&account);
        debug::print(&lsize);
    }
}
PreviousCompanyNextFootball Card

Last updated 2 years ago