Skip to content

Commit

Permalink
forge fmt
Browse files Browse the repository at this point in the history
  • Loading branch information
skosito committed Jul 29, 2024
1 parent d00c832 commit b8db384
Show file tree
Hide file tree
Showing 19 changed files with 289 additions and 134 deletions.
2 changes: 1 addition & 1 deletion v2/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"test": "test"
},
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
"test": "forge clean && forge test -vv"
},
"devDependencies": {
},
Expand Down
31 changes: 25 additions & 6 deletions v2/src/evm/ERC20CustodyNew.sol
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.20;

import "./interfaces//IGatewayEVM.sol";
import "./interfaces/IERC20CustodyNew.sol";
import "@openzeppelin/contracts/token/ERC20/IERC20.sol";
import "@openzeppelin/contracts/token/ERC20/utils/SafeERC20.sol";
import "@openzeppelin/contracts/utils/ReentrancyGuard.sol";
import "./interfaces//IGatewayEVM.sol";
import "./interfaces/IERC20CustodyNew.sol";

/// @title ERC20CustodyNew
/// @notice Holds the ERC20 tokens deposited on ZetaChain and includes functionality to call a contract.
Expand Down Expand Up @@ -51,7 +51,16 @@ contract ERC20CustodyNew is IERC20CustodyNewEvents, IERC20CustodyNewErrors, Reen
/// @param to Address of the contract to call.
/// @param amount Amount of tokens to withdraw.
/// @param data Calldata to pass to the contract call.
function withdrawAndCall(address token, address to, uint256 amount, bytes calldata data) public nonReentrant onlyTSS {
function withdrawAndCall(
address token,
address to,
uint256 amount,
bytes calldata data
)
public
nonReentrant
onlyTSS
{
// Transfer the tokens to the Gateway contract
IERC20(token).safeTransfer(address(gateway), amount);

Expand All @@ -61,13 +70,23 @@ contract ERC20CustodyNew is IERC20CustodyNewEvents, IERC20CustodyNewErrors, Reen
emit WithdrawAndCall(token, to, amount, data);
}

/// @notice WithdrawAndRevert transfers tokens to Gateway and call a contract with a revert functionality through the Gateway.
/// @notice WithdrawAndRevert transfers tokens to Gateway and call a contract with a revert functionality through
/// the Gateway.
/// @dev This function can only be called by the TSS address.
/// @param token Address of the ERC20 token.
/// @param to Address of the contract to call.
/// @param amount Amount of tokens to withdraw.
/// @param data Calldata to pass to the contract call.
function withdrawAndRevert(address token, address to, uint256 amount, bytes calldata data) public nonReentrant onlyTSS {
function withdrawAndRevert(
address token,
address to,
uint256 amount,
bytes calldata data
)
public
nonReentrant
onlyTSS
{
// Transfer the tokens to the Gateway contract
IERC20(token).safeTransfer(address(gateway), amount);

Expand All @@ -76,4 +95,4 @@ contract ERC20CustodyNew is IERC20CustodyNewEvents, IERC20CustodyNewErrors, Reen

emit WithdrawAndRevert(token, to, amount, data);
}
}
}
40 changes: 29 additions & 11 deletions v2/src/evm/GatewayEVM.sol
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,25 @@ pragma solidity ^0.8.20;

import "./ZetaConnectorNewBase.sol";
import "./interfaces/IGatewayEVM.sol";
import "@openzeppelin/contracts/token/ERC20/IERC20.sol";
import "@openzeppelin/contracts/token/ERC20/utils/SafeERC20.sol";
import "@openzeppelin/contracts-upgradeable/proxy/utils/Initializable.sol";

import "@openzeppelin/contracts-upgradeable/access/OwnableUpgradeable.sol";
import "@openzeppelin/contracts-upgradeable/proxy/utils/Initializable.sol";
import "@openzeppelin/contracts-upgradeable/proxy/utils/UUPSUpgradeable.sol";
import "@openzeppelin/contracts-upgradeable/utils/ReentrancyGuardUpgradeable.sol";
import "@openzeppelin/contracts/token/ERC20/IERC20.sol";
import "@openzeppelin/contracts/token/ERC20/utils/SafeERC20.sol";

