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

# TypeScript

> Read a position with viem.

Read `RiskLens` with viem. Do not send a transaction to learn an account's borrowing power.

Addresses: [Addresses](/resources/addresses). ABI: [`RiskLens.json`](/abis/RiskLens.json).

<CodeGroup>
  ```typescript theme={"system"}
  import { createPublicClient, http, parseAbi } from "viem"
  import { addresses, chainId } from "./addresses"

  const client = createPublicClient({
    chain: {
      id: chainId,
      name: "Robinhood Chain testnet",
      nativeCurrency: { name: "ETH", symbol: "ETH", decimals: 18 },
      rpcUrls: { default: { http: ["https://rpc.testnet.chain.robinhood.com"] } },
    },
    transport: http(),
  })

  const lensAbi = parseAbi([
    "function snapshot(address account) view returns ((uint256 health, uint256 maxBorrow, (uint16 bookId, address collateral, uint256 amount, uint256 amountUi, uint256 debt, uint256 liquidationPrice)[] books))",
  ])

  export async function readPosition(account: `0x${string}`) {
    return client.readContract({
      address: addresses.riskLens,
      abi: lensAbi,
      functionName: "snapshot",
      args: [account],
    })
  }
  ```

  ```tsx theme={"system"}
  "use client"
  import { useEffect, useState } from "react"
  import { readPosition } from "./read-position"

  export function PositionCard({ account }: { account: `0x${string}` }) {
    const [snap, setSnap] = useState<Awaited<ReturnType<typeof readPosition>>>()
    useEffect(() => {
      readPosition(account).then(setSnap)
    }, [account])
    if (!snap) return <p>Loading</p>
    return <p>Max borrow {snap.maxBorrow.toString()}</p>
  }
  ```

  ```solidity theme={"system"}
  interface IRiskLens {
      function maxBorrow(address account) external view returns (uint256);
  }
  ```
</CodeGroup>

`snapshot` is the call the app should cache. `health` is WAD. `1e18` is the line. `amountUi` uses `uiMultiplier` for display only.

Book ids follow creation order on testnet. Confirm on [Books and tiers](/concepts/books-and-tiers).

## What can go wrong

* RPC timeout. Retry reads. Do not retry writes from this page.
* `UnknownBook` if you later call `liquidationPrice` with a missing id.
* Mixing `amountUi` into risk math. Use raw `amount`.
* Assuming 18 decimals on mainnet USDG.

Next: [Read borrowing power](/developers/positions/borrowing-power), [Open a loan](/developers/positions/open-loan).

This getting-started page is the shortest path into the protocol from a script. The Stockline app uses the same `RiskLens` surface for borrowing power, the line, and per-book debt. Once this read works, every other guide is a write on top of it.

Install viem, copy the address snippet from Addresses, and load `RiskLens.json` from `/abis`. Do not construct ABIs from memory. Book ids on testnet follow `createBook` order. Confirm NVDA and SPY against Books and tiers before you hardcode an id.

`snapshot` returns health in WAD, max borrow in USDG wei, and an array of books the account is listed on. `amount` is the raw ERC-20 amount. `amountUi` applies `uiMultiplier` when the stock token exposes it. Display `amountUi`. Risk math must use `amount`.

Robinhood Chain testnet is chain id 46630. Mainnet, when the file exists, is listed on Addresses. Testnet USDG is 18 decimals. Do not assume the same on mainnet.
