Skip to content

Latest commit

 

History

History
91 lines (65 loc) · 2.19 KB

README.md

File metadata and controls

91 lines (65 loc) · 2.19 KB

Hello NEAR Contract

The smart contract exposes two methods to enable storing and retrieving a greeting in the NEAR network.

const DEFAULT_MESSAGE: &str = "Hello";

#[near_bindgen]
#[derive(BorshDeserialize, BorshSerialize)]
pub struct Contract {
    greeting: String,
}

impl Default for Contract {
    fn default() -> Self {
        Self{greeting: DEFAULT_MESSAGE.to_string()}
    }
}

#[near_bindgen]
impl Contract {
    // Public: Returns the stored greeting, defaulting to 'Hello'
    pub fn get_greeting(&self) -> String {
        return self.greeting.clone();
    }

    // Public: Takes a greeting, such as 'howdy', and records it
    pub fn set_greeting(&mut self, greeting: String) {
        // Record a log permanently to the blockchain!
        log!("Saving greeting {}", greeting);
        self.greeting = greeting;
    }
}

Quickstart

  1. Make sure you have installed rust.
  2. Install the NEAR CLI

1. Build and Deploy the Contract

You can automatically compile and deploy the contract in the NEAR testnet by running:

./deploy.sh

Once finished, check the neardev/dev-account file to find the address in which the contract was deployed:

cat ./neardev/dev-account
# e.g. dev-1659899566943-21539992274727

2. Retrieve the Greeting

get_greeting is a read-only method (aka view method).

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

# Use near-cli to get the greeting
near view <dev-account> get_greeting

3. Store a New Greeting

set_greeting changes the contract's state, for which it is a change method.

Change methods can only be invoked using a NEAR account, since the account needs to pay GAS for the transaction.

# Use near-cli to set a new greeting
near call <dev-account> set_greeting '{"greeting":"howdy"}' --accountId <dev-account>

Tip: If you would like to call set_greeting using your own account, first login into NEAR using:

# Use near-cli to login your NEAR account
near login

and then use the logged account to sign the transaction: --accountId <your-account>.