Issue fetching token balance from connected wallet using Solana, Jupiter Aggregator, and Jupiter APIs

I’m having trouble fetching the balance of a specific token from the connected wallet when swapping tokens using Solana, Jupiter Aggregator, and Jupiter APIs. I’ve implemented a server-side API to handle the swap process, which includes fetching quotes, generating swap instructions, and transferring platform fees using Jupiter APIs. However, I’m encountering an issue where the balance from the connected wallet is not being fetched correctly.

I’m using the getTokenAccountBalance and getAssociatedTokenAddress methods from the @solana/web3.js library to fetch the balance. Here’s the relevant code snippet from my server.js file:

app.get("/fetchBalance", async (req, res) => {
  const { address, walletAddress } = req.query;
  try {
    const tokenPublicKey = new PublicKey(address);
    const walletPublicKey = new PublicKey(walletAddress);

    const balance = await connection.getTokenAccountBalance(
      await connection.getAssociatedTokenAddress(tokenPublicKey, walletPublicKey)
    );

    return res.status(200).json({
      balance: balance.value.uiAmount,
      decimals: balance.value.decimals
    });
  } catch (error) {
    console.error("Error fetching token balance:", error);
    return res.status(500).json({ error: "Error fetching token balance" });
  }
});

And here’s the relevant code snippet from my swap.js file:

const fetchTokenBalance = useCallback(async (tokenAddress) => {
  if (!publicKey) return;

  try {
    const response = await axios.get("http://localhost:3001/fetchBalance", {
      params: {
        address: tokenAddress,
        walletAddress: publicKey.toString(),
      },
    });
    const balance = response.data.balance;
    setBalances(prevBalances => ({ ...prevBalances, [tokenAddress]: balance }));
    updateTokenBalances({ ...balances, [tokenAddress]: balance });
  } catch (error) {
    console.error("Error fetching token balance:", error);
    messageApi.error("Failed to fetch token balance");
  }
}, [publicKey, updateTokenBalances, balances, messageApi]);

I’m also using Jupiter APIs to fetch prices for the tokens being swapped. Here’s the relevant code snippet from my server.js file:

app.get("/tokenPrice", async (req, res) => {
  const { addressOne, addressTwo } = req.query;
  try {
    const response = await axios.get(`https://price.jup.ag/v6/price`, {
      params: { ids: addressOne, vsToken: addressTwo },
    });
    const priceData = response.data.data[addressOne];
    if (!priceData || !priceData.price) {
      throw new Error("Invalid response from price API");
    }
    const usdPrices = {
      tokenOne: priceData.price,
      tokenTwo: 1 / priceData.price,
      ratio: priceData.price,
    };
    return res.status(200).json(usdPrices);
  } catch (error) {
    console.error("Error fetching token prices:", error);
    return res.status(500).json({ error: "Error fetching token prices" });
  }
});

I’ve added some logging statements to the fetchTokenBalance function to see if it’s being called correctly and if the correct token address and wallet address are being passed to the getTokenAccountBalance and getAssociatedTokenAddress methods. However, I’m still not able to determine the root cause of the issue.

Can anyone help me understand what might be causing the issue with fetching the balance from the connected wallet, and how I can fix it? Additionally, is fetching the balance important for swapping tokens, and if so, how does it fit into the overall swap process?

i have tried everything hlep plz

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật