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

# Queries

> Read balances, token info, and smart contract state with the SDK

# Queries

All read operations use the `XYZClient` returned by `createClient()`. No wallet connection is needed for queries.

## Setup

```typescript theme={null}
import { createClient } from "@xyz-chain/sdk";

const client = await createClient({
  rpcEndpoint: "http://67.205.164.156:26657",
});
```

## Native Balance

```typescript theme={null}
import { getBalance, getAllBalances, formatXYZ } from "@xyz-chain/sdk";

// Get XYZ balance
const balance = await getBalance(client, "xyz1...");
console.log(balance.denom);              // "uxyz"
console.log(balance.amount);             // "1500000000" (raw)
console.log(formatXYZ(balance.amount));  // "1500" (human-readable)

// Get all token balances
const allBalances = await getAllBalances(client, "xyz1...");
for (const coin of allBalances) {
  console.log(coin.denom, coin.amount);
}
```

## CW20 Token Balance

```typescript theme={null}
import { getTokenBalance } from "@xyz-chain/sdk";

const balance = await getTokenBalance(
  client,
  "xyz1...contract",  // CW20 token contract address
  "xyz1...owner"       // Wallet address to check
);
console.log(balance);  // "5000000" (in micro-units)
```

## CW20 Token Info

```typescript theme={null}
import { getTokenInfo, getFormattedTokenInfo } from "@xyz-chain/sdk";

// Raw token info
const info = await getTokenInfo(client, "xyz1...contract");
console.log(info.name);          // "My Token"
console.log(info.symbol);        // "MTK"
console.log(info.decimals);      // 6
console.log(info.total_supply);  // "100000000000000"

// Formatted with human-readable values
const formatted = await getFormattedTokenInfo(client, "xyz1...contract");
console.log(formatted);  // includes human-readable supply, etc.
```

## Token Marketing Info

```typescript theme={null}
import { getTokenMarketingInfo } from "@xyz-chain/sdk";

const marketing = await getTokenMarketingInfo(client, "xyz1...contract");
if (marketing) {
  console.log(marketing.project);      // Project name
  console.log(marketing.description);  // Token description
  console.log(marketing.logo);         // Logo URL or embedded data
}
```

## Generic Contract Queries

Query any CosmWasm contract with arbitrary messages:

```typescript theme={null}
import { queryContract } from "@xyz-chain/sdk";

// Query a launchpad contract
const curve = await queryContract(client, launchpadAddress, {
  curve: { token_address: "xyz1...token" },
});
console.log(curve.current_price);
console.log(curve.tokens_sold);

// Query an AMM contract
const pool = await queryContract(client, ammAddress, {
  pool: { token_address: "xyz1...token" },
});
console.log(pool.xyz_reserve);
console.log(pool.price);
```

## Contract Info

```typescript theme={null}
import { getContractInfo } from "@xyz-chain/sdk";

const info = await getContractInfo(client, "xyz1...contract");
console.log(info.codeId);    // Code ID the contract was instantiated from
console.log(info.creator);   // Address that deployed the contract
console.log(info.admin);     // Admin address (if set)
console.log(info.label);     // Human-readable label
```

## Chain Info

```typescript theme={null}
// These methods are on the client directly
const chainId = await client.getChainId();  // "xyz-1"
const height = await client.getHeight();    // Current block height
```

## CW20 Allowance

```typescript theme={null}
import { getCW20Allowance } from "@xyz-chain/sdk";

const allowance = await getCW20Allowance(
  client,
  "xyz1...token",    // CW20 contract
  "xyz1...owner",    // Token owner
  "xyz1...spender"   // Authorized spender
);
console.log(allowance.allowance);  // "1000000"
console.log(allowance.expires);    // Expiration info
```

## Next Steps

<CardGroup cols={2}>
  <Card title="Transactions" icon="paper-plane" href="/sdk/transactions">
    Send tokens and execute contracts
  </Card>

  <Card title="Wallets" icon="wallet" href="/sdk/wallets">
    Connect Keplr, Leap, or XYZ Wallet
  </Card>
</CardGroup>
