Join our
Discord!
LogoLogo
SupraScan ExplorerStarKey WalletDiscord
MoveVM
  • Network
  • Oracles
  • Automation
  • SupraNova
  • Guides
MoveVM
  • Overview
  • Getting Started
    • Install Supra CLI with Docker
    • Create a Supra Account
    • Create a Move Package
      • Initialize a Package
      • Write a Module
      • Compile and Publish
      • Interact with a Package
    • Create a dApp with StarKey
  • Network Information
  • Token Standards
  • Learn Move 101
    • Getting Started with Move
    • Unsigned Integers in Move
    • Math Operations in Move
    • Using Vectors in Move
    • Reading Resource Data with borrow_global
    • Passing Data in Move: Value vs. Reference
    • Adding Elements with vector::push_back
    • Emitting Events with event::emit
  • Move Book
    • Getting Started
      • Modules and Scripts
      • Move Tutorial
    • Primitive Types
      • Integers
      • Bool
      • Address
      • Vector
      • Signer
      • References
      • Tuples and Unit
    • Basic Concepts
      • Local Variables and Scope
      • Equality
      • Abort and Assert
      • Conditionals
      • While, For, and Loop
      • Functions
      • Structs and Resources
      • Constants
      • Generics
      • Type Abilities
      • Uses and Aliases
      • Friends
      • Packages
      • Package Upgrades
      • Unit Tests
    • Global Storage
      • Structure
      • Operators
    • Reference
      • Standard Library
      • Coding Conventions
  • TypeScript SDK
    • Guides
      • Create Supra Accounts
      • Publish a Package
    • Documentation
    • Repository
  • Rest API
    • Mainnet
      • Accounts
      • Faucet
      • Transactions
      • Block
      • View
      • Consensus
      • Events
      • Tables
    • Testnet
      • Accounts
      • Faucet
      • Transactions
      • Block
      • View
      • Events
      • Tables
  • Developer Resources
    • Supra Dapp Templates
    • Supra Move VS Code Extension
  • Links
    • Supra DevHub
    • SupraScan Block Explorer
    • StarKey Wallet
    • Live Data Feeds
    • Whitepapers
    • Security Audits
    • Supra's Official GitHub
Powered by GitBook
On this page
Edit on GitHub
  1. Getting Started
  2. Create a Move Package

Write a Module

Create and deploy your first move module.

PreviousInitialize a PackageNextCompile and Publish

Last updated 18 days ago

If you have not already, open your newly initiatlized project within your code editor of choice on your host machine. Remember, the container configs directory is bound to the supra_configs directory on your host machine. Any changes made to these files on the host machine will be reflected within the container.

If you are using VS Code, we highly recommend that you.

1

Create a new move file within the sources directory.

In the previous step, we set the named address of @exampleAddress to that of your own account. Notice how the name of this module is exampleAddress::transfer. Modules are defined with the following format: ADDRESS::MODULE_NAME In Move, global storage is a forest with trees rooted at an account address. Accounts can store both resource data and module code. Modules are deployed to an account, rather than a unique address being generated at deployment as you may be custom to with other VMs. At compilation, exampleAddress will be replaced with the value you set in the named addresses within the Move.toml file. Upon publishing the module, it will be published at that designated account.

The sources directory holds the move modules that you will be working on within your project. Within the sources directory, create the example.move file with the following code.

You can create the file through your code editor, or by executing one of the following commands: touch example.move or echo > example.move

example.move
module exampleAddress::transfer {

    use supra_framework::supra_coin;
    use supra_framework::coin;

    // Function to transfer the specified amount to two destinations from the source signer
    public entry fun two_by_two(
        first: &signer,
        amount_first: u64,
        dst_first: address,
        dst_second: address,
    ) {
        // Transfer the specified amount to the first destination
        coin::transfer<supra_coin::SupraCoin>(first, dst_first,amount_first);
        // Transfer the same amount to the second destination
        coin::transfer<supra_coin::SupraCoin>(first,dst_second, amount_first);
    }

     // Function to view the balance of an address
    #[view]
    public fun view_balance(address: address): u64 {
        coin::balance<supra_coin::SupraCoin>(address)
    }

}

The two_by_two function provided has four parameters. The signer, amount to be transferred, and two destination addresses. The function will transfer the passed amount to both destination addresses.

Signer is a built-in Move resource. It indicates which account is responsible for calling the transaction. This value is automatically injected by the VM and not passed by the user.

install the Supra Move extension