Skip to content

Commit

Permalink
Added treasury redirect to contract (#20)
Browse files Browse the repository at this point in the history
  • Loading branch information
gabrielfior authored Dec 27, 2024
1 parent be92049 commit d782565
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 7 deletions.
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 | [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

Expand Down
24 changes: 23 additions & 1 deletion src/NFT/AgentCommunication.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand All @@ -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);
Expand Down
27 changes: 22 additions & 5 deletions test/AgentCommunication.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand All @@ -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);
}
Expand Down Expand Up @@ -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);
}
Expand Down

0 comments on commit d782565

Please sign in to comment.