From 25b3e700d3299310cbb0aae4cb20000a6d67cece Mon Sep 17 00:00:00 2001 From: gabrielfior Date: Thu, 19 Dec 2024 23:54:27 -0300 Subject: [PATCH 1/2] Added value to structs --- src/NFT/AgentCommunication.sol | 17 +++++----- src/NFT/DoubleEndedStructQueue.sol | 1 + test/AgentCommunication.t.sol | 50 ++++++++++++++++++------------ 3 files changed, 38 insertions(+), 30 deletions(-) diff --git a/src/NFT/AgentCommunication.sol b/src/NFT/AgentCommunication.sol index d47f97c..8113e02 100644 --- a/src/NFT/AgentCommunication.sol +++ b/src/NFT/AgentCommunication.sol @@ -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 @@ -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) @@ -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; } } diff --git a/src/NFT/DoubleEndedStructQueue.sol b/src/NFT/DoubleEndedStructQueue.sol index 15950b4..a2cb97e 100644 --- a/src/NFT/DoubleEndedStructQueue.sol +++ b/src/NFT/DoubleEndedStructQueue.sol @@ -10,6 +10,7 @@ library DoubleEndedStructQueue { address sender; address recipient; bytes message; + uint256 value; } struct Bytes32Deque { diff --git a/test/AgentCommunication.t.sol b/test/AgentCommunication.t.sol index ead9e4c..11be3cf 100644 --- a/test/AgentCommunication.t.sol +++ b/test/AgentCommunication.t.sol @@ -15,7 +15,8 @@ contract AgentCommunicationTest is Test { return DoubleEndedStructQueue.MessageContainer({ sender: agent, recipient: address(0x789), - message: "Hello, Agent!" + message: "Hello, Agent!", + value: 1000000000000000000 }); } @@ -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); @@ -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); @@ -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(); From e0ff4dc6094f480f2d5082710ce1607ba451ba9d Mon Sep 17 00:00:00 2001 From: gabrielfior Date: Thu, 26 Dec 2024 11:47:04 -0300 Subject: [PATCH 2/2] Updated contract address --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 5dc4511..1f6a3ae 100644 --- a/README.md +++ b/README.md @@ -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