Skip to content

Commit

Permalink
feat: decode event data with struct
Browse files Browse the repository at this point in the history
  • Loading branch information
guidanoli committed Oct 9, 2023
1 parent 6f45119 commit b1f776c
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,13 @@ import {Vm} from "forge-std/Vm.sol";
contract AuthorityFactoryTest is Test {
AuthorityFactory factory;

// event emitted in the factory
event AuthorityCreated(address authorityOwner, Authority authority);

struct AuthorityCreatedEventData {
address authorityOwner;
Authority authority;
}

function setUp() public {
factory = new AuthorityFactory();
}
Expand Down Expand Up @@ -46,13 +50,12 @@ contract AuthorityFactoryTest is Test {
) {
++numOfAuthorityCreated;

address a;
address b;
AuthorityCreatedEventData memory eventData;

(a, b) = abi.decode(entry.data, (address, address));
eventData = abi.decode(entry.data, (AuthorityCreatedEventData));

assertEq(_authorityOwner, a);
assertEq(address(_authority), b);
assertEq(_authorityOwner, eventData.authorityOwner);
assertEq(address(_authority), address(eventData.authority));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,31 @@ contract AuthorityFactoryTest is Test {
IHistoryFactory historyFactory
);

struct FactoryCreatedEventData {
IAuthorityFactory authorityFactory;
IHistoryFactory historyFactory;
}

event AuthorityCreated(address authorityOwner, Authority authority);

struct AuthorityCreatedEventData {
address authorityOwner;
Authority authority;
}

event HistoryCreated(address historyOwner, History history);

struct HistoryCreatedEventData {
address historyOwner;
History history;
}

event NewHistory(IHistory history);

struct NewHistoryEventData{
IHistory history;
}

event OwnershipTransferred(
address indexed previousOwner,
address indexed newOwner
Expand Down Expand Up @@ -66,13 +85,12 @@ contract AuthorityFactoryTest is Test {
) {
++numOfFactoryCreated;

address a;
address b;
FactoryCreatedEventData memory eventData;

(a, b) = abi.decode(entry.data, (address, address));
eventData = abi.decode(entry.data, (FactoryCreatedEventData));

assertEq(address(authorityFactory), a);
assertEq(address(historyFactory), b);
assertEq(address(authorityFactory), address(eventData.authorityFactory));
assertEq(address(historyFactory), address(eventData.historyFactory));
}
}

Expand Down Expand Up @@ -118,13 +136,12 @@ contract AuthorityFactoryTest is Test {
) {
++numOfAuthorityCreated;

address a;
address b;
AuthorityCreatedEventData memory eventData;

(a, b) = abi.decode(entry.data, (address, address));
eventData = abi.decode(entry.data, (AuthorityCreatedEventData));

assertEq(address(factory), a);
assertEq(address(_authority), b);
assertEq(address(factory), eventData.authorityOwner);
assertEq(address(_authority), address(eventData.authority));
}

if (
Expand All @@ -133,13 +150,12 @@ contract AuthorityFactoryTest is Test {
) {
++numOfHistoryCreated;

address a;
address b;
HistoryCreatedEventData memory eventData;

(a, b) = abi.decode(entry.data, (address, address));
eventData = abi.decode(entry.data, (HistoryCreatedEventData));

assertEq(address(_authority), a);
assertEq(address(_history), b);
assertEq(address(_authority), eventData.historyOwner);
assertEq(address(_history), address(eventData.history));
}

if (
Expand All @@ -148,11 +164,11 @@ contract AuthorityFactoryTest is Test {
) {
++numOfNewHistory;

address a;
NewHistoryEventData memory eventData;

a = abi.decode(entry.data, (address));
eventData = abi.decode(entry.data, (NewHistoryEventData));

assertEq(address(_history), a);
assertEq(address(_history), address(eventData.history));
}

if (
Expand Down
18 changes: 11 additions & 7 deletions onchain/rollups/test/foundry/dapp/CartesiDAppFactory.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,12 @@ contract CartesiDAppFactoryTest is TestBase {
CartesiDApp application
);

struct ApplicationCreatedEventData {
address dappOwner;
bytes32 templateHash;
CartesiDApp application;
}

function testNewApplication(
address _dappOwner,
bytes32 _templateHash
Expand Down Expand Up @@ -158,15 +164,13 @@ contract CartesiDAppFactoryTest is TestBase {
bytes32(uint256(uint160(address(consensus))))
);

address a;
bytes32 b;
address c;
ApplicationCreatedEventData memory eventData;

(a, b, c) = abi.decode(entry.data, (address, bytes32, address));
eventData = abi.decode(entry.data, (ApplicationCreatedEventData));

assertEq(_dappOwner, a);
assertEq(_templateHash, b);
assertEq(address(_dapp), c);
assertEq(_dappOwner, eventData.dappOwner);
assertEq(_templateHash, eventData.templateHash);
assertEq(address(_dapp), address(eventData.application));
}
}

Expand Down
14 changes: 9 additions & 5 deletions onchain/rollups/test/foundry/history/HistoryFactory.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@ contract HistoryFactoryTest is Test {

event HistoryCreated(address historyOwner, History history);

struct HistoryCreatedEventData {
address historyOwner;
History history;
}

function setUp() public {
factory = new HistoryFactory();
}
Expand Down Expand Up @@ -45,13 +50,12 @@ contract HistoryFactoryTest is Test {
) {
++numOfHistoryCreated;

address a;
address b;
HistoryCreatedEventData memory eventData;

(a, b) = abi.decode(entry.data, (address, address));
eventData = abi.decode(entry.data, (HistoryCreatedEventData));

assertEq(_historyOwner, a);
assertEq(address(_history), b);
assertEq(_historyOwner, eventData.historyOwner);
assertEq(address(_history), address(eventData.history));
}
}

Expand Down

0 comments on commit b1f776c

Please sign in to comment.