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

# Cách lấy token SPL trên Solana: Hướng dẫn API đầy đủ

> Truy xuất và truy vấn dữ liệu token SPL trên Solana bằng Helius: số dư, tài khoản token, tổng cung, người nắm giữ và tiện ích mở rộng token có thể thay thế, kèm theo các ví dụ mã.

## Tổng quan

Hướng dẫn này trình bày cách đọc token có thể thay thế trên Solana: số dư tài khoản, tài khoản token theo chủ sở hữu hoặc mint, tổng cung, những người nắm giữ nhiều nhất và tiện ích mở rộng token có thể thay thế của DAS. Helius cung cấp cả các phương thức token RPC tiêu chuẩn của Solana và các phương thức DAS bổ sung siêu dữ liệu cùng giá theo USD.

Đối với NFT, NFT nén, phiên bản và bằng chứng, hãy xem [hướng dẫn Lấy tài sản](/docs/vi/das/get-nfts). Trang này tập trung vào các token có thể thay thế (SPL và Token-2022).

## Khi nào nên sử dụng

Sử dụng các phương thức trên trang này khi cần:

* Đọc số dư của một tài khoản token
* Liệt kê mọi tài khoản token mà một ví nắm giữ
* Tìm tất cả tài khoản nắm giữ một mint cụ thể
* Kiểm tra tổng cung của token hoặc những người nắm giữ nhiều nhất
* Truy xuất siêu dữ liệu token và giá theo USD cùng với số dư

## Số dư tài khoản token

Lấy số dư của một tài khoản token cụ thể bằng RPC tiêu chuẩn:

```typescript theme={"system"}
const response = await fetch("https://mainnet.helius-rpc.com/?api-key=YOUR_API_KEY", {
  method: 'POST',
  headers: { 'Content-Type': 'application/json' },
  body: JSON.stringify({
    jsonrpc: '2.0',
    id: '1',
    method: 'getTokenAccountBalance',
    params: [
      '3emsAVdmGKERbHjmGfQ6oZ1e35dkf5iYcS6U4CPKFVaa'
    ]
  })
});
const data = await response.json();
console.log(data);
```

<Card title="API Reference" horizontal icon="code" href="/docs/vi/api-reference/rpc/http/gettokenaccountbalance">
  getTokenAccountBalance
</Card>

## Tài khoản token theo chủ sở hữu

Liệt kê tất cả tài khoản token thuộc sở hữu của một ví:

```typescript theme={"system"}
const response = await fetch("https://mainnet.helius-rpc.com/?api-key=YOUR_API_KEY", {
  method: 'POST',
  headers: { 'Content-Type': 'application/json' },
  body: JSON.stringify({
    jsonrpc: '2.0',
    id: '1',
    method: 'getTokenAccountsByOwner',
    params: [
      '86xCnPeV69n6t3DnyGvkKobf9FdN2H9oiVDdaMpo2MMY',
      {
        programId: 'TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA'
      },
      {
        encoding: 'jsonParsed'
      }
    ]
  })
});
const data = await response.json();
console.log(data);
```

<Card title="API Reference" horizontal icon="code" href="/docs/vi/api-reference/rpc/http/gettokenaccountsbyowner">
  getTokenAccountsByOwner
</Card>

## Tài khoản token theo mint

Liệt kê tất cả tài khoản nắm giữ một token cụ thể:

```typescript theme={"system"}
const response = await fetch("https://mainnet.helius-rpc.com/?api-key=YOUR_API_KEY", {
  method: 'POST',
  headers: { 'Content-Type': 'application/json' },
  body: JSON.stringify({
    jsonrpc: '2.0',
    id: '1',
    method: 'getTokenAccountsByOwner',
    params: [
      'CEXq1uy9y15PL2Wb4vDQwQfcJakBGjaAjeuR2nKLj8dk',
      {
        mint: "8wXtPeU6557ETkp9WHFY1n1EcU6NxDvbAggHGsMYiHsB"
      },
      {
        encoding: 'jsonParsed'
      }
    ]
  })
});
const data = await response.json();
console.log(data);
```

<Card title="API Reference" horizontal icon="code" href="/docs/vi/api-reference/rpc/http/gettokenaccountsbyowner">
  getTokenAccountsByOwner
</Card>

Để tìm mọi tài khoản nắm giữ một mint trên tất cả chủ sở hữu (không chỉ một chủ sở hữu), hãy sử dụng phương thức DAS `getTokenAccounts` được mô tả bên dưới.

## Tổng cung token

Kiểm tra tổng cung của một token:

```typescript theme={"system"}
const response = await fetch("https://mainnet.helius-rpc.com/?api-key=YOUR_API_KEY", {
  method: 'POST',
  headers: { 'Content-Type': 'application/json' },
  body: JSON.stringify({
    jsonrpc: '2.0',
    id: '1',
    method: 'getTokenSupply',
    params: [
      'EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v'
    ]
  })
});
const data = await response.json();
console.log(data);
```

<Card title="API Reference" horizontal icon="code" href="/docs/vi/api-reference/rpc/http/gettokensupply">
  getTokenSupply
</Card>

## Những người nắm giữ nhiều token nhất

Xác định các tài khoản nắm giữ nhiều token nhất:

```typescript theme={"system"}
const response = await fetch("https://mainnet.helius-rpc.com/?api-key=YOUR_API_KEY", {
  method: 'POST',
  headers: { 'Content-Type': 'application/json' },
  body: JSON.stringify({
    jsonrpc: '2.0',
    id: '1',
    method: 'getTokenLargestAccounts',
    params: [
      'he1iusmfkpAdwvxLNGV8Y1iSbj4rUy6yMhEA3fotn9A'
    ]
  })
});
const data = await response.json();
console.log(data);
```

