---
# System prepended metadata

title: Untitled

---

fetch data


```javascript=
  // requestCounts: 14
  async getPoolByIndex(index) {
    // requestCounts: 1
    const poolContract = await this.getPoolContractByIndex(index);
    // requestCounts: 1
    const token0Contract = await this.getPoolTokenContract(0, poolContract);
    // requestCounts: 1
    const token1Contract = await this.getPoolTokenContract(1, poolContract);

    // requestCounts: 11
    const _pool = {
      id: randomID(6),
      contract: poolContract,
      token0: {
        contract: token0Contract,
      },
      token1: {
        contract: token1Contract,
      },
    };

    const pool = await this.getPoolDetail(_pool);
    // requestCounts: 4
    await this.getTokenByContract(token0Contract);
    // requestCounts: 4
    await this.getTokenByContract(token1Contract);

    return pool;
  }
```
```javascript=
  async getPoolContractByIndex(index) {
    const indexData = index.toString(16).padStart(64, "0");
    if (!this.factoryContract) {
      this.getFactoryContract();
    }
    const result = await this.getData(
      `allPairs(uint256)`,
      indexData,
      this.factoryContract
    );
    return `0x${result.slice(26, 66)}`;
  }

  async getFactoryContract() {
    const contract = await this.getData(`factory()`, null, this.routerContract);
    this.factoryContract = `0x${contract.slice(26, 66)}`;
    console.log(`this.factoryContract`, this.factoryContract);
  }

  async getPoolTokenContract(index, poolContract) {
    const result = await this.getData(`token${index}()`, null, poolContract);
    const token = `0x${result.slice(26, 66)}`;
    return token;
  }

```

```javascript=
 // requestCounts: 4
  async getPoolDetail(pool) {
    // requestCounts: 1
    const reserveData = await this.getData(
      `getReserves()`,
      null,
      pool.contract
    );
    const reserve = sliceData(reserveData.replace("0x", ""), 64);
    const poolBalanceOfToken0 = SafeMath.toCurrencyUint(
      SafeMath.toBn(reserve[0]),
      this.assetList[pool.token0.contract].decimals
    );
    const poolBalanceOfToken1 = SafeMath.toCurrencyUint(
      SafeMath.toBn(reserve[1]),
      this.assetList[pool.token1.contract].decimals
    );
    console.log(`reserve`, reserve);
    console.log(`poolBalanceOfToken0`, poolBalanceOfToken0);
    console.log(`poolBalanceOfToken1`, poolBalanceOfToken1);

    // requestCounts: 1
    const decimalsResult = await this.getData(
      `decimals()`,
      null,
      pool.contract
    );
    const decimals = parseInt(decimalsResult, 16);
    // requestCounts: 1
    const totalSupplyResult = await this.getData(
      `totalSupply()`,
      null,
      pool.contract
    );
    const totalSupply = SafeMath.toCurrencyUint(
      parseInt(totalSupplyResult, 16),
      decimals
    );
    const _pool = {
      ...pool,
      decimals,
      totalSupply,
      token0: {
        ...pool.token0,
        poolBalanceOfToken: poolBalanceOfToken0,
      },
      token1: {
        ...pool.token1,
        poolBalanceOfToken: poolBalanceOfToken1,
      },
    };
    // requestCounts: 1
    return await this.updatePools(_pool);
  }
```

```javascript=
  async getTokenByContract(tokenContract) {
    let symbol, decimals, totalSupply, name;

    let token = this.assetList[tokenContract];
    if (!token) {
      // requestCounts: 4
      try {
        const result = await this.lunar.getAsset({
          contract: tokenContract,
        });
        symbol = result.symbol;
        decimals = result.decimals;
        totalSupply = result.totalSupply;
        name = result.name;
      } catch (error) {
        const symbolResult = await this.getData(
          `symbol()`,
          null,
          tokenContract
        );
        symbol = hexToAscii(sliceData(symbolResult)[2]);
        const decimalsResult = await this.getData(
          `decimals()`,
          null,
          tokenContract
        );
        decimals = parseInt(decimalsResult, 16);
        const totalSupplyResult = await this.getData(
          `totalSupply()`,
          null,
          tokenContract
        );
        totalSupply = parseInt(totalSupplyResult, 16);
        const nameResult = await this.getData(`name()`, null, tokenContract);
        name = hexToAscii(sliceData(nameResult)[2]);
      }
      token = {
        id: randomID(6),
        contract: tokenContract,
        iconSrc: erc20,
        name,
        symbol,
        decimals,
        totalSupply,
      };
    }
    // requestCounts: 1
    return await this.updateAssets(token);
  }
```


