Skip to Content
Supra Layer 1SupraMVMGetting StartedCreate a Move PackageWrite a Module

Write a Module

Write the hello_blockchain module.

Edit the package’s files on your host machine with your code editor. The container’s /supra/move_workspace directory is the move_workspace directory of your network’s directory on the host, so the CLI in the container reads the files you save there.

If you use VS Code, the Supra Move extension adds Move support to it.

Create a Move file in the sources directory

In Move, global storage is a forest of trees rooted at account addresses. An account stores both resource data and module code. A module is published to an account, rather than to an address generated at deployment as on some other VMs.

The sources directory holds the package’s modules. Create the hello_blockchain.move file in it:


execute me!
echo > /supra/move_workspace/exampleContract/sources/hello_blockchain.move

Open hello_blockchain.move in your code editor and add the following code:


hello_blockchain.move
module hello_blockchain::message { use std::error; use std::signer; use std::string; use supra_framework::event; //:!:>resource struct MessageHolder has key { message: string::String, } //<:!:resource #[event] struct MessageChange has drop, store { account: address, from_message: string::String, to_message: string::String, } /// There is no message present const ENO_MESSAGE: u64 = 0; #[view] public fun get_message(addr: address): string::String acquires MessageHolder { assert!(exists<MessageHolder>(addr), error::not_found(ENO_MESSAGE)); borrow_global<MessageHolder>(addr).message } public entry fun set_message(account: signer, message: string::String) acquires MessageHolder { let account_addr = signer::address_of(&account); if (!exists<MessageHolder>(account_addr)) { move_to(&account, MessageHolder { message, }) } else { let old_message_holder = borrow_global_mut<MessageHolder>(account_addr); let from_message = old_message_holder.message; event::emit(MessageChange { account: account_addr, from_message, to_message: copy message, }); old_message_holder.message = message; } } #[test(account = @0x1)] public entry fun sender_can_set_message(account: signer) acquires MessageHolder { let addr = signer::address_of(&account); supra_framework::account::create_account_for_test(addr); set_message(account, string::utf8(b"Hello, Blockchain")); assert!( get_message(addr) == string::utf8(b"Hello, Blockchain"), ENO_MESSAGE ); } }
Last updated on