From eae219e94a4b46b54d0d6bcd7d9d38d832a2c56d Mon Sep 17 00:00:00 2001 From: Vincent Geddes <117534+vgeddes@users.noreply.github.com> Date: Sun, 27 Oct 2024 18:49:22 +0200 Subject: [PATCH] review feedback --- contracts/src/Gateway.sol | 24 ++++++++++++++++------- contracts/src/SubstrateTypes.sol | 28 +++++++++++++++++++++------ contracts/src/interfaces/IGateway.sol | 19 +++++++++++++----- contracts/src/v2/Calls.sol | 25 +++++++++++++++++------- contracts/src/v2/Types.sol | 3 ++- 5 files changed, 73 insertions(+), 26 deletions(-) diff --git a/contracts/src/Gateway.sol b/contracts/src/Gateway.sol index a8468a34c5..794d7c3b4d 100644 --- a/contracts/src/Gateway.sol +++ b/contracts/src/Gateway.sol @@ -500,21 +500,31 @@ contract Gateway is IGateway, IInitializable, IUpgradable { return true; } + function v2_isDispatched(uint64 nonce) external view returns (bool) { + CoreStorage.Layout storage $ = CoreStorage.layout(); + return $.inboundNonce.get(nonce); + } + // See docs for `IGateway.sendMessage` - function sendMessage(bytes calldata xcm, bytes[] calldata assets) external payable { - CallsV2.sendMessage(xcm, assets); + function v2_sendMessage( + bytes calldata xcm, + bytes[] calldata assets, + bytes calldata claimer + ) external payable { + CallsV2.sendMessage(xcm, assets, claimer); } // See docs for `IGateway.registerToken` - function registerToken(address token, uint128 xcmFeeAHP) external payable { + function v2_registerToken(address token, uint128 xcmFeeAHP) external payable { CallsV2.registerToken(token, xcmFeeAHP); } // See docs for `IGateway.registerTokenOnKusama` - function registerTokenOnKusama(address token, uint128 xcmFeeAHP, uint128 xcmFeeAHK) - external - payable - { + function v2_registerTokenOnKusama( + address token, + uint128 xcmFeeAHP, + uint128 xcmFeeAHK + ) external payable { CallsV2.registerTokenOnKusama(token, xcmFeeAHP, xcmFeeAHK); } diff --git a/contracts/src/SubstrateTypes.sol b/contracts/src/SubstrateTypes.sol index f0554f3afa..7e2227a522 100644 --- a/contracts/src/SubstrateTypes.sol +++ b/contracts/src/SubstrateTypes.sol @@ -54,6 +54,15 @@ library SubstrateTypes { } } + // solhint-disable-next-line func-name-mixedcase + function OptionVecU8(bytes memory v) internal pure returns (bytes memory) { + if (v.length == 0) { + return hex"00"; + } else { + return bytes.concat(bytes1(0x01), VecU8(v)); + } + } + /** * @dev SCALE-encodes `router_primitives::inbound::VersionedMessage` containing payload * `NativeTokensMessage::Create` @@ -213,14 +222,21 @@ library SubstrateTypes { // origin: H160, // assets: Vec // xcm: Vec + // claimer: Option> // } + // ``` // - function encodePayloadV2(address origin, bytes[] memory assets, bytes memory xcm) - internal - pure - returns (bytes memory) - { - return bytes.concat(abi.encodePacked(origin), VecAsset(assets), VecU8(xcm)); + // + // + function encodePayloadV2( + address origin, + bytes[] memory assets, + bytes memory xcm, + bytes memory claimer + ) internal pure returns (bytes memory) { + return bytes.concat( + abi.encodePacked(origin), VecAsset(assets), VecU8(xcm), OptionVecU8(claimer) + ); } // Encode `Vec` diff --git a/contracts/src/interfaces/IGateway.sol b/contracts/src/interfaces/IGateway.sol index e3f8280fdd..b381c187f6 100644 --- a/contracts/src/interfaces/IGateway.sol +++ b/contracts/src/interfaces/IGateway.sol @@ -194,15 +194,24 @@ interface IGateway { // at least cover the total cost of delivery to Polkadot. This ether be sent across // the bridge as WETH, and given to the relayer as compensation and incentivization. // - function sendMessage(bytes calldata xcm, bytes[] calldata assets) external payable; + function v2_sendMessage( + bytes calldata xcm, + bytes[] calldata assets, + bytes calldata claimer + ) external payable; // Register Ethereum-native token on AHP, using `xcmFeeAHP` of `msg.value` // to pay for execution on AHP. - function registerToken(address token, uint128 xcmFeeAHP) external payable; + function v2_registerToken(address token, uint128 xcmFeeAHP) external payable; // Register Ethereum-native token on AHK, using `xcmFeeAHP` and `xcmFeeAHK` // of `msg.value` to pay for execution on AHP and AHK respectively. - function registerTokenOnKusama(address token, uint128 xcmFeeAHP, uint128 xcmFeeAHK) - external - payable; + function v2_registerTokenOnKusama( + address token, + uint128 xcmFeeAHP, + uint128 xcmFeeAHK + ) external payable; + + // Check if an inbound message was previously accepted and dispatched + function v2_isDispatched(uint64 nonce) external returns (bool); } diff --git a/contracts/src/v2/Calls.sol b/contracts/src/v2/Calls.sol index 6260242a9b..48f086aaec 100644 --- a/contracts/src/v2/Calls.sol +++ b/contracts/src/v2/Calls.sol @@ -70,8 +70,12 @@ library CallsV2 { // 2. Handle the assets in holding, either depositing them into // some account, or forwarding them to another destination. // - function sendMessage(bytes calldata xcm, bytes[] calldata assets) external { - _sendMessage(msg.sender, xcm, assets, msg.value); + function sendMessage( + bytes calldata xcm, + bytes[] calldata assets, + bytes calldata claimer + ) external { + _sendMessage(msg.sender, xcm, assets, claimer, msg.value); } // Register Ethereum-native token on AHP, using `xcmFeeAHP` of `msg.value` @@ -96,6 +100,7 @@ library CallsV2 { address origin, bytes memory xcm, bytes[] memory assets, + bytes memory claimer, uint256 reward ) internal { if (assets.length > MAX_ASSETS) { @@ -106,8 +111,13 @@ library CallsV2 { for (uint256 i = 0; i < assets.length; i++) { encodedAssets[i] = _handleAsset(assets[i]); } - Ticket memory ticket = - Ticket({origin: origin, assets: encodedAssets, xcm: xcm, reward: reward}); + Ticket memory ticket = Ticket({ + origin: origin, + assets: encodedAssets, + xcm: xcm, + claimer: claimer, + reward: reward + }); _submitOutbound(ticket); } @@ -138,7 +148,7 @@ library CallsV2 { bytes[] memory assets = new bytes[](1); assets[0] = abi.encode(0, WETH_ADDRESS, xcmFee); - _sendMessage(address(this), xcm, assets, msg.value - xcmFee); + _sendMessage(address(this), xcm, assets, "", msg.value - xcmFee); } // Submit an outbound message to Polkadot, after taking fees @@ -153,8 +163,9 @@ library CallsV2 { $.outboundNonce = $.outboundNonce + 1; - bytes memory payload = - SubstrateTypes.encodePayloadV2(ticket.origin, ticket.assets, ticket.xcm); + bytes memory payload = SubstrateTypes.encodePayloadV2( + ticket.origin, ticket.assets, ticket.xcm, ticket.claimer + ); emit IGateway.OutboundMessageAccepted($.outboundNonce, ticket.reward, payload); } diff --git a/contracts/src/v2/Types.sol b/contracts/src/v2/Types.sol index 8159b10d1e..be3ae1a14c 100644 --- a/contracts/src/v2/Types.sol +++ b/contracts/src/v2/Types.sol @@ -16,7 +16,7 @@ struct InboundMessage { struct Command { uint8 kind; - uint256 gas; + uint64 gas; bytes payload; } @@ -34,6 +34,7 @@ struct Ticket { address origin; bytes[] assets; bytes xcm; + bytes claimer; uint256 reward; }