The Move contract tx::app
provides a simple on-chain messaging service where users can sign and store messages. Each message is stored on the blockchain and associated with a timestamp and the sender's address. Here's a breakdown of the contract's key components:
-
MODULE_ADMIN
:- The address
@tx
is designated as the admin of the module. Only this admin can initialize the contract.
- The address
-
EINVALID_ADMIN_ADDR
:- An error code (
1
) representing an invalid admin address, used to ensure only the correct admin can initialize the contract.
- An error code (
-
Txs (struct)
:- A data structure that stores a vector of signed messages (
messages: vector<String>
).
- A data structure that stores a vector of signed messages (
-
NewTx (event)
:- An event structure with three fields:
timestamp: u64
: The time the message was signed.user: address
: The address of the user who signed the message.message: String
: The content of the signed message.
- The event is emitted every time a new message is signed.
- An event structure with three fields:
- Initializes the contract and can only be called by the admin (designated by
@tx
). - Validates that the caller is the admin by checking if the caller's address matches
MODULE_ADMIN
. - If valid, it creates a
Txs
struct with an empty list of messages and moves it under the admin's account. - Error Handling: If the caller's address is not
@tx
, it aborts with the errorEINVALID_ADMIN_ADDR
.
- Allows any user to sign a message and store it in the
Txs
struct. - The function:
- Adds the message to the
messages
vector in theTxs
struct. - Emits a
NewTx
event with the message, the sender's address, and the timestamp.
- Adds the message to the
- This event helps track when and by whom messages are signed on the blockchain.
- A view-only function that retrieves all stored messages.
- Returns the list of messages stored in the
Txs
struct. - This function does not modify the blockchain's state, making it safe for read-only operations.
All the parameters and dependendecies are defined in Move.toml
- Initial Setup** Initialize your Aptos CLI wallet and configure your environment.
aptos init
- Check Account Information
aptos account list
aptos account balance
- Compile Move Code
aptos move compile
- Compile with Named Addresses
aptos move compile --named-addresses tx=<your_address>
- Publish Your Contract On-Chain
aptos move publish --named-addresses tx=<your_address>
- Initialize the Contract
aptos move run --function-id <module_address>::tx::app::init --profile default
- Sign a Message
aptos move run --function-id <module_address>::tx::app::sign_message --profile default --args <message>