Skip to content

Commit

Permalink
chore: revamp individual bridge adapters
Browse files Browse the repository at this point in the history
  • Loading branch information
sujithsomraaj committed Dec 17, 2023
1 parent af88123 commit 6d2b5d6
Show file tree
Hide file tree
Showing 8 changed files with 40 additions and 44 deletions.
7 changes: 0 additions & 7 deletions src/adapters/BaseSenderAdapter.sol
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,6 @@ abstract contract BaseSenderAdapter is IMessageSenderAdapter {
/*/////////////////////////////////////////////////////////////////
MODIFIER
////////////////////////////////////////////////////////////////*/
modifier onlyMultiBridgeMessageSender() {
if (msg.sender != senderGAC.multiBridgeMessageSender()) {
revert Error.CALLER_NOT_MULTI_MESSAGE_SENDER();
}
_;
}

modifier onlyGlobalOwner() {
if (!senderGAC.isGlobalOwner(msg.sender)) {
revert Error.CALLER_NOT_OWNER();
Expand Down
13 changes: 5 additions & 8 deletions src/adapters/axelar/AxelarReceiverAdapter.sol
Original file line number Diff line number Diff line change
Expand Up @@ -80,19 +80,16 @@ contract AxelarReceiverAdapter is BaseReceiverAdapter, IAxelarExecutable {
revert Error.INVALID_RECEIVER_ADAPTER();
}

/// @dev step-5: validate the destination
// if (decodedPayload.finalDestination != receiverGAC.multiBridgeMsgReceiver()) {
// revert Error.INVALID_FINAL_DESTINATION();
// }

isMessageExecuted[msgId] = true;
commandIdStatus[_commandId] = true;

MessageLibrary.Message memory _data = abi.decode(decodedPayload.data, (MessageLibrary.Message));

try IMultiBridgeMessageReceiver(receiverGAC.multiBridgeMsgReceiver()).receiveMessage(_data) {
emit MessageIdExecuted(_data.srcChainId, msgId);
} catch (bytes memory lowLevelData) {
(bool success, bytes memory lowLevelData) = decodedPayload.to.call{value: msg.value}(decodedPayload.data);

if (success) {
MessageIdExecuted(msgId);
} else {
revert MessageFailure(msgId, lowLevelData);
}
}
Expand Down
8 changes: 3 additions & 5 deletions src/adapters/axelar/AxelarSenderAdapter.sol
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@ contract AxelarSenderAdapter is BaseSenderAdapter {
external
payable
override
onlyMultiBridgeMessageSender
returns (bytes32 msgId)
{
address receiverAdapter = receiverAdapters[_receiverChainId];
Expand Down Expand Up @@ -106,18 +105,17 @@ contract AxelarSenderAdapter is BaseSenderAdapter {
/// @param _destinationChain The name of the destination chain.
/// @param _receiverAdapter The address of the adapter on the destination chain that will receive the message.
/// @param _msgId The ID of the message to be relayed.
/// @param _multibridgeReceiver The address of the MultibridgeReceiver contract on the destination chain that will receive the message.
/// @param _to The address of the MultibridgeReceiver contract on the destination chain that will receive the message.
/// @param _data The bytes data to pass to the contract on the destination chain.
function _callContract(
string memory _destinationChain,
address _receiverAdapter,
bytes32 _msgId,
address _multibridgeReceiver,
address _to,
bytes calldata _data
) internal {
string memory receiverAdapterInString = StringAddressConversion.toString(_receiverAdapter);
bytes memory payload =
abi.encode(AdapterPayload(_msgId, address(msg.sender), _receiverAdapter, _multibridgeReceiver, _data));
bytes memory payload = abi.encode(AdapterPayload(_msgId, address(msg.sender), _receiverAdapter, _to, _data));

gasService.payNativeGasForContractCall{value: msg.value}(
msg.sender, _destinationChain, receiverAdapterInString, payload, msg.sender
Expand Down
13 changes: 4 additions & 9 deletions src/adapters/wormhole/WormholeReceiverAdapter.sol
Original file line number Diff line number Diff line change
Expand Up @@ -92,16 +92,11 @@ contract WormholeReceiverAdapter is BaseReceiverAdapter, IWormholeReceiver {
revert Error.INVALID_RECEIVER_ADAPTER();
}

/// @dev step-4: validate the destination
if (decodedPayload.finalDestination != receiverGAC.multiBridgeMsgReceiver()) {
revert Error.INVALID_FINAL_DESTINATION();
}

MessageLibrary.Message memory _data = abi.decode(decodedPayload.data, (MessageLibrary.Message));
(bool success, bytes memory lowLevelData) = decodedPayload.to.call{value: msg.value}(decodedPayload.data);

try IMultiBridgeMessageReceiver(decodedPayload.finalDestination).receiveMessage(_data) {
emit MessageIdExecuted(_data.srcChainId, msgId);
} catch (bytes memory lowLevelData) {
if (success) {
MessageIdExecuted(msgId);
} else {
revert MessageFailure(msgId, lowLevelData);
}
}
Expand Down
1 change: 0 additions & 1 deletion src/adapters/wormhole/WormholeSenderAdapter.sol
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ contract WormholeSenderAdapter is BaseSenderAdapter {
external
payable
override
onlyMultiBridgeMessageSender
returns (bytes32 msgId)
{
address receiverAdapter = receiverAdapters[_receiverChainId];
Expand Down
2 changes: 1 addition & 1 deletion src/interfaces/EIP5164/MessageExecutor.sol
Original file line number Diff line number Diff line change
Expand Up @@ -35,5 +35,5 @@ interface MessageExecutor {
/**
* @dev MessageIdExecuted MUST be emitted once a message or message batch has been executed.
*/
event MessageIdExecuted(uint256 indexed fromChainId, bytes32 indexed messageId);
event MessageIdExecuted(bytes32 indexed messageId);
}
5 changes: 2 additions & 3 deletions src/libraries/Types.sol
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
// SPDX-License-Identifier: GPL-3.0-only

pragma solidity >=0.8.20;

struct AdapterPayload {
bytes32 msgId;
address senderAdapterCaller;
address senderAdapter;
address receiverAdapter;
address finalDestination;
address to;
bytes data;
}
35 changes: 25 additions & 10 deletions src/token/xERC20.sol
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
// SPDX-License-Identifier: GPL-3.0-only
pragma solidity ^0.8.13;

/// library imports
import {ERC20} from "@openzeppelin/contracts/token/ERC20/ERC20.sol";

/// local imports
import {IXERC20} from "src/interfaces/EIP7281/IXERC20.sol";
import {IMessageSenderAdapter} from "src/interfaces/adapters/IMessageSenderAdapter.sol";

interface IMultiMessageSender {
function remoteCall(
Expand All @@ -21,6 +23,8 @@ interface IMultiMessageSender {
}

contract xERC20 is IXERC20, ERC20 {
uint256 public constant MULTI_BRIDGE_ID = 1;

IMultiMessageSender public mmaSender;
address public mmaReceiver;

Expand All @@ -29,6 +33,8 @@ contract xERC20 is IXERC20, ERC20 {
_;
}

mapping(uint256 bridgeId => address bridge) public bridgeAddress;

constructor(
string memory _name,
string memory _symbol,
Expand All @@ -43,18 +49,27 @@ contract xERC20 is IXERC20, ERC20 {
_mint(_initialOwner, 1e24);
}

function xChainTransfer(uint256 _dstChainId, uint256[] calldata _fees, address _receiver, uint256 _amount)
external
payable
{
function xChainTransfer(
uint256 _dstChainId,
uint256 _bridgeId,
uint256[] calldata _fees,
address _receiver,
uint256 _amount
) external payable {
_burn(msg.sender, _amount);

// assume CREATE2
// assume msg has 29 day expiration
// assume msg.sender as refund address
mmaSender.remoteCall{value: msg.value}(
_dstChainId, address(this), bytes(""), _amount, 29 days, msg.sender, _fees, 2, new address[](0)
);
if (_bridgeId == MULTI_BRIDGE_ID) {
// assume CREATE2
// assume msg has 29 day expiration
// assume msg.sender as refund address
mmaSender.remoteCall{value: msg.value}(
_dstChainId, address(this), bytes(""), _amount, 29 days, msg.sender, _fees, 2, new address[](0)
);
} else {
IMessageSenderAdapter(bridgeAddress[_bridgeId]).dispatchMessage{value: msg.value}(
_dstChainId, address(this), abi.encode(_receiver, _amount)
);
}
}

function setLockbox(address _lockbox) external override {
Expand Down

0 comments on commit 6d2b5d6

Please sign in to comment.