Skip to content
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

Added value to event #19

Merged
merged 2 commits into from
Dec 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ Repository holding the contracts made by Gnosis Labs team.
| OmenThumbnailMapping | Manages IPFS hashes for market thumbnails on Omen 2.0 | [0xe0cf08311F03850497B0ed6A2cf067f1750C3eFc](https://gnosisscan.io/address/0xe0cf08311f03850497b0ed6a2cf067f1750c3efc#code) | [omen-thumbnailmapping](https://thegraph.com/studio/subgraph/omen-thumbnailmapping/) |
| OmenAgentResultMapping | Maps prediction results to markets on Omen 2.0 | [0xbe1F6944496923683ca849fc0cC93fD10523cB83](https://gnosisscan.io/address/0x260E1077dEA98e738324A6cEfB0EE9A272eD471a#code) | [omen-agentresultmapping](https://thegraph.com/studio/subgraph/omen-agentresultmapping/) |
| Agent NFT | Agent NFTs that control mechs for NFT game | [0x0D7C0Bd4169D090038c6F41CFd066958fe7619D0](https://gnosisscan.io/address/0x0D7C0Bd4169D090038c6F41CFd066958fe7619D0#code) | |
| Agent communication contract | Simple contract storing message queue for each agent | [0x68670EDDa41d26F25DAcd9fADE75ec6E6a104AC3](https://gnosisscan.io/address/0x68670EDDa41d26F25DAcd9fADE75ec6E6a104AC3#code) | |
| Agent communication contract | Simple contract storing message queue for each agent | [0x62872578920427ae24b2527697dAb90CD1F4CA45](https://gnosisscan.io/address/0x62872578920427ae24b2527697dAb90CD1F4CA45#code) | |

## Set up contracts development

Expand Down
17 changes: 7 additions & 10 deletions src/NFT/AgentCommunication.sol
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@ contract AgentCommunication is Ownable {
mapping(address => DoubleEndedStructQueue.Bytes32Deque) public queues;
uint256 public minimumValueForSendingMessageInWei;

event NewMessageSent(address indexed sender, address indexed agentAddress, bytes message);
event MessagePopped(address indexed agentAddress, bytes message);
event LogMessage(address indexed sender, address indexed agentAddress, bytes message, uint256 value);

constructor() Ownable(msg.sender) {
minimumValueForSendingMessageInWei = 10000000000000; // 0.00001 xDAI
Expand All @@ -30,13 +29,11 @@ contract AgentCommunication is Ownable {
return DoubleEndedStructQueue.length(queues[agentAddress]);
}

function sendMessage(address agentAddress, DoubleEndedStructQueue.MessageContainer memory message)
public
payable
mustPayMoreThanMinimum
{
DoubleEndedStructQueue.pushBack(queues[agentAddress], message);
emit NewMessageSent(msg.sender, agentAddress, message.message);
function sendMessage(address agentAddress, bytes memory message) public payable mustPayMoreThanMinimum {
DoubleEndedStructQueue.MessageContainer memory messageContainer =
DoubleEndedStructQueue.MessageContainer(msg.sender, agentAddress, message, msg.value);
DoubleEndedStructQueue.pushBack(queues[agentAddress], messageContainer);
emit LogMessage(msg.sender, agentAddress, messageContainer.message, msg.value);
}

function getAtIndex(address agentAddress, uint256 idx)
Expand All @@ -52,7 +49,7 @@ contract AgentCommunication is Ownable {
revert MessageNotSentByAgent();
}
DoubleEndedStructQueue.MessageContainer memory message = DoubleEndedStructQueue.popFront(queues[agentAddress]);
emit MessagePopped(agentAddress, message.message);
emit LogMessage(message.sender, agentAddress, message.message, message.value);
return message;
}
}
1 change: 1 addition & 0 deletions src/NFT/DoubleEndedStructQueue.sol
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ library DoubleEndedStructQueue {
address sender;
address recipient;
bytes message;
uint256 value;
}

struct Bytes32Deque {
Expand Down
50 changes: 30 additions & 20 deletions test/AgentCommunication.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ contract AgentCommunicationTest is Test {
return DoubleEndedStructQueue.MessageContainer({
sender: agent,
recipient: address(0x789),
message: "Hello, Agent!"
message: "Hello, Agent!",
value: 1000000000000000000
});
}

Expand Down Expand Up @@ -56,7 +57,7 @@ contract AgentCommunicationTest is Test {
DoubleEndedStructQueue.MessageContainer memory message = buildMessage();
vm.deal(agent, 1 ether);
vm.startPrank(agent);
agentComm.sendMessage{value: 10000000000000}(agent, message);
agentComm.sendMessage{value: 10000000000000}(agent, message.message);
vm.stopPrank();

DoubleEndedStructQueue.MessageContainer memory storedMessage = agentComm.getAtIndex(agent, 0);
Expand All @@ -68,50 +69,59 @@ contract AgentCommunicationTest is Test {
vm.deal(agent, 1 ether);
vm.startPrank(agent);
vm.expectRevert("Insufficient message value");
agentComm.sendMessage{value: 5000}(agent, message);
agentComm.sendMessage{value: 5000}(agent, message.message);
vm.stopPrank();
}

function testNewMessageSentEvent() public {
address recipient = address(0x789);
DoubleEndedStructQueue.MessageContainer memory message = buildMessage();
vm.deal(agent, 1 ether);
vm.startPrank(agent);

// Expect the NewMessageSent event to be emitted
vm.expectEmit(true, true, false, true);
emit AgentCommunication.NewMessageSent(agent, message.recipient, message.message);
// Expect the LogMessage event to be emitted
vm.expectEmit(true, true, true, true);
emit AgentCommunication.LogMessage(message.sender, message.recipient, message.message, message.value);

// Send the message
agentComm.sendMessage{value: 0.2 ether}(recipient, message);
agentComm.sendMessage{value: message.value}(address(0x789), message.message);
vm.stopPrank();
}

function testPopNextMessage() public {
// Create a message container
DoubleEndedStructQueue.MessageContainer memory message = buildMessage();

// Fund the agent and start the prank
vm.deal(agent, 1 ether);
vm.startPrank(agent);
agentComm.sendMessage{value: 10000000000000}(agent, message);

// Send the message
agentComm.sendMessage{value: message.value}(message.recipient, message.message);
vm.stopPrank();

// Expect the MessagePopped event to be emitted
vm.expectEmit(true, true, false, true);
emit AgentCommunication.MessagePopped(agent, message.message);
vm.startPrank(agent);
DoubleEndedStructQueue.MessageContainer memory poppedMessage = agentComm.popNextMessage(agent);
// Start the prank again for popping the message
vm.startPrank(message.recipient);

// Expect the LogMessage event to be emitted when popping the message
vm.expectEmit(true, true, true, true);
emit AgentCommunication.LogMessage(message.sender, message.recipient, message.message, message.value);

// Pop the next message
DoubleEndedStructQueue.MessageContainer memory poppedMessage = agentComm.popNextMessage(message.recipient);
vm.stopPrank();

// Assert that the popped message matches the original message
assertEq(poppedMessage.sender, message.sender);
assertEq(poppedMessage.recipient, message.recipient);
assertEq(poppedMessage.message, message.message);
uint256 numMessages = agentComm.countMessages(agent);
assertEq(numMessages, 0);
assertEq(poppedMessage.value, message.value);
}

// ToDo - reset name
function testPopNextMessageNotByAgent() public {
DoubleEndedStructQueue.MessageContainer memory message = buildMessage();
vm.deal(agent, 1 ether);
vm.startPrank(agent);
agentComm.sendMessage{value: 10000000000000}(agent, message);
agentComm.sendMessage{value: 10000000000000}(agent, message.message);
vm.stopPrank();

address notAgent = address(0x789);
Expand All @@ -132,8 +142,8 @@ contract AgentCommunicationTest is Test {
vm.startPrank(agent);

// Send two messages
agentComm.sendMessage{value: 10000000000000}(agent, message1);
agentComm.sendMessage{value: 10000000000000}(agent, message2);
agentComm.sendMessage{value: 10000000000000}(agent, message1.message);
agentComm.sendMessage{value: 10000000000000}(agent, message2.message);

// Stop the prank
vm.stopPrank();
Expand Down
Loading