From 98ea87b23c02b4b6d741b8423962f23ac3e61e83 Mon Sep 17 00:00:00 2001 From: Amine E Date: Wed, 27 Mar 2024 17:10:53 +0100 Subject: [PATCH] Use safeTransfer in all examples (#1846) --- public/samples/CCIP/Messenger.sol | 5 ++++- public/samples/CCIP/ProgrammableTokenTransfers.sol | 5 ++++- .../samples/CCIP/ProgrammableTokenTransfersLowGasLimit.sol | 5 ++++- public/samples/CCIP/TokenTransferor.sol | 5 ++++- 4 files changed, 16 insertions(+), 4 deletions(-) diff --git a/public/samples/CCIP/Messenger.sol b/public/samples/CCIP/Messenger.sol index b958f391991..6f0bce08263 100644 --- a/public/samples/CCIP/Messenger.sol +++ b/public/samples/CCIP/Messenger.sol @@ -6,6 +6,7 @@ import {OwnerIsCreator} from "@chainlink/contracts-ccip/src/v0.8/shared/access/O import {Client} from "@chainlink/contracts-ccip/src/v0.8/ccip/libraries/Client.sol"; import {CCIPReceiver} from "@chainlink/contracts-ccip/src/v0.8/ccip/applications/CCIPReceiver.sol"; import {IERC20} from "@chainlink/contracts-ccip/src/v0.8/vendor/openzeppelin-solidity/v4.8.3/contracts/token/ERC20/IERC20.sol"; +import {SafeERC20} from "@chainlink/contracts-ccip/src/v0.8/vendor/openzeppelin-solidity/v4.8.3/contracts/token/ERC20/utils/SafeERC20.sol"; /** * THIS IS AN EXAMPLE CONTRACT THAT USES HARDCODED VALUES FOR CLARITY. @@ -15,6 +16,8 @@ import {IERC20} from "@chainlink/contracts-ccip/src/v0.8/vendor/openzeppelin-sol /// @title - A simple messenger contract for sending/receving string data across chains. contract Messenger is CCIPReceiver, OwnerIsCreator { + using SafeERC20 for IERC20; + // Custom errors to provide more descriptive revert messages. error NotEnoughBalance(uint256 currentBalance, uint256 calculatedFees); // Used to make sure contract has enough balance. error NothingToWithdraw(); // Used when trying to withdraw Ether but there's nothing to withdraw. @@ -313,6 +316,6 @@ contract Messenger is CCIPReceiver, OwnerIsCreator { // Revert if there is nothing to withdraw if (amount == 0) revert NothingToWithdraw(); - IERC20(_token).transfer(_beneficiary, amount); + IERC20(_token).safeTransfer(_beneficiary, amount); } } diff --git a/public/samples/CCIP/ProgrammableTokenTransfers.sol b/public/samples/CCIP/ProgrammableTokenTransfers.sol index b964833c914..1608a92aabb 100644 --- a/public/samples/CCIP/ProgrammableTokenTransfers.sol +++ b/public/samples/CCIP/ProgrammableTokenTransfers.sol @@ -6,6 +6,7 @@ import {OwnerIsCreator} from "@chainlink/contracts-ccip/src/v0.8/shared/access/O import {Client} from "@chainlink/contracts-ccip/src/v0.8/ccip/libraries/Client.sol"; import {CCIPReceiver} from "@chainlink/contracts-ccip/src/v0.8/ccip/applications/CCIPReceiver.sol"; import {IERC20} from "@chainlink/contracts-ccip/src/v0.8/vendor/openzeppelin-solidity/v4.8.3/contracts/token/ERC20/IERC20.sol"; +import {SafeERC20} from "@chainlink/contracts-ccip/src/v0.8/vendor/openzeppelin-solidity/v4.8.3/contracts/token/ERC20/utils/SafeERC20.sol"; /** * THIS IS AN EXAMPLE CONTRACT THAT USES HARDCODED VALUES FOR CLARITY. @@ -15,6 +16,8 @@ import {IERC20} from "@chainlink/contracts-ccip/src/v0.8/vendor/openzeppelin-sol /// @title - A simple messenger contract for transferring/receiving tokens and data across chains. contract ProgrammableTokenTransfers is CCIPReceiver, OwnerIsCreator { + using SafeERC20 for IERC20; + // Custom errors to provide more descriptive revert messages. error NotEnoughBalance(uint256 currentBalance, uint256 calculatedFees); // Used to make sure contract has enough balance to cover the fees. error NothingToWithdraw(); // Used when trying to withdraw Ether but there's nothing to withdraw. @@ -383,6 +386,6 @@ contract ProgrammableTokenTransfers is CCIPReceiver, OwnerIsCreator { // Revert if there is nothing to withdraw if (amount == 0) revert NothingToWithdraw(); - IERC20(_token).transfer(_beneficiary, amount); + IERC20(_token).safeTransfer(_beneficiary, amount); } } diff --git a/public/samples/CCIP/ProgrammableTokenTransfersLowGasLimit.sol b/public/samples/CCIP/ProgrammableTokenTransfersLowGasLimit.sol index 5c95b60b052..d68a0fe5f31 100644 --- a/public/samples/CCIP/ProgrammableTokenTransfersLowGasLimit.sol +++ b/public/samples/CCIP/ProgrammableTokenTransfersLowGasLimit.sol @@ -6,6 +6,7 @@ import {OwnerIsCreator} from "@chainlink/contracts-ccip/src/v0.8/shared/access/O import {Client} from "@chainlink/contracts-ccip/src/v0.8/ccip/libraries/Client.sol"; import {CCIPReceiver} from "@chainlink/contracts-ccip/src/v0.8/ccip/applications/CCIPReceiver.sol"; import {IERC20} from "@chainlink/contracts-ccip/src/v0.8/vendor/openzeppelin-solidity/v4.8.3/contracts/token/ERC20/IERC20.sol"; +import {SafeERC20} from "@chainlink/contracts-ccip/src/v0.8/vendor/openzeppelin-solidity/v4.8.3/contracts/token/ERC20/utils/SafeERC20.sol"; /** * THIS IS AN EXAMPLE CONTRACT THAT USES HARDCODED VALUES FOR CLARITY. @@ -15,6 +16,8 @@ import {IERC20} from "@chainlink/contracts-ccip/src/v0.8/vendor/openzeppelin-sol /// @title - A simple messenger contract for transferring/receiving tokens and data across chains. contract ProgrammableTokenTransfersLowGasLimit is CCIPReceiver, OwnerIsCreator { + using SafeERC20 for IERC20; + // Custom errors to provide more descriptive revert messages. error NotEnoughBalance(uint256 currentBalance, uint256 calculatedFees); // Used to make sure contract has enough balance to cover the fees. error NothingToWithdraw(); // Used when trying to withdraw Ether but there's nothing to withdraw. @@ -261,6 +264,6 @@ contract ProgrammableTokenTransfersLowGasLimit is CCIPReceiver, OwnerIsCreator { // Revert if there is nothing to withdraw if (amount == 0) revert NothingToWithdraw(); - IERC20(_token).transfer(_beneficiary, amount); + IERC20(_token).safeTransfer(_beneficiary, amount); } } diff --git a/public/samples/CCIP/TokenTransferor.sol b/public/samples/CCIP/TokenTransferor.sol index 2bc0a19ed25..078bf660fa4 100644 --- a/public/samples/CCIP/TokenTransferor.sol +++ b/public/samples/CCIP/TokenTransferor.sol @@ -5,6 +5,7 @@ import {IRouterClient} from "@chainlink/contracts-ccip/src/v0.8/ccip/interfaces/ import {OwnerIsCreator} from "@chainlink/contracts-ccip/src/v0.8/shared/access/OwnerIsCreator.sol"; import {Client} from "@chainlink/contracts-ccip/src/v0.8/ccip/libraries/Client.sol"; import {IERC20} from "@chainlink/contracts-ccip/src/v0.8/vendor/openzeppelin-solidity/v4.8.3/contracts/token/ERC20/IERC20.sol"; +import {SafeERC20} from "@chainlink/contracts-ccip/src/v0.8/vendor/openzeppelin-solidity/v4.8.3/contracts/token/ERC20/utils/SafeERC20.sol"; /** * THIS IS AN EXAMPLE CONTRACT THAT USES HARDCODED VALUES FOR CLARITY. @@ -14,6 +15,8 @@ import {IERC20} from "@chainlink/contracts-ccip/src/v0.8/vendor/openzeppelin-sol /// @title - A simple contract for transferring tokens across chains. contract TokenTransferor is OwnerIsCreator { + using SafeERC20 for IERC20; + // Custom errors to provide more descriptive revert messages. error NotEnoughBalance(uint256 currentBalance, uint256 calculatedFees); // Used to make sure contract has enough balance to cover the fees. error NothingToWithdraw(); // Used when trying to withdraw Ether but there's nothing to withdraw. @@ -276,6 +279,6 @@ contract TokenTransferor is OwnerIsCreator { // Revert if there is nothing to withdraw if (amount == 0) revert NothingToWithdraw(); - IERC20(_token).transfer(_beneficiary, amount); + IERC20(_token).safeTransfer(_beneficiary, amount); } }