Skip to content

Commit

Permalink
[In progress] Add parsing of LZ metadata
Browse files Browse the repository at this point in the history
  • Loading branch information
ltyu committed Dec 22, 2023
1 parent 9bf01ca commit 122d953
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 5 deletions.
73 changes: 71 additions & 2 deletions solidity/contracts/hooks/LayerZeroHook.sol
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,29 @@ import {IPostDispatchHook} from "../interfaces/hooks/IPostDispatchHook.sol";
import {AbstractPostDispatchHook} from "./libs/AbstractPostDispatchHook.sol";
import {StandardHookMetadata} from "./libs/StandardHookMetadata.sol";

interface ILayerZeroEndpoint {
// @notice gets a quote in source native gas, for the amount that send() requires to pay for message delivery
// @param _dstChainId - the destination chain identifier
// @param _userApplication - the user app address on this EVM chain
// @param _payload - the custom message to send over LayerZero
// @param _payInZRO - if false, user app pays the protocol fee in native token
// @param _adapterParam - parameters for the adapter service, e.g. send some dust native token to dstChain
function estimateFees(
uint16 _dstChainId,
address _userApplication,
bytes calldata _payload,
bool _payInZRO,
bytes calldata _adapterParam
) external view returns (uint nativeFee, uint zroFee);
}

contract LayerZeroHook is AbstractPostDispatchHook, MailboxClient, Indexed {
constructor(address _mailbox) MailboxClient(_mailbox) {}
using StandardHookMetadata for bytes;
ILayerZeroEndpoint immutable lZEndpoint;

constructor(address _mailbox, address _lZEndpoint) MailboxClient(_mailbox) {
lZEndpoint = ILayerZeroEndpoint(_lZEndpoint);
}

// ============ External Functions ============

Expand All @@ -40,5 +61,53 @@ contract LayerZeroHook is AbstractPostDispatchHook, MailboxClient, Indexed {
function _quoteDispatch(
bytes calldata metadata,
bytes calldata message
) internal view virtual override returns (uint256) {}
) internal view virtual override returns (uint256) {
bytes calldata lZMetadata = metadata.getCustomMetadata();
(uint16 dstChainId, address userApplication) = parseLzMetadata(
lZMetadata
);
// return lZEndpoint.estimateFees();
}

function formatLzMetadata(
uint16 _dstChainId,
address _userApplication,
address _zroPaymentAddress,
address payable _refundAddress,
bytes calldata _payload,
bytes calldata _destination,
bytes calldata _adapterParam
) public view returns (bytes memory) {
return
abi.encodePacked(
_dstChainId,
_userApplication,
_zroPaymentAddress,
_refundAddress,
_payload,
_destination,
_adapterParam
);
}

function parseLzMetadata(
bytes calldata lZMetadata
) public view returns (uint16 dstChainId, address userApplication) {
dstChainId = _getDstChainId(lZMetadata);
userApplication = _getUserApplication(lZMetadata);

// Checks if _zroPaymentAddress is set, then set _payInZRO
}

function _getDstChainId(
bytes calldata lZMetadata
) internal pure returns (uint16) {
return uint16(bytes2(lZMetadata[0:16]));
}

function _getUserApplication(
bytes calldata lZMetadata
) internal pure returns (address) {
return address(bytes20(lZMetadata[16:36]));
}
}
10 changes: 7 additions & 3 deletions solidity/test/hooks/LayerZeroHook.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,17 @@ contract LayerZeroHookTest is Test {
function setUp() public {
lZEndpointMock = new LZEndpointMock(uint16(block.chainid));
mailbox = new TestMailbox(0);
hook = new LayerZeroHook(address(mailbox));
hook = new LayerZeroHook(address(lZEndpointMock), address(mailbox));
}

// function testPostDispatch_emit() public {}
function testParseLzMetadata_ReturnsCorrectData() public {
// format metadata
}

function testQuoteDispatch() public {
assertEq(hook.quoteDispatch("", ""), 0);
// Build metadata to include LZ-specific data

assertEq(hook.quoteDispatch("", ""), 252);
}

function testHookType() public {
Expand Down

0 comments on commit 122d953

Please sign in to comment.