Skip to content

Commit

Permalink
Merge pull request #44 from exp-table/debridge
Browse files Browse the repository at this point in the history
patch: updated DeBridgeHelper to allow different gates on src vs dst
  • Loading branch information
supervaulter authored Jan 21, 2025
2 parents 0ef5187 + cad844b commit d326bb0
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 22 deletions.
52 changes: 32 additions & 20 deletions src/debridge/DebridgeHelper.sol
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ pragma solidity >=0.8.0;
import "forge-std/Test.sol";
import {IDebridgeGate} from "./interfaces/IDebridgeGate.sol";
import {DeBridgeSignatureVerifierMock} from "./mocks/DeBridgeSignatureVerifierMock.sol";
import {console} from "forge-std/console.sol";

/// @title Debridge Helper
/// @notice helps simulate Debridge message relaying
Expand All @@ -15,7 +14,8 @@ contract DebridgeHelper is Test {
);

struct HelpArgs {
address gate;
address srcGate;
address dstGate;
uint256 forkId;
uint256 destinationChainId;
bytes32 eventSelector;
Expand Down Expand Up @@ -46,13 +46,15 @@ contract DebridgeHelper is Test {
// EXTERNAL FUNCTIONS //
//////////////////////////////////////////////////////////////
/// @notice helps process multiple destination messages to relay
/// @param deBridgeGate represents the deBridge gate on the source chain
/// @param srcGate represents the source deBridge gate
/// @param dstGates represents the destination deBridge gates
/// @param forkIds represents the destination chain fork ids
/// @param destinationChainIds represents the destination chain ids
/// @param debridgeGateAdmins represents the admin of the debridge gate
/// @param logs represents the recorded message logs
function help(
address deBridgeGate,
address srcGate,
address[] memory dstGates,
uint256[] memory forkIds,
uint256[] memory destinationChainIds,
address[] memory debridgeGateAdmins,
Expand All @@ -62,7 +64,8 @@ contract DebridgeHelper is Test {
for (uint256 i; i < chains;) {
_help(
HelpArgs({
gate: deBridgeGate,
srcGate: srcGate,
dstGate: dstGates[i],
forkId: forkIds[i],
destinationChainId: destinationChainIds[i],
eventSelector: DebridgeSend,
Expand All @@ -76,15 +79,17 @@ contract DebridgeHelper is Test {
}
}
/// @notice helps process multiple destination messages to relay
/// @param deBridgeGate represents the deBridge gate on the source chain
/// @param srcGate represents the source deBridge gate
/// @param dstGates represents the destination deBridge gate
/// @param forkIds represents the destination chain fork ids
/// @param destinationChainIds represents the destination chain ids
/// @param debridgeGateAdmins represents the admin of the debridge gate
/// @param eventSelector represents a custom event selector
/// @param logs represents the recorded message logs

function help(
address deBridgeGate,
address srcGate,
address[] memory dstGates,
uint256[] memory forkIds,
uint256[] memory destinationChainIds,
address[] memory debridgeGateAdmins,
Expand All @@ -95,7 +100,8 @@ contract DebridgeHelper is Test {
for (uint256 i; i < chains;) {
_help(
HelpArgs({
gate: deBridgeGate,
srcGate: srcGate,
dstGate: dstGates[i],
forkId: forkIds[i],
destinationChainId: destinationChainIds[i],
eventSelector: eventSelector,
Expand All @@ -111,20 +117,23 @@ contract DebridgeHelper is Test {

/// @notice helps process single destination message to relay
/// @param debridgeGateAdmin represents the admin of the debridge gate
/// @param deBridgeGate represents the deBridge gate on the source chain
/// @param srcGate represents the source deBridge gate
/// @param dstGate represents the destination deBridge gate
/// @param forkId represents the destination chain fork id
/// @param destinationChainId represents the destination chain id
/// @param logs represents the recorded message logs
function help(
address debridgeGateAdmin,
address deBridgeGate,
address srcGate,
address dstGate,
uint256 forkId,
uint256 destinationChainId,
Vm.Log[] calldata logs
) external {
_help(
HelpArgs({
gate: deBridgeGate,
srcGate: srcGate,
dstGate: dstGate,
forkId: forkId,
destinationChainId: destinationChainId,
eventSelector: DebridgeSend,
Expand All @@ -136,22 +145,25 @@ contract DebridgeHelper is Test {

/// @notice helps process single destination message to relay
/// @param debridgeGateAdmin represents the admin of the debridge gate
/// @param deBridgeGate represents the deBridge gate on the source chain
/// @param srcGate represents the source deBridge gate
/// @param dstGate represents the destination deBridge gate
/// @param forkId represents the destination chain fork id
/// @param destinationChainId represents the destination chain id
/// @param eventSelector represents a custom event selector
/// @param logs represents the recorded message logs
function help(
address debridgeGateAdmin,
address deBridgeGate,
address srcGate,
address dstGate,
uint256 forkId,
uint256 destinationChainId,
bytes32 eventSelector,
Vm.Log[] calldata logs
) external {
_help(
HelpArgs({
gate: deBridgeGate,
srcGate: srcGate,
dstGate: dstGate,
forkId: forkId,
destinationChainId: destinationChainId,
eventSelector: eventSelector,
Expand All @@ -167,14 +179,14 @@ contract DebridgeHelper is Test {
LocalVars memory vars;
vars.originChainId = uint256(block.chainid);
vars.prevForkId = vm.activeFork();
vm.selectFork(args.forkId);

uint256 count = args.logs.length;
for (uint256 i; i < count;) {
// https://docs.debridge.finance/the-debridge-messaging-protocol/development-guides/building-an-evm-based-dapp/evm-smart-contract-interfaces
// DeBridgeSend is the event selector for the Sent event emitted by the DeBridge gate contract
// Requests must be filled using the `.claim()` function of the DeBridge gate contract.
if (args.logs[i].topics[0] == args.eventSelector && args.logs[i].emitter == args.gate) {
if (args.logs[i].topics[0] == args.eventSelector && args.logs[i].emitter == args.srcGate) {
vm.selectFork(args.forkId);
vars.destinationChainId = uint256(args.logs[i].topics[2]);

if (vars.destinationChainId == args.destinationChainId) {
Expand All @@ -187,15 +199,15 @@ contract DebridgeHelper is Test {
DeBridgeSignatureVerifierMock _verifier = new DeBridgeSignatureVerifierMock();
// -- need to overwrite the signature verifier address
vm.startPrank(debridgeGateAdmin);
IDebridgeGate(args.gate).setSignatureVerifier(address(_verifier));
IDebridgeGate(args.dstGate).setSignatureVerifier(address(_verifier));
vm.stopPrank();

IDebridgeGate.DebridgeInfo memory debridgeInfo =
IDebridgeGate(args.gate).getDebridge(logData.debridgeId);
deal(debridgeInfo.tokenAddress, args.gate, logData.amount);
IDebridgeGate(args.dstGate).getDebridge(logData.debridgeId);
deal(debridgeInfo.tokenAddress, args.dstGate, logData.amount);

address receiver = address(bytes20(logData.receiver));
IDebridgeGate(args.gate).claim(
IDebridgeGate(args.dstGate).claim(
logData.debridgeId,
logData.amount,
vars.originChainId,
Expand Down
8 changes: 6 additions & 2 deletions test/Debridge.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ contract DebridgeHelperTest is Test {
_someCrossChainFunctionInYourContract(L1_debridge, ARBITRUM_ID, amount);
Vm.Log[] memory logs = vm.getRecordedLogs();

debridgeHelper.help(ARBITRUM_ADMIN, L1_debridge, ARBITRUM_FORK_ID, ARBITRUM_ID, logs);
debridgeHelper.help(ARBITRUM_ADMIN, L1_debridge, ARBITRUM_debridge, ARBITRUM_FORK_ID, ARBITRUM_ID, logs);
// /\
// ||
// ||
Expand Down Expand Up @@ -115,7 +115,11 @@ contract DebridgeHelperTest is Test {
debridgeGateAdmins[0] = ARBITRUM_ADMIN;
debridgeGateAdmins[1] = POLYGON_ADMIN;

debridgeHelper.help(L1_debridge, allDstForks, allDstChainIds, debridgeGateAdmins, logs);
address[] memory dstGates = new address[](2);
dstGates[0] = ARBITRUM_debridge;
dstGates[1] = POLYGON_debridge;

debridgeHelper.help(L1_debridge, dstGates, allDstForks, allDstChainIds, debridgeGateAdmins, logs);

vm.selectFork(ARBITRUM_FORK_ID);
assertApproxEqAbs(IERC20(ARBITRUM_DEBRIDGE_TOKEN).balanceOf(target), amount, amount * 1e4 / 1e5);
Expand Down

0 comments on commit d326bb0

Please sign in to comment.