This smart contract stores messages from users. If the user attaches more than 0.1 NEAR tokens the message is marked as premium.
Install cargo-near
and run:
cargo near build
cargo test
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>
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.
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>
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}'
- cargo-near - NEAR smart contract development toolkit for Rust
- near CLI-rs - Iteract with NEAR blockchain from command line
- NEAR Rust SDK Documentation
- NEAR Documentation
- NEAR StackOverflow
- NEAR Discord
- NEAR Telegram Developers Community Group
- NEAR DevHub: Telegram, Twitter