Skip to content

Latest commit

 

History

History

contract-rs

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 
 
 
 
 

Guest Book Contract

This smart contract stores messages from users. If the user attaches more than 0.1 NEAR tokens the message is marked as premium.

How to Build Locally?

Install cargo-near and run:

cargo near build

How to Test Locally?

cargo test

How to Deploy?

To deploy manually, install cargo-near and run:

# Create a new account
cargo near create-dev-account

# Deploy the contract on it
cargo near deploy <account-id>

How to Interact?

In this example we will be using NEAR CLI to intract with the NEAR blockchain and the smart contract

If you want full control over of your interactions we recommend using the near-cli-rs.

1. Add a Message

add_message adds a message to the vector of messages and marks it as premium if the user attached more than 0.1 NEAR.

// Public - Adds a new message.
#[payable]
pub fn add_message(&mut self, text: String) {
  // If the user attaches more than 0.01N the message is premium
  let premium = env::attached_deposit() >= POINT_ONE;
  let sender = env::predecessor_account_id();

  let message = PostedMessage{premium, sender, text};
  self.messages.push(&message);
}
near call <dev-account> add_message '{"text": "a message"}' --amount 0.1 --accountId <account>

2. Retrieve the Stored Messages

get_messages is a read-only method (view method) that returns a slice of the vector messages. Please note that from_index and limit are optional parameters.

View methods can be called for free by anyone, even people without a NEAR account!

    // Public Method - Returns a slice of the messages.
    pub fn get_messages(&self, from_index: Option<U64>, limit: Option<U64>) -> Vec<&PostedMessage> {
        let from = u64::from(from_index.unwrap_or(U64(0)));
        let limit = u64::from(limit.unwrap_or(U64(10)));

        self.messages
            .iter()
            .skip(from as usize)
            .take(limit as usize)
            .collect()
    }
near view <dev-account> get_messages '{"from_index":0, "limit":10}'

Useful Links