michaelhly / solana-py

Solana Python SDK
https://michaelhly.github.io/solana-py
MIT License
1.04k stars 273 forks source link

Simple Example Implementation for Token Transfer and Solana Transfer #351

Open SeveighTech-Management opened 1 year ago

SeveighTech-Management commented 1 year ago

To assist every newbie, here is a simple implementation to get you started with the basic aspects of the solana-py library

SeveighTech-Management commented 1 year ago

I know having this would definitely have saved me two days' worth of work.

moonlightnexus commented 1 year ago

Thanks @seveightech was looking for something like this

moonlightnexus commented 1 year ago

If you don't mind helping I'm stuck here with SignerError: keypair-pubkey mismatch and tried and checked almost everything IDK am I doing something wrong or why this issue persist

from solders.account import Account from solana.rpc.api import Client from solana.transaction import Transaction from spl.token.constants import TOKEN_PROGRAM_ID from spl.token.instructions import initialize_account, InitializeAccountParams from solders.pubkey import Pubkey from solana.blockhash import Hash kp = Keypair()

from solana.rpc.api import Client from solana.blockhash import Hash from solders.null_signer import NullSigner

recent_blockhash = client.get_latest_blockhash().value.blockhash

print(f"Most recent blockhash: {recent_blockhash}")

user2_public_key = kp.pubkey() spl_token_mint_address = Pubkey.from_string(Mint_Address["usdt"])

new_account = Account(lamports=100000, data=b"sys_user2", owner=kp.pubkey())

initialize_instruction = initialize_account(InitializeAccountParams( TOKEN_PROGRAM_ID, new_account.owner, spl_token_mint_address, user2_public_key,) )

transaction = Transaction(fee_payer=Main_wallet.pubkey(), recent_blockhash=recent_blockhash).add(initialize_instruction)

transaction.sign(Main_wallet, kp)

tx_signature = client.send_transaction(transaction)

print(f"Transaction signature: {tx_signature}")

daanrod commented 12 months ago

you know how to interact with Raydium?

SeveighTech-Management commented 11 months ago

you know how to interact with Raydium?

I have not tried doing that, no. @daanrod

SeveighTech-Management commented 11 months ago

If you don't mind helping I'm stuck here with SignerError: keypair-pubkey mismatch and tried and checked almost everything IDK am I doing something wrong or why this issue persist

from solders.account import Account from solana.rpc.api import Client from solana.transaction import Transaction from spl.token.constants import TOKEN_PROGRAM_ID from spl.token.instructions import initialize_account, InitializeAccountParams from solders.pubkey import Pubkey from solana.blockhash import Hash kp = Keypair()

from solana.rpc.api import Client from solana.blockhash import Hash from solders.null_signer import NullSigner

recent_blockhash = client.get_latest_blockhash().value.blockhash

print(f"Most recent blockhash: {recent_blockhash}")

user2_public_key = kp.pubkey() spl_token_mint_address = Pubkey.from_string(Mint_Address["usdt"])

new_account = Account(lamports=100000, data=b"sys_user2", owner=kp.pubkey())

initialize_instruction = initialize_account(InitializeAccountParams( TOKEN_PROGRAM_ID, new_account.owner, spl_token_mint_address, user2_public_key,) )

transaction = Transaction(fee_payer=Main_wallet.pubkey(), recent_blockhash=recent_blockhash).add(initialize_instruction)

transaction.sign(Main_wallet, kp)

tx_signature = client.send_transaction(transaction)

print(f"Transaction signature: {tx_signature}")

I'm not so sure what your problem is exactly, but looking at the code these are the things I noticed.

initialize_instruction = initialize_account(InitializeAccountParams(
TOKEN_PROGRAM_ID,
new_account.owner, #new_account_owner variable not set previously but being used
spl_token_mint_address,
user2_public_key,)
)

transaction = Transaction(fee_payer=Main_wallet.pubkey(), recent_blockhash=recent_blockhash).add(initialize_instruction) #Main_wallet was not set previously before use

Also, try using the Keypair in the Transaction() rather than the pubkey

If you have already found a solution to this, then that's fine.

@moonlightnexus

SeveighTech-Management commented 11 months ago

Follow up @moonlightnexus

You set kp = Keypair() Meaning it is not the Keypair of any wallet. It is just a class/function on its own, yet you are using it to sign a transaction: transaction.sign(Main_wallet, kp)

You need the actual Keypair value to sign a transaction. Something along the lines of Main_wallet.Keypair() (if i remember correctly.

SeveighTech-Management commented 11 months ago

Kindly check this @daanrod Possible way of interacting directly with Raydium