michaelhly / solana-py

Solana Python SDK
https://michaelhly.github.io/solana-py
MIT License
980 stars 252 forks source link

Cannot find reference 'keypair' in 'init.py' #348

Closed Nirco99 closed 1 year ago

Nirco99 commented 1 year ago

Hello, I want to check if a wallet contain an NFT by its address, but when I try to import keypair module I get an error that it does not exist.

import os
import json
from solana.rpc.api import Client
from solana.keypair import Keypair
from nft_addresses import nft_addresses

# Set up Solana RPC endpoint
rpc_endpoint = 'https://api.mainnet-beta.solana.com'
client = Client(rpc_endpoint)

# Set up Solana wallet keypair
wallet_keypair = Keypair.from_secret_key(bytes.fromhex(os.environ.get('WALLET_SECRET_KEY')))

# Define Discord bot command
async def check_nft(ctx):
    # Get user's Solana wallet address
    user_address = 'user-address-here'

    # Check if user holds one of the NFTs
    for nft_address in nft_addresses:
        token_account_info = await client.get_account_info(nft_address)
        token_data = json.loads(token_account_info['data'][0])
        for account in token_data['accountList']:
            if account['account'] == user_address and account['amount'] > 0:
                await ctx.send('You hold one of the NFTs in our collection!')
                return

    await ctx.send('Sorry, you do not hold any NFTs in our collection.')

# Add Discord bot command to client
client.add_command('check_nft', check_nft)

# Connect bot to Discord server
client.run(os.environ.get('DISCORD_BOT_TOKEN'))

The error that I get is

Details

Cannot find reference 'keypair' in 'init.py'

kevinheavey commented 1 year ago

Keypair was moved to the Solders library in January: https://github.com/michaelhly/solana-py/blob/master/CHANGELOG.md#0290---2023-01-13

So now you'll want to do from solders.keypair import Keypair