Near Learn

Your first contract

A counter, in both worlds. A NEAR contract is a Rust struct plus an impl block — the fields are storage, the methods are your ABI.

Beginner3 min read3-question check

In Solidity a contract is a contract block: state variables at the top, functions below. On NEAR a contract is a Rust struct that holds the state, plus an `impl` block whose public methods are the callable interface.

The #[near(contract_state)] macro marks the struct as the contract’s persisted state. #[near] on the impl exposes its pub methods as contract calls and generates the (de)serialization glue.

A counter contract
Solidity
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.24;

contract Counter {
    uint256 public count;

    function increment() external {
        count += 1;
    }
}
Rust
use near_sdk::near;

#[near(contract_state)]
#[derive(Default)]
pub struct Counter {
    count: u64,
}

#[near]
impl Counter {
    pub fn increment(&mut self) {
        self.count += 1;
    }

    pub fn get_count(&self) -> u64 {
        self.count
    }
}
Coming from EVM

A method that writes takes &mut self; one that only reads takes &self. That receiver is the whole story — there is no external / public / view keyword.

Solidity’s public state variable auto-generates a getter. NEAR does not: if you want to read count from outside, you write an explicit &self view method (get_count).

Check yourself

3 questions · progress saved in this browser

  1. 1.In Solidity, state lives in state variables inside the contract block. Where does a NEAR contract's persisted state live?
  2. 2.Solidity's uint256 public count; auto-generates a getter. What do you do on NEAR so outsiders can read count?
  3. 3.The counter contract has no constructor. How does it start with count = 0?