From a6d9809162b35c8759b62090f9db596527d69031 Mon Sep 17 00:00:00 2001 From: Denis Fadeev Date: Wed, 20 Nov 2024 20:37:36 +0300 Subject: [PATCH] safeTransferFrom --- examples/call/contracts/Connected.sol | 21 +++++++-------------- 1 file changed, 7 insertions(+), 14 deletions(-) diff --git a/examples/call/contracts/Connected.sol b/examples/call/contracts/Connected.sol index 2c89558..5cd333c 100644 --- a/examples/call/contracts/Connected.sol +++ b/examples/call/contracts/Connected.sol @@ -3,15 +3,16 @@ 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() { @@ -44,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); } @@ -60,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); }