diff --git a/README.md b/README.md index 1f6a3ae..f4b822a 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 | [0x62872578920427ae24b2527697dAb90CD1F4CA45](https://gnosisscan.io/address/0x62872578920427ae24b2527697dAb90CD1F4CA45#code) | | +| Agent communication contract | Simple contract storing message queue for each agent | [0xd422e0059ed819e8d792af936da206878188e34f](https://gnosisscan.io/address/0xd422e0059ed819e8d792af936da206878188e34f#code) | | ## Set up contracts development diff --git a/src/NFT/AgentCommunication.sol b/src/NFT/AgentCommunication.sol index 8113e02..6b9ea1e 100644 --- a/src/NFT/AgentCommunication.sol +++ b/src/NFT/AgentCommunication.sol @@ -5,14 +5,20 @@ import {Ownable} from "@openzeppelin/contracts/access/Ownable.sol"; import "./DoubleEndedStructQueue.sol"; contract AgentCommunication is Ownable { + address payable public treasury; + uint256 public pctToTreasuryInBasisPoints; //70% becomes 7000 + error MessageNotSentByAgent(); mapping(address => DoubleEndedStructQueue.Bytes32Deque) public queues; + uint256 public minimumValueForSendingMessageInWei; event LogMessage(address indexed sender, address indexed agentAddress, bytes message, uint256 value); - constructor() Ownable(msg.sender) { + constructor(address payable _treasury, uint256 _pctToTreasuryInBasisPoints) Ownable(msg.sender) { + treasury = _treasury; + pctToTreasuryInBasisPoints = _pctToTreasuryInBasisPoints; minimumValueForSendingMessageInWei = 10000000000000; // 0.00001 xDAI } @@ -29,7 +35,23 @@ contract AgentCommunication is Ownable { return DoubleEndedStructQueue.length(queues[agentAddress]); } + // Private function to calculate the amounts + function _calculateAmounts(uint256 totalValue) private view returns (uint256, uint256) { + uint256 amountForTreasury = (totalValue * pctToTreasuryInBasisPoints) / 10000; // 10000 since basis points are used + uint256 amountForAgent = totalValue - amountForTreasury; + return (amountForTreasury, amountForAgent); + } + function sendMessage(address agentAddress, bytes memory message) public payable mustPayMoreThanMinimum { + // split message value between treasury and agent + (uint256 amountForTreasury, uint256 amountForAgent) = _calculateAmounts(msg.value); + + // Transfer the amounts + (bool sentTreasury,) = treasury.call{value: amountForTreasury}(""); + require(sentTreasury, "Failed to send Ether"); + (bool sentAgent,) = payable(agentAddress).call{value: amountForAgent}(""); + require(sentAgent, "Failed to send Ether"); + DoubleEndedStructQueue.MessageContainer memory messageContainer = DoubleEndedStructQueue.MessageContainer(msg.sender, agentAddress, message, msg.value); DoubleEndedStructQueue.pushBack(queues[agentAddress], messageContainer); diff --git a/test/AgentCommunication.t.sol b/test/AgentCommunication.t.sol index 11be3cf..34de1b3 100644 --- a/test/AgentCommunication.t.sol +++ b/test/AgentCommunication.t.sol @@ -10,6 +10,8 @@ contract AgentCommunicationTest is Test { AgentCommunication agentComm; address owner = address(0x123); address agent = address(0x456); + address payable treasury = payable(address(0x789)); + uint256 pctToTreasuryInBasisPoints = 7000; function buildMessage() public view returns (DoubleEndedStructQueue.MessageContainer memory) { return DoubleEndedStructQueue.MessageContainer({ @@ -22,11 +24,11 @@ contract AgentCommunicationTest is Test { function setUp() public { vm.startPrank(owner); - agentComm = new AgentCommunication(); + agentComm = new AgentCommunication(treasury, pctToTreasuryInBasisPoints); vm.stopPrank(); } - function testInitialMinimumValue() public { + function testInitialMinimumValue() public view { uint256 expectedValue = 10000000000000; // 0.00001 xDAI assertEq(agentComm.minimumValueForSendingMessageInWei(), expectedValue); } @@ -55,11 +57,26 @@ contract AgentCommunicationTest is Test { function testSendMessage() public { DoubleEndedStructQueue.MessageContainer memory message = buildMessage(); - vm.deal(agent, 1 ether); - vm.startPrank(agent); - agentComm.sendMessage{value: 10000000000000}(agent, message.message); + vm.deal(owner, 1 ether); + + // Record initial balances + uint256 initialBalanceTreasury = treasury.balance; + uint256 initialBalanceAgent = agent.balance; + + assertEq(address(agentComm).balance, 0); + + vm.startPrank(owner); + uint256 messageValue = 10000000000000; + agentComm.sendMessage{value: messageValue}(agent, message.message); vm.stopPrank(); + // Assert treasuries increased correctly + uint256 diffBalanceTreasury = treasury.balance - initialBalanceTreasury; + uint256 diffBalanceAgent = agent.balance - initialBalanceAgent; + assertEq(messageValue * pctToTreasuryInBasisPoints / 10000, diffBalanceTreasury); + assertEq(messageValue * (10000 - pctToTreasuryInBasisPoints) / 10000, diffBalanceAgent); + assertEq(address(agentComm).balance, 0); + DoubleEndedStructQueue.MessageContainer memory storedMessage = agentComm.getAtIndex(agent, 0); assertEq(storedMessage.message, message.message); }