最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

python - How to view data and preform actions on a Solana Program with an IDL? - Stack Overflow

programmeradmin0浏览0评论

I've create a python script to output the raffles from the Solana NFT raffle site /

import asyncio
import json
from anchorpy import Provider, Program, Idl
from solana.rpc.async_api import AsyncClient
from solders.pubkey import Pubkey

async def main():
    # Connect to the cluster
    client = AsyncClient("") # RPC endpoint here
    provider = Provider(client, None)

    # Load the IDL from your file
    with open("SolanaIDL.json", "r") as f:
        idl_json_str = f.read()
    idl = Idl.from_json(idl_json_str)

    # Use the program ID from your IDL
    program_id = Pubkey.from_string("9ehXDD5bnhSpFVRf99veikjgq8VajtRH7e3D9aVPLqYd")
    program = Program(idl, program_id, provider)

    try:
        print(f"Program ID: {program_id}")
        print(f"Program name from IDL: {idl.name}")
        
        # Fetch all accounts of type "raffle"
        print("Attempting to fetch raffle accounts...")
        raffle_accounts = await program.account["raffle"].all()
        
        if not raffle_accounts:
            print("No raffle accounts found.")
            print("Debug info:")
            print(f"- Network: {client._provider.endpoint_uri}")
            print(f"- Program ID: {program_id}")

            
            # Add connection test
            print("\nTesting connection...")
            slot = await client.get_slot()
            print(f"Current slot: {slot}")
        else:
            print(f"Found {len(raffle_accounts)} raffle accounts")
            print(raffle_accounts)
    except Exception as e:
        print(f"Error fetching raffle data: {e}")
    finally:
        await client.close()

if __name__ == "__main__":
    asyncio.run(main())

I expected it to output a list of the raffle_accounts, but the list was empty so it outputted the following:

Program ID: 9ehXDD5bnhSpFVRf99veikjgq8VajtRH7e3D9aVPLqYd
Program name from IDL: rafffle
Attempting to fetch raffle accounts...
No raffle accounts found.
Debug info:
- Network: 
- Program ID: 9ehXDD5bnhSpFVRf99veikjgq8VajtRH7e3D9aVPLqYd
Testing connection...
Current slot: GetSlotResp(317701589)

My question is, why is the raffle_accounts an empty list and not full of the actual raffle's info?

I made sure the program ID and IDL where both correct, it really should be outputting the raffle_accounts. What am I over looking?

Here is the IDL for the program:

发布评论

评论列表(0)

  1. 暂无评论