From 251284b918cc0b38902bd9c05872a1d92da02a91 Mon Sep 17 00:00:00 2001 From: Hamid Roohi <52012062+hamidroohi71@users.noreply.github.com> Date: Tue, 14 May 2024 15:12:50 +0330 Subject: [PATCH 01/10] add source codes to msgInterface and Order Clearing and XAccount --- src/components/EnablingDiverseItems.tsx | 12 +- src/components/MsgportInterface.tsx | 113 +++---- src/data/code.tsx | 374 +++++++++++++++--------- 3 files changed, 291 insertions(+), 208 deletions(-) diff --git a/src/components/EnablingDiverseItems.tsx b/src/components/EnablingDiverseItems.tsx index 9a7edb8..95543fd 100644 --- a/src/components/EnablingDiverseItems.tsx +++ b/src/components/EnablingDiverseItems.tsx @@ -7,8 +7,12 @@ interface EnablingDiverseTypes { } const EnablingDiverseItems = ({ isDiverse, text }: EnablingDiverseTypes) => { - const activeTitle = menu[2].title; - const activeMenu = menu.find(({ title }) => title === activeTitle) || menu[2]; + const activeTitle = menu[5].title; + const activeMenu = menu.find(({ title }) => title === activeTitle) || menu[5]; + + const activeTitleOrder = menu[6].title; + const activeMenuOrder = + menu.find(({ title }) => title === activeTitleOrder) || menu[6]; return (
@@ -34,8 +38,8 @@ const EnablingDiverseItems = ({ isDiverse, text }: EnablingDiverseTypes) => { {text}

{ const [selectedItem, setSelectedItem] = useState(0); - const activeTitle = menu[0].title; - const activeMenu = menu.find(({ title }) => title === activeTitle) || menu[0]; + + const activeMenu = + menu.find((item) => item.title === data[selectedItem - 1]?.text) || menu[0]; + return (

- Msgport Interface + {activeMenu.title}

- This interface provides developers with a generic message passing - interface to send arbitrary data between contracts on different - blockchain networks + {activeMenu.description}

-
@@ -34,78 +32,51 @@ const MsgportInterface = () => {

Msgport

- - {data.map((item) => { - return ( -
- - {selectedItem === item.id && ( -
-
-

- This interface provides developers with a generic message - passing interface to send arbitrary data between contracts - on different blockchain networks -

- -
-
- )} -
- ); - })} - {/* {datamobile.map((item) => { - return ( + {data.map((item) => ( +
- ); - })} */} - + {selectedItem === item.id && ( +
+
+

+ {activeMenu.description} +

+ +
+
+ )} +
+ ))} + + + Try it now + + goArrow +
diff --git a/src/data/code.tsx b/src/data/code.tsx index e7d129b..4df23ab 100644 --- a/src/data/code.tsx +++ b/src/data/code.tsx @@ -9,158 +9,266 @@ export const menu: { description: "This interface provides developers with a generic message passing interface to send arbitrary data between contracts on different blockchain networks.", code: ` - // This file is part of Msgport. - // Copyright (C) 2024 Msgport - // SPDX-License-Identifier: GPL-3.0 - - pragma solidity ^0.8.0; - - interface IMessagePort { - error MessageFailure(bytes errorData); - - /// @dev Send a cross-chain message over the MessagePort. - /// @notice Send a cross-chain message over the MessagePort. - /// @param toChainId The message destination chain id. - /// @param toDapp The user application contract address which receive the message. - /// @param message The calldata which encoded by ABI Encoding. - /// @param params Extend parameters to adapt to different message protocols. - function send(uint256 toChainId, address toDapp, bytes calldata message, bytes calldata params) external payable; - - /// @notice Get a quote in source native gas, for the amount that send() requires to pay for message delivery. - /// It should be noted that not all ports will implement this interface. - /// @dev If the messaging protocol does not support on-chain fetch fee, then revert with "Unimplemented!". - /// @param toChainId The message destination chain id. - /// @param toDapp The user application contract address which receive the message. - /// @param message The calldata which encoded by ABI Encoding. - /// @param params Extend parameters to adapt to different message protocols. - function fee(uint256 toChainId, address toDapp, bytes calldata message, bytes calldata params) - external - view - returns (uint256); - }`, + // SPDX-License-Identifier: MIT + pragma solidity ^0.8.0; + + interface IMessagePort { + event MessageSent( + bytes32 indexed msgId, address fromDapp, uint256 toChainId, address toDapp, bytes message, bytes params + ); + event MessageRecv(bytes32 indexed msgId, bool result, bytes returnData); + + /// @dev Send a cross-chain message over the MessagePort. + /// @notice Send a cross-chain message over the MessagePort. + /// @param toChainId The message destination chain id. + /// @param toDapp The user application contract address which receive the message. + /// @param message The calldata which encoded by ABI Encoding. + /// @param params Extend parameters to adapt to different message protocols. + /// @return msgId Return the ID of message. + function send(uint256 toChainId, address toDapp, bytes calldata message, bytes calldata params) + external + payable + returns (bytes32 msgId); + + /// @notice Get a quote in source native gas, for the amount that send() requires to pay for message delivery. + /// It should be noted that not all ports will implement this interface. + /// @dev If the messaging protocol does not support on-chain fetch fee, then revert with "Unimplemented!". + /// @param toChainId The message destination chain id. + /// @param fromDapp The user application contract address which send the message. + /// @param toDapp The user application contract address which receive the message. + /// @param message The calldata which encoded by ABI Encoding. + /// @param params Extend parameters to adapt to different message protocols. + function fee(uint256 toChainId, address fromDapp, address toDapp, bytes calldata message, bytes calldata params) + external + view + returns (uint256); + }`, language: "solidity", }, { title: "Deploy ExampleReceiverDapp", description: "Deploy a receiver contract on the target chain to receive messages. (for example purposes only)", - code: ` - // This file is part of Msgport. - // Copyright (C) 2024 Msgport - // SPDX-License-Identifier: GPL-3.0 - - pragma solidity ^0.8.17; - - import "https://github.com/msgport/msgport/blob/main/src/user/Application.sol"; - - contract ExampleReceiverDapp is Application { - event DappMessageRecv(uint256 fromChainId, address fromDapp, address localPort, bytes message); - - // local port address - address public immutable PORT; - // remote dapp address - address public immutable DAPP; - - constructor(address port, address dapp) { - PORT = port; - DAPP = dapp; - } - - /// @notice You could check the fromDapp address or messagePort address. - function testReceive(bytes calldata message) external { - uint256 fromChainId = _fromChainId(); - address fromDapp = _xmsgSender(); - address localPort = _msgPort(); - require(localPort == PORT); - require(fromDapp == DAPP); - emit DappMessageRecv(fromChainId, fromDapp, localPort, message); - } - }`, + code: `// SPDX-License-Identifier: MIT + pragma solidity ^0.8.17; + + import "https://github.com/msgport/msgport/blob/main/src/user/Application.sol"; + + contract ExampleReceiverDapp is Application { + event DappMessageRecv(uint256 fromChainId, address fromDapp, address localPort, bytes message); + + // local port address + address public immutable PORT; + // remote dapp address + address public immutable DAPP; + + constructor(address port, address dapp) { + PORT = port; + DAPP = dapp; + } + + /// @notice You could check the fromDapp address or messagePort address. + function testReceive(bytes calldata message) external { + uint256 fromChainId = _fromChainId(); + address fromDapp = _xmsgSender(); + address localPort = _msgPort(); + require(localPort == PORT); + require(fromDapp == DAPP); + emit DappMessageRecv(fromChainId, fromDapp, localPort, message); + } + }`, language: "solidity", }, { - title: "Encode calldata", + title: "Encode Calldata", description: "Build the remote call data as the message payload.", - code: ` - import { ethers } from 'ethers'; - - const privateKey = process.env.PRIVATE_KEY; - const providerUrl = ; - const receiverDappAddr = ; - - function encodeReceiveCall() { - const receiverABI = [{ - "inputs": [ - { - "internalType": "bytes", - "name": "message", - "type": "bytes" - } - ], - "name": "testReceive", - "outputs": [], - "stateMutability": "nonpayable", - "type": "function" - }]; - const testMessage = "0x1234"; - const provider = new ethers.JsonRpcProvider(providerUrl); - const signer = new ethers.Wallet(privateKey, provider); - const receiverDapp = new ethers.Contract(receiverDappAddr, receiverABI, signer); - const callData = receiverDapp.interface.encodeFunctionData('testReceive', [testMessage]); - console.log(callData); - } - - encodeReceiveCall();`, + code: `import { ethers } from 'ethers'; + + const privateKey = process.env.PRIVATE_KEY; + const providerUrl = ; + const receiverDappAddr = ; + + function encodeReceiveCall() { + const receiverABI = [{ + "inputs": [ + { + "internalType": "bytes", + "name": "message", + "type": "bytes" + } + ], + "name": "testReceive", + "outputs": [], + "stateMutability": "nonpayable", + "type": "function" + }]; + const testMessage = "0x1234"; + const provider = new ethers.JsonRpcProvider(providerUrl); + const signer = new ethers.Wallet(privateKey, provider); + const receiverDapp = new ethers.Contract(receiverDappAddr, receiverABI, signer); + const callData = receiverDapp.interface.encodeFunctionData('testReceive', [testMessage]); + console.log(callData); + } + + encodeReceiveCall();`, language: "javascript", }, { - title: "Get fee and params from Msgport API", + title: "Get Fee And Params From Msgport API", description: "Estimate fee and get adaptation params from Msgport API.", code: `import axios from 'axios'; - - async function getFeeParams() { - const requestBody = { - 'from_chain_id': , - 'to_chain_id': , - 'payload': , - 'from_address': , - 'to_address': , - 'refund_address': , - }; - const result = await axios.get("https://api.msgport.xyz/ormp/fee", { params: requestBody }); - const { fee, params } = result.data.data; - console.log(fee, params); - } - - await getFeeParams();`, + + async function getFeeParams() { + const requestBody = { + 'from_chain_id': , + 'to_chain_id': , + 'payload': , + 'from_address': , + 'to_address': , + 'refund_address': , + }; + const result = await axios.get("https://api.msgport.xyz/ormp/fee", { params: requestBody }); + const { fee, params } = result.data.data; + console.log(fee, params); + } + + await getFeeParams();`, language: "javascript", }, { - title: "Sending message", + title: "Sending Message", + description: "", + code: `pragma solidity ^0.8.17; + + import "https://github.com/msgport/msgport/blob/main/src/interfaces/IMessagePort.sol"; + + contract ExampleSenderDapp { + event DappMessageSent(address localPort, bytes message); + + // local port address + address public immutable PORT; + + constructor(address port) { + PORT = port; + } + + function testSend(uint256 toChainId, address toDapp, bytes calldata message, bytes calldata params) external payable{ + IMessagePort(PORT).send{value: msg.value}(toChainId, toDapp, message, params); + emit DappMessageSent(PORT, message); + } + }`, + language: "solidity", + }, + { + title: "XAccount", + description: "", + code: `pragma solidity ^0.8.0; + + contract ExampleXAccount { + // XAccountFactory address + address public factory; + // PortRegistry address + address public registry; + + constructor(address factory_, address registry_) { + factory = factory_; + registry = registry_; + } + + /// @dev The function is utilized to create a xAccount on the target chain. + function createXAccountOnTargetChain(bytes4 code, uint256 toChainId, bytes calldata params, address recovery) + public + payable + { + IXAccountFactory(factory).xCreate{value: msg.value}(code, toChainId, params, recovery); + } + + /// @dev The function facilitates the execution of an xCall across a xAccount. + function crossChainCall( + bytes4 code, + uint256 toChainId, + bytes calldata params, + address target, + uint256 value, + bytes calldata data, + uint8 operation + ) public payable { + bytes memory message = + abi.encodeWithSelector(ISafeMsgportModule.xExecute.selector, target, value, data, operation); + address port = IPortRegistry(registry).get(toChainId, code); + (, address module) = IXAccountFactory(factory).xAccountOf(block.chainid, toChainId, address(this)); + IMessagePort(port).send{value: msg.value}(toChainId, module, message, params); + } + }`, + language: "solidity", + }, + { + title: "Order Clearing", description: "", - code: `// This file is part of Msgport. - // Copyright (C) 2024 Msgport - // SPDX-License-Identifier: GPL-3.0 - - pragma solidity ^0.8.17; - - import "https://github.com/msgport/msgport/blob/main/src/interfaces/IMessagePort.sol"; - - contract ExampleSenderDapp { - event DappMessageSent(address localPort, bytes message); - - // local port address - address public immutable PORT; - - constructor(address port) { - PORT = port; - } - - function testSend(uint256 toChainId, address toDapp, bytes calldata message, bytes calldata params) external payable{ - IMessagePort(PORT).send{value: msg.value}(toChainId, toDapp, message, params); - emit DappMessageSent(PORT, message); - } - }`, + code: `// SPDX-License-Identifier: MIT + + pragma solidity ^0.8.17; + + contract MsglineMessager is Application, AccessController { + IMessageLine public immutable msgline; + struct RemoteMessager { + uint256 msglineRemoteChainId; + address messager; + } + mapping(address=>bool) public whiteList; + // app remoteChainId => msgline remote messager + mapping(uint256=>RemoteMessager) public remoteMessagers; + // token bridge pair + // hash(msglineRemoteChainId, localAppAddress) => remoteAppAddress + mapping(bytes32=>address) public remoteAppReceivers; + mapping(bytes32=>address) public remoteAppSenders; + event CallerUnMatched(uint256 srcAppChainId, address srcAppAddress); + event CallResult(uint256 srcAppChainId, bool result); + modifier onlyWhiteList() { + require(whiteList[msg.sender], "msg.sender not in whitelist"); + _; + } + modifier onlyMsgline() { + require(msg.sender == address(msgline), "invalid caller"); + _; + } + constructor(address _dao, address _msgline) { + _initialize(_dao); + msgline = IMessageLine(_msgline); + } + function setRemoteMessager(uint256 _appRemoteChainId, uint256 _msglineRemoteChainId, address _remoteMessager) onlyDao external { + remoteMessagers[_appRemoteChainId] = RemoteMessager(_msglineRemoteChainId, _remoteMessager); + } + function setWhiteList(address _caller, bool _enable) external onlyDao { + whiteList[_caller] = _enable; + } + function registerRemoteReceiver(uint256 _remoteChainId, address _remoteBridge) onlyWhiteList external { + RemoteMessager memory remoteMessager = remoteMessagers[_remoteChainId]; + require(remoteMessager.messager != address(0), "remote not configured"); + bytes32 key = keccak256(abi.encodePacked(remoteMessager.msglineRemoteChainId, msg.sender)); + remoteAppReceivers[key] = _remoteBridge; + } + function registerRemoteSender(uint256 _remoteChainId, address _remoteBridge) onlyWhiteList external { + RemoteMessager memory remoteMessager = remoteMessagers[_remoteChainId]; + require(remoteMessager.messager != address(0), "remote not configured"); + bytes32 key = keccak256(abi.encodePacked(remoteMessager.msglineRemoteChainId, msg.sender)); + remoteAppSenders[key] = _remoteBridge; + } + function sendMessage(uint256 _remoteChainId, bytes memory _message, bytes memory _params) onlyWhiteList external payable { + RemoteMessager memory remoteMessager = remoteMessagers[_remoteChainId]; + require(remoteMessager.messager != address(0), "remote not configured"); + bytes32 key = keccak256(abi.encodePacked(remoteMessager.msglineRemoteChainId, msg.sender)); + address remoteAppAddress = remoteAppReceivers[key]; + require(remoteAppAddress != address(0), "app pair not registered"); + bytes memory msglinePayload = messagePayload(msg.sender, remoteAppAddress, _message); + msgline.send{ value: msg.value }( + remoteMessager.msglineRemoteChainId, + remoteMessager.messager, + msglinePayload, + _params + ); + } + }`, language: "solidity", }, ]; From cbd5965e1008f6a07f665b5406f17ce32411a36c Mon Sep 17 00:00:00 2001 From: Hamid Roohi <52012062+hamidroohi71@users.noreply.github.com> Date: Tue, 14 May 2024 16:25:10 +0330 Subject: [PATCH 02/10] fix style of desc --- src/components/MsgportInterface.tsx | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/components/MsgportInterface.tsx b/src/components/MsgportInterface.tsx index b5ec736..bdbce86 100644 --- a/src/components/MsgportInterface.tsx +++ b/src/components/MsgportInterface.tsx @@ -16,7 +16,7 @@ const MsgportInterface = () => {

{activeMenu.title}

-

+

{activeMenu.description}

{