Skip to content

Commit

Permalink
refactor(evm): refactors Sygma Adapter and adds the Reporter
Browse files Browse the repository at this point in the history
  • Loading branch information
allemanfredi committed Jan 25, 2024
1 parent 26e40a6 commit 4cdd184
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 224 deletions.
30 changes: 5 additions & 25 deletions packages/evm/contracts/adapters/Sygma/SygmaAdapter.sol
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
// SPDX-License-Identifier: LGPL-3.0-only
pragma solidity ^0.8.17;

import "@openzeppelin/contracts/access/AccessControl.sol";
import "../OracleAdapter.sol";
import {AccessControl} from "@openzeppelin/contracts/access/AccessControl.sol";
import "../BlockHashOracleAdapter.sol";

contract SygmaAdapter is AccessControl, OracleAdapter, BlockHashOracleAdapter {
contract SygmaAdapter is AccessControl, BlockHashOracleAdapter {
string public constant PROVIDER = "sygma";

struct Reporter {
uint128 chainID;
bool enabled;
Expand All @@ -21,9 +22,6 @@ contract SygmaAdapter is AccessControl, OracleAdapter, BlockHashOracleAdapter {

event ReporterSet(address reporterAddress, uint256 chainID, bool enabled);

/**
@param handler Contract address of the generic handler.
*/
constructor(address handler) {
_handler = handler;
_setupRole(DEFAULT_ADMIN_ROLE, msg.sender);
Expand All @@ -34,35 +32,17 @@ contract SygmaAdapter is AccessControl, OracleAdapter, BlockHashOracleAdapter {
_;
}

/**
@dev Sets parameters of a source chain hash reporter.
@param reporterAddress Hash reporter address on the source chain.
@param chainID ChainID of the source chain.
@param enabled Status of the reporter.
*/
function setReporter(address reporterAddress, uint128 chainID, bool enabled) public onlyAdmin {
reporters[reporterAddress] = Reporter(chainID, enabled);
emit ReporterSet(reporterAddress, chainID, enabled);
}

/**
@dev Stores the hashes for a given array of ids.
@param reporterAddress Hash reporter address on the source chain.
@param ids Array of block numbers for which to set the hashes.
@param hashes Array of hashes to set for the given block numbers.
@notice Only callable by `_handler` with a message passed from an authorized reporter.
@notice Will revert if array lengths do not match.
*/
function storeHashes(address reporterAddress, uint256[] calldata ids, bytes32[] calldata hashes) public {
if (ids.length != hashes.length) revert ArrayLengthMismatch();
if (msg.sender != _handler) revert InvalidHandler(msg.sender);

Reporter memory reporter = reporters[reporterAddress];
if (!reporter.enabled) revert InvalidReporter(reporterAddress);
uint256 chainID = uint256(reporter.chainID);

for (uint i = 0; i < ids.length; i++) {
_storeHash(chainID, ids[i], hashes[i]);
}
_storeHashes(chainID, ids, hashes);
}
}
59 changes: 0 additions & 59 deletions packages/evm/contracts/adapters/Sygma/SygmaHeaderReporter.sol

This file was deleted.

38 changes: 0 additions & 38 deletions packages/evm/contracts/adapters/Sygma/SygmaMessageRelay.sol

This file was deleted.

71 changes: 47 additions & 24 deletions packages/evm/contracts/adapters/Sygma/SygmaReporter.sol
Original file line number Diff line number Diff line change
@@ -1,29 +1,46 @@
// SPDX-License-Identifier: LGPL-3.0-only
pragma solidity ^0.8.17;

import "./interfaces/ISygmaAdapter.sol";
import "./interfaces/IBridge.sol";

contract SygmaReporter {
address public immutable _bridge;
bytes32 public immutable _resourceID;
uint8 public immutable _defaultDestinationDomainID;
address public immutable _defaultSygmaAdapter;

constructor(address bridge, bytes32 resourceID, uint8 defaultDestinationDomainID, address defaultSygmaAdapter) {
_bridge = bridge;
_resourceID = resourceID;
_defaultDestinationDomainID = defaultDestinationDomainID;
_defaultSygmaAdapter = defaultSygmaAdapter;
import { Ownable } from "@openzeppelin/contracts/access/Ownable.sol";
import { Reporter } from "../Reporter.sol";
import { ISygmaAdapter } from "./interfaces/ISygmaAdapter.sol";
import { IBridge } from "./interfaces/IBridge.sol";

contract SygmaReporter is Reporter, Ownable {
string public constant PROVIDER = "sygma";

IBridge public immutable BRIDGE;

mapping(uint256 => uint8) public domainIds;
mapping(uint256 => bytes32) public resourceIds;

error DomainIdNotAvailable();
error ResourceIdNotAvailable();

event DomainIdSet(uint256 indexed chainId, uint8 indexed domainId);
event ResourceIdSet(uint256 indexed chainId, bytes32 indexed resourceId);

constructor(address headerStorage, address yaho, address bridge) Reporter(headerStorage, yaho) {
BRIDGE = IBridge(bridge);
}

function _reportData(
uint256[] memory messageIds,
bytes32[] memory hashes,
address sygmaAdapter,
uint8 destinationDomainID,
bytes memory feeData
) internal returns (uint64 depositNonce, bytes memory handlerResponse) {
function setDomainIdAndResourceIdByChainId(uint256 chainId, uint8 domainId, bytes32 resourceId) external onlyOwner {
domainIds[chainId] = domainId;
resourceIds[chainId] = resourceId;
emit DomainIdSet(chainId, domainId);
emit ResourceIdSet(chainId, resourceId);
}

function _dispatch(
uint256 toChainId,
address adapter,
uint256[] memory ids,
bytes32[] memory hashes
) internal override returns (bytes32) {
uint8 domainId = domainIds[toChainId];
if (domainId == 0) revert DomainIdNotAvailable();
bytes32 resourceId = resourceIds[toChainId];
if (resourceId == bytes32(0)) revert ResourceIdNotAvailable();
bytes memory depositData = abi.encodePacked(
// uint256 maxFee
uint256(950000),
Expand All @@ -34,15 +51,21 @@ contract SygmaReporter {
// uint8 len(executeContractAddress)
uint8(20),
// bytes executeContractAddress
sygmaAdapter,
adapter,
// uint8 len(executionDataDepositor)
uint8(20),
// bytes executionDataDepositor
address(this),
// bytes executionDataDepositor + executionData
prepareDepositData(messageIds, hashes)
prepareDepositData(ids, hashes)
);
(uint64 nonce, bytes memory handlerResponse) = BRIDGE.deposit{ value: msg.value }(
domainId,
resourceId,
depositData,
"" // feeData
);
return IBridge(_bridge).deposit{ value: msg.value }(destinationDomainID, _resourceID, depositData, feeData);
return bytes32(keccak256(abi.encode(nonce, handlerResponse)));
}

function slice(bytes calldata input, uint256 position) public pure returns (bytes memory) {
Expand Down
78 changes: 0 additions & 78 deletions packages/evm/contracts/adapters/Sygma/test/SygmaTestContracts.sol

This file was deleted.

0 comments on commit 4cdd184

Please sign in to comment.