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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from groq import Groq
|
| 2 |
+
import json
|
| 3 |
+
from solders.keypair import Keypair
|
| 4 |
+
from solana.rpc.async_api import AsyncClient
|
| 5 |
+
from .mongodbconnection import provideClient
|
| 6 |
+
|
| 7 |
+
|
| 8 |
+
client=provideClient()
|
| 9 |
+
db=client["agentpaymentdb"]
|
| 10 |
+
coll=db["agentpaymentcol"]
|
| 11 |
+
|
| 12 |
+
rpc=AsyncClient("https://mainnet.helius-rpc.com/?api-key=4e833ada-d32c-48c5-b020-c11b2253f25b")
|
| 13 |
+
|
| 14 |
+
|
| 15 |
+
def create_keypair(agent):
|
| 16 |
+
"""Create a new keypair and return it as MongoDB-friendly format"""
|
| 17 |
+
keypair = Keypair()
|
| 18 |
+
keypair_data = {
|
| 19 |
+
"agent_id": agent,
|
| 20 |
+
"public_key": str(keypair.pubkey())
|
| 21 |
+
"secret_key": bytes(keypair).hex()
|
| 22 |
+
}
|
| 23 |
+
coll.update_one(
|
| 24 |
+
{"owner": data.owner},
|
| 25 |
+
{"$push": {"agents": keypair_data}}
|
| 26 |
+
)
|
| 27 |
+
|
| 28 |
+
|
| 29 |
+
async def transfer_sol(from_keypair, to_pubkey, amount_sol):
|
| 30 |
+
"""Transfer SOL from one account to another"""
|
| 31 |
+
try:
|
| 32 |
+
lamports = int(amount_sol * 1e9)
|
| 33 |
+
|
| 34 |
+
transfer_ix = transfer(
|
| 35 |
+
TransferParams(
|
| 36 |
+
from_pubkey=from_keypair.pubkey(),
|
| 37 |
+
to_pubkey=to_pubkey,
|
| 38 |
+
lamports=lamports
|
| 39 |
+
))
|
| 40 |
+
|
| 41 |
+
recent_blockhash =rpc.get_latest_blockhash()
|
| 42 |
+
|
| 43 |
+
txn = Transaction(recent_blockhash=recent_blockhash.value.blockhash)
|
| 44 |
+
txn.add(transfer_ix)
|
| 45 |
+
txn.sign(from_keypair)
|
| 46 |
+
result = await rpc.client.send_transaction(txn, from_keypair)
|
| 47 |
+
print(f"✅ SOL Transfer successful!")
|
| 48 |
+
print(f"📝 Signature: {result.value}")
|
| 49 |
+
return True
|
| 50 |
+
|
| 51 |
+
except Exception as e:
|
| 52 |
+
print(f"❌ Transfer failed: {e}")
|
| 53 |
+
return False
|
| 54 |
+
|
| 55 |
+
|
| 56 |
+
def load_keypair(agent):
|
| 57 |
+
"""Load keypair from file"""
|
| 58 |
+
|
| 59 |
+
documents=coll.find_one({"owner":"system"})
|
| 60 |
+
keypair=None
|
| 61 |
+
|
| 62 |
+
for doc in documents:
|
| 63 |
+
if doc["agent_id"]==agent:
|
| 64 |
+
keypair=doc["secret_key"]
|
| 65 |
+
|
| 66 |
+
secret_key_bytes =bytes.fromhex(keypair)
|
| 67 |
+
keypair = Keypair.from_bytes(secret_key)
|
| 68 |
+
|
| 69 |
+
print(f"✅ Keypair loaded from {path}")
|
| 70 |
+
print(f"📍 Public Key: {keypair.pubkey()}")
|
| 71 |
+
|
| 72 |
+
return keypair
|
| 73 |
+
|
| 74 |
+
|
| 75 |
+
|
| 76 |
+
|
| 77 |
+
|
| 78 |
+
|
| 79 |
+
|
| 80 |
+
class VelocityPayableAgent():
|
| 81 |
+
|
| 82 |
+
def __init__(self,agent):
|
| 83 |
+
|
| 84 |
+
self.agent=agent
|
| 85 |
+
self.wallet=create_keypair(agent)
|
| 86 |
+
self.tools = [load_keypair_tool,transfer_sol_tool]
|
| 87 |
+
|
| 88 |
+
|
| 89 |
+
|
| 90 |
+
def execute_tool_call(self,tool_call):
|
| 91 |
+
"""Parse and execute a single tool call"""
|
| 92 |
+
function_name = tool_call.function.name
|
| 93 |
+
function_to_call = available_functions[function_name]
|
| 94 |
+
function_args = json.loads(tool_call.function.arguments)
|
| 95 |
+
return function_to_call(**function_args)
|
| 96 |
+
|
| 97 |
+
|
| 98 |
+
|
| 99 |
+
def Agent(self):
|
| 100 |
+
client = Groq(api_key="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")
|
| 101 |
+
available_functions= {
|
| 102 |
+
"load_keypair": load_keypair,
|
| 103 |
+
"transfer_sol":transfer_sol,
|
| 104 |
+
}
|
| 105 |
+
load_keypair_tool = {
|
| 106 |
+
"type": "function",
|
| 107 |
+
"function": {
|
| 108 |
+
"name": "load_keypair",
|
| 109 |
+
"description": "Load the Solana keypair for this agent from MongoDB.",
|
| 110 |
+
"parameters": {
|
| 111 |
+
"type": "object",
|
| 112 |
+
"properties": {
|
| 113 |
+
"agent":{
|
| 114 |
+
"type":"string",
|
| 115 |
+
"description":"agents id"
|
| 116 |
+
|
| 117 |
+
}
|
| 118 |
+
},
|
| 119 |
+
"required": ["agent"]
|
| 120 |
+
}
|
| 121 |
+
}
|
| 122 |
+
}
|
| 123 |
+
|
| 124 |
+
transfer_sol_tool = {
|
| 125 |
+
"type": "function",
|
| 126 |
+
"function": {
|
| 127 |
+
"name": "transfer_sol",
|
| 128 |
+
"description": "Transfer SOL from one account to other on Solana.",
|
| 129 |
+
"parameters": {
|
| 130 |
+
"type": "object",
|
| 131 |
+
"properties": {
|
| 132 |
+
"from_keypair": {
|
| 133 |
+
"type": "string",
|
| 134 |
+
"description": "secret key of the agent"
|
| 135 |
+
},
|
| 136 |
+
"to_pubkey": {
|
| 137 |
+
"type": "string",
|
| 138 |
+
"description": "Destination public key."
|
| 139 |
+
},
|
| 140 |
+
"amount_sol": {
|
| 141 |
+
"type": "number",
|
| 142 |
+
"description": "Amount of SOL to send."
|
| 143 |
+
}
|
| 144 |
+
},
|
| 145 |
+
"required": ["from_keypair", "to_pubkey", "amount_sol"]
|
| 146 |
+
}
|
| 147 |
+
}
|
| 148 |
+
}
|
| 149 |
+
|
| 150 |
+
messages=[
|
| 151 |
+
{"role":"system","content":"""
|
| 152 |
+
You are a tool-only agent.
|
| 153 |
+
Rules:
|
| 154 |
+
1. You MUST call a tool for every query from the user.
|
| 155 |
+
2. You are NOT allowed to answer directly.
|
| 156 |
+
3. If multiple tools exist, choose the one that is closest to the user's intent.
|
| 157 |
+
4. Always return a tool call in the correct function-calling JSON format.
|
| 158 |
+
5. When unsure, still pick a tool instead of answering directly.
|
| 159 |
+
6. Do not apologize, do not explain, do not output reasoning—only valid tool calls.
|
| 160 |
+
"""},
|
| 161 |
+
{"role": "user", "content": user_input}]
|
| 162 |
+
|
| 163 |
+
response = client.chat.completions.create(
|
| 164 |
+
model="llama-3.1-8b-instant",
|
| 165 |
+
messages=messages,
|
| 166 |
+
tools=self.tools,
|
| 167 |
+
)
|
| 168 |
+
messages.append(response.choices[0].message)
|
| 169 |
+
|
| 170 |
+
|
| 171 |
+
if response.choices[0].message.tool_calls:
|
| 172 |
+
for tool_call in response.choices[0].message.tool_calls:
|
| 173 |
+
function_response = execute_tool_call(tool_call)
|
| 174 |
+
messages.append({
|
| 175 |
+
"role": "tool",
|
| 176 |
+
"tool_call_id": tool_call.id,
|
| 177 |
+
"name": tool_call.function.name,
|
| 178 |
+
"content": str(function_response)
|
| 179 |
+
})
|
| 180 |
+
|
| 181 |
+
final = client.chat.completions.create(
|
| 182 |
+
model="llama-3.1-8b-instant",
|
| 183 |
+
messages=messages
|
| 184 |
+
)
|
| 185 |
+
print(final.choices[0].message.content)
|
| 186 |
+
return final.choices[0].message.content
|
| 187 |
+
|
| 188 |
+
|
| 189 |
+
|
| 190 |
+
|
| 191 |
+
|