Spaces:
Sleeping
Sleeping
Update components/paymenthandling.py
Browse files
components/paymenthandling.py
CHANGED
|
@@ -1,20 +1,60 @@
|
|
| 1 |
from solders.keypair import Keypair
|
| 2 |
from solana.rpc.async_api import AsyncClient
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3 |
|
| 4 |
|
| 5 |
class PaymentHandler():
|
| 6 |
def __init__(self,agent):
|
| 7 |
self.agent=agent
|
|
|
|
| 8 |
|
| 9 |
|
| 10 |
-
def create_keypair(self
|
| 11 |
"""Create a new keypair and return it as MongoDB-friendly format"""
|
| 12 |
keypair = Keypair()
|
| 13 |
keypair_data = {
|
| 14 |
-
"agent_id":
|
| 15 |
"public_key": str(keypair.pubkey())
|
| 16 |
"secret_key": bytes(keypair).hex()
|
| 17 |
}
|
| 18 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
|
| 20 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
from solders.keypair import Keypair
|
| 2 |
from solana.rpc.async_api import AsyncClient
|
| 3 |
+
from .mongodbconnection import provideClient
|
| 4 |
+
|
| 5 |
+
|
| 6 |
+
|
| 7 |
+
client=provideClient()
|
| 8 |
+
db=client["agentpaymentdb"]
|
| 9 |
+
coll=db["agentpaymentcol"]
|
| 10 |
+
|
| 11 |
+
|
| 12 |
+
|
| 13 |
|
| 14 |
|
| 15 |
class PaymentHandler():
|
| 16 |
def __init__(self,agent):
|
| 17 |
self.agent=agent
|
| 18 |
+
self.rpc=AsyncClient("https://mainnet.helius-rpc.com/?api-key=4e833ada-d32c-48c5-b020-c11b2253f25b")
|
| 19 |
|
| 20 |
|
| 21 |
+
def create_keypair(self):
|
| 22 |
"""Create a new keypair and return it as MongoDB-friendly format"""
|
| 23 |
keypair = Keypair()
|
| 24 |
keypair_data = {
|
| 25 |
+
"agent_id": agent,
|
| 26 |
"public_key": str(keypair.pubkey())
|
| 27 |
"secret_key": bytes(keypair).hex()
|
| 28 |
}
|
| 29 |
+
coll.update_one(
|
| 30 |
+
{"owner": data.owner},
|
| 31 |
+
{"$push": {"agents": keypair_data}}
|
| 32 |
+
)
|
| 33 |
+
|
| 34 |
+
|
| 35 |
+
async def transfer_sol(self, from_keypair, to_pubkey, amount_sol):
|
| 36 |
+
"""Transfer SOL from one account to another"""
|
| 37 |
+
try:
|
| 38 |
+
lamports = int(amount_sol * 1e9)
|
| 39 |
+
|
| 40 |
+
transfer_ix = transfer(
|
| 41 |
+
TransferParams(
|
| 42 |
+
from_pubkey=from_keypair.pubkey(),
|
| 43 |
+
to_pubkey=to_pubkey,
|
| 44 |
+
lamports=lamports
|
| 45 |
+
))
|
| 46 |
|
| 47 |
+
recent_blockhash =self.rpc.get_latest_blockhash()
|
| 48 |
+
|
| 49 |
+
txn = Transaction(recent_blockhash=recent_blockhash.value.blockhash)
|
| 50 |
+
txn.add(transfer_ix)
|
| 51 |
+
txn.sign(from_keypair)
|
| 52 |
+
result = await self.client.send_transaction(txn, from_keypair)
|
| 53 |
+
print(f"✅ SOL Transfer successful!")
|
| 54 |
+
print(f"📝 Signature: {result.value}")
|
| 55 |
+
return True
|
| 56 |
+
|
| 57 |
+
except Exception as e:
|
| 58 |
+
print(f"❌ Transfer failed: {e}")
|
| 59 |
+
return False
|
| 60 |
+
|