From 6d8c76e4f0c989e5364caee808033e173ddaa7a5 Mon Sep 17 00:00:00 2001
From: Denis Fadeev <denis@fadeev.org>
Date: Thu, 28 Nov 2024 13:29:09 +0300
Subject: [PATCH] wip

---
 examples/router/contracts/Connected.sol       | 38 ++++++++++++++++++-
 examples/router/contracts/ConnectedRouter.sol | 22 +----------
 examples/router/contracts/Universal.sol       | 11 ------
 examples/router/tasks/transfer.ts             |  1 +
 4 files changed, 40 insertions(+), 32 deletions(-)

diff --git a/examples/router/contracts/Connected.sol b/examples/router/contracts/Connected.sol
index 1b8a522..77316cf 100644
--- a/examples/router/contracts/Connected.sol
+++ b/examples/router/contracts/Connected.sol
@@ -44,6 +44,42 @@ contract Connected is ConnectedRouter {
         CallOptions memory callOptions,
         RevertOptions memory revertOptions
     ) external payable {
-        gatewaySendMessage(targetToken, data, callOptions, revertOptions);
+        bytes memory message = abi.encode(
+            abi.encodePacked(counterparty),
+            targetToken,
+            data,
+            callOptions,
+            revertOptions
+        );
+        gateway.depositAndCall{value: msg.value}(
+            router,
+            message,
+            revertOptions
+        );
     }
+
+    // function sendMessage(
+    //     address targetToken,
+    //     uint256 amount,
+    //     address asset,
+    //     bytes memory data,
+    //     CallOptions memory callOptions,
+    //     RevertOptions memory revertOptions
+    // ) external {
+    //     bytes memory message = abi.encode(
+    //         abi.encodePacked(counterparty),
+    //         targetToken,
+    //         data,
+    //         callOptions,
+    //         revertOptions
+    //     );
+    //     if (!IERC20(asset).transferFrom(msg.sender, address(this), amount)) {
+    //         revert TransferFailed();
+    //     }
+    //     if (!IERC20(asset).approve(address(gateway), amount)) {
+    //         revert ApprovalFailed();
+    //     }
+
+    //     gateway.depositAndCall(router, amount, asset, message, revertOptions);
+    // }
 }
diff --git a/examples/router/contracts/ConnectedRouter.sol b/examples/router/contracts/ConnectedRouter.sol
index 4bd6461..81594ed 100644
--- a/examples/router/contracts/ConnectedRouter.sol
+++ b/examples/router/contracts/ConnectedRouter.sol
@@ -17,6 +17,8 @@ contract ConnectedRouter is Ownable {
     event OnRevertEvent(string, RevertContext);
 
     error Unauthorized();
+    error TransferFailed();
+    error ApprovalFailed();
 
     function setCounterparty(address contractAddress) external onlyOwner {
         counterparty = contractAddress;
@@ -36,26 +38,6 @@ contract ConnectedRouter is Ownable {
         router = routerAddress;
     }
 
-    function gatewaySendMessage(
-        address targetToken,
-        bytes memory data,
-        CallOptions memory callOptions,
-        RevertOptions memory revertOptions
-    ) public payable {
-        bytes memory message = abi.encode(
-            abi.encodePacked(counterparty),
-            targetToken,
-            data,
-            callOptions,
-            revertOptions
-        );
-        gateway.depositAndCall{value: msg.value}(
-            router,
-            message,
-            revertOptions
-        );
-    }
-
     function onCall(
         MessageContext calldata context,
         bytes calldata message
diff --git a/examples/router/contracts/Universal.sol b/examples/router/contracts/Universal.sol
index 3586045..8f4473b 100644
--- a/examples/router/contracts/Universal.sol
+++ b/examples/router/contracts/Universal.sol
@@ -18,9 +18,6 @@ contract Universal is UniversalContract, Ownable {
     error Unauthorized();
     error InvalidAddress();
 
-    mapping(address => bytes) public counterparty;
-
-    event CounterpartySet(address indexed zrc20, bytes indexed contractAddress);
     event GasFeeAndOut(uint256 gasFee, uint256 out);
     event RevertEvent(string);
 
@@ -45,14 +42,6 @@ contract Universal is UniversalContract, Ownable {
         uniswapRouter = uniswapRouterAddress;
     }
 
-    function setCounterparty(
-        address zrc20,
-        bytes memory contractAddress
-    ) external onlyOwner {
-        counterparty[zrc20] = contractAddress;
-        emit CounterpartySet(zrc20, contractAddress);
-    }
-
     function onCall(
         MessageContext calldata context,
         address zrc20,
diff --git a/examples/router/tasks/transfer.ts b/examples/router/tasks/transfer.ts
index d86ab5c..73c2d59 100644
--- a/examples/router/tasks/transfer.ts
+++ b/examples/router/tasks/transfer.ts
@@ -152,4 +152,5 @@ task("transfer", "Make a cross-chain call", main)
     "function",
     "The function to call on the destination chain (only for arbitrary calls)"
   )
+  .addOptionalParam("erc20", "The address of the ERC20 token to transfer")
   .addVariadicPositionalParam("values", "The values of the parameters");