Junaidb's picture
Update components/sendpayment.py
4cef2ce verified
#import base58
from solders.keypair import Keypair
from solders.pubkey import Pubkey
from .systemaccount import system
from solders.system_program import transfer, TransferParams
from solders.transaction import VersionedTransaction
from solders.message import MessageV0
from solana.rpc.async_api import AsyncClient
async def SendSol(recipient_address):
try:
fixed_lamports=35_000_000
RPC_URL="https://solana-devnet.g.alchemy.com/v2/NTjpYbT0tuqX-NN8PJzXQrgxJ1SSBtXg"
#keypair = Keypair.from_bytes(bytes(system["secret_bytes"]))
priv = bytes(system["secret_bytes"]) # 32 bytes
#Convert your public key string to bytes
pub = bytes(Pubkey.from_string(system["public"])) # 32 bytes
#Concatenate to make a 64-byte sequence
secret_keypair_bytes = priv + pub
#Construct the signer keypair
keypair = Keypair.from_bytes(secret_keypair_bytes)
client = AsyncClient(RPC_URL)
recipient_pubkey = Pubkey.from_string(recipient_address)
blockhash_resp = await client.get_latest_blockhash()
latest_blockhash = blockhash_resp.value.blockhash
transfer_instruction = transfer(
TransferParams(
from_pubkey=keypair.pubkey(),
to_pubkey=recipient_pubkey,
lamports=fixed_lamports
)
)
message = MessageV0.try_compile(
payer=keypair.pubkey(),
instructions=[transfer_instruction],
address_lookup_table_accounts=[],
recent_blockhash=latest_blockhash
)
transaction = VersionedTransaction(message, [keypair])
response = await client.send_transaction(transaction)
signature = response.value
await client.close()
#return signature
print("look down here guys ")
print(signature)
return {"status":True,"data":str(signature)}
except Exception as e:
print(e)
return {"status":False,"data":str(e)}