> ## Documentation Index
> Fetch the complete documentation index at: https://docs.near.org/llms.txt
> Use this file to discover all available pages before exploring further.

# Basic Anatomy

> Learn the basic anatomy of all smart contracts.

export const File = ({children}) => {
  return children;
};

export const Block = ({children}) => {
  return children;
};

export const ExplainCode = ({children, languages}) => {
  const langList = typeof languages === 'string' ? languages.split(',') : languages || [];
  const [language, setLanguage] = useState(langList[0]);
  const [activeBlock, setActiveBlock] = useState(0);
  const [code, setCode] = useState(null);
  const blockRefs = useRef({});
  const codeContainerRef = useRef(null);
  const ratioMap = useRef({});
  const lang2label = {
    rust: '🦀 Rust',
    js: '🌐 JS',
    ts: '🌐 TS',
    python: '🐍 Python',
    go: '🐹 Go'
  };
  const typeAccentClass = {
    state: 'border-l-4 border-emerald-500',
    warning: 'border-l-4 border-amber-500',
    note: 'border-l-4 border-amber-500'
  };
  const accentBorder = t => typeAccentClass[t] || 'border-l-4 border-indigo-500';
  function toRaw(ref) {
    const fullUrl = ref.slice(ref.indexOf('https'));
    const [url] = fullUrl.split('#');
    const [org, repo, , branch, ...pathSeg] = new URL(url).pathname.split('/').slice(1);
    return `https://raw.githubusercontent.com/${org}/${repo}/${branch}/${pathSeg.join('/')}`;
  }
  async function fetchRaw(url, fromLine, toLine) {
    let res;
    if (typeof window !== 'undefined') {
      const validUntil = localStorage.getItem(`${url}-until`);
      if (validUntil && Number(validUntil) > Date.now()) {
        res = localStorage.getItem(url);
      }
    }
    if (!res) {
      try {
        res = await (await fetch(url)).text();
        if (typeof window !== 'undefined') {
          localStorage.setItem(url, res);
          localStorage.setItem(`${url}-until`, String(Date.now() + 60000));
        }
      } catch {
        return 'Error fetching code, please try reloading';
      }
    }
    let lines = res.split('\n');
    const from = fromLine ? Number(fromLine) - 1 : 0;
    const to = toLine ? Number(toLine) : lines.length;
    lines = lines.slice(from, to);
    const indent = lines.reduce((prev, line) => {
      if (!line.length) return prev;
      const m = line.match(/^\s+/);
      return m ? Math.min(prev, m[0].length) : 0;
    }, Infinity);
    return lines.map(l => l.slice(indent === Infinity ? 0 : indent)).join('\n');
  }
  function parseHighlights(str) {
    const set = new Set();
    if (!str) return set;
    String(str).split(',').forEach(part => {
      const [a, b] = part.trim().split('-');
      if (b) {
        for (let i = Number(a); i <= Number(b); i++) set.add(i);
      } else if (a) set.add(Number(a));
    });
    return set;
  }
  const childArray = Array.isArray(children) ? children.flat(Infinity) : children ? [children] : [];
  const blocks = [];
  const files = [];
  for (const child of childArray) {
    if (!child || typeof child !== 'object' || !child.props) continue;
    if (child.props.highlights !== undefined) {
      let hl = {};
      try {
        hl = JSON.parse(child.props.highlights);
      } catch {}
      if ((language in hl)) {
        blocks.push({
          text: child.props.children,
          highlight: hl[language],
          fname: child.props.fname,
          type: child.props.type
        });
      }
    } else if (child.props.language === language) {
      files.push({
        ...child.props
      });
    }
  }
  const activeFname = blocks[activeBlock]?.fname || files[0]?.fname;
  const fileKey = `${activeFname}-${language}`;
  const currentFile = files.find(f => f.fname === activeFname) || files[0];
  useEffect(() => {
    setActiveBlock(0);
    setCode(null);
    ratioMap.current = {};
  }, [language]);
  useEffect(() => {
    const observedEls = Object.entries(blockRefs.current).filter(([, el]) => el);
    const observer = new IntersectionObserver(entries => {
      entries.forEach(entry => {
        const idx = Number(entry.target.dataset.blockIdx);
        ratioMap.current[idx] = entry.intersectionRatio;
      });
      let bestIdx = -1;
      let bestRatio = 0;
      Object.entries(ratioMap.current).forEach(([idx, ratio]) => {
        if (ratio > bestRatio) {
          bestRatio = ratio;
          bestIdx = Number(idx);
        }
      });
      if (bestIdx !== -1) {
        setActiveBlock(bestIdx);
      } else {
        const zoneTop = window.innerHeight * 0.2;
        const allAboveZone = Object.entries(blockRefs.current).filter(([, el]) => el).every(([, el]) => el.getBoundingClientRect().bottom < zoneTop);
        if (allAboveZone) setActiveBlock(-1);
      }
    }, {
      threshold: [0, 0.1, 0.25, 0.5, 0.75, 1],
      rootMargin: '-20% 0px -50% 0px'
    });
    observedEls.forEach(([idx, el]) => {
      el.dataset.blockIdx = idx;
      observer.observe(el);
    });
    return () => observer.disconnect();
  }, [blocks.length]);
  useEffect(() => {
    const container = codeContainerRef.current;
    if (!container) return;
    const firstHL = container.querySelector('[data-first-hl]');
    if (!firstHL) return;
    const targetScrollTop = container.scrollTop + firstHL.getBoundingClientRect().top - container.getBoundingClientRect().top - container.clientHeight / 2 + firstHL.offsetHeight / 2;
    container.scrollTo({
      top: Math.max(0, targetScrollTop),
      behavior: 'smooth'
    });
  }, [activeBlock]);
  useEffect(() => {
    if (!currentFile?.url) return;
    setCode(null);
    const rawUrl = toRaw(currentFile.url);
    fetchRaw(rawUrl, currentFile.start, currentFile.end).then(setCode);
  }, [fileKey]);
  const startLine = currentFile?.start ? Number(currentFile.start) : 1;
  const highlighted = parseHighlights(blocks[activeBlock]?.highlight);
  const firstHlLine = highlighted.size > 0 ? Math.min(...highlighted) : -1;
  return <div className="my-6 not-prose">
      {}
      <div className="flex border-b border-gray-200 dark:border-gray-700 mb-4">
        {langList.map(lang => <button key={lang} onClick={() => setLanguage(lang)} className={['px-4 py-2 text-sm font-medium bg-transparent cursor-pointer', 'border-0 border-b-2 -mb-px transition-colors outline-none', language === lang ? 'border-blue-500 text-blue-500 dark:text-blue-400 dark:border-blue-400' : 'border-transparent text-gray-500 dark:text-gray-400 hover:text-gray-800 dark:hover:text-gray-200'].join(' ')}>
            {lang2label[lang] || lang}
          </button>)}
      </div>

      {}
      <div className="flex gap-8 items-start">
        {}
        <div className="flex-[5] flex flex-col gap-3 min-w-0 pb-[40vh]">
          {blocks.map((block, i) => <div key={i} ref={el => {
    blockRefs.current[i] = el;
  }} onClick={() => !block.type && setActiveBlock(i)} className={['px-5 py-4 rounded-md transition-all duration-200', block.type ? `cursor-default ${accentBorder(block.type)} opacity-75` : activeBlock === i ? `cursor-pointer ${accentBorder(block.type)} shadow-sm` : `cursor-pointer ${accentBorder(block.type)} opacity-60 hover:opacity-90`].join(' ')} style={{
    backgroundColor: block.type ? 'var(--explain-card-bg)' : activeBlock === i ? 'var(--explain-card-active-bg)' : 'var(--explain-card-bg)',
    border: !block.type && activeBlock === i ? '1px solid var(--explain-card-border)' : '1px solid transparent'
  }}>
              {block.text}
            </div>)}
        </div>

        {}
        {currentFile && <div className="flex-[6] min-w-0 rounded-[10px] border border-[#d0d7de] dark:border-[#30363d] overflow-hidden sticky top-6 max-h-[calc(100vh-5rem)] flex flex-col shadow-md bg-white dark:bg-[#0d1117]">
            {}
            <div className="flex items-center justify-between px-[14px] py-2 bg-[#f6f8fa] dark:bg-[#161b22] border-b border-[#d0d7de] dark:border-[#30363d] shrink-0">
              <div className="flex items-center gap-2">
                <svg width="14" height="14" viewBox="0 0 24 24" className="fill-[#657279] dark:fill-[#8b949e]">
                  <path d="M12 0C5.37 0 0 5.37 0 12c0 5.31 3.435 9.795 8.205 11.385.6.105.825-.255.825-.57 0-.285-.015-1.23-.015-2.235-3.015.555-3.795-.735-4.035-1.41-.135-.345-.72-1.41-1.23-1.695-.42-.225-1.02-.78-.015-.795.945-.015 1.62.87 1.845 1.23 1.08 1.815 2.805 1.305 3.495.99.105-.78.42-1.305.765-1.605-2.67-.3-5.46-1.335-5.46-5.925 0-1.305.465-2.385 1.23-3.225-.12-.3-.54-1.53.12-3.18 0 0 1.005-.315 3.3 1.23.96-.27 1.98-.405 3-.405s2.04.135 3 .405c2.295-1.56 3.3-1.23 3.3-1.23.66 1.65.24 2.88.12 3.18.765.84 1.23 1.905 1.23 3.225 0 4.605-2.805 5.625-5.475 5.925.435.375.81 1.095.81 2.22 0 1.605-.015 2.895-.015 3.3 0 .315.225.69.825.57A12.02 12.02 0 0 0 24 12c0-6.63-5.37-12-12-12z" />
                </svg>
                <span className="text-[0.8125rem] font-medium text-[#1f2328] dark:text-[#e6edf3]">{currentFile.fname}</span>
              </div>
              <a href={`${currentFile.url}#L${currentFile.start}-L${currentFile.end}`} target="_blank" rel="noreferrer noopener" className="text-[0.6875rem] no-underline flex items-center gap-1 transition-colors text-[#656d76] dark:text-[#8b949e] hover:text-[#1f2328] dark:hover:text-[#e6edf3]">
                View on GitHub
                <svg width="10" height="10" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2.5" strokeLinecap="round" strokeLinejoin="round">
                  <path d="M18 13v6a2 2 0 0 1-2 2H5a2 2 0 0 1-2-2V8a2 2 0 0 1 2-2h6" />
                  <polyline points="15 3 21 3 21 9" />
                  <line x1="10" y1="14" x2="21" y2="3" />
                </svg>
              </a>
            </div>

            {}
            <div className="overflow-auto flex-1" ref={codeContainerRef}>
              {code === null ? <div className="p-4 text-xs text-gray-500 dark:text-gray-400">Loading...</div> : <table className="w-full border-collapse font-mono text-[13px] leading-relaxed">
                  <tbody>
                    {code.split('\n').map((line, i) => {
    const lineNum = startLine + i;
    const isHL = highlighted.has(i + 1);
    return <tr key={i} {...i + 1 === firstHlLine ? {
      'data-first-hl': ''
    } : {}} style={isHL ? {
      backgroundColor: 'rgba(250,204,21,0.08)'
    } : undefined}>
                          <td style={{
      minWidth: '70px'
    }} className="py-0 pl-3 pr-3 text-right text-[0.7rem] text-[#8c959f] dark:text-gray-400 w-12 min-w-[3rem] whitespace-nowrap border-r border-[#d0d7de] dark:border-gray-700 select-none align-top">
                            {lineNum}
                          </td>
                          <td style={isHL ? {
      borderLeft: '2px solid #facc15'
    } : undefined} className="pl-4 pr-6 whitespace-pre text-[#1f2328] dark:text-[#e6edf3] align-top">
                            {line || ' '}
                          </td>
                        </tr>;
  })}
                  </tbody>
                </table>}
            </div>
          </div>}
      </div>
    </div>;
};

