There are several Actions an account can do, including sending the winner of the crossword puzzle NEAR using the Transfer Action

Actions (including sending NEAR)

We’re going to introduce a new Action: Transfer. In this chapter, we’d like the first person to solve the crossword puzzle to earn some prize money, sent in NEAR.

Two hands exchanging a coin emblazoned with the NEAR Protocol logo. Art created by qiqi04.near
Art by qiqi04.near

We’ve already used Actions in the previous chapter, when we deployed and initialized the contract, which used the DeployContract and FunctionCall Action, respectively.

The full list of Actions are available at the NEAR specification site.

By the end of this entire tutorial we’ll have used all the Actions highlighted below:

All Actions that will be used when the entire crossword puzzle tutorial is complete

Actions from within a contract

When we deployed and initialized the contract, we used NEAR CLI in our Terminal or Command Prompt app. At a high level, this might feel like we’re lobbing a transaction into the blockchain, instructing it to do a couple actions.

It’s important to note that you can also execute Actions inside a smart contract, which is what we’ll be doing. In the sidebar on the left, you’ll see a section called Promises, which provides examples of this. Perhaps it’s worth mentioning that for the Rust SDK, Promises and Actions are somewhat synonymous.

Define the prize amount

Let’s make it simple and hardcode the prize amount. This is how much NEAR will be given to the first person who solves the crossword puzzle, and will apply to all the crossword puzzles we add. We’ll make this amount adjustable in future chapters.

At the top of the lib.rs file we’ll add this constant:

As the code comment mentions, this is 5 NEAR, but look at all those zeroes in the code!

That’s the value in yoctoNEAR. This concept is similar to other blockchains. Bitcoin’s smallest unit is a satoshi and Ethereum’s is a wei.

Depiction of bills of NEAR, coins for partial NEAR, and then a magnifying glass showing a tiny yoctoNEAR next to an ant. Art created by jrbemint.near
Art by jrbemint.near

Adding Transfer

In the last chapter we had a simple function called guess_solution that returned true if the solution was correct, and false otherwise. We’ll be replacing that function with submit_solution as shown below:

Note the last line in this function, which sends NEAR to the predecessor.

Predecessor, signer, and current account

When writing a smart contract you’ll commonly want to use env and the details it provides. We used this in the last chapter for:

  • logging (ex: env::log_str("hello friend"))
  • hashing using sha256 (ex: env::sha256(solution.as_bytes()))

There are more functions detailed in the SDK reference docs.

Let’s cover three commonly-used functions regarding accounts: predecessor, signer, and current account.

Illustration of Alice sending a transaction to a smart contract named Banana, which does a cross-contract call to the smart contract Cucumber. Art created by yasuoarts.near
Alice sends a transaction to the contract on banana.near, which does a cross-contract call to cucumber.near.
From the perspective of a contract on cucumber.near, we see a list of the predecessor, signer, and current account.
Art by yasuoarts.near
Generate comment with AI 2 nL
Scroll to Top