Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 65 additions & 0 deletions scripts/Adapters/DeployWormholeExecutorAdapter.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
// SPDX-License-Identifier: BUSL-1.1
pragma solidity ^0.8.0;

import {WormholeExecutorAdapter, IWormholeExecutorAdapter, IBaseAdapter} from '../../src/contracts/adapters/wormhole/WormholeExecutorAdapter.sol';
import './BaseAdapterScript.sol';

library WormholeExecutorAdapterDeploymentHelper {
struct WormholeExecutorAdapterArgs {
BaseAdapterArgs baseArgs;
address wormholeCore;
address executorQuoterRouter;
address quoter;
address refundAddress;
}

function getAdapterCode(
WormholeExecutorAdapterArgs memory wormholeArgs
) internal pure returns (bytes memory) {
bytes memory creationCode = type(WormholeExecutorAdapter).creationCode;

return
abi.encodePacked(
creationCode,
abi.encode(
wormholeArgs.baseArgs.crossChainController,
wormholeArgs.wormholeCore,
wormholeArgs.executorQuoterRouter,
wormholeArgs.quoter,
wormholeArgs.refundAddress,
wormholeArgs.baseArgs.providerGasLimit,
wormholeArgs.baseArgs.trustedRemotes
)
);
}
}

abstract contract BaseWormholeExecutorAdapter is BaseAdapterScript {
function WORMHOLE_CORE() internal view virtual returns (address);

function EXECUTOR_QUOTER_ROUTER() internal view virtual returns (address);

function QUOTER() internal view virtual returns (address);

/// @dev refund address is only relevant on the sending side; receive-only deployments may return 0
function REFUND_ADDRESS() internal view virtual returns (address);

function _getAdapterByteCode(
BaseAdapterArgs memory baseArgs
) internal view override returns (bytes memory) {
require(WORMHOLE_CORE() != address(0), 'Wormhole core can not be 0');
require(EXECUTOR_QUOTER_ROUTER() != address(0), 'Wormhole quoter router can not be 0');
require(QUOTER() != address(0), 'Wormhole quoter can not be 0');

return
WormholeExecutorAdapterDeploymentHelper.getAdapterCode(
WormholeExecutorAdapterDeploymentHelper.WormholeExecutorAdapterArgs({
baseArgs: baseArgs,
wormholeCore: WORMHOLE_CORE(),
executorQuoterRouter: EXECUTOR_QUOTER_ROUTER(),
quoter: QUOTER(),
refundAddress: REFUND_ADDRESS()
})
);
}
}
Binary file not shown.
2 changes: 1 addition & 1 deletion src/contracts/adapters/BaseAdapter.sol
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ abstract contract BaseAdapter is IBaseAdapter {
* @param _payload bytes containing the bridged message
* @param originChainId id of the chain where the message originated
*/
function _registerReceivedMessage(bytes calldata _payload, uint256 originChainId) internal {
function _registerReceivedMessage(bytes memory _payload, uint256 originChainId) internal {
// this method should be always called via call
require(address(this) == _selfAddress, Errors.DELEGATE_CALL_FORBIDDEN);
CROSS_CHAIN_CONTROLLER.receiveCrossChainMessage(_payload, originChainId);
Expand Down
46 changes: 46 additions & 0 deletions src/contracts/adapters/wormhole/IWormholeExecutorAdapter.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;

/**
* @title IWormholeExecutorAdapter
* @author Aave Labs
* @notice interface containing the methods definitions used in the Wormhole Executor bridge adapter
*/
interface IWormholeExecutorAdapter {
/**
* @notice method to get the Wormhole Core bridge address
* @return address of the Wormhole Core bridge
*/
function WORMHOLE_CORE() external view returns (address);

/**
* @notice method to get the Wormhole Executor quoter router address
* @return address of the Wormhole Executor quoter router
*/
function EXECUTOR_QUOTER_ROUTER() external view returns (address);

/**
* @notice method to get the on-chain quoter address of the chosen relay provider
* @return address of the relay provider quoter
*/
function QUOTER() external view returns (address);

/**
* @notice method to get the refund address on destination chain
* @return address that will receive the refunds
* @dev should be CrossChainController on destination chain
*/
function REFUND_ADDRESS() external view returns (address);

/**
* @notice method to get the Wormhole chain id of the chain where this adapter is deployed
* @return the Wormhole chain id used as the emitter chain of published messages
*/
function WORMHOLE_SOURCE_CHAIN_ID() external view returns (uint16);

/**
* @notice method to get the Wormhole consistency (finality) level used when publishing messages
* @return the consistency level (chain specific, eg. 202 = finalized on Ethereum, 0 on Monad)
*/
function CONSISTENCY_LEVEL() external view returns (uint8);
}
256 changes: 256 additions & 0 deletions src/contracts/adapters/wormhole/WormholeExecutorAdapter.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,256 @@
// SPDX-License-Identifier: BUSL-1.1
pragma solidity ^0.8.0;

import {ChainIds} from 'solidity-utils/contracts/utils/ChainHelpers.sol';
import {SafeCast} from 'openzeppelin-contracts/contracts/utils/math/SafeCast.sol';
import {ICoreBridge, CoreBridgeVM} from './interfaces/ICoreBridge.sol';
import {IExecutorQuoterRouter} from './interfaces/IExecutorQuoterRouter.sol';
import {IVaaV1Receiver} from './interfaces/IVaaV1Receiver.sol';
import {IWormholeExecutorAdapter} from './IWormholeExecutorAdapter.sol';
import {BaseAdapter, IBaseAdapter} from '../BaseAdapter.sol';

import {Errors} from '../../libs/Errors.sol';
import {WormholeExecutorEncoding} from './libs/WormholeExecutorEncoding.sol';


/**
* @title WormholeExecutorAdapter
* @author Aave Labs
* @notice Wormhole bridge adapter built on the Wormhole Executor framework. Used to send and receive
* messages cross chain on networks that do not have the legacy Wormhole standard relayer
* (eg. Monad).
* @dev to bridge a message it publishes it to the Wormhole Core bridge and requests its relaying via the
* Executor quoter router using an on-chain quote. It uses the eth balance of the CrossChainController
* contract to pay for message bridging as `forwardMessage` is called via delegate call.
* @dev delivery on the destination chain is permissionless (anyone may call `executeVAAv1`), so the
* received VAA is verified against the Wormhole Core bridge and its emitter checked against the
* configured trusted remotes.
*/
contract WormholeExecutorAdapter is BaseAdapter, IWormholeExecutorAdapter, IVaaV1Receiver {
using WormholeExecutorEncoding for *;

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unused ? I see call using WormholeExecutorEncoding.xxx() instead ?


/// @inheritdoc IWormholeExecutorAdapter
address public immutable WORMHOLE_CORE;

/// @inheritdoc IWormholeExecutorAdapter
address public immutable EXECUTOR_QUOTER_ROUTER;

/// @inheritdoc IWormholeExecutorAdapter
address public immutable QUOTER;

/// @inheritdoc IWormholeExecutorAdapter
address public immutable REFUND_ADDRESS;

/// @inheritdoc IWormholeExecutorAdapter
uint16 public immutable WORMHOLE_SOURCE_CHAIN_ID;

/// @inheritdoc IWormholeExecutorAdapter
uint8 public constant CONSISTENCY_LEVEL = 202;

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Interface natspec says this should be chain specific, but it's hardcoded here ?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah i'll fix the natspec, but checked with wormhole team and this number acts as a sentinel value for 'finalised' consistency across chains

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay, thanks for the explanation. Good for me to amend the Natspec to mention it clearly then.


/**
* @param crossChainController address of the cross chain controller that will use this bridge adapter
* @param wormholeCore Wormhole Core bridge entry point address
* @param executorQuoterRouter Wormhole Executor on-chain quoter router address
* @param quoter on-chain quoter address of the chosen relay provider
* @param refundAddress address that will receive left over gas on destination
* @param providerGasLimit base gas limit used by the bridge adapter
* @param trustedRemotes list of remote configurations to set as trusted
*/
constructor(
address crossChainController,
address wormholeCore,
address executorQuoterRouter,
address quoter,
address refundAddress,
uint256 providerGasLimit,
TrustedRemotesConfig[] memory trustedRemotes
)
BaseAdapter(crossChainController, providerGasLimit, 'Wormhole Executor adapter', trustedRemotes)
{
require(wormholeCore != address(0), Errors.WORMHOLE_CORE_CANT_BE_ADDRESS_0);
require(executorQuoterRouter != address(0), Errors.WORMHOLE_QUOTER_ROUTER_CANT_BE_ADDRESS_0);
require(quoter != address(0), Errors.WORMHOLE_QUOTER_CANT_BE_ADDRESS_0);

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why not asserting refundAddress as well ? When do we want it to be address(0) ?

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fwiw the refund goes to refundAddr on the source chain so natspec saying destination is off. BNB and Monad are deployed with 0 but they only receive so it’s fine.

WORMHOLE_CORE = wormholeCore;
EXECUTOR_QUOTER_ROUTER = executorQuoterRouter;
QUOTER = quoter;
REFUND_ADDRESS = refundAddress;
WORMHOLE_SOURCE_CHAIN_ID = ICoreBridge(wormholeCore).chainId();
}

/// @inheritdoc IBaseAdapter
function forwardMessage(
address receiver,
uint256 executionGasLimit,
uint256 destinationChainId,
bytes calldata message
) external returns (address, uint256) {
uint16 nativeChainId = SafeCast.toUint16(infraToNativeChainId(destinationChainId));
require(nativeChainId != uint16(0), Errors.DESTINATION_CHAIN_ID_NOT_SUPPORTED);
require(receiver != address(0), Errors.RECEIVER_NOT_SET);

bytes memory relayInstructions = WormholeExecutorEncoding.encodeGas(

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

executeVAAv1 verifies guardian signatures inside the relayed gas limit, but BASE_GAS_LIMIT defaults to 0. Suggest requiring a non-zero value sized via fork test, or adding verification cost when building relayInstructions.

SafeCast.toUint128(executionGasLimit + BASE_GAS_LIMIT),
0
);

uint64 sequence = _publishAndRequest(
nativeChainId,
bytes32(uint256(uint160(receiver))),
relayInstructions,
message
);

return (EXECUTOR_QUOTER_ROUTER, uint256(sequence));
}

/**
* @notice Publishes the message to the Wormhole Core bridge and requests its relaying via the Executor
* @return sequence the Wormhole sequence number of the published message
*/
function _publishAndRequest(
uint16 nativeChainId,
bytes32 dstAddr,
bytes memory relayInstructions,
bytes calldata message
) private returns (uint64 sequence) {
bytes32 emitter = bytes32(uint256(uint160(address(this))));

// quote using a placeholder sequence: the executor fee does not depend on the sequence number

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can the quoter's price ever depend on the message number? The fee is quoted for placeholder sequence 0 but paid for the real one. Would quoting after publishMessage be safer?

uint256 executorFee = IExecutorQuoterRouter(EXECUTOR_QUOTER_ROUTER).quoteExecution(
nativeChainId,
dstAddr,
REFUND_ADDRESS,
QUOTER,
WormholeExecutorEncoding.encodeVaaMultiSigRequest(WORMHOLE_SOURCE_CHAIN_ID, emitter, 0),
relayInstructions
);

uint256 messageFee = ICoreBridge(WORMHOLE_CORE).messageFee();
require(
executorFee + messageFee <= address(this).balance,
Errors.NOT_ENOUGH_VALUE_TO_PAY_BRIDGE_FEES
);

// the message is the a.DI Transaction envelope, which already carries the destination chain id and is
// validated by the destination CrossChainController, so it is published as-is
sequence = ICoreBridge(WORMHOLE_CORE).publishMessage{value: messageFee}(
0, // nonce, unused (uniqueness comes from the auto-incremented sequence)
message,
CONSISTENCY_LEVEL
);

IExecutorQuoterRouter(EXECUTOR_QUOTER_ROUTER).requestExecution{value: executorFee}(
nativeChainId,
dstAddr,
REFUND_ADDRESS,
QUOTER,
WormholeExecutorEncoding.encodeVaaMultiSigRequest(WORMHOLE_SOURCE_CHAIN_ID, emitter, sequence),
relayInstructions
);
}

/// @inheritdoc IVaaV1Receiver
function executeVAAv1(bytes memory multiSigVaa) external payable {
(CoreBridgeVM memory vm, bool valid, string memory reason) = ICoreBridge(WORMHOLE_CORE)
.parseAndVerifyVM(multiSigVaa);
require(valid, bytes(reason).length == 0 ? Errors.INVALID_VAA : reason);

address srcAddress = address(uint160(uint256(vm.emitterAddress)));
uint256 originChainId = nativeToInfraChainId(vm.emitterChainId);
require(
_trustedRemotes[originChainId] == srcAddress && srcAddress != address(0),
Errors.REMOTE_NOT_TRUSTED
);

// delivery via `executeVAAv1` is permissionless, but the CrossChainController deduplicates messages
// per adapter and validates the envelope's destination chain id, so no extra replay/destination guard
// is needed here
_registerReceivedMessage(vm.payload, originChainId);
}

/// @inheritdoc IBaseAdapter
function nativeToInfraChainId(
uint256 nativeChainId
) public pure virtual override returns (uint256) {
if (nativeChainId == 2) {
return ChainIds.ETHEREUM;
} else if (nativeChainId == 4) {
return ChainIds.BNB;
} else if (nativeChainId == 5) {
return ChainIds.POLYGON;
} else if (nativeChainId == 6) {
return ChainIds.AVALANCHE;
} else if (nativeChainId == 14) {
return ChainIds.CELO;
} else if (nativeChainId == 23) {
return ChainIds.ARBITRUM;
} else if (nativeChainId == 24) {
return ChainIds.OPTIMISM;
} else if (nativeChainId == 30) {
return ChainIds.BASE;
} else if (nativeChainId == 34) {
return ChainIds.SCROLL;
} else if (nativeChainId == 35) {
return ChainIds.MANTLE;
} else if (nativeChainId == 37) {
return ChainIds.XLAYER;
} else if (nativeChainId == 38) {
return ChainIds.LINEA;
} else if (nativeChainId == 46) {
return ChainIds.INK;
} else if (nativeChainId == 48) {
return ChainIds.MONAD;
} else if (nativeChainId == 52) {
return ChainIds.SONIC;
} else if (nativeChainId == 58) {
return ChainIds.PLASMA;
} else if (nativeChainId == 64) {
return ChainIds.MEGAETH;
} else {
return 0;
}
}

/// @inheritdoc IBaseAdapter
function infraToNativeChainId(
uint256 infraChainId
) public pure virtual override returns (uint256) {
if (infraChainId == ChainIds.ETHEREUM) {
return 2;
} else if (infraChainId == ChainIds.BNB) {
return 4;
} else if (infraChainId == ChainIds.POLYGON) {
return 5;
} else if (infraChainId == ChainIds.AVALANCHE) {
return 6;
} else if (infraChainId == ChainIds.CELO) {
return 14;
} else if (infraChainId == ChainIds.ARBITRUM) {
return 23;
} else if (infraChainId == ChainIds.OPTIMISM) {
return 24;
} else if (infraChainId == ChainIds.BASE) {
return 30;
} else if (infraChainId == ChainIds.SCROLL) {
return 34;
} else if (infraChainId == ChainIds.MANTLE) {
return 35;
} else if (infraChainId == ChainIds.XLAYER) {
return 37;
} else if (infraChainId == ChainIds.LINEA) {
return 38;
} else if (infraChainId == ChainIds.INK) {
return 46;
} else if (infraChainId == ChainIds.MONAD) {
return 48;
} else if (infraChainId == ChainIds.SONIC) {
return 52;
} else if (infraChainId == ChainIds.PLASMA) {
return 58;
} else if (infraChainId == ChainIds.MEGAETH) {
return 64;
} else {
return 0;
}
}
}
Loading
Loading