Guest Book

To Share and +4 nLEARNs

Our Guest Book example is a simple app composed by two main components:

  1. A smart contract that stores messages from users, allowing to attach money to them.
  2. A simple web-based frontend that displays the last 10 messages posted.

img


Obtaining the Guest book Example

You have two options to start the Guest book Example.

  1. You can use the app through GitHub Codespaces, which will open a web-based interactive environment.
  2. Clone the repository locally and use it from your computer.
Codespaces Clone locally
Open in GitHub Codespaces 🌠https://github.com/near-examples/guest-book-examples

Structure of the Example

The example is divided in two main components:

  1. The smart contract, available in two flavors: Rust and JavaScript
  2. The frontend, that interacts with an already deployed contract.

“`bash
┌── sandbox-ts # sandbox testing
│ ├── src
│ │ └── main.ava.ts
│ ├── ava.config.cjs
│ └── package.json
├── src # contract’s code
│ ├── contract.ts
│ └── model.ts
├── package.json # package manager
├── README.md
└── tsconfig.json # test script
“`

“`bash
┌── tests # workspaces testing
│ ├── workspaces.rs
├── src # contract’s code
│ └── lib.rs
├── Cargo.toml # package manager
├── README.md
└── rust-toolchain.toml
“`


Frontend

The guest book example includes a frontend that interacts with an already deployed smart contract, allowing user to sign a message.


Running the Frontend

To start the frontend you will need to install the dependencies and start the server.

cd frontend
yarn
yarn dev

Go ahead and login with your NEAR account. If you don’t have one, you will be able to create one in the moment. Once logged in, you will be able to sign a message in the guest book. You can further send some money alongside your message. If you attach more than 0.01Ⓝ then your message will be marked as “premium”.


Understanding the Frontend

The frontend is a Next.JS project generated by create-near-app. Check _app.js and index.js to understand how components are displayed and interacting with the contract.





Smart Contract

The contract presents 3 methods: add_message, get_message and total_messages.








Testing the Contract

The contract readily includes a set of unit and sandbox testing to validate its functionality. To execute the tests, run the following commands:


“`bash
cd contract-ts
yarn
yarn test
“`


“`bash
cd contract-rs
cargo test
“`


Deploying the Contract to the NEAR network

In order to deploy the contract you will need to create a NEAR account.


“`bash
# Create a new account pre-funded by a faucet
near create-account –useFaucet
“`

“`bash
# Create a new account pre-funded by a faucet
near account create-account sponsor-by-faucet-service .testnet autogenerate-new-keypair save-to-keychain network-config testnet create
“`

Go into the directory containing the smart contract (cd contract-ts or cd contract-rs), build and deploy it:

cargo near build

cargo near deploy <accountId>

CLI: Interacting with the Contract

To interact with the contract through the console, you can use the following commands.

Get messages


“`bash
near view guestbook.near-examples.testnet get_messages ‘{“from_index”: “0”,”limit”: “10”}’
“`

“`bash
near contract call-function as-read-only guestbook.near-examples.testnet get_messages json-args ‘{“from_index”: “0”,”limit”: “10”}’ network-config testnet now
“`


Get total number of messages


“`bash
near view guestbook.near-examples.testnet total_messages
“`

“`bash
near contract call-function as-read-only guestbook.near-examples.testnet total_messages json-args {} network-config testnet now
“`


Add a message


“`bash
# Replace with your account ID
# Required a text
# Optional deposit to make the message premium
near call guestbook.near-examples.testnet add_message ‘{“text”:”Hello Near”}’ –accountId –deposit 0.1
“`

“`bash
# Replace with your account ID
# Required a text
# Optional deposit to make the message premium
near contract call-function as-transaction guestbook.near-examples.testnet add_message json-args ‘{“text”:”Hello Near”}’ prepaid-gas ‘30.0 Tgas’ attached-deposit ‘0.1 NEAR’ sign-as network-config testnet sign-with-keychain send
“`



Moving Forward

A nice way to learn is by trying to expand a contract. You can modify the guestbook example to incorporate a feature where users can give likes to messages. Additionally, implement a method to toggle the like.

Generate comment with AI 2 nL
Scroll to Top