Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simple Example Implementation for Token Transfer and Solana Transfer #351

Open
seveightech opened this issue May 2, 2023 · 8 comments
Open

Comments

@seveightech
Copy link

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

@seveightech
Copy link
Author

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

@moonlightnexus
Copy link

Thanks @seveightech was looking for something like this

@moonlightnexus
Copy link

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
Copy link

daanrod commented Nov 13, 2023

you know how to interact with Raydium?

@seveightech
Copy link
Author

seveightech commented Nov 22, 2023

you know how to interact with Raydium?

I have not tried doing that, no. @daanrod

@seveightech
Copy link
Author

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
Copy link
Author

seveightech commented Nov 22, 2023

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
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants