-
Notifications
You must be signed in to change notification settings - Fork 341
Description
`from anchorpy import Provider, Wallet
from solders.pubkey import Pubkey
from solders.keypair import Keypair
from solana.rpc.async_api import AsyncClient
from solana.transaction import Transaction
from spl.token.instructions import transfer_checked, get_associated_token_address, TransferCheckedParams
from spl.token.constants import TOKEN_PROGRAM_ID
import asyncio
Initialize Solana Client (Mainnet RPC)
async def main():
client = AsyncClient("https://api.mainnet-beta.solana.com")
# Load Sender's Wallet (Use a secure method to store your private key)
private_key = ""
sender = Keypair.from_base58_string(private_key)
sender_pubkey = sender.pubkey()
print(f"Sender Public Key: {sender_pubkey}")
# Receiver's Wallet Address
receiver_pubkey = Pubkey.from_string("")
# USDT Token Mint Address on Solana (Mainnet)
usdt_mint = Pubkey.from_string("Es9vMFrzaCERmJfrF4H2FYD4KCoNkY11McCe8BenwNYB")
# Associated Token Accounts for Sender and Receiver
sender_usdt_account = get_associated_token_address(sender_pubkey, usdt_mint)
receiver_usdt_account = get_associated_token_address(receiver_pubkey, usdt_mint)
print(f"Sender USDT Account: {sender_usdt_account}")
print(f"Receiver USDT Account: {receiver_usdt_account}")
# Function to get token balance safely
async def get_token_balance(account):
try:
balance_resp = await client.get_token_account_balance(account)
if hasattr(balance_resp, 'value') and balance_resp.value:
return int(balance_resp.value.amount)
else:
return 0
except Exception as e:
print(f"Error fetching balance for {account}: {e}")
return 0
# Fetch Balances
sender_balance = await get_token_balance(sender_usdt_account)
receiver_balance = await get_token_balance(receiver_usdt_account)
print(f"Sender USDT Balance: {sender_balance}")
print(f"Receiver USDT Balance: {receiver_balance}")
# Amount to Send (USDT has 6 decimals, so multiply by 10^6)
amount_usdt = 0.00045 # 1 USDT
amount = int(amount_usdt * 10**6) # Convert to smallest unit
print(f"Sending {amount_usdt} USDT ({amount} lamports)")
print(sender_balance)
# Ensure sender has enough balance
if sender_balance < amount:
print("Insufficient balance!")
await client.close()
return
# Create Transfer Instruction
instruction = transfer_checked(
TransferCheckedParams(
program_id=TOKEN_PROGRAM_ID,
source=sender_usdt_account,
mint=usdt_mint,
dest=receiver_usdt_account,
owner=sender_pubkey,
amount=amount,
decimals=6
)
)
# Create and Sign Transaction
transaction = Transaction()
transaction.add(instruction)
try:
# Send Transaction
result = await client.send_transaction(transaction, sender)
print("Transaction Successful! Transaction Signature:", result)
except Exception as e:
print("An error occurred:", e)
finally:
await client.close()
asyncio.run(main())
`
I Got this error
An error occurred: SendTransactionPreflightFailureMessage { message: "Transaction simulation failed: Error processing Instruction 0: invalid account data for instruction", data: RpcSimulateTransactionResult(RpcSimulateTransactionResult { err: Some(InstructionError(0, InvalidAccountData)), logs: Some(["Program TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA invoke [1]", "Program log: Instruction: TransferChecked", "Program log: Error: InvalidAccountData", "Program TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA consumed 2984 of 200000 compute units", "Program TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA failed: invalid account data for instruction"]), accounts: None, units_consumed: Some(2984), return_data: None, inner_instructions: None }) }
Please i need help fixing this