diff --git a/examples/call/contracts/Connected.sol b/examples/call/contracts/Connected.sol index 5a9b57bb..5cd333c3 100644 --- a/examples/call/contracts/Connected.sol +++ b/examples/call/contracts/Connected.sol @@ -3,18 +3,20 @@ pragma solidity 0.8.26; import {RevertContext} from "@zetachain/protocol-contracts/contracts/Revert.sol"; import "@zetachain/protocol-contracts/contracts/evm/GatewayEVM.sol"; +import "@openzeppelin/contracts/token/ERC20/utils/SafeERC20.sol"; contract Connected { + using SafeERC20 for IERC20; // Use SafeERC20 for IERC20 operations + GatewayEVM public immutable gateway; event RevertEvent(string, RevertContext); event HelloEvent(string, string); - error TransferFailed(); - error ApprovalFailed(); + error Unauthorized(); modifier onlyGateway() { - require(msg.sender == address(gateway), "Caller is not the gateway"); + if (msg.sender != address(gateway)) revert Unauthorized(); _; } @@ -43,12 +45,8 @@ contract Connected { address asset, RevertOptions memory revertOptions ) external { - if (!IERC20(asset).transferFrom(msg.sender, address(this), amount)) { - revert TransferFailed(); - } - if (!IERC20(asset).approve(address(gateway), amount)) { - revert ApprovalFailed(); - } + IERC20(asset).safeTransferFrom(msg.sender, address(this), amount); + IERC20(asset).approve(address(gateway), amount); gateway.deposit(receiver, amount, asset, revertOptions); } @@ -59,12 +57,8 @@ contract Connected { bytes calldata message, RevertOptions memory revertOptions ) external { - if (!IERC20(asset).transferFrom(msg.sender, address(this), amount)) { - revert TransferFailed(); - } - if (!IERC20(asset).approve(address(gateway), amount)) { - revert ApprovalFailed(); - } + IERC20(asset).safeTransferFrom(msg.sender, address(this), amount); + IERC20(asset).approve(address(gateway), amount); gateway.depositAndCall(receiver, amount, asset, message, revertOptions); } diff --git a/examples/call/contracts/Universal.sol b/examples/call/contracts/Universal.sol index 5b9f6c05..ca8f8402 100644 --- a/examples/call/contracts/Universal.sol +++ b/examples/call/contracts/Universal.sol @@ -11,10 +11,12 @@ contract Universal is UniversalContract { event HelloEvent(string, string); event RevertEvent(string, RevertContext); + error TransferFailed(); + error Unauthorized(); modifier onlyGateway() { - require(msg.sender == address(gateway), "Caller is not the gateway"); + if (msg.sender != address(gateway)) revert Unauthorized(); _; } @@ -32,8 +34,9 @@ contract Universal is UniversalContract { (, uint256 gasFee) = IZRC20(zrc20).withdrawGasFeeWithGasLimit( callOptions.gasLimit ); - if (!IZRC20(zrc20).transferFrom(msg.sender, address(this), gasFee)) + if (!IZRC20(zrc20).transferFrom(msg.sender, address(this), gasFee)) { revert TransferFailed(); + } IZRC20(zrc20).approve(address(gateway), gasFee); gateway.call(receiver, zrc20, message, callOptions, revertOptions); } @@ -46,8 +49,9 @@ contract Universal is UniversalContract { ) external { (address gasZRC20, uint256 gasFee) = IZRC20(zrc20).withdrawGasFee(); uint256 target = zrc20 == gasZRC20 ? amount + gasFee : amount; - if (!IZRC20(zrc20).transferFrom(msg.sender, address(this), target)) + if (!IZRC20(zrc20).transferFrom(msg.sender, address(this), target)) { revert TransferFailed(); + } IZRC20(zrc20).approve(address(gateway), target); if (zrc20 != gasZRC20) { if ( @@ -56,7 +60,9 @@ contract Universal is UniversalContract { address(this), gasFee ) - ) revert TransferFailed(); + ) { + revert TransferFailed(); + } IZRC20(gasZRC20).approve(address(gateway), gasFee); } gateway.withdraw(receiver, amount, zrc20, revertOptions); @@ -83,7 +89,9 @@ contract Universal is UniversalContract { address(this), gasFee ) - ) revert TransferFailed(); + ) { + revert TransferFailed(); + } IZRC20(gasZRC20).approve(address(gateway), gasFee); } gateway.withdrawAndCall( diff --git a/examples/hello/contracts/Universal.sol b/examples/hello/contracts/Universal.sol index b83fc684..fa4920e9 100644 --- a/examples/hello/contracts/Universal.sol +++ b/examples/hello/contracts/Universal.sol @@ -7,9 +7,10 @@ contract Universal is UniversalContract { GatewayZEVM public immutable gateway; event HelloEvent(string, string); + error Unauthorized(); modifier onlyGateway() { - require(msg.sender == address(gateway), "Caller is not the gateway"); + if (msg.sender != address(gateway)) revert Unauthorized(); _; } diff --git a/examples/router/hardhat.config.ts b/examples/router/hardhat.config.ts index b2a4c734..ada81896 100644 --- a/examples/router/hardhat.config.ts +++ b/examples/router/hardhat.config.ts @@ -1,9 +1,10 @@ import "./tasks/deploy"; -import "./tasks/deploy"; -import "./tasks/transfer"; -import "./tasks/universalSetCounterparty"; -import "./tasks/connectedSetCounterparty"; -import "./tasks/connectedSetRouter"; +import "./tasks/universalCall"; +import "./tasks/connectedCall"; +import "./tasks/connectedDeposit"; +import "./tasks/connectedDepositAndCall"; +import "./tasks/universalWithdraw"; +import "./tasks/universalWithdrawAndCall"; import "@zetachain/localnet/tasks"; import "@nomicfoundation/hardhat-toolbox"; import "@zetachain/toolkit/tasks"; @@ -15,16 +16,7 @@ const config: HardhatUserConfig = { networks: { ...getHardhatConfigNetworks(), }, - solidity: { - version: "0.8.26", - settings: { - optimizer: { - enabled: true, - runs: 200, - }, - viaIR: true, - }, - }, + solidity: "0.8.26", }; export default config; diff --git a/examples/router/tasks/connectedSetCounterparty.ts b/examples/router/tasks/connectedSetCounterparty.ts index 88621f1d..b9b77955 100644 --- a/examples/router/tasks/connectedSetCounterparty.ts +++ b/examples/router/tasks/connectedSetCounterparty.ts @@ -15,13 +15,14 @@ const main = async (args: any, hre: HardhatRuntimeEnvironment) => { args.contract ); - const tx = await contract.setCounterparty(args.counterparty); + const tx = await contract.setUniversal(args.universal); + const receipt = await tx.wait(); if (args.json) { console.log( JSON.stringify({ contractAddress: args.contract, - universalContract: args.counterparty, + universalContract: args.universal, transactionHash: tx.hash, }) ); @@ -33,8 +34,7 @@ const main = async (args: any, hre: HardhatRuntimeEnvironment) => { } }; -task("connected-set-counterparty", "Sets the universal contract address", main) +task("connected-set-universal", "Sets the universal contract address", main) .addParam("contract", "The address of the deployed contract") - .addParam("counterparty", "The address of the universal contract to set") - .addOptionalParam("name", "The contract name to interact with", "Connected") + .addParam("universal", "The address of the universal contract to set") .addFlag("json", "Output the result in JSON format"); diff --git a/examples/router/tasks/connectedSetRouter.ts b/examples/router/tasks/connectedSetRouter.ts index fd5530a9..75605df1 100644 --- a/examples/router/tasks/connectedSetRouter.ts +++ b/examples/router/tasks/connectedSetRouter.ts @@ -15,13 +15,13 @@ const main = async (args: any, hre: HardhatRuntimeEnvironment) => { args.contract ); - const tx = await contract.setRouter(args.counterparty); + const tx = await contract.setUniversal(args.universal); if (args.json) { console.log( JSON.stringify({ contractAddress: args.contract, - universalContract: args.counterparty, + universalContract: args.universal, transactionHash: tx.hash, }) ); @@ -33,8 +33,7 @@ const main = async (args: any, hre: HardhatRuntimeEnvironment) => { } }; -task("connected-set-router", "Sets the universal contract address", main) +task("connected-set-universal", "Sets the universal contract address", main) .addParam("contract", "The address of the deployed contract") - .addParam("counterparty", "The address of the universal contract to set") - .addOptionalParam("name", "The contract name to interact with", "Connected") + .addParam("universal", "The address of the universal contract to set") .addFlag("json", "Output the result in JSON format"); diff --git a/examples/router/tasks/universalSetCounterparty.ts b/examples/router/tasks/universalSetCounterparty.ts index 1cd2d194..81292b54 100644 --- a/examples/router/tasks/universalSetCounterparty.ts +++ b/examples/router/tasks/universalSetCounterparty.ts @@ -15,15 +15,14 @@ const main = async (args: any, hre: HardhatRuntimeEnvironment) => { args.contract ); - const tx = await contract.setCounterparty(args.zrc20, args.counterparty); - const receipt = await tx.wait(); + const tx = await contract.setConnected(args.zrc20, args.connected); if (args.json) { console.log( JSON.stringify({ contractAddress: args.contract, zrc20: args.zrc20, - connectedContractAddress: args.counterparty, + connectedContractAddress: args.connected, transactionHash: tx.hash, }) ); @@ -36,8 +35,8 @@ const main = async (args: any, hre: HardhatRuntimeEnvironment) => { } }; -task("universal-set-counterparty", "Sets the connected contract address", main) +task("universal-set-connected", "Sets the connected contract address", main) .addParam("contract", "The address of the deployed contract") .addParam("zrc20", "The ZRC20 address to link to the connected contract") - .addParam("counterparty", "The address of the connected contract to set") + .addParam("connected", "The address of the connected contract to set") .addFlag("json", "Output the result in JSON format"); diff --git a/examples/swap/contracts/Swap.sol b/examples/swap/contracts/Swap.sol index f8c1ebf5..d6c8ade1 100644 --- a/examples/swap/contracts/Swap.sol +++ b/examples/swap/contracts/Swap.sol @@ -17,9 +17,10 @@ contract Swap is UniversalContract { uint256 constant BITCOIN = 18332; error InvalidAddress(); + error Unauthorized(); modifier onlyGateway() { - require(msg.sender == address(gateway), "Caller is not the gateway"); + if (msg.sender != address(gateway)) revert Unauthorized(); _; } diff --git a/examples/swap/contracts/SwapToAnyToken.sol b/examples/swap/contracts/SwapToAnyToken.sol index 9642687b..e272777b 100644 --- a/examples/swap/contracts/SwapToAnyToken.sol +++ b/examples/swap/contracts/SwapToAnyToken.sol @@ -18,8 +18,11 @@ contract SwapToAnyToken is UniversalContract { uint256 constant BITCOIN = 18332; error InvalidAddress(); + error InvalidAddress(); + error Unauthorized(); + modifier onlyGateway() { - require(msg.sender == address(gateway), "Caller is not the gateway"); + if (msg.sender != address(gateway)) revert Unauthorized(); _; }