generated from PaulRBerg/hardhat-template
-
Notifications
You must be signed in to change notification settings - Fork 52
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #24 from viatrix/sygma-message-relayer
feat: add SygmaMessageRelayer
- Loading branch information
Showing
7 changed files
with
398 additions
and
49 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
38 changes: 38 additions & 0 deletions
38
packages/evm/contracts/adapters/Sygma/SygmaMessageRelayer.sol
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
// SPDX-License-Identifier: LGPL-3.0-only | ||
pragma solidity ^0.8.17; | ||
|
||
import { IMessageRelay } from "../../interfaces/IMessageRelay.sol"; | ||
import { SygmaReporter } from "./SygmaReporter.sol"; | ||
import { Yaho } from "../../Yaho.sol"; | ||
|
||
contract SygmaMessageRelayer is SygmaReporter, IMessageRelay { | ||
Yaho public immutable _yaho; | ||
|
||
event MessageRelayed(address indexed emitter, uint256 indexed messageId); | ||
|
||
constructor( | ||
address bridge, | ||
Yaho yaho, | ||
bytes32 resourceID, | ||
uint8 defaultDestinationDomainID, | ||
address defaultSygmaAdapter | ||
) SygmaReporter(bridge, resourceID, defaultDestinationDomainID, defaultSygmaAdapter) { | ||
_yaho = yaho; | ||
} | ||
|
||
/** | ||
@dev Relays the messages via the Sygma bridge to default domain. | ||
@param messageIds IDs of the messages to pass over the Sygma bridge. | ||
@param sygmaAdapter Address of the Sygma adapter on the target chain. | ||
*/ | ||
function relayMessages(uint256[] memory messageIds, address sygmaAdapter) public payable returns (bytes32) { | ||
bytes32[] memory hashes = new bytes32[](messageIds.length); | ||
for (uint256 i = 0; i < messageIds.length; i++) { | ||
uint256 id = messageIds[i]; | ||
hashes[i] = _yaho.hashes(id); | ||
emit MessageRelayed(address(this), messageIds[i]); | ||
} | ||
(uint64 depositNonce, ) = _reportData(messageIds, hashes, sygmaAdapter, _defaultDestinationDomainID, ""); | ||
return bytes32(uint256(depositNonce)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
// 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; | ||
} | ||
|
||
function _reportData( | ||
uint256[] memory messageIds, | ||
bytes32[] memory hashes, | ||
address sygmaAdapter, | ||
uint8 destinationDomainID, | ||
bytes memory feeData | ||
) internal returns (uint64 depositNonce, bytes memory handlerResponse) { | ||
bytes memory depositData = abi.encodePacked( | ||
// uint256 maxFee | ||
uint256(0), | ||
// uint16 len(executeFuncSignature) | ||
uint16(4), | ||
// bytes executeFuncSignature | ||
ISygmaAdapter(address(0)).storeHashes.selector, | ||
// uint8 len(executeContractAddress) | ||
uint8(20), | ||
// bytes executeContractAddress | ||
sygmaAdapter, | ||
// uint8 len(executionDataDepositor) | ||
uint8(20), | ||
// bytes executionDataDepositor | ||
address(this), | ||
// bytes executionDataDepositor + executionData | ||
prepareDepositData(messageIds, hashes) | ||
); | ||
return IBridge(_bridge).deposit{ value: msg.value }(destinationDomainID, _resourceID, depositData, feeData); | ||
} | ||
|
||
function slice(bytes calldata input, uint256 position) public pure returns (bytes memory) { | ||
return input[position:]; | ||
} | ||
|
||
function prepareDepositData( | ||
uint256[] memory messageIds, | ||
bytes32[] memory hashes | ||
) public view returns (bytes memory) { | ||
bytes memory encoded = abi.encode(address(0), messageIds, hashes); | ||
return this.slice(encoded, 32); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
120 changes: 120 additions & 0 deletions
120
packages/evm/test/adapters/Sygma/03_SygmaMessageRelayer.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
import { expect } from "chai" | ||
import { ethers, network } from "hardhat" | ||
|
||
const DOMAIN_ID = 5 | ||
const resourceID = "0x0000000000000000000000000000000000000000000000000000000000000500" | ||
|
||
const setup = async () => { | ||
await network.provider.request({ method: "hardhat_reset", params: [] }) | ||
const signers = await ethers.getSigners() | ||
const sender = signers[0].address | ||
const otherAddress = signers[2].address | ||
const Yaho = await ethers.getContractFactory("Yaho") | ||
const yaho = await Yaho.deploy() | ||
const SygmaBridge = await ethers.getContractFactory("MockSygmaBridge") | ||
const sygmaBridge = await SygmaBridge.deploy() | ||
const SygmaMessageRelayer = await ethers.getContractFactory("SygmaMessageRelayer") | ||
const SygmaAdapter = await ethers.getContractFactory("SygmaAdapter") | ||
const sygmaAdapter = await SygmaAdapter.deploy(sygmaBridge.address) | ||
// IBridge bridge, HeaderStorage headerStorage, bytes32 resourceID, uint8 defaultDestinationDomainID, address defaultSygmaAdapter | ||
const sygmaMessageRelayer = await SygmaMessageRelayer.deploy( | ||
sygmaBridge.address, | ||
yaho.address, | ||
resourceID, | ||
DOMAIN_ID, | ||
sygmaAdapter.address, | ||
) | ||
|
||
await sygmaAdapter.setReporter(sygmaMessageRelayer.address, DOMAIN_ID, true) | ||
|
||
const PingPong = await ethers.getContractFactory("PingPong") | ||
const pingPong = await PingPong.deploy() | ||
const message_1 = { | ||
to: pingPong.address, | ||
toChainId: 1, | ||
data: pingPong.interface.getSighash("ping"), | ||
} | ||
await yaho.dispatchMessages([message_1, message_1]) | ||
// await mine(10) | ||
return { | ||
sender, | ||
sygmaAdapter, | ||
otherAddress, | ||
yaho, | ||
sygmaBridge, | ||
sygmaMessageRelayer, | ||
pingPong, | ||
message_1, | ||
} | ||
} | ||
|
||
const prepareDepositData = async (reporterAddress: string, ids: string[], hashes: string[], adapter: string) => { | ||
const abiCoder = ethers.utils.defaultAbiCoder | ||
const executionData = abiCoder | ||
.encode(["address", "uint256[]", "bytes32[]"], [ethers.constants.AddressZero, ids, hashes]) | ||
.substring(66) | ||
|
||
const SygmaAdapter = await ethers.getContractFactory("SygmaAdapter") | ||
const functionSig = SygmaAdapter.interface.getSighash("storeHashes") | ||
|
||
// bytes memory depositData = abi.encodePacked( | ||
// uint256(0), | ||
// uint16(4), | ||
// IDepositAdapterTarget(address(0)).execute.selector, | ||
// uint8(20), | ||
// _targetDepositAdapter, | ||
// uint8(20), | ||
// _depositorAddress, | ||
// abi.encode(depositContractCalldata) | ||
// ); | ||
|
||
const depositData = | ||
ethers.utils.hexZeroPad("0x0", 32) + | ||
"0004" + | ||
functionSig.substring(2) + | ||
"14" + | ||
adapter.toLowerCase().substring(2) + | ||
"14" + | ||
reporterAddress.toLowerCase().substring(2) + | ||
executionData | ||
return depositData | ||
} | ||
|
||
describe("SygmaMessageRelayer", function () { | ||
describe("Deploy", function () { | ||
it("Successfully deploys contract", async function () { | ||
const { sygmaBridge, yaho, sygmaAdapter, sygmaMessageRelayer } = await setup() | ||
expect(await sygmaMessageRelayer.deployed()) | ||
expect(await sygmaMessageRelayer._bridge()).to.equal(sygmaBridge.address) | ||
expect(await sygmaMessageRelayer._yaho()).to.equal(yaho.address) | ||
expect(await sygmaMessageRelayer._resourceID()).to.equal(resourceID) | ||
expect(await sygmaMessageRelayer._defaultDestinationDomainID()).to.equal(DOMAIN_ID) | ||
expect(await sygmaMessageRelayer._defaultSygmaAdapter()).to.equal(sygmaAdapter.address) | ||
}) | ||
}) | ||
|
||
describe("relayMessages()", function () { | ||
it("Relays messages to Sygma to default domain", async function () { | ||
const { sender, sygmaMessageRelayer, sygmaAdapter, sygmaBridge, yaho, message_1 } = await setup() | ||
const hash0 = await yaho.calculateHash(network.config.chainId, 0, yaho.address, sender, message_1) | ||
const hash1 = await yaho.calculateHash(network.config.chainId, 1, yaho.address, sender, message_1) | ||
const depositData = await prepareDepositData( | ||
sygmaMessageRelayer.address, | ||
["0", "1"], | ||
[hash0, hash1], | ||
sygmaAdapter.address, | ||
) | ||
expect(await sygmaMessageRelayer.callStatic.relayMessages([0, 1], sygmaAdapter.address)).to.equal( | ||
"0x0000000000000000000000000000000000000000000000000000000000000001", | ||
) | ||
await expect(sygmaMessageRelayer.relayMessages([0, 1], sygmaAdapter.address)) | ||
.to.emit(sygmaMessageRelayer, "MessageRelayed") | ||
.withArgs(sygmaMessageRelayer.address, 0) | ||
.and.to.emit(sygmaMessageRelayer, "MessageRelayed") | ||
.withArgs(sygmaMessageRelayer.address, 1) | ||
.and.to.emit(sygmaBridge, "Deposit") | ||
// (destinationDomainID, resourceID, 1, msg.sender, depositData, feeData); | ||
.withArgs(DOMAIN_ID, resourceID, 1, sygmaMessageRelayer.address, depositData, "0x") | ||
}) | ||
}) | ||
}) |
Oops, something went wrong.