# read tokens

Discover every token ever launched on Durianfun, plus their live state.

## Get the full list

```javascript
import { JsonRpcProvider, Contract } from "ethers";

const provider = new JsonRpcProvider("https://rpc.bitkubchain.io");

const FACTORY = "0xdf4f3dB298A9aDe853191F58b4b2a322D47EC005";
const FACTORY_ABI = [
  "function totalTokens() view returns (uint256)",
  "function getTokens(uint256 offset, uint256 limit) view returns (tuple(address token, address market, address creator, string name, string symbol, uint256 totalSupply, uint256 createdAt)[])",
];

const factory = new Contract(FACTORY, FACTORY_ABI, provider);

async function allTokens() {
  const total = Number(await factory.totalTokens());
  const tokens = await factory.getTokens(0, total);
  return tokens;
}
```

**Returns** (array of structs):

```javascript
[
  {
    token:       "0x...",       // the ERC-20 token contract
    market:      "0x...",       // its BondingCurveMarket
    creator:     "0x...",       // original launcher
    name:        "Durian King",
    symbol:      "DKING",
    totalSupply: 1_000_000_000_000_000_000_000_000_000n, // 1B * 1e18
    createdAt:   1712345678n,    // unix timestamp
  },
  // ...
]
```

## Pagination

For many tokens, paginate:

```javascript
const total = Number(await factory.totalTokens());
const pageSize = 50;

for (let offset = 0; offset < total; offset += pageSize) {
  const batch = await factory.getTokens(offset, pageSize);
  // process batch
}
```

`getTokens` returns newest-first (sorted by `createdAt` descending).

## Live market state for a token

```javascript
const MARKET_ABI = [
  "function kubRaised() view returns (uint256)",
  "function tokensSold() view returns (uint256)",
  "function graduated() view returns (bool)",
  "function ammPool() view returns (address)",
  "function currentPricePerToken() view returns (uint256)",
  "function graduationProgress() view returns (uint256)",
  "function totalFeeCollected() view returns (uint256)",
  "function getTokensOut(uint256 kubIn) view returns (uint256)",
  "function getKubOut(uint256 tokenIn) view returns (uint256)",
];

const market = new Contract(tokenInfo.market, MARKET_ABI, provider);

const [kubRaised, tokensSold, graduated, ammPool, price, progress] = await Promise.all([
  market.kubRaised(),
  market.tokensSold(),
  market.graduated(),
  market.ammPool(),
  market.currentPricePerToken(),
  market.graduationProgress(),
]);

console.log({
  kubRaised:  Number(kubRaised)  / 1e18,  // KUB
  price:      Number(price)      / 1e18,  // KUB per token
  progress:   Number(progress)   / 1e16,  // 0-100%
  graduated,
  ammPool, // 0x0 if not graduated yet
});
```

## Post-graduation AMM state

Once a token has graduated, its `ammPool` address is non-zero. Read AMM reserves:

```javascript
const AMM_ABI = [
  "function reserveKub() view returns (uint256)",
  "function reserveToken() view returns (uint256)",
  "function currentPrice() view returns (uint256)",
  "function initialized() view returns (bool)",
  "function isCooldownActive() view returns (bool)",
  "function cooldownBlocksLeft() view returns (uint256)",
  "function TREASURY_FEE_PPM() view returns (uint256)",
  "function LP_FEE_PPM() view returns (uint256)",
  "function getTokensOut(uint256 kubIn) view returns (uint256)",
  "function getKubOut(uint256 tokenIn) view returns (uint256)",
];

const amm = new Contract(ammPool, AMM_ABI, provider);

const [reserveKub, reserveToken, price] = await Promise.all([
  amm.reserveKub(),
  amm.reserveToken(),
  amm.currentPrice(),
]);
```

## Events

Subscribe (via WebSocket provider) or query historical logs:

```javascript
const TOKEN_CREATED = factory.filters.TokenCreated();
const logs = await factory.queryFilter(TOKEN_CREATED, fromBlock, toBlock);

logs.forEach(log => {
  const { token, market, creator, name, symbol } = log.args;
  console.log("new token:", symbol, token);
});
```

Key events (in `factory` and `market`):

| Contract | Event          | Purpose                         |
| -------- | -------------- | ------------------------------- |
| Factory  | `TokenCreated` | new token launch                |
| Market   | `TokensBought` | buy executed                    |
| Market   | `TokensSold`   | sell executed                   |
| Market   | `BuyRefunded`  | excess KUB refunded at grad cap |
| Market   | `Graduated`    | market graduated to AMM         |
| AMM      | `Initialized`  | AMM deployed + seeded           |
| AMM      | `Swapped`      | post-graduation swap            |

## Filtering by creator

To find all tokens by a specific creator wallet, scan `TokenCreated` events filtered by the `creator` indexed param:

```javascript
const creator = "0xYourWalletHere";
const filter = factory.filters.TokenCreated(null, null, creator);
const logs = await factory.queryFilter(filter);
```

## Get token image

```javascript
const TOKEN_ABI = [
  "function imageUrl() view returns (string)",
  "function creator() view returns (address)",
  "function market() view returns (address)",
  "function name() view returns (string)",
  "function symbol() view returns (string)",
  "function totalSupply() view returns (uint256)",
];

const token = new Contract(tokenAddress, TOKEN_ABI, provider);
const imageUrl = await token.imageUrl();

// imageUrl is one of:
//   "data:image/jpeg;base64,..."   (inline)
//   "https://..."                   (external)
//   "ipfs://Qm..."                  (IPFS)
```

Render directly in any `<img>` tag — all three formats work.


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://durianandfun.gitbook.io/durianfun/durian-launchpad/integration/read-tokens.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
