Skip to content

Commit

Permalink
add router bridge support
Browse files Browse the repository at this point in the history
  • Loading branch information
ss-sonic committed Sep 20, 2024
1 parent 262c3f2 commit d92b5a7
Show file tree
Hide file tree
Showing 5 changed files with 278 additions and 5 deletions.
52 changes: 52 additions & 0 deletions src/apps/nitro/RouterNitroApp.sol
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,29 @@ contract RouterNitroApp is AppAccountBase, IRouterNitroApp {
gateway.iDeposit{ value: msg.value }(depositData, destToken, recipient);
}

/**
* @dev This function is used to bridge mintable assets from one network to another.
* @param transferPayload The data related to the transfer.
*/
function bridgeMintableAssets(IRouterNitroGateway.TransferPayload memory transferPayload)
external
payable
nonReentrant
requiresAuthorizedOperationsParty
{
// Get the app beacon instance
IRouterNitroAppBeacon appBeacon = _getAppBeacon();

// Get the gateway instance from the app beacon
IRouterNitroGateway gateway = IRouterNitroGateway(appBeacon.routerAssetBridgeGateway());

// Approve the gateway to spend the source token on behalf of the contract
IERC20(transferPayload.srcTokenAddress).approve(address(gateway), transferPayload.srcTokenAmount);

// Call the transferToken function on the gateway
gateway.transferToken{ value: msg.value }(transferPayload);
}

/**
* @dev This function is used to update the bridge transaction data.
* @param srcToken The source token address.
Expand Down Expand Up @@ -131,6 +154,35 @@ contract RouterNitroApp is AppAccountBase, IRouterNitroApp {
gateway.iDepositMessage{ value: msg.value }(depositData, destToken, recipient, message);
}

/**
* @dev This function is used to bridge mintable assets with an instruction from one network to another.
* @param transferPayload The data related to the transfer.
* @param destGasLimit The destination gas limit.
* @param instruction The instruction to be sent along with the transaction.
*/
function bridgeMintableAssetsWithInstruction(
IRouterNitroGateway.TransferPayload memory transferPayload,
uint64 destGasLimit,
bytes calldata instruction
)
external
payable
nonReentrant
requiresAuthorizedOperationsParty
{
// Get the app beacon instance
IRouterNitroAppBeacon appBeacon = _getAppBeacon();

// Get the gateway instance from the app beacon
IRouterNitroGateway gateway = IRouterNitroGateway(appBeacon.routerAssetBridgeGateway());

// Approve the gateway to spend the source token on behalf of the contract
IERC20(transferPayload.srcTokenAddress).approve(address(gateway), transferPayload.srcTokenAmount);

// Call the transferTokenWithInstruction function on the gateway
gateway.transferTokenWithInstruction{ value: msg.value }(transferPayload, destGasLimit, instruction);
}

/**
* @dev Returns the beacon contract for the Curve StableSwap app.
* @return The beacon contract for the Curve StableSwap app.
Expand Down
23 changes: 22 additions & 1 deletion src/apps/nitro/RouterNitroAppBeacon.sol
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,39 @@
// SPDX-License-Identifier: MIT
pragma solidity >=0.8.21;

// Importing the base class for application beacons
import { AppBeaconBase } from "src/apps/base/AppBeaconBase.sol";

/**
* @title RouterNitroAppBeacon
* @dev This contract extends AppBeaconBase to provide a beacon for the RouterNitro application.
* The beacon points to the latest implementation of the application and stores addresses for the RouterNitroGateway and
* RouterAssetBridgeGateway.
*/
contract RouterNitroAppBeacon is AppBeaconBase {
// Address of the RouterNitroGateway contract
address public immutable routerNitroGateway;
// Address of the RouterAssetBridgeGateway contract
address public immutable routerAssetBridgeGateway;

/**
* @dev Constructs a new RouterNitroAppBeacon contract.
* @param _owner The owner of the contract.
* @param _latestAppImplementation The address of the latest implementation of the RouterNitro application.
* @param _routerNitroGateway The address of the RouterNitroGateway contract.
* @param _routerAssetBridgeGateway The address of the RouterAssetBridgeGateway contract.
*/
constructor(
address _owner,
address _latestAppImplementation,
address _routerNitroGateway
address _routerNitroGateway,
address _routerAssetBridgeGateway
)
// Call the constructor of the base class
AppBeaconBase(_owner, _latestAppImplementation, "RouterNitro")
{
// Initialize the RouterNitroGateway and RouterAssetBridgeGateway addresses
routerNitroGateway = _routerNitroGateway;
routerAssetBridgeGateway = _routerAssetBridgeGateway;
}
}
26 changes: 24 additions & 2 deletions src/interfaces/nitro/IRouterNitroApp.sol
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,17 @@ pragma solidity >=0.8.21;

import { IRouterNitroGateway } from "../nitro/IRouterNitroGateway.sol";

/// @title Interface for handler contracts that support deposits and deposit executions.
/// @author Router Protocol.
/**
* @title IRouterNitroApp
* @dev Interface for handler contracts that support deposits and deposit executions.
*/
interface IRouterNitroApp {
/**
* @dev Bridge assets from the source chain to the destination chain.
* @param depositData The deposit data containing the necessary information for the bridge transaction.
* @param destToken The destination token address.
* @param recipient The recipient address on the destination chain.
*/
function bridgeAssets(
IRouterNitroGateway.DepositData memory depositData,
bytes memory destToken,
Expand All @@ -14,6 +22,13 @@ interface IRouterNitroApp {
external
payable;

/**
* @dev Update the bridge transaction data.
* @param srcToken The source token address.
* @param feeAmount The fee amount for the bridge transaction.
* @param depositId The deposit ID.
* @param initiatewithdrawal Flag indicating whether to initiate a withdrawal on the destination chain.
*/
function updateBridgeTxnData(
address srcToken,
uint256 feeAmount,
Expand All @@ -23,6 +38,13 @@ interface IRouterNitroApp {
external
payable;

/**
* @dev Bridge assets from the source chain to the destination chain with a message.
* @param depositData The deposit data containing the necessary information for the bridge transaction.
* @param destToken The destination token address.
* @param recipient The recipient address on the destination chain.
* @param message The message to include in the bridge transaction.
*/
function bridgeAssetsWithMessage(
IRouterNitroGateway.DepositData memory depositData,
bytes memory destToken,
Expand Down
16 changes: 14 additions & 2 deletions src/interfaces/nitro/IRouterNitroAppBeacon.sol
Original file line number Diff line number Diff line change
@@ -1,8 +1,20 @@
// SPDX-License-Identifier: MIT
pragma solidity >=0.8.21;

/// @title Interface for handler contracts that support deposits and deposit executions.
/// @author Router Protocol.
/**
* @title IRouterNitroAppBeacon
* @dev Interface for the Nitro App Beacon Router.
*/
interface IRouterNitroAppBeacon {
/**
* @dev Returns the address of the Nitro Gateway Router.
* @return The address of the Nitro Gateway Router.
*/
function routerNitroGateway() external view returns (address);

/**
* @dev Returns the address of the Asset Bridge Gateway Router.
* @return The address of the Asset Bridge Gateway Router.
*/
function routerAssetBridgeGateway() external view returns (address);
}
Loading

0 comments on commit d92b5a7

Please sign in to comment.