---
id: types
title: SDK Types
hide_table_of_contents: true
---
import Tabs from '@theme/Tabs';
import TabItem from '@theme/TabItem';
import {CodeTabs, Language, Github} from "@site/src/components/codetabs"
import CodeBlock from '@theme/CodeBlock'

import {ExplainCode, Block, File} from '@site/src/components/CodeExplainer/code-explainer';

Lets discuss which types smart contracts use to input and output data, as well as how such data is stored and handled in the contract's code.

<ExplainCode languages={["js", "rust"]}>
  <Block highlights={{"js":"5,8,13"}} fname="hello-near">
    ### Native Types
    Smart contracts can receive, store and return data using JS native types:
    - `string`
    - `number`
    - `boolean`
    - `Array`
    - `Map`
    - `Object`
    - `BigInt`
  </Block>
  <Block highlights={{"rust":"6,13,22,27"}} fname="hello-near">
    ### Native Types
    Smart contracts can receive, store and return data using the following Rust types:
    - `string`
    - `i8-i32/u8-u32`
    - **`u64/128`**: It is preferable to use SDK types `U64` and `U128`
    - `bool`
    - `HashMap`
    - `Vector`
  </Block>
  <Block highlights={{"rust": "1,15,22,64"}} fname="auction">
    #### `U64/U128`
    Smart contracts can store `u64` and `u128`, but these types need to be converted to `string` for input/output

    To simplify development, the SDK provides the `U64` and `U128` types which are automatically casted to `u64/u128` when stored, and to `string` when used as input/output
  </Block>
  <Block highlights={{"js":"3-6", "rust": "6-9"}} fname="auction">
    ### Complex Objects
    Smart contracts can store and return complex objects

    **Note:** Objects will always be received and returned as JSON 
  </Block>
  <Block highlights={{"rust": "4"}} fname="auction">
    #### Serializers
    Objects that will be used as input or output need to be serializable to JSON, add the `#[near(serializer=json)]` macro

    Objects that will be stored in the contract's state need to be serializable to Borsh, add the `#[near(serializer=borsh)]` macro
  </Block>
  <Block highlights={{"js": "5,11,47"}} fname="auction">
    ### Handling Tokens
    `$NEAR` tokens are represented using `BigInt` in JS, and they are always represented in `yoctonear`

    **Note:** 1 NEAR = 10^24 yoctoNEAR
  </Block>
  <Block highlights={{"rust": ""}} fname="auction">
    ### Handling Tokens
    `$NEAR` tokens are handled through the `NearToken` struct, which exposes methods to represent the value in `yoctonear`, `milinear` and `near`

    **Note:** 1 NEAR = 10^24 yoctonear
  </Block>
  <Block highlights={{"js": "4", "rust": "7"}} fname="auction">
    ### Account
    The SDK exposes a special type to handle NEAR Accounts, which automatically checks if the account address is valid
  </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="18"
  />
  <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="js"
    fname="auction" 
    url="https://github.com/near-examples/auction-examples/blob/main/contract-ts/src/contract.ts"
    start="2"
    end="51"
  />
  <File
    language="rust"
    fname="auction"
    url="https://github.com/near-examples/auction-examples/blob/main/contract-rs/src/lib.rs"
    start="2"
    end="68"
  />
</ExplainCode>