Let's illustrate the basic anatomy of a simple "Hello World" contract. The code on this page comes from our [Hello NEAR repository](https://github.com/near-examples/hello-near-examples) on GitHub.

<ExplainCode languages="rust,js,python,go">
  <Block highlights="{&#x22;js&#x22;: &#x22;1&#x22;, &#x22;rust&#x22;: &#x22;1&#x22;, &#x22;python&#x22;: &#x22;1&#x22;,&#x22;go&#x22;:&#x22;3-5&#x22;}" fname="hello-near">
    ### Importing the SDK

    All contracts will import the **NEAR SDK**, enabling them to [access the execution environment](./environment), [call other contracts](./crosscontract), [transfer tokens](./actions), and much more.

    You can also use third-party libraries, though some might not work due to the limitations of the contract runtime.
  </Block>

  <Block highlights="{&#x22;js&#x22;: &#x22;5-22&#x22;, &#x22;rust&#x22;:&#x22;5-7,20-31&#x22;, &#x22;python&#x22;: &#x22;5-19&#x22;,&#x22;go&#x22;:&#x22;7-27&#x22;}" fname="hello-near">
    ### Contract's Main Structure

    The contract is described through a structure:

    * The attributes define which data the contract stores
    * The functions define its public (and private) interface
  </Block>

  <Block highlights="{&#x22;go&#x22;: &#x22;7,12,18,23&#x22;}" fname="hello-near">
    ### Comment Directives

    Unlike languages with built-in decorators, the Near Go SDK uses **Comment Directives** to control how your code is compiled into a smart contract.

    The `near-go` CLI scans your comments for `@contract:` tags — such as `@contract:state`, `@contract:init`, `@contract:view`, `@contract:mutating`, `@contract:payable`, and `@contract:promise_callback` — and generates the contract bindings automatically.

    **Note:** Methods exported to WASM are automatically converted to `snake_case` (e.g., `SetGreeting` becomes `set_greeting`).
  </Block>

  <Block highlights="{&#x22;js&#x22;: &#x22;3&#x22;}" fname="hello-near">
    ### Contract Class Decorator

    Note that the contract's class is decorated with `@NearBindgen`. This decorator tells the SDK which class defines the contract, so it knows:

    1. What to fetch from storage when the contract is loaded
    2. What to store when the contract is done executing
    3. The methods that are exposed to the outside world
    4. If the contract needs to be initialized (we will cover this later)

    **Note:** Only one class can be decorated with the `@NearBindgen` decorator
  </Block>

  <Block highlights="{&#x22;python&#x22;: &#x22;4&#x22;}" fname="hello-near">
    ### Python Class Structure

    In Python, we use a class to define our contract. Unlike JavaScript or Rust, there isn't a specific decorator for the class itself. Instead, each method that should be exposed to the blockchain is decorated with the appropriate decorator (`@view`, `@call`, or `@init`).

    The contract's state is managed through instance variables and can be persisted using the Storage API or collections.
  </Block>

  <Block highlights="{&#x22;rust&#x22;: &#x22;4,19&#x22;}" fname="hello-near">
    ### Contract Struct Macro

    Note that the contract's struct definition and the implementation are decorated with macros

    The `#[near(contract_state)]` macro tells the SDK that this structure defines the contract's state, so it knows:

    1. What to fetch from storage when the contract is loaded
    2. What to store when the contract is done executing

    The `#[near]` macro tells the SDK which functions are exposed to the outside world.

    **Note:** Only one struct can be decorated with the `#[near(contract_state)]` macro.
  </Block>

  <Block highlights="{&#x22;rust&#x22;: &#x22;4,19&#x22;}" fname="hello-near" type="info">
    <Accordion title="Interaction with other macros">
      When `near` is built for the wasm32 target, it generates the external NEAR contract bindings.  To achieve this it is actually generating another function with the signature `pub extern "C" fn function_name()` that first deserializes the contract struct from NEAR storage and then calls the `contract.function_name(parameter1, parameter2, ...)`.

      If you have annotated your function with any attribute-like macros, these are then executed *twice*.  Specifically if the attribute like macro makes any modification to the function signature, or inserts any code that depends on the contract struct (in the form of `&self`, `&mut self`, or `self`) this will fail in the second invocation, because the externally exposed function does not have any concept of this struct.

      It is possible to detect this by checking which build target you are building for and limit the execution of the macro to operate only on the first pass.
    </Accordion>
  </Block>

  <Block highlights="{&#x22;js&#x22;: &#x22;5&#x22;, &#x22;rust&#x22;: &#x22;6,10-16&#x22;, &#x22;python&#x22;: &#x22;7-8&#x22;, &#x22;go&#x22;: &#x22;9,14,20,25&#x22;}" fname="hello-near">
    ### Storage (State)

    We call the data stored in the contract [the contract's state](./storage).

    In our Hello World example, the contract stores a single string (`greeting`), and the state starts initialized with the default value `"Hello"`.

    **Note:** We will cover more about the contract's state in the [state section](./storage).
  </Block>

  <Block highlights="{&#x22;js&#x22;: &#x22;7-9&#x22;}" fname="hello-near">
    Javascript contracts need to further include a `schema` object that defines the contract's state and its types. This object is used by the SDK to correctly serialize and deserialize the contract's state.
  </Block>

  <Block highlights="{&#x22;python&#x22;: &#x22;5-5,10-10,15-15&#x22;}" fname="hello-near">
    ### Method Decorators

    In Python, contract methods are decorated with `@view`, `@call`, or `@init` to define how they can be accessed.
    These decorators handle input parsing and serializing return values automatically.
  </Block>

  <Block highlights="{&#x22;js&#x22;: &#x22;12-14&#x22;, &#x22;rust&#x22;: &#x22;22-24&#x22;, &#x22;python&#x22;: &#x22;10-13&#x22;,&#x22;go&#x22;: &#x22;18-21&#x22;}" fname="hello-near">
    ### Read Only Functions

    Contract's functions can be read-only, meaning they don't modify the state. Calling them is free for everyone, and does not require to have a NEAR account.

    **Note:** We will cover more about function types in the [functions section](./functions).
  </Block>

  <Block highlights="{&#x22;js&#x22;: &#x22;17-20&#x22;, &#x22;rust&#x22;: &#x22;27-30&#x22;, &#x22;python&#x22;: &#x22;15-19&#x22;,&#x22;go&#x22;: &#x22;23-27&#x22;}" fname="hello-near">
    ### State Mutating Functions

    Functions that modify the state or call other contracts are considered state mutating functions. It is necessary to have a NEAR account to call them, as they require a transaction to be sent to the network.

    **Note:** We will cover more about function types in the [functions section](./functions).
  </Block>

  <File language="js" fname="hello-near" url="https://github.com/near-examples/hello-near-examples/blob/main/contract-ts/src/contract.ts" start="2" end="32" />

  <File language="rust" fname="hello-near" url="https://github.com/near-examples/hello-near-examples/blob/main/contract-rs/src/lib.rs" start="2" end="32" />

  <File language="python" fname="hello-near" url="https://github.com/r-near/near-py-examples/blob/main/hello-near.py" start="2" end="32" />

  <File language="go" fname="hello-near" url="https://github.com/Emir-Asanov/near-go-examples/blob/example-release-1/greeting/main.go" start="1" end="27" />
</ExplainCode>
