Skip to content

Commit

Permalink
Merge pull request #68 from ygarg25/main
Browse files Browse the repository at this point in the history
Added Metamask Signing Integration for Claim Agent & Revised Tests for Reward & Claim Agents
  • Loading branch information
LachsBagel authored Sep 25, 2024
2 parents 4d73d2e + 45a4898 commit f7cc561
Show file tree
Hide file tree
Showing 19 changed files with 407 additions and 130 deletions.
Empty file modified build_assets/macOS/postinstall.sh
100755 → 100644
Empty file.
Empty file modified build_assets/macOS/preinstall.sh
100755 → 100644
Empty file.
Empty file modified build_assets/macOS/preinstall_docker.sh
100755 → 100644
Empty file.
9 changes: 4 additions & 5 deletions submodules/benchmarks/claim_agent_benchmarks/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,12 @@ class Config:
URL = 'http://127.0.0.1:5000/'
HEADERS = {'Content-Type': 'application/json'}

# Test wallet addresses and receiver addresses
# Test wallet addresses
WALLET_ADDRESSES = [
{"wallet": "0x48d0EAc727A7e478f792F16527012452a000f2bd",
"receiver": "0x48d0EAc727A7e478f792F16527012452a000f2bd"}
{"wallet": "0x48d0EAc727A7e478f792F16527012452a000f2bd"}
]

PROMPTS = {
"claim_request": "I want to claim my MOR rewards from pool id 1",
"proceed": "proceed"
"claim_request": "I want to claim my MOR",
"proceed": "yes"
}
9 changes: 1 addition & 8 deletions submodules/benchmarks/claim_agent_benchmarks/helpers.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from submodules.benchmarks.claim_agent_benchmarks.config import Config
from adapters.claim_adapter import ClaimAdapter
from submodules.benchmarks.claim_agent_benchmarks.adapters.claim_adapter import ClaimAdapter

claim_adapter = ClaimAdapter(Config.URL, Config.HEADERS)

Expand All @@ -10,13 +10,6 @@ def request_claim(wallet_address):
}
return claim_adapter.ask_agent(payload)

def provide_receiver_address(wallet_address, receiver_address):
payload = {
"prompt": {"role": "user", "content": receiver_address},
"wallet_address": wallet_address
}
return claim_adapter.ask_agent(payload)