```javascript=
  async updatePools(pool, [force = false]) {
    // TODO get pool price and volume
    let updatePool;
    if (
      SafeMath.gt(
        SafeMath.minus(Date.now(), this.lastSyncTime),
        this.syncInterval
      ) ||
      force
    )
      updatePool = {
        ...pool,
        liquidity: "--",
        yield: "--",
        volume: {
          value: `${(Math.random() * 10).toFixed(2)}m`,
          change: `${Math.random() * 1 > 0.5 ? "+" : "-"}${(
            Math.random() * 1
          ).toFixed(2)}`,
        },
        tvl: {
          value: `${(Math.random() * 10).toFixed(2)}m`,
          change: `${Math.random() * 1 > 0.5 ? "+" : "-"}${(
            Math.random() * 1
          ).toFixed(2)}`,
        },
        irr: "3",
        interest24: `${(Math.random() * 10).toFixed(2)}m`,
      };
    else updatePool = { ...pool };

    if (this.isConnected && this.connectedAccount) {
      updatePool = await this.getPoolBalanceOf(updatePool);
    }

    this.poolList[updatePool.contract] = updatePool;
    return updatePool;
  }

```

```javascript=
 async updateAssets(token, [force = false]) {
    // TODO get token price and volume
    let updateToken;
    if (
      SafeMath.gt(
        SafeMath.minus(Date.now(), this.lastSyncTime),
        this.syncInterval
      ) ||
      force
    )
      updateToken = {
        ...token,
        balance: "--",
        price: {
          value: `${(Math.random() * 100000).toFixed(2)}m`,
          change: `${Math.random() * 1 > 0.5 ? "+" : "-"}${(
            Math.random() * 1
          ).toFixed(2)}`,
        },
        volume: {
          value: `${(Math.random() * 10).toFixed(2)}m`,
          change: `${Math.random() * 1 > 0.5 ? "+" : "-"}${(
            Math.random() * 1
          ).toFixed(2)}`,
        },
      };
    else updateToken = { ...token };
    if (this.isConnected && this.connectedAccount) {
      updateToken = await this.getAssetBalanceOf(updateToken);
    }
    this.assetList[updateToken.contract] = updateToken;
    return updateToken;
  }
```

```javascript=
  // requestCounts: 6
  async addToken(contract) {
    // if (/^0x[a-fA-F0-9]{40}$/.test(contract)) return null;
    let token = this.assetList[contract];
    if (token) return token;
    token = await this.getTokenByContract(contract);
    if (this.isConnected && this.connectedAccount) {
      token = await this.getAssetBalanceOf(token);
    }
    // const
    return token;
  }

```


```javascript=
  async getSelectedPool(supportedPools, active, passive) {
    if (!active || !passive) return;
    const index = supportedPools.findIndex(
      (pool) =>
        (active.contract === pool.token0.contract ||
          active.contract === pool.token1.contract) &&
        (passive.contract === pool.token0.contract ||
          passive.contract === pool.token1.contract)
    );
    console.log(`getSelectedPool findIndex`, index);
    if (index === -1) {
      const poolContract = await this.getPoolContractByTokens(
        active.contract,
        passive.contract
      );
      console.log(`poolContract `, poolContract);

      if (SafeMath.gt(parseInt(poolContract, 16), "0")) {
        // requestCounts: 1
        const token0Contract = await this.getPoolTokenContract(0, poolContract);
        // requestCounts: 1
        const token1Contract = await this.getPoolTokenContract(1, poolContract);
        const pool = await this.getPoolDetail(
          poolContract,
          token0Contract,
          token1Contract
        );
        return pool;
      } else return null;
    }
    console.log(`supportedPools[index] `, supportedPools[index]);

    return supportedPools[index];
  }
```