/// @title GatewayEVM
/// @notice The GatewayEVM contract is the endpoint to call smart contracts on external chains.
/// @dev The contract doesn't hold any funds and should never have active allowances.
contract GatewayEVM is Initializable, OwnableUpgradeable, UUPSUpgradeable, IGatewayEVMErrors, IGatewayEVMEvents, ReentrancyGuardUpgradeable {
contract GatewayEVM is
Initializable,
OwnableUpgradeable,
UUPSUpgradeable,
IGatewayEVMErrors,
IGatewayEVMEvents,
ReentrancyGuardUpgradeable
{
using SafeERC20 for IERC20;

/// @notice The address of the custody contract.
Expand Down Expand Up @@ -61,7 +69,7 @@ contract GatewayEVM is Initializable, OwnableUpgradeable, UUPSUpgradeable, IGate

/// @dev Authorizes the upgrade of the contract, sender must be owner.
/// @param newImplementation Address of the new implementation.
function _authorizeUpgrade(address newImplementation) internal override onlyOwner() {}
function _authorizeUpgrade(address newImplementation) internal override onlyOwner { }

/// @dev Internal function to execute a call to a destination address.
/// @param destination Address to call.
Expand All @@ -79,7 +87,7 @@ contract GatewayEVM is Initializable, OwnableUpgradeable, UUPSUpgradeable, IGate
/// @param destination Address to call.
/// @param data Calldata to pass to the call.
function executeRevert(address destination, bytes calldata data) public payable onlyTSS {
(bool success, bytes memory result) = destination.call{value: msg.value}("");
(bool success, bytes memory result) = destination.call{ value: msg.value }("");
if (!success) revert ExecutionFailed();
Revertable(destination).onRevert(data);

Expand All @@ -91,7 +99,7 @@ contract GatewayEVM is Initializable, OwnableUpgradeable, UUPSUpgradeable, IGate
/// @param destination Address to call.
/// @param data Calldata to pass to the call.
/// @return The result of the call.
function execute(address destination, bytes calldata data) external payable onlyTSS returns (bytes memory) {
function execute(address destination, bytes calldata data) external payable onlyTSS returns (bytes memory) {
bytes memory result = _execute(destination, data);

emit Executed(destination, msg.value, data);
Expand All @@ -111,7 +119,11 @@ contract GatewayEVM is Initializable, OwnableUpgradeable, UUPSUpgradeable, IGate
address to,
uint256 amount,
bytes calldata data
) public nonReentrant onlyAssetHandler {
)
public
nonReentrant
onlyAssetHandler
{
if (amount == 0) revert InsufficientERC20Amount();
// Approve the target contract to spend the tokens
if (!resetApproval(token, to)) revert ApprovalFailed();
Expand Down Expand Up @@ -142,7 +154,11 @@ contract GatewayEVM is Initializable, OwnableUpgradeable, UUPSUpgradeable, IGate
address to,
uint256 amount,
bytes calldata data
) external nonReentrant onlyAssetHandler {
)
external
nonReentrant
onlyAssetHandler
{
if (amount == 0) revert InsufficientERC20Amount();

IERC20(token).safeTransfer(address(to), amount);
Expand Down Expand Up @@ -234,7 +250,8 @@ contract GatewayEVM is Initializable, OwnableUpgradeable, UUPSUpgradeable, IGate
}

/// @dev Transfers tokens from the sender to the asset handler.
/// This function handles the transfer of tokens to either the connector or custody contract based on the asset type.
/// This function handles the transfer of tokens to either the connector or custody contract based on the asset
/// type.
/// @param from Address of the sender.
/// @param token Address of the ERC20 token.
/// @param amount Amount of tokens to transfer.
Expand All @@ -254,7 +271,8 @@ contract GatewayEVM is Initializable, OwnableUpgradeable, UUPSUpgradeable, IGate
}

/// @dev Transfers tokens to the asset handler.
/// This function handles the transfer of tokens to either the connector or custody contract based on the asset type.
/// This function handles the transfer of tokens to either the connector or custody contract based on the asset
/// type.
/// @param token Address of the ERC20 token.
/// @param amount Amount of tokens to transfer.
function transferToAssetHandler(address token, uint256 amount) private {
Expand Down
32 changes: 28 additions & 4 deletions v2/src/evm/ZetaConnectorNative.sol
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,13 @@ import "@openzeppelin/contracts/token/ERC20/utils/SafeERC20.sol";
contract ZetaConnectorNative is ZetaConnectorNewBase {
using SafeERC20 for IERC20;

constructor(address _gateway, address _zetaToken, address _tssAddress)
constructor(
address _gateway,
address _zetaToken,
address _tssAddress
)
ZetaConnectorNewBase(_gateway, _zetaToken, _tssAddress)
{}
{ }

/// @notice Withdraw tokens to a specified address.
/// @param to The address to withdraw tokens to.
Expand All @@ -31,7 +35,17 @@ contract ZetaConnectorNative is ZetaConnectorNewBase {
/// @param data The calldata to pass to the contract call.
/// @param internalSendHash A hash used for internal tracking of the transaction.
/// @dev This function can only be called by the TSS address.
function withdrawAndCall(address to, uint256 amount, bytes calldata data, bytes32 internalSendHash) external override nonReentrant onlyTSS {
function withdrawAndCall(
address to,
uint256 amount,
bytes calldata data,
bytes32 internalSendHash
)
external
override
nonReentrant
onlyTSS
{
// Transfer zetaToken to the Gateway contract
IERC20(zetaToken).safeTransfer(address(gateway), amount);

Expand All @@ -47,7 +61,17 @@ contract ZetaConnectorNative is ZetaConnectorNewBase {
/// @param data The calldata to pass to the contract call.
/// @param internalSendHash A hash used for internal tracking of the transaction.
/// @dev This function can only be called by the TSS address.
function withdrawAndRevert(address to, uint256 amount, bytes calldata data, bytes32 internalSendHash) external override nonReentrant onlyTSS {
function withdrawAndRevert(
address to,
uint256 amount,
bytes calldata data,
bytes32 internalSendHash
)
external
override
nonReentrant
onlyTSS
{
// Transfer zetaToken to the Gateway contract
IERC20(zetaToken).safeTransfer(address(gateway), amount);

Expand Down
18 changes: 16 additions & 2 deletions v2/src/evm/ZetaConnectorNewBase.sol
Original file line number Diff line number Diff line change
Expand Up @@ -54,14 +54,28 @@ abstract contract ZetaConnectorNewBase is IZetaConnectorEvents, ReentrancyGuard
/// @param amount The amount of tokens to withdraw.
/// @param data The calldata to pass to the contract call.
/// @param internalSendHash A hash used for internal tracking of the transaction.
function withdrawAndCall(address to, uint256 amount, bytes calldata data, bytes32 internalSendHash) external virtual;
function withdrawAndCall(
address to,
uint256 amount,
bytes calldata data,
bytes32 internalSendHash
)
external
virtual;

/// @notice Withdraw tokens and call a contract with a revert callback through Gateway.
/// @param to The address to withdraw tokens to.
/// @param amount The amount of tokens to withdraw.
/// @param data The calldata to pass to the contract call.
/// @param internalSendHash A hash used for internal tracking of the transaction.
function withdrawAndRevert(address to, uint256 amount, bytes calldata data, bytes32 internalSendHash) external virtual;
function withdrawAndRevert(
address to,
uint256 amount,
bytes calldata data,
bytes32 internalSendHash
)
external
virtual;

/// @notice Handle received tokens.
/// @param amount The amount of tokens received.
Expand Down
36 changes: 30 additions & 6 deletions v2/src/evm/ZetaConnectorNonNative.sol
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,21 @@ contract ZetaConnectorNonNative is ZetaConnectorNewBase {
/// @notice Event triggered when max supply is updated.
/// @param maxSupply New max supply.
event MaxSupplyUpdated(uint256 maxSupply);

error ExceedsMaxSupply();

constructor(address _gateway, address _zetaToken, address _tssAddress)
constructor(
address _gateway,
address _zetaToken,
address _tssAddress
)
ZetaConnectorNewBase(_gateway, _zetaToken, _tssAddress)
{}
{ }


/// @notice Set max supply for minting.
/// @param _maxSupply New max supply.
/// @dev This function can only be called by the TSS address.
function setMaxSupply(uint256 _maxSupply) external onlyTSS() {
function setMaxSupply(uint256 _maxSupply) external onlyTSS {
maxSupply = _maxSupply;
emit MaxSupplyUpdated(_maxSupply);
}
Expand All @@ -48,7 +52,17 @@ contract ZetaConnectorNonNative is ZetaConnectorNewBase {
/// @param data The calldata to pass to the contract call.
/// @param internalSendHash A hash used for internal tracking of the transaction.
/// @dev This function can only be called by the TSS address, and mints if supply is not reached.
function withdrawAndCall(address to, uint256 amount, bytes calldata data, bytes32 internalSendHash) external override nonReentrant onlyTSS {
function withdrawAndCall(
address to,
uint256 amount,
bytes calldata data,
bytes32 internalSendHash
)
external
override
nonReentrant
onlyTSS
{
if (amount + IERC20(zetaToken).totalSupply() > maxSupply) revert ExceedsMaxSupply();

// Mint zetaToken to the Gateway contract
Expand All @@ -66,7 +80,17 @@ contract ZetaConnectorNonNative is ZetaConnectorNewBase {
/// @param data The calldata to pass to the contract call.
/// @param internalSendHash A hash used for internal tracking of the transaction.
/// @dev This function can only be called by the TSS address, and mints if supply is not reached.
function withdrawAndRevert(address to, uint256 amount, bytes calldata data, bytes32 internalSendHash) external override nonReentrant onlyTSS {
function withdrawAndRevert(
address to,
uint256 amount,
bytes calldata data,
bytes32 internalSendHash
)
external
override
nonReentrant
onlyTSS
{
if (amount + IERC20(zetaToken).totalSupply() > maxSupply) revert ExceedsMaxSupply();

// Mint zetaToken to the Gateway contract
Expand Down
2 changes: 1 addition & 1 deletion v2/src/evm/interfaces/IERC20CustodyNew.sol
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,4 @@ interface IERC20CustodyNewErrors {

/// @notice Error for invalid sender.
error InvalidSender();
}
}
14 changes: 2 additions & 12 deletions v2/src/evm/interfaces/IGatewayEVM.sol
Original file line number Diff line number Diff line change
Expand Up @@ -81,12 +81,7 @@ interface IGatewayEVM {
/// @param to The address of the contract to call.
/// @param amount The amount of tokens to transfer.
/// @param data The calldata to pass to the contract call.
function executeWithERC20(
address token,
address to,
uint256 amount,
bytes calldata data
) external;
function executeWithERC20(address token, address to, uint256 amount, bytes calldata data) external;

/// @notice Executes a call to a contract.
/// @param destination The address of the contract to call.
Expand All @@ -99,12 +94,7 @@ interface IGatewayEVM {
/// @param to The address of the contract to call.
/// @param amount The amount of tokens to transfer.
/// @param data The calldata to pass to the contract call.
function revertWithERC20(
address token,
address to,
uint256 amount,
bytes calldata data
) external;
function revertWithERC20(address token, address to, uint256 amount, bytes calldata data) external;
}

/// @title Revertable
Expand Down
2 changes: 1 addition & 1 deletion v2/src/evm/interfaces/IZetaNonEthNew.sol
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,4 @@ interface IZetaNonEthNew is IERC20 {
/// @param internalSendHash A hash used for internal tracking of the minting transaction.
/// @dev Emits a {Transfer} event with `from` set to the zero address.
function mint(address mintee, uint256 value, bytes32 internalSendHash) external;
}
}
Loading

0 comments on commit b8db384

Please sign in to comment.