Skip to content
Retr0 Academy
Web3 Fundamentals

Web3 Fundamentals · 6 min read

Smart Contracts

What smart contracts are, how they hold value and run code, and how NEAR runs them as WebAssembly on accounts.

A smart contract is a program stored on the blockchain. Once deployed, it runs exactly as written, and anyone can interact with it by sending transactions.

Think of a vending machine: put in the right coins, press a button, and the machine follows its rules to give you a snack. No shopkeeper is needed. A smart contract is a digital vending machine for rules about money, ownership, and data.

What can smart contracts do?

  • Tokens: create currencies and points (NEP-141 on NEAR)
  • NFTs: prove ownership of unique items or credentials (NEP-171 on NEAR)
  • Decentralized exchanges: swap tokens without a company in the middle
  • Lending: borrow and lend with automatic interest
  • DAOs: groups that vote on decisions and share a treasury
  • Escrow and crowdfunding: release funds only when conditions are met

How they work on NEAR

  1. A developer writes the contract in Rust (near-sdk) or JavaScript/TypeScript (near-sdk-js).
  2. The build turns it into WebAssembly (Wasm), which every validator can run safely.
  3. The developer deploys the Wasm to a NEAR account, for example counter.alice.testnet. That account is the contract.
  4. Users call its methods: view methods over RPC for free, change methods with a signed transaction.

bash

Rust / JS source  ──cargo near build──►  contract.wasm  ──deploy──►  counter.alice.testnet

rust

#[near(contract_state)]
#[derive(Default)]
pub struct Counter { value: u64 }
 
#[near]
impl Counter {
    pub fn get(&self) -> u64 { self.value }          // view: free
    pub fn increment(&mut self) { self.value += 1; } // change: needs a transaction
}

Accounts and contracts

On NEAR there is one kind of account. Every account has a balance and access keys, and can also hold one contract. A contract account can still have keys (so its owner can upgrade it), or have all keys removed so nobody can change the code.

Account without a contractAccount with a contract
Controlled byIts access keysIts keys and its code's rules
Can start transactions?Yes, with a keyIts code can create cross-contract calls when called
Has a balance?YesYes, and pays storage for its state
Readable name?Yes, like alice.testnetYes, like diploma.retr0school.testnet

Reading vs. writing

  • View methods just read state. They are free over RPC and need no wallet.
  • Change methods write state. They need a signed transaction and cost gas, and often a deposit for storage.

Describing the interface

NEAR contracts take and return JSON by default, so apps call them with method names and JSON arguments:

json

{ "method_name": "verify_diploma", "args": { "account_id": "alice.testnet", "track": "near" } }

cargo near can also generate an ABI file that lists every method and its argument types for tools and frontends.

Try it: simulating contract rules

This JavaScript simulation shows how a simple "piggy bank" contract enforces its rules. A real contract would enforce the same rules on-chain, where nobody (not even its creator) could bypass them.

<div class="bank">
<h3>🐷 PiggyBank contract (simulated)</h3>
<p>Owner: <strong>alice</strong> | Unlocks at block <strong>5</strong></p>
<p>Current block: <strong id="block">0</strong> | Balance: <strong id="bal">0</strong> NEAR</p>
<label>Caller
  <select id="who"><option>alice</option><option>bob</option></select>
</label>
<button id="deposit">deposit(1 NEAR)</button>
<button id="withdraw">withdraw()</button>
<button id="mine">Next block ⏭️</button>
<pre id="log"></pre>
</div>

Edit this example in the Playground.

Open in Playground

Immutability: a superpower and a risk

A NEAR contract can be redeployed by anyone holding a full-access key to its account. That makes upgrades easy (this academy's certificate contract was upgraded in place, keeping every certificate), but users must trust the key holder. Remove all keys, or hand the account to a DAO, and the code becomes immutable: trusted rules, but permanent bugs.

Oracles

Contracts can only see data that is on the blockchain. To use outside information (like the price of a stock or the weather), they rely on oracles, services that bring off-chain data on-chain, such as price feeds.

Summary

  • A smart contract is a program on the blockchain that runs exactly as written.
  • On NEAR, contracts are WebAssembly deployed to a named account, written in Rust or JavaScript.
  • View methods are free; change methods need a transaction and gas.
  • Methods take JSON arguments; cargo near can generate an ABI.
  • Whoever holds the account's full-access keys can upgrade it, so security and key management matter enormously.

Coming from Ethereum?

Ethereum contracts are written in Solidity and run on the EVM at hex addresses; NEAR contracts are Wasm on named accounts, calls between contracts are asynchronous, and storage is paid by staking instead of per write. The ideas (code that holds value, public methods, irreversible calls) transfer; the tooling and standards do not. The next course, NEAR Smart Contracts, takes you from a hello-world contract to testnet.

Exercise

Name two differences between a NEAR contract and a typical Ethereum contract (accounts, language, or how you call a method). You do not need to write code yet.