Skip to content

Commit

Permalink
refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
0xIryna committed Jan 16, 2025
1 parent f3f8d96 commit a31ea0b
Show file tree
Hide file tree
Showing 15 changed files with 118 additions and 291 deletions.
15 changes: 2 additions & 13 deletions script/deploy/DeployBase.sol
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ contract DeployBase is Script, Utils {

struct HubConfiguration {
address mToken;
address smartMToken;
address registrar;
WormholeConfiguration wormhole;
}
Expand Down Expand Up @@ -133,12 +132,7 @@ contract DeployBase is Script, Utils {
}

function _deployHubPortal(address deployer_, HubConfiguration memory config_) internal returns (address) {
HubPortal implementation_ = new HubPortal(
config_.mToken,
config_.smartMToken,
config_.registrar,
config_.wormhole.chainId
);
HubPortal implementation_ = new HubPortal(config_.mToken, config_.registrar, config_.wormhole.chainId);
HubPortal hubPortalProxy_ = HubPortal(
_deployCreate3Proxy(address(implementation_), _computeSalt(deployer_, "Portal"))
);
Expand All @@ -156,10 +150,7 @@ contract DeployBase is Script, Utils {
address registrar_,
uint16 wormholeChainId_
) internal returns (address) {
// Pre-compute the expected SpokeSmartMToken proxy address.
address expectedSmartMTokenProxy_ = ContractHelper.getContractFrom(deployer_, _SPOKE_SMART_M_TOKEN_PROXY_NONCE);

SpokePortal implementation_ = new SpokePortal(mToken_, expectedSmartMTokenProxy_, registrar_, wormholeChainId_);
SpokePortal implementation_ = new SpokePortal(mToken_, registrar_, wormholeChainId_);
SpokePortal spokePortalProxy_ = SpokePortal(
_deployCreate3Proxy(address(implementation_), _computeSalt(deployer_, "Portal"))
);
Expand Down Expand Up @@ -349,11 +340,9 @@ contract DeployBase is Script, Utils {
console.log("Hub configuration for chain ID %s loaded:", chainId_);

hubConfig_.mToken = file_.readAddress(_readKey(hub_, "m_token"));
hubConfig_.smartMToken = file_.readAddress(_readKey(hub_, "smart_m_token"));
hubConfig_.registrar = file_.readAddress(_readKey(hub_, "registrar"));

console.log("M Token:", hubConfig_.mToken);
console.log("Smart M Token:", hubConfig_.smartMToken);
console.log("Registrar:", hubConfig_.registrar);

hubConfig_.wormhole = _loadWormholeConfig(file_, hub_);
Expand Down
17 changes: 2 additions & 15 deletions script/upgrade/UpgradeBase.sol
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ contract UpgradeBase is Script, Utils {

struct PortalConfiguration {
address mToken;
address smartMToken;
address registrar;
address portal;
uint16 wormholeChainId;
Expand Down Expand Up @@ -58,25 +57,15 @@ contract UpgradeBase is Script, Utils {
}

function _upgradeHubPortal(PortalConfiguration memory config_) internal {
HubPortal implementation_ = new HubPortal(
config_.mToken,
config_.smartMToken,
config_.registrar,
config_.wormholeChainId
);
HubPortal implementation_ = new HubPortal(config_.mToken, config_.registrar, config_.wormholeChainId);

console.log("HubPortal implementation deployed at: ", address(implementation_));

IManagerBase(config_.portal).upgrade(address(implementation_));
}

function _upgradeSpokePortal(PortalConfiguration memory config_) internal {
SpokePortal implementation_ = new SpokePortal(
config_.mToken,
config_.smartMToken,
config_.registrar,
config_.wormholeChainId
);
SpokePortal implementation_ = new SpokePortal(config_.mToken, config_.registrar, config_.wormholeChainId);

console.log("SpokePortal implementation deployed at: ", address(implementation_));

Expand All @@ -95,13 +84,11 @@ contract UpgradeBase is Script, Utils {
console.log("Portal configuration for chain ID %s loaded:", chainId_);

portalConfig_.mToken = file_.readAddress(_readKey(config_, "m_token"));
portalConfig_.smartMToken = file_.readAddress(_readKey(config_, "smart_m_token"));
portalConfig_.registrar = file_.readAddress(_readKey(config_, "registrar"));
portalConfig_.portal = file_.readAddress(_readKey(config_, "portal"));
portalConfig_.wormholeChainId = uint16(file_.readUint(_readKey(config_, "wormhole.chain_id")));

console.log("M Token:", portalConfig_.mToken);
console.log("Smart M Token:", portalConfig_.smartMToken);
console.log("Registrar:", portalConfig_.registrar);
console.log("Portal:", portalConfig_.portal);
console.log("Wormhole chain ID:", portalConfig_.wormholeChainId);
Expand Down
4 changes: 1 addition & 3 deletions src/HubPortal.sol
Original file line number Diff line number Diff line change
Expand Up @@ -33,16 +33,14 @@ contract HubPortal is IHubPortal, Portal {
/**
* @notice Constructs the contract.
* @param mToken_ The address of the M token to bridge.
* @param smartMToken_ The address of the Smart M token to bridge.
* @param registrar_ The address of the Registrar.
* @param chainId_ Wormhole chain id.
*/
constructor(
address mToken_,
address smartMToken_,
address registrar_,
uint16 chainId_
) Portal(mToken_, smartMToken_, registrar_, Mode.LOCKING, chainId_) {}
) Portal(mToken_, registrar_, Mode.LOCKING, chainId_) {}

/* ============ Interactive Functions ============ */

Expand Down
125 changes: 46 additions & 79 deletions src/Portal.sol
Original file line number Diff line number Diff line change
Expand Up @@ -37,30 +37,25 @@ abstract contract Portal is NttManagerNoRateLimiting, IPortal {
address public immutable registrar;

/// @inheritdoc IPortal
address public immutable smartMToken;

/// @inheritdoc IPortal
mapping(uint16 remoteChainId => bytes32 smartMToken) public remoteSmartMToken;
mapping(address sourceWrappedToken => mapping(uint16 destinationChainId => bytes32 destinationWrappedToken))
public destinationWrappedMToken;

/* ============ Constructor ============ */

/**
* @notice Constructs the contract.
* @param mToken_ The address of the M token to bridge.
* @param smartMToken_ The address of the Smart M token to bridge.
* @param registrar_ The address of the Registrar.
* @param mode_ The NttManager token transfer mode - LOCKING or BURNING.
* @param chainId_ The Wormhole chain id.
*/
constructor(
address mToken_,
address smartMToken_,
address registrar_,
Mode mode_,
uint16 chainId_
) NttManagerNoRateLimiting(mToken_, mode_, chainId_) {
if (mToken_ == address(0)) revert ZeroMToken();
if ((smartMToken = smartMToken_) == address(0)) revert ZeroSmartMToken();
if ((registrar = registrar_) == address(0)) revert ZeroRegistrar();
}

Expand All @@ -79,47 +74,64 @@ abstract contract Portal is NttManagerNoRateLimiting, IPortal {
/* ============ External Interactive Functions ============ */

/// @inheritdoc IPortal
function setRemoteSmartMToken(uint16 remoteChainId_, bytes32 smartMToken_) external onlyOwner {
if (remoteChainId_ == chainId) revert InvalidRemoteChain(remoteChainId_);

remoteSmartMToken[remoteChainId_] = smartMToken_;
emit RemoteSmartMTokenSet(remoteChainId_, smartMToken_);
}

/// @inheritdoc IPortal
function transferSmartMToken(
uint256 amount_,
function setDestinationWrappedMToken(
address sourceWrappedToken_,
uint16 destinationChainId_,
bytes32 recipient_,
bytes32 refundAddress_
) external payable returns (bytes32 messageId_) {
messageId_ = _transferWrappedMToken(
amount_,
smartMToken,
remoteSmartMToken[destinationChainId_],
destinationChainId_,
recipient_,
refundAddress_
);
bytes32 destinationWrappedToken_
) external onlyOwner {
if (destinationChainId_ == chainId) revert InvalidDestinationChain(destinationChainId_);

destinationWrappedMToken[sourceWrappedToken_][destinationChainId_] = destinationWrappedToken_;
emit DestinationWrappedMTokenSet(sourceWrappedToken_, destinationChainId_, destinationWrappedToken_);
}

/// @inheritdoc IPortal
function transferWrappedMToken(
uint256 amount_,
address sourceWrappedToken_,
bytes32 destinationWrappedToken_,
uint16 destinationChainId_,
bytes32 recipient_,
bytes32 refundAddress_
) external payable returns (bytes32 messageId_) {
messageId_ = _transferWrappedMToken(
amount_,
sourceWrappedToken_,
if (amount_ == 0) revert ZeroAmount();
if (recipient_ == bytes32(0)) revert InvalidRecipient();
if (refundAddress_ == bytes32(0)) revert InvalidRefundAddress();

bytes32 destinationWrappedToken_ = destinationWrappedMToken[sourceWrappedToken_][destinationChainId_];

if (destinationWrappedToken_ == bytes32(0))
revert UnsupportedDestinationToken(sourceWrappedToken_, destinationChainId_);

// transfer Wrapped M from the sender
IERC20(sourceWrappedToken_).transferFrom(msg.sender, address(this), amount_);

// unwrap Wrapped M token to M Token
amount_ = IWrappedMTokenLike(sourceWrappedToken_).unwrap(address(this), amount_);

// NOTE: the following code has been adapted from NTT manager `transfer` or `_transferEntryPoint` functions.
// We cannot call those functions directly here as they attempt to transfer M Token from the msg.sender.

uint64 sequence_ = _useMessageSequence();
uint128 index_ = _currentIndex();

TransceiverStructs.NttManagerMessage memory message_;
(, message_, messageId_) = _encodeTokenTransfer(
_trimTransferAmount(amount_, destinationChainId_),
index_,
recipient_,
destinationWrappedToken_,
destinationChainId_,
recipient_,
refundAddress_
sequence_,
msg.sender
);

uint256 totalPriceQuote_ = _sendMessage(destinationChainId_, refundAddress_, message_);

emit MTokenSent(destinationChainId_, messageId_, msg.sender, recipient_, amount_, index_);

// Emit NTT events
emit TransferSent(recipient_, refundAddress_, amount_, totalPriceQuote_, destinationChainId_, sequence_);
emit TransferSent(messageId_);
}
/* ============ Internal/Private Interactive Functions ============ */

Expand Down Expand Up @@ -181,51 +193,6 @@ abstract contract Portal is NttManagerNoRateLimiting, IPortal {
messageId_ = TransceiverStructs.nttManagerMessageDigest(chainId, message_);
}

/// @dev Transfers a Wrapped M token to the destination chain by unwrapping it to M Token
function _transferWrappedMToken(
uint256 amount_,
address sourceWrappedToken_,
bytes32 destinationWrappedToken_,
uint16 destinationChainId_,
bytes32 recipient_,
bytes32 refundAddress_
) private returns (bytes32 messageId_) {
if (amount_ == 0) revert ZeroAmount();
if (recipient_ == bytes32(0)) revert InvalidRecipient();
if (refundAddress_ == bytes32(0)) revert InvalidRefundAddress();

// transfer Wrapped M from the sender
IERC20(sourceWrappedToken_).transferFrom(msg.sender, address(this), amount_);

// unwrap Wrapped M token to M Token
amount_ = IWrappedMTokenLike(sourceWrappedToken_).unwrap(address(this), amount_);

// NOTE: the following code has been adapted from NTT manager `transfer` or `_transferEntryPoint` functions.
// We cannot call those functions directly here as they attempt to transfer M Token from the msg.sender.

uint64 sequence_ = _useMessageSequence();
uint128 index_ = _currentIndex();

TransceiverStructs.NttManagerMessage memory message_;
(, message_, messageId_) = _encodeTokenTransfer(
_trimTransferAmount(amount_, destinationChainId_),
index_,
recipient_,
destinationWrappedToken_,
destinationChainId_,
sequence_,
msg.sender
);

uint256 totalPriceQuote_ = _sendMessage(destinationChainId_, refundAddress_, message_);

emit MTokenSent(destinationChainId_, messageId_, msg.sender, recipient_, amount_, index_);

// Emit NTT events
emit TransferSent(recipient_, refundAddress_, amount_, totalPriceQuote_, destinationChainId_, sequence_);
emit TransferSent(messageId_);
}

/// @notice Sends a generic message to the destination chain.
/// @dev The implementation is adapted from `NttManager` `_transfer` function.
function _sendMessage(
Expand Down
4 changes: 1 addition & 3 deletions src/SpokePortal.sol
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,14 @@ contract SpokePortal is ISpokePortal, Portal {
/**
* @notice Constructs the contract.
* @param mToken_ The address of the M token to bridge.
* @param smartMToken_ The address of the Smart M token to bridge.
* @param registrar_ The address of the Registrar.
* @param chainId_ Wormhole chain id.
*/
constructor(
address mToken_,
address smartMToken_,
address registrar_,
uint16 chainId_
) Portal(mToken_, smartMToken_, registrar_, Mode.BURNING, chainId_) {}
) Portal(mToken_, registrar_, Mode.BURNING, chainId_) {}

/* ============ Internal/Private Interactive Functions ============ */

Expand Down
15 changes: 2 additions & 13 deletions src/governance/Migrator.sol
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ abstract contract Migrator is IMigrator {
/// @dev Portal migration parameters.
struct PortalMigrateParams {
address mToken;
address smartMToken;
address registrar;
uint16 wormholeChainId;
}
Expand Down Expand Up @@ -67,12 +66,7 @@ abstract contract Migrator is IMigrator {
* @param params_ The parameters for the migrate.
*/
function _migrateHubPortal(PortalMigrateParams memory params_) internal {
HubPortal implementation_ = new HubPortal(
params_.mToken,
params_.smartMToken,
params_.registrar,
params_.wormholeChainId
);
HubPortal implementation_ = new HubPortal(params_.mToken, params_.registrar, params_.wormholeChainId);
IManagerBase(portal).upgrade(address(implementation_));
}

Expand All @@ -81,12 +75,7 @@ abstract contract Migrator is IMigrator {
* @param params_ The parameters for the migrate.
*/
function _migrateSpokePortal(PortalMigrateParams memory params_) internal {
SpokePortal implementation_ = new SpokePortal(
params_.mToken,
params_.smartMToken,
params_.registrar,
params_.wormholeChainId
);
SpokePortal implementation_ = new SpokePortal(params_.mToken, params_.registrar, params_.wormholeChainId);
IManagerBase(portal).upgrade(address(implementation_));
}

Expand Down
Loading

0 comments on commit a31ea0b

Please sign in to comment.