Skip to content

Commit

Permalink
Switch to SafeERC20
Browse files Browse the repository at this point in the history
  • Loading branch information
wirew0lf committed Aug 8, 2024
1 parent 3f54c92 commit 81c9b10
Showing 1 changed file with 12 additions and 13 deletions.
25 changes: 12 additions & 13 deletions CCTPRelayer/src/CCTPRelayer.sol
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,12 @@ import {ICCTPRelayer} from "./interfaces/ICCTPRelayer.sol";
import {ITokenMessenger} from "./interfaces/ITokenMessenger.sol";
import {IMessageTransmitter} from "./interfaces/IMessageTransmitter.sol";

import {SafeERC20} from "openzeppelin-contracts/token/ERC20/utils/SafeERC20.sol";
import {IERC20} from "openzeppelin-contracts/token/ERC20/IERC20.sol";

contract CCTPRelayer is ICCTPRelayer, Initializable, UUPSUpgradeable, Ownable2StepUpgradeable {
using SafeERC20 for IERC20;

IERC20 public usdc;
ITokenMessenger public messenger;
IMessageTransmitter public transmitter;
Expand Down Expand Up @@ -50,8 +53,8 @@ contract CCTPRelayer is ICCTPRelayer, Initializable, UUPSUpgradeable, Ownable2St

function makePaymentForRelay(uint64 nonce, uint256 paymentAmount) external {
if (paymentAmount == 0) revert PaymentCannotBeZero();
// Transfer the funds from the user into the contract and fail if the transfer reverts.
if (!usdc.transferFrom(msg.sender, address(this), paymentAmount)) revert TransferFailed();
// Transfer the funds from the user into the contract.
usdc.safeTransferFrom(msg.sender, address(this), paymentAmount);

// If the transfer succeeds, emit the payment event.
emit PaymentForRelay(nonce, paymentAmount);
Expand All @@ -67,7 +70,7 @@ contract CCTPRelayer is ICCTPRelayer, Initializable, UUPSUpgradeable, Ownable2St
if (transferAmount == 0) revert PaymentCannotBeZero();
if (feeAmount == 0) revert PaymentCannotBeZero();
// In order to save gas do the transfer only once, of both transfer amount and fee amount.
if (!usdc.transferFrom(msg.sender, address(this), transferAmount + feeAmount)) revert TransferFailed();
usdc.safeTransferFrom(msg.sender, address(this), transferAmount + feeAmount);

// Only give allowance of the transfer amount, as we want the fee amount to stay in the contract.
usdc.approve(address(messenger), transferAmount);
Expand Down Expand Up @@ -118,8 +121,6 @@ contract CCTPRelayer is ICCTPRelayer, Initializable, UUPSUpgradeable, Ownable2St

uint256 outputAmount;
if (inputToken == address(0)) {
IERC20 token = IERC20(inputToken);

// Native Token
if (inputAmount != msg.value) revert InsufficientNativeToken();

Expand Down Expand Up @@ -153,7 +154,7 @@ contract CCTPRelayer is ICCTPRelayer, Initializable, UUPSUpgradeable, Ownable2St
uint256 preOutputBalance = usdc.balanceOf(address(this));

// Transfer input ERC20 tokens to the contract
token.transferFrom(msg.sender, address(this), inputAmount);
token.safeTransferFrom(msg.sender, address(this), inputAmount);

// Approve the swap router to spend the input tokens
token.approve(swapRouter, inputAmount);
Expand All @@ -173,7 +174,7 @@ contract CCTPRelayer is ICCTPRelayer, Initializable, UUPSUpgradeable, Ownable2St
// Refund the remaining amount
uint256 dust = postInputBalance - preInputBalance;
if (dust != 0) {
token.transfer(msg.sender, dust);
token.safeTransfer(msg.sender, dust);

// Revoke Approval
token.approve(swapRouter, 0);
Expand Down Expand Up @@ -212,8 +213,6 @@ contract CCTPRelayer is ICCTPRelayer, Initializable, UUPSUpgradeable, Ownable2St
// Native Token
if (inputAmount != msg.value) revert InsufficientNativeToken();

IERC20 token = IERC20(inputToken);

// Get the contract's balances previous to the swap
uint256 preInputBalance = address(this).balance - inputAmount;
uint256 preOutputBalance = usdc.balanceOf(address(this));
Expand Down Expand Up @@ -244,7 +243,7 @@ contract CCTPRelayer is ICCTPRelayer, Initializable, UUPSUpgradeable, Ownable2St
uint256 preOutputBalance = usdc.balanceOf(address(this));

// Transfer input ERC20 tokens to the contract
token.transferFrom(msg.sender, address(this), inputAmount);
token.safeTransferFrom(msg.sender, address(this), inputAmount);

// Approve the swap router to spend the input tokens
token.approve(swapRouter, inputAmount);
Expand All @@ -264,7 +263,7 @@ contract CCTPRelayer is ICCTPRelayer, Initializable, UUPSUpgradeable, Ownable2St
// Refund the remaining amount
uint256 dust = postInputBalance - preInputBalance;
if (dust != 0) {
token.transfer(msg.sender, dust);
token.safeTransfer(msg.sender, dust);

// Revoke Approval
token.approve(swapRouter, 0);
Expand Down Expand Up @@ -311,8 +310,8 @@ contract CCTPRelayer is ICCTPRelayer, Initializable, UUPSUpgradeable, Ownable2St
// Check that the contract has enough balance.
if (usdc.balanceOf(address(this)) < amount) revert MissingBalance();

// Check that the transfer succeeds.
if (!usdc.transfer(receiver, amount)) revert TransferFailed();
// Transfer the amount to the receiver.
usdc.safeTransfer(receiver, amount);
}

fallback() external payable {}
Expand Down

0 comments on commit 81c9b10

Please sign in to comment.