<Card title="API Reference" horizontal icon="code" href="/docs/vi/api-reference/rpc/http/gettokenlargestaccounts">
  getTokenLargestAccounts
</Card>

Trả về tối đa 20 tài khoản lớn nhất của mint:

```json theme={"system"}
{
  "context": { "slot": 0 },
  "value": [
    { "address": "...", "amount": "1000000000000", "decimals": 9, "uiAmount": 1000.0, "uiAmountString": "1000" }
  ]
}
```

## Tài khoản token bằng DAS API

Phương thức DAS `getTokenAccounts` trả về các tài khoản token theo mint hoặc chủ sở hữu, bao gồm cả số dư, trong một lệnh gọi có phân trang duy nhất. Không giống `getTokenAccountsByOwner`, bạn có thể chỉ truy vấn theo `mint` để liệt kê mọi tài khoản nắm giữ một token trên tất cả chủ sở hữu.

```typescript theme={"system"}
const url = `https://mainnet.helius-rpc.com/?api-key=YOUR_API_KEY`;

const getTokenAccounts = async (params) => {
  const response = await fetch(url, {
    method: "POST",
    headers: {
      "Content-Type": "application/json",
    },
    body: JSON.stringify({
      jsonrpc: "2.0",
      id: "my-request-id",
      method: "getTokenAccounts",
      params: params,
    }),
  });

  const { result } = await response.json();
  return result;
};

// Example: Get all accounts holding a specific token
getTokenAccounts({
  mint: "EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v", // USDC
  page: 1,
  limit: 100
});
```

<Card title="API Reference" horizontal icon="code" href="/docs/vi/api-reference/das/gettokenaccounts">
  getTokenAccounts
</Card>

Kết quả được phân trang; mỗi mục bao gồm tài khoản, mint, chủ sở hữu và số dư:

```json theme={"system"}
{
  "total": 100,
  "limit": 100,
  "page": 1,
  "token_accounts": [
    { "address": "...", "mint": "EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v", "owner": "...", "amount": 12345678 }
  ]
}
```

## Siêu dữ liệu và giá token bằng DAS API

Để lấy siêu dữ liệu token và giá theo USD, hãy gọi phương thức DAS `getAsset` với `showFungible` được bật. Giá của các token đã xác minh được trả về trong `token_info.price_info`.

<Note>
  Dữ liệu giá từ `getAsset` được lưu vào bộ nhớ đệm trong tối đa 600 giây, vì vậy dữ liệu có thể cũ đến 10 phút.
</Note>

```typescript theme={"system"}
const response = await fetch("https://mainnet.helius-rpc.com/?api-key=YOUR_API_KEY", {
  method: 'POST',
  headers: { 'Content-Type': 'application/json' },
  body: JSON.stringify({
    jsonrpc: '2.0',
    id: '1',
    method: 'getAsset',
    params: {
      id: 'DezXAZ8z7PnrnRJjz3wXBoRgixCa6xjnB7YaB1pPB263', // Bonk
      options: {
        showFungible: true
      }
    }
  })
});
const { result } = await response.json();
console.log(result.token_info.price_info);
```

<Card title="API Reference" horizontal icon="code" href="/docs/vi/api-reference/das/getasset">
  getAsset
</Card>

Phản hồi trả về tổng cung, số chữ số thập phân và giá trong `token_info`:

```json theme={"system"}
{
  "token_info": {
    "symbol": "Bonk",
    "supply": 8881594973561640000,
    "decimals": 5,
    "token_program": "TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA",
    "price_info": {
      "price_per_token": 0.0000192271,
      "currency": "USDC"
    }
  }
}
```

### Tính vốn hóa thị trường

Nhân giá với tổng cung đã điều chỉnh theo số chữ số thập phân:

```typescript theme={"system"}
const { price_per_token } = result.token_info.price_info;
const { supply, decimals } = result.token_info;
const marketCap = (supply / Math.pow(10, decimals)) * price_per_token;
```

Để liệt kê mọi token có thể thay thế trong một ví (kèm theo số dư và giá) chỉ bằng một lệnh gọi, hãy sử dụng `getAssetsByOwner` hoặc `searchAssets` cùng với `tokenType: "fungible"`. Xem [tiện ích mở rộng token có thể thay thế](/docs/vi/das/fungible-token-extension) để biết `tokenType`, số dư, tiện ích mở rộng Token-2022 và dữ liệu giá xuất hiện như thế nào trong phản hồi.

## Các phương pháp hay nhất

* Sử dụng phân trang cho các phương thức trả về tập kết quả lớn. Xem [hướng dẫn Phân trang](/docs/vi/das/pagination).
* Ưu tiên các phương thức DAS (`getAsset`, `getAssetsByOwner`, `getTokenAccounts`) khi cần siêu dữ liệu hoặc giá theo USD; sử dụng các phương thức RPC tiêu chuẩn để lấy số dư và tổng cung thô trên chuỗi.
* Xử lý lỗi phù hợp bằng các khối try/catch và cơ chế thử lại.
* Lưu phản hồi vào bộ nhớ đệm khi thích hợp để giảm số lệnh gọi API.

## Các bước tiếp theo

<CardGroup cols={3}>
  <Card title="Fungible Token Extension" icon="coins" href="/docs/vi/das/fungible-token-extension">
    Cách DAS trả về token có thể thay thế, tiện ích mở rộng Token-2022 và giá.
  </Card>

  <Card title="Get Assets (NFTs)" icon="image" href="/docs/vi/das/get-nfts">
    Truy xuất NFT, NFT nén, phiên bản và bằng chứng.
  </Card>

  <Card title="DAS API reference" icon="code" href="/docs/vi/api-reference/das">
    Lược đồ đầy đủ cho mọi phương thức DAS.
  </Card>
</CardGroup>