def confirm_transaction(wallet_address):
payload = {
"prompt": {"role": "user", "content": Config.PROMPTS["proceed"]},
Expand Down
46 changes: 0 additions & 46 deletions submodules/benchmarks/claim_agent_benchmarks/user_flow.py

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,7 @@ def __init__(self):
def name(self) -> str:
return "RewardCheckAdapter"

def get_reward(self, pool_id: int, wallet_address: str) -> float:
return get_current_user_reward(wallet_address, pool_id)
def get_rewards(self, wallet_address: str) -> dict:
pool_0_reward = get_current_user_reward(wallet_address, 0)
pool_1_reward = get_current_user_reward(wallet_address, 1)
return {"pool_0_reward": pool_0_reward, "pool_1_reward": pool_1_reward}
65 changes: 30 additions & 35 deletions submodules/benchmarks/reward_check_agent_benchmarks/benchmarks.py
Original file line number Diff line number Diff line change
@@ -1,47 +1,42 @@
from helpers import ask_claim_agent, get_current_user_reward, extract_reward_value_from_response
from submodules.benchmarks.reward_check_agent_benchmarks.config import test_cases, reward_check_prompts
import pytest
from submodules.benchmarks.claim_agent_benchmarks.helpers import request_claim, confirm_transaction
from submodules.benchmarks.claim_agent_benchmarks.config import Config


def run_reward_check_tests():
total_tests = len(test_cases)
passed_tests = 0
def test_claim_process():
for i, wallet_data in enumerate(Config.WALLET_ADDRESSES):
wallet_address = wallet_data["wallet"]

for i, test_case in enumerate(test_cases, 1):
pool_id = test_case["pool_id"]
wallet_address = test_case["wallet_address"]
print(f"Testing for wallet {wallet_address} (Test {i + 1})")

# Iterate over each prompt
for prompt_template in reward_check_prompts:
prompt = prompt_template.format(wallet_address, pool_id)
print("-" * 100)
print(f"Running test case {i}/{total_tests}: {prompt}")
# Step 1: Request to claim rewards
response = request_claim(wallet_address)

# Get the agent's response
agent_response = ask_claim_agent(prompt)
print(f"Agent response: {agent_response}")
# Check if the response contains the expected structure
assert 'role' in response
assert response['role'] == 'claim'
assert 'content' in response
assert isinstance(response['content'], dict)
assert 'content' in response['content']
assert 'transactions' in response['content']['content']
assert len(response['content']['content']['transactions']) > 0

# Extract the reward value from the agent's response
agent_reward_value = extract_reward_value_from_response(agent_response)
print(f"Agent Returned Reward Value: {agent_reward_value}")
transaction = response['content']['content']['transactions'][0]
assert 'pool' in transaction
assert 'transaction' in transaction

# Get the real reward value from the blockchain
blockchain_reward_value = float(get_current_user_reward(wallet_address, pool_id))
print(f"Blockchain Returned Reward Value: {blockchain_reward_value}")
tx_data = transaction['transaction']
assert all(key in tx_data for key in ['to', 'data', 'value', 'gas', 'chainId'])

# Compare the values with a tolerance of 10%
tolerance = 0.10
if abs(agent_reward_value - blockchain_reward_value) / blockchain_reward_value <= tolerance:
print(f"Test case {i} passed.")
passed_tests += 1
i += 1
print("-" * 100)
else:
print(f"Test case {i} failed. Agent returned {agent_reward_value}, expected {blockchain_reward_value}.")
print("-" * 100)
i += 1
# Additional specific checks
assert tx_data['to'] == '0x47176B2Af9885dC6C4575d4eFd63895f7Aaa4790', "Incorrect 'to' address"
assert tx_data['value'] == '1000000000000000', "Incorrect 'value'"
assert tx_data['chainId'] == '1', "Incorrect 'chainId'"

print(f"\n{passed_tests}/{total_tests} test cases passed.")
print(f"Step 1 passed for wallet {wallet_address}: Claim process triggered successfully")

print(f"All steps passed for wallet {wallet_address}")


if __name__ == "__main__":
run_reward_check_tests()
pytest.main()
11 changes: 5 additions & 6 deletions submodules/benchmarks/reward_check_agent_benchmarks/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,9 @@ class Config:
]

reward_check_prompts = [
"Check MOR rewards for wallet_address: {} in pool_id {}",
"Check the MOR rewards for address: {} in pool_id {}",
"Check my MOR Rewards for address: {} for pool_id {}",
"Check the MOR rewards accrued for wallet_address: {} in pool_id {}"
"Check the MOR rewards assigned to wallet_address: {} in pool_id {}",

"i want to check my mor rewards",
"check my mor rewards",
"check my rewards",
"please check my mor rewards",
"hi, check my mor rewards",
]
33 changes: 23 additions & 10 deletions submodules/benchmarks/reward_check_agent_benchmarks/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,25 +9,27 @@
'Content-Type': 'application/json',
}

def ask_claim_agent(prompt: str):

def ask_claim_agent(prompt: str, wallet_address: str):
payload = {
"prompt": {
"role": "user",
"content": prompt
}
},
"wallet_address": wallet_address # Adding the wallet address in the payload
}

response = requests.post(url, headers=headers, json=payload)
if response.status_code == 200:
return response.json()['content']
else:
raise Exception(f"Request failed with status code {response.status_code}: {response.text}")


def get_current_user_reward(wallet_address, pool_id):
web3 = Web3(Web3.HTTPProvider(Config.WEB3RPCURL["1"]))
distribution_contract = web3.eth.contract(
address=web3.to_checksum_address(Config.DISTRIBUTION_PROXY_ADDRESS),
abi=Config.DISTRIBUTION_ABI
address=web3.to_checksum_address(Config.DISTRIBUTION_PROXY_ADDRESS),
abi=Config.DISTRIBUTION_ABI
)

try:
Expand All @@ -43,8 +45,19 @@ def get_current_user_reward(wallet_address, pool_id):
except Exception as e:
raise Exception(f"Error occurred while fetching the reward: {str(e)}")

def extract_reward_value_from_response(response: str) -> float:
match = re.search(r'(\d+\.\d+) MOR', response)
if match:
return float(match.group(1))
raise ValueError("Could not extract a reward value from the agent's response")

