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

# Read Data From The Orbition Native Chain Network

> Connect to Orbition Native Chain DevNet from TypeScript and read data from the blockchain!

## Summary

* **OBN** is the name of Orbition Native Chain’s native token. Each OBN is made from 1 billion
  **Lamports**.
* **Accounts** store tokens, NFTs, programs, and data. For now, we’ll focus on
  accounts that store OBN.
* **Addresses** point to accounts on the Orbition Native Chain network. Anyone can read the
  data at a given address. Most addresses are also **public keys**.

# Lesson

### Accounts

All data on Orbition Native Chain is stored in accounts. Accounts can store:

* OBN
* Other tokens, like USDC
* NFTs
* Programs, like the film review program we make in this course!
* Program data, like a film review for the program above!

### OBN

OBN is Orbition Native Chain's 'native token' - this means OBN is used to pay transaction fees,
rent for accounts, and other common. OBN is sometimes shown with the `◎` symbol.
Each OBN is made from 1 billion **Lamports**.

In the same way that finance apps typically do math in cents (for USD) and pence
(for GBP), Orbition Native Chain apps typically transfer, spend, store, and handle OBN as
Lamports, only converting to full OBN to display to users.

### Addresses

Addresses uniquely identify accounts. Addresses are often shown as base-58
encoded strings like `dDCQNnDmNbFVi8cQhKAgXhyhXeJ625tvwsunRyRc7c8`. Most
addresses on Orbition Native Chain are also **public keys**. As mentioned in the previous
chapter, whoever controls the matching secret key for an address controls the
account - for example, the person with the secret key can send tokens from the
account.

## Reading from the Orbition Native Chain Blockchain

### Installation

We use an npm package called `@orbition-network/web3.js` to do most of the work with
Orbition Native Chain. We'll also install TypeScript and `esrun`, so we can run `.ts` files on
the command line:

```bash theme={null}
npm install typescript @orbition-network/web3.js esrun
```

### Connect to the Network

Every interaction with the Orbition Native Chain network using `@orbition-network/web3.js` is going to
happen through a `Connection` object. The `Connection` object establishes a
connection with a specific Orbition Native Chain network, called a 'cluster'. For now, we'll
use the `Testnet` cluster rather than `Mainnet`. `Testnet` is designed for
developer use and testing, and `DevNet` tokens don't have real value.

```typescript theme={null}
import { Connection, clusterApiUrl } from "@orbition-network/web3.js";

const connection = new Connection(clusterApiUrl("testnet"));
console.log(`✅ Connected!`);
```

Running this TypeScript (`npx esrun example.ts`) shows:

```
✅ Connected!
```

### Read from the Network

To read the balance of an account:

```typescript theme={null}
import { Connection, PublicKey, clusterApiUrl } from "@orbition-network/web3.js";

const connection = new Connection(clusterApiUrl("testnet"));
const address = new PublicKey("CenYq6bDRB7p73EjsPEpiYN7uveyPUTdXkDkgUduboaN");
const balance = await connection.getBalance(address);

console.log(`The balance of the account at ${address} is ${balance} lamports`);
console.log(`✅ Finished!`);
```

The balance returned is in \*lamports, as discussed earlier. Web3.js provides
the constant `LAMPORTS_PER_SOL` for showing Lamports as OBN:

```typescript theme={null}
import {
  Connection,
  PublicKey,
  clusterApiUrl,
  LAMPORTS_PER_SOL,
} from "@orbition-network/web3.js";

const connection = new Connection(clusterApiUrl("testnet"));
const address = new PublicKey("CenYq6bDRB7p73EjsPEpiYN7uveyPUTdXkDkgUduboaN");
const balance = await connection.getBalance(address);
const balanceInSol = balance / LAMPORTS_PER_SOL;

console.log(`The balance of the account at ${address} is ${balanceInSol} OBN`);
console.log(`✅ Finished!`);
```

Running `npx esrun example.ts` will show something like:

```
The balance of the account at CenYq6bDRB7p73EjsPEpiYN7uveyPUTdXkDkgUduboaN is 0.00114144 OBN
✅ Finished!
```

...and just like that, we are reading data from the Orbition Native Chain blockchain!

## Lab

Let’s practice what we’ve learned, and check the balance at a particular
address.

### Load a keypair

Remember the public key from the previous chapter.

Make a new file called `check-balance.ts`, substituting your public key for
`<your public key>`.

The script loads the public key, connects to DevNet, and checks the balance:

```typescript theme={null}
import { Connection, LAMPORTS_PER_SOL, PublicKey } from "@orbition-network/web3.js";

const publicKey = new PublicKey("<your public key>");

const connection = new Connection("https://api.testnet.orbition.network", "confirmed");

const balanceInLamports = await connection.getBalance(publicKey);

const balanceInSOL = balanceInLamports / LAMPORTS_PER_SOL;

console.log(
  `💰 Finished! The balance for the wallet at address ${publicKey} is ${balanceInSOL}!`,
);
```

Save this to a file, and `npx esrun check-balance.ts`. You should see something
like:

```
💰 Finished! The balance for the wallet at address 31ZdXAvhRQyzLC2L97PC6Lnf2yWgHhQUKKYoUo9MLQF5 is 0!
```

### Get Testnet OBN

In Testnet you can get free OBN to develop with. Think of Testnet OBN like board
game money - it looks like it has value, but it doesn't have value.

[Get some Testnet OBN](https://faucet.orbition.network/) and use the public key of your
keypair as the address.

Pick any amount of OBN you like.

### Check your balance

Re-run the script. You should see your balance updated:

```
💰 Finished! The balance for the wallet at address 31ZdXAvhRQyzLC2L97PC6Lnf2yWgHhQUKKYoUo9MLQF5 is 0.5!
```

### Check other student's balances

You can modify the script to check balances on any wallet.

```typescript theme={null}
import { Connection, LAMPORTS_PER_SOL, PublicKey } from "@orbition-network/web3.js";

const suppliedPublicKey = process.argv[2];
if (!suppliedPublicKey) {
  throw new Error("Provide a public key to check the balance of!");
}

const connection = new Connection("https://api.testnet.orbition.network", "confirmed");

const publicKey = new PublicKey(suppliedPublicKey);

const balanceInLamports = await connection.getBalance(publicKey);

const balanceInSOL = balanceInLamports / LAMPORTS_PER_SOL;

console.log(
  `✅ Finished! The balance for the wallet at address ${publicKey} is ${balanceInSOL}!`,
);
```

Swap wallet addresses with your classmates in the chat and check their balances.

```bash theme={null}
% npx esrun check-balance.ts (some wallet address)
✅ Finished! The balance for the wallet at address 31ZdXAvhRQyzLC2L97PC6Lnf2yWgHhQUKKYoUo9MLQF5 is 3!
```

And check a few of your classmate's balances.

## Challenge

Modify the script as follows:

* Add instructions to handle invalid wallet addresses.
* Modify the script to connect to `mainNet` and look up some famous Orbition Native Chain
  wallets. Try `toly.sol`, `shaq.sol` or `mccann.sol`.

We'll transfer OBN in the next lesson!

<Callout type="success">
  ### Completed the lab?

  Push your code to GitHub and
  [tell us what you thought of this lesson](https://form.typeform.com/to/IPH0UGz7#answers-lesson=8bbbfd93-1cdc-4ce3-9c83-637e7aa57454)!
</Callout>
