Skip to content

Commit

Permalink
merge main
Browse files Browse the repository at this point in the history
  • Loading branch information
fadeev committed Nov 29, 2024
2 parents 6d8c76e + 02373a6 commit 11af7b7
Show file tree
Hide file tree
Showing 9 changed files with 50 additions and 53 deletions.
24 changes: 9 additions & 15 deletions examples/call/contracts/Connected.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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();
_;
}

Expand Down Expand Up @@ -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);
}

Expand All @@ -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);
}

Expand Down
18 changes: 13 additions & 5 deletions examples/call/contracts/Universal.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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();
_;
}

Expand All @@ -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);
}
Expand All @@ -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 (
Expand All @@ -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);
Expand All @@ -83,7 +89,9 @@ contract Universal is UniversalContract {
address(this),
gasFee
)
) revert TransferFailed();
) {
revert TransferFailed();
}
IZRC20(gasZRC20).approve(address(gateway), gasFee);
}
gateway.withdrawAndCall(
Expand Down
3 changes: 2 additions & 1 deletion examples/hello/contracts/Universal.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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();
_;
}

Expand Down
22 changes: 7 additions & 15 deletions examples/router/hardhat.config.ts
Original file line number Diff line number Diff line change
@@ -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";
Expand All @@ -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;
10 changes: 5 additions & 5 deletions examples/router/tasks/connectedSetCounterparty.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
})
);
Expand All @@ -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");
9 changes: 4 additions & 5 deletions examples/router/tasks/connectedSetRouter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
})
);
Expand All @@ -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");
9 changes: 4 additions & 5 deletions examples/router/tasks/universalSetCounterparty.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
})
);
Expand All @@ -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");
3 changes: 2 additions & 1 deletion examples/swap/contracts/Swap.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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();
_;
}

Expand Down
5 changes: 4 additions & 1 deletion examples/swap/contracts/SwapToAnyToken.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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();
_;
}

Expand Down

0 comments on commit 11af7b7

Please sign in to comment.