-
Notifications
You must be signed in to change notification settings - Fork 7
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Sending messages from general agent #581
Changes from all commits
c1b2ac9
5fe2710
00e17f8
deac15f
b3c6dc3
d40864d
a1bd5a5
22ca08f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,19 @@ | ||
from prediction_market_agent_tooling.gtypes import xDai | ||
from prediction_market_agent_tooling.loggers import logger | ||
from prediction_market_agent_tooling.markets.omen.omen import OMEN_TINY_BET_AMOUNT | ||
|
||
from prediction_market_agent.utils import APIKeys | ||
|
||
|
||
class MicrochainAgentKeys(APIKeys): | ||
# Double check to make sure you want to actually post on public social media. | ||
ENABLE_SOCIAL_MEDIA: bool = False | ||
# Double check to not spend big money during testing. | ||
SENDING_XDAI_CAP: float | None = OMEN_TINY_BET_AMOUNT | ||
|
||
def cap_sending_xdai(self, amount: xDai) -> xDai: | ||
if self.SENDING_XDAI_CAP is None: | ||
return amount | ||
amount = xDai(min(amount, self.SENDING_XDAI_CAP)) | ||
logger.warning(f"Caping sending xDai value to {amount}.") | ||
return amount |
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -4,7 +4,9 @@ | |||||||||||||||||||||||||||||||||||||||||||||||||||
from prediction_market_agent_tooling.tools.web3_utils import send_xdai_to, xdai_to_wei | ||||||||||||||||||||||||||||||||||||||||||||||||||||
from web3 import Web3 | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
from prediction_market_agent.utils import APIKeys | ||||||||||||||||||||||||||||||||||||||||||||||||||||
from prediction_market_agent.agents.microchain_agent.microchain_agent_keys import ( | ||||||||||||||||||||||||||||||||||||||||||||||||||||
MicrochainAgentKeys, | ||||||||||||||||||||||||||||||||||||||||||||||||||||
) | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
class SendXDAI(Function): | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
@@ -21,14 +23,14 @@ def __call__( | |||||||||||||||||||||||||||||||||||||||||||||||||||
address: str, | ||||||||||||||||||||||||||||||||||||||||||||||||||||
amount: float, | ||||||||||||||||||||||||||||||||||||||||||||||||||||
) -> str: | ||||||||||||||||||||||||||||||||||||||||||||||||||||
keys = APIKeys() | ||||||||||||||||||||||||||||||||||||||||||||||||||||
keys = MicrochainAgentKeys() | ||||||||||||||||||||||||||||||||||||||||||||||||||||
web3 = ContractOnGnosisChain.get_web3() | ||||||||||||||||||||||||||||||||||||||||||||||||||||
address_checksum = Web3.to_checksum_address(address) | ||||||||||||||||||||||||||||||||||||||||||||||||||||
send_xdai_to( | ||||||||||||||||||||||||||||||||||||||||||||||||||||
web3, | ||||||||||||||||||||||||||||||||||||||||||||||||||||
keys.bet_from_private_key, | ||||||||||||||||||||||||||||||||||||||||||||||||||||
address_checksum, | ||||||||||||||||||||||||||||||||||||||||||||||||||||
xdai_to_wei(xdai_type(amount)), | ||||||||||||||||||||||||||||||||||||||||||||||||||||
xdai_to_wei(keys.cap_sending_xdai(xdai_type(amount))), | ||||||||||||||||||||||||||||||||||||||||||||||||||||
Comment on lines
+26
to
+33
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🛠️ Refactor suggestion Add error handling for transaction failures The Apply this diff to add error handling: +from prediction_market_agent_tooling.loggers import logger
def __call__(
self,
address: str,
amount: float,
) -> str:
keys = MicrochainAgentKeys()
web3 = ContractOnGnosisChain.get_web3()
address_checksum = Web3.to_checksum_address(address)
+ try:
send_xdai_to(
web3,
keys.bet_from_private_key,
address_checksum,
xdai_to_wei(keys.cap_sending_xdai(xdai_type(amount))),
)
- return f"Sent {amount} xDAI to {address_checksum}."
+ return f"Sent {amount} xDai to {address_checksum}."
+ except Exception as e:
+ logger.error(f"Failed to send xDai: {e}")
+ return "Failed to send xDai due to an error." 📝 Committable suggestion
Suggested change
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
) | ||||||||||||||||||||||||||||||||||||||||||||||||||||
return f"Sent {amount} xDAI to {address_checksum}." | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
@@ -1,4 +1,5 @@ | ||||||||||||||||||||||||||||||||||
import typing as t | ||||||||||||||||||||||||||||||||||
import zlib | ||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||
import pandas as pd | ||||||||||||||||||||||||||||||||||
from microchain import Agent | ||||||||||||||||||||||||||||||||||
|
@@ -140,3 +141,12 @@ def get_function_useage_from_history( | |||||||||||||||||||||||||||||||||
data={"Usage Count": list(function_useage.values())}, | ||||||||||||||||||||||||||||||||||
index=function_names, | ||||||||||||||||||||||||||||||||||
) | ||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||
def compress_message(message: str) -> bytes: | ||||||||||||||||||||||||||||||||||
"""Used to reduce size of the message before sending it to reduce gas costs.""" | ||||||||||||||||||||||||||||||||||
return zlib.compress(message.encode(), level=zlib.Z_BEST_COMPRESSION) | ||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||
def decompress_message(message: bytes) -> str: | ||||||||||||||||||||||||||||||||||
return zlib.decompress(message).decode() | ||||||||||||||||||||||||||||||||||
Comment on lines
+151
to
+152
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Add error handling for malformed compressed data The decompression function should handle potential errors from malformed input. Add error handling: def decompress_message(message: bytes) -> str:
- return zlib.decompress(message).decode()
+ """Decompresses a message previously compressed with compress_message.
+
+ Args:
+ message: The compressed message bytes
+ Returns:
+ Original string message
+ Raises:
+ ValueError: If the message is corrupted or not valid compressed data
+ """
+ try:
+ return zlib.decompress(message).decode()
+ except (zlib.error, UnicodeDecodeError) as e:
+ raise ValueError(f"Failed to decompress message: {e}") 📝 Committable suggestion
Suggested change
Comment on lines
+144
to
+152
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 💡 Codebase verification Compression functions are properly integrated but message decompression is missing in handlers
🔗 Analysis chainVerify compression usage in message handlers The compression utilities are well implemented, but we should verify their integration with message handlers. Let's examine the actual usage of these compression functions in the message handlers. 🏁 Scripts executedThe following scripts were executed for the analysis: Script: #!/bin/bash
# Search for usage of compression functions in message handlers
echo "Searching for compression function usage..."
rg -l "compress_message|decompress_message" --type py
# Look for potential message size constants or limits
echo "Checking for message size limits..."
rg -i "message.*size|size.*limit|max.*size" --type py
Length of output: 609 Script: #!/bin/bash
# Check the implementation in messages_functions.py
echo "Content of messages_functions.py:"
cat prediction_market_agent/agents/microchain_agent/messages_functions.py
# Check the test implementation
echo -e "\nContent of test_utils.py:"
cat tests/agents/microchain/test_utils.py
Length of output: 3109 |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
from prediction_market_agent.agents.microchain_agent.utils import ( | ||
compress_message, | ||
decompress_message, | ||
) | ||
|
||
|
||
def test_message_compression() -> None: | ||
message = "Hello!" | ||
encoded = compress_message(message) | ||
assert message == decompress_message(encoded) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🛠️ Refactor suggestion
Add error handling for transaction failures
The
__call__
method does not handle exceptions that may occur during thesend_xdai_to
function call. To enhance robustness, add error handling to manage potential errors such as transaction failures, invalid addresses, or insufficient funds.Apply this diff to add error handling:
📝 Committable suggestion