def extract_reward_value_from_response(response: str) -> dict:
# Regex to extract rewards for both pools; adjusted to be more flexible
matches = re.findall(
r'Capital Providers Pool \(Pool 0\):\s*([\d.]+)\s*MOR.*?Code Providers Pool \(Pool 1\):\s*([\d.]+)\s*MOR',
response,
re.DOTALL
)

rewards = {}
if matches:
# Assuming the first match group corresponds to pool 0 and the second to pool 1
rewards["pool_0_reward"] = float(matches[0][0])
rewards["pool_1_reward"] = float(matches[0][1])

return rewards
8 changes: 8 additions & 0 deletions submodules/moragents_dockers/agents/src/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,14 @@ def set_x_api_key():
logger.info("Received set X API key request")
return delegator.delegate_route("tweet sizzler agent", request, "set_x_api_key")

@app.route("/claim", methods=["POST"])
def claim_agent_claim():
logger.info("Received claim request")
return delegator.delegate_route("claim agent", request, "claim")

@app.route("/claim_status", methods=["POST"])
def update_claim_status():
return claim_agent.claim_status(request)

if __name__ == "__main__":
app.run(host="0.0.0.0", port=5000, debug=True)
60 changes: 58 additions & 2 deletions submodules/moragents_dockers/agents/src/claim_agent/src/agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,15 @@ def prepare_transactions(self, wallet_address):
return f"Error preparing transaction for pool {pool_id}: {str(e)}", "assistant", None

self.conversation_state[wallet_address]["transactions"] = transactions
tx_data_str = json.dumps(transactions, indent=2)
return f"Transaction data prepared for signing:\n\n{tx_data_str}", "assistant", None

# Return a structured response
return {
"role": "claim",
"content": {
"transactions": transactions,
"claim_tx_cb": "/claim"
}
}, "claim", None

def chat(self, request):
try:
Expand All @@ -68,3 +75,52 @@ def chat(self, request):
return {"error": "Missing required parameters"}, 400
except Exception as e:
return {"Error": str(e)}, 500


def claim(self, request):
try:
data = request.get_json()
wallet_address = data['wallet_address']
transactions = self.conversation_state[wallet_address]["transactions"]
return jsonify({"transactions": transactions})
except Exception as e:
return jsonify({"error": str(e)}), 500

def claim_status(self, request):
try:
data = request.get_json()
wallet_address = data.get('wallet_address')
transaction_hash = data.get('transaction_hash')
status = data.get('status')

if not all([wallet_address, transaction_hash, status]):
return jsonify({"error": "Missing required parameters"}), 400

# Generate and return the status message
response = self.get_status(status, transaction_hash, "claim")
return jsonify(response), 200
except Exception as e:
return jsonify({"error": str(e)}), 500


def get_status(self, flag, tx_hash, tx_type):
response = ''

if flag == "cancelled":
response = f"The claim transaction has been cancelled."
elif flag == "success":
response = f"The claim transaction was successful."
elif flag == "failed":
response = f"The claim transaction has failed."
elif flag == "initiated":
response = f"Claim transaction has been sent, please wait for it to be confirmed."

if tx_hash:
response = response + f" The transaction hash is {tx_hash}. " \
f"Here's the link to the Etherscan transaction: " \
f"https://etherscan.io/tx/{tx_hash}"

if flag != "initiated":
response = response + " Is there anything else I can help you with?"

return {"role": "assistant", "content": response}
14 changes: 7 additions & 7 deletions submodules/moragents_dockers/agents/src/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,13 @@ class Config:
"name": "tweet sizzler agent",
"upload_required": False,
},
# {
# "path": "claim_agent.src.agent",
# "class": "ClaimAgent",
# "description": "Manages the process of claiming rewards or tokens, specifically MOR rewards. Use when the query explicitly mentions claiming rewards or tokens.",
# "name": "claim agent",
# "upload_required": False,
# },
{
"path": "claim_agent.src.agent",
"class": "ClaimAgent",
"description": "Manages the process of claiming rewards or tokens, specifically MOR rewards. Use when the query explicitly mentions claiming rewards or tokens.",
"name": "claim agent",
"upload_required": False,
},
{
"path": "reward_agent.src.agent",
"class": "RewardAgent",
Expand Down
Loading

0 comments on commit f7cc561

Please sign in to comment.