Skip to content

Commit

Permalink
Add events (#3)
Browse files Browse the repository at this point in the history
  • Loading branch information
kongzii authored Jun 28, 2024
1 parent 9448a1f commit 8a0cd14
Show file tree
Hide file tree
Showing 4 changed files with 102 additions and 6 deletions.
64 changes: 64 additions & 0 deletions abi/omen_thumbnailmapping.abi.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
[
{
"type": "function",
"name": "get",
"inputs": [
{ "name": "marketAddress", "type": "address", "internalType": "address" }
],
"outputs": [{ "name": "", "type": "bytes32", "internalType": "bytes32" }],
"stateMutability": "view"
},
{
"type": "function",
"name": "getLatestImageChanger",
"inputs": [
{ "name": "marketAddress", "type": "address", "internalType": "address" }
],
"outputs": [{ "name": "", "type": "address", "internalType": "address" }],
"stateMutability": "view"
},
{
"type": "function",
"name": "remove",
"inputs": [
{ "name": "marketAddress", "type": "address", "internalType": "address" }
],
"outputs": [],
"stateMutability": "nonpayable"
},
{
"type": "function",
"name": "set",
"inputs": [
{ "name": "marketAddress", "type": "address", "internalType": "address" },
{ "name": "image_hash", "type": "bytes32", "internalType": "bytes32" }
],
"outputs": [],
"stateMutability": "nonpayable"
},
{
"type": "event",
"name": "ImageUpdated",
"inputs": [
{
"name": "marketAddress",
"type": "address",
"indexed": true,
"internalType": "address"
},
{
"name": "image_hash",
"type": "bytes32",
"indexed": false,
"internalType": "bytes32"
},
{
"name": "changer",
"type": "address",
"indexed": true,
"internalType": "address"
}
],
"anonymous": false
}
]
17 changes: 12 additions & 5 deletions src/OmenThumbnailMapping.sol
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,31 @@ import {IERC20} from "../lib/openzeppelin-contracts/contracts/interfaces/IERC20.

/// @dev Mapping from market's address to image's IPFS hash
contract OmenThumbnailMapping {
event ImageUpdated(address indexed marketAddress, bytes32 image_hash, address indexed changer);

mapping(address => bytes32) private marketAddressToIPFSHash;
mapping(address => address) private marketAddressToLatestImageChanger;

/// @dev Get IPFS hash of thumbnail for the given market.
function get(address marketAddress) external view returns (bytes32) {
function get(address marketAddress) public view returns (bytes32) {
return marketAddressToIPFSHash[marketAddress];
}

/// @dev Update IPFS hash of thumbnail for the given market.
function set(address marketAddress, bytes32 image_hash) external requireThatSenderCanChangeImage(marketAddress) {
function set(address marketAddress, bytes32 image_hash) public requireThatSenderCanChangeImage(marketAddress) {
marketAddressToIPFSHash[marketAddress] = image_hash;
marketAddressToLatestImageChanger[marketAddress] = msg.sender;
emit ImageUpdated(marketAddress, image_hash, msg.sender);
}

/// @dev Remove IPFS hash of thumbnail for the given market.
function remove(address marketAddress) external requireThatSenderCanChangeImage(marketAddress) {
delete marketAddressToIPFSHash[marketAddress];
marketAddressToLatestImageChanger[marketAddress] = msg.sender;
function remove(address marketAddress) public requireThatSenderCanChangeImage(marketAddress) {
set(marketAddress, bytes32(0));
}

/// @dev Get the address of the latest person who updated the image for the given market.
function getLatestImageChanger(address marketAddress) public view returns (address) {
return marketAddressToLatestImageChanger[marketAddress];
}

/// @dev Verify that sender is allowed to update IPFS hash for the given market.
Expand Down
25 changes: 25 additions & 0 deletions test/OmenThumbnailMapping.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ contract OmenThumbnailMappingTest is Test {

function testCanUpdateImageWithAnyFundsInMarketIfThisIsFirstUpdater() public {
productMarketMaker.mint(address(this), 1);
vm.expectEmit(true, true, true, true);
emit OmenThumbnailMapping.ImageUpdated(address(productMarketMaker), "Qm123", address(this));
omenThumbnailMapping.set(address(productMarketMaker), "Qm123");
assertEq(omenThumbnailMapping.get(address(productMarketMaker)), "Qm123");
}
Expand Down Expand Up @@ -56,4 +58,27 @@ contract OmenThumbnailMappingTest is Test {
omenThumbnailMapping.set(address(productMarketMaker), "Qm456");
assertEq(omenThumbnailMapping.get(address(productMarketMaker)), "Qm456");
}

function testNonSetImageIsEqualToNullBytes32() public {
assertEq(omenThumbnailMapping.get(address(productMarketMaker)), bytes32(0));
}

function testImageIsNullBytesAfterRemovalAndChangerIsUpdatedCorrectly() public {
productMarketMaker.mint(address(this), 1);
vm.expectEmit(true, true, true, true);
emit OmenThumbnailMapping.ImageUpdated(address(productMarketMaker), "Qm123", address(this));
omenThumbnailMapping.set(address(productMarketMaker), "Qm123");
assertEq(omenThumbnailMapping.get(address(productMarketMaker)), "Qm123");
assertEq(omenThumbnailMapping.getLatestImageChanger(address(productMarketMaker)), address(this));

address anotherUser = address(0x123);
productMarketMaker.mint(anotherUser, 2);
vm.expectEmit(true, true, true, true);
emit OmenThumbnailMapping.ImageUpdated(address(productMarketMaker), bytes32(0), anotherUser);
vm.prank(anotherUser);

omenThumbnailMapping.remove(address(productMarketMaker));
assertEq(omenThumbnailMapping.get(address(productMarketMaker)), bytes32(0));
assertEq(omenThumbnailMapping.getLatestImageChanger(address(productMarketMaker)), anotherUser);
}
}

0 comments on commit 8a0cd14

Please sign in to comment.