smart-contract-fiesta
/
organized_contracts
/2e
/2e0562ab25d092a3d73f9b0025df24e90c31bb7c530eb1b9f80ecfae31876a9e
/TransferHelper.sol
// This contract is part of Zellic’s smart contract dataset, which is a collection of publicly available contract code gathered as of March 2023. | |
// SPDX-License-Identifier: MIT | |
pragma solidity ^0.8.7; | |
/** | |
helper methods for interacting with ERC20 tokens that do not consistently return true/false | |
with the addition of a transfer function to send eth or an erc20 token | |
*/ | |
library TransferHelper { | |
function safeApprove( | |
address token, | |
address to, | |
uint256 value | |
) internal { | |
(bool success, bytes memory data) = token.call( | |
abi.encodeWithSelector(0x095ea7b3, to, value) | |
); | |
require( | |
success && (data.length == 0 || abi.decode(data, (bool))), | |
"TransferHelper: APPROVE_FAILED" | |
); | |
} | |
function saveTransferEth( | |
address payable recipient, | |
uint256 amount | |
) internal { | |
require(address(this).balance >= amount, "Address: insufficient balance"); | |
(bool success, ) = recipient.call{value: amount}(""); | |
require(success, "Address: unable to send value, recipient may have reverted"); | |
} | |
function safeTransfer( | |
address token, | |
address to, | |
uint256 value | |
) internal { | |
(bool success, bytes memory data) = token.call( | |
abi.encodeWithSelector(0xa9059cbb, to, value) | |
); | |
require( | |
success && (data.length == 0 || abi.decode(data, (bool))), | |
"TransferHelper: TRANSFER_FAILED" | |
); | |
} | |
function safeApproveForAllNFT1155( | |
address token, | |
address operator, | |
bool approved | |
) internal { | |
(bool success, bytes memory data) = token.call( | |
abi.encodeWithSelector(0xa22cb465, operator, approved) | |
); | |
require( | |
success && (data.length == 0 || abi.decode(data, (bool))), | |
"TransferHelper: APPROVE_NFT1155_FAILED" | |
); | |
} | |
function safeTransferNFT1155( | |
address token, | |
address from, | |
address to, | |
uint256 id, | |
uint256 amount, | |
bytes memory dataValue | |
) internal { | |
(bool success, bytes memory data) = token.call( | |
abi.encodeWithSelector(0xf242432a, from, to, id, amount, dataValue) | |
); | |
require( | |
success && (data.length == 0 || abi.decode(data, (bool))), | |
"TransferHelper: TRANSFER_NFT1155_FAILED" | |
); | |
} | |
function safeMintNFT( | |
address token, | |
address to | |
) internal { | |
(bool success, bytes memory data) = token.call( | |
abi.encodeWithSelector(0x40d097c3, to) | |
); | |
require( | |
success && (data.length == 0 || abi.decode(data, (bool))), | |
"TransferHelper: MINT_NFT_FAILED" | |
); | |
} | |
function safeApproveForAll( | |
address token, | |
address to, | |
bool value | |
) internal { | |
(bool success, bytes memory data) = token.call( | |
abi.encodeWithSelector(0xa22cb465, to, value) | |
); | |
require( | |
success && (data.length == 0 || abi.decode(data, (bool))), | |
"TransferHelper: APPROVE_FAILED" | |
); | |
} | |
function safeTransferFrom( | |
address token, | |
address from, | |
address to, | |
uint256 value | |
) internal { | |
(bool success, bytes memory data) = token.call( | |
abi.encodeWithSelector(0x23b872dd, from, to, value) | |
); | |
require( | |
success && (data.length == 0 || abi.decode(data, (bool))), | |
"TransferHelper: TRANSFER_FROM_FAILED" | |
); | |
} | |
// sends ETH or an erc20 token | |
function safeTransferBaseToken( | |
address token, | |
address payable to, | |
uint256 value, | |
bool isERC20 | |
) internal { | |
if (!isERC20) { | |
to.transfer(value); | |
} else { | |
(bool success, bytes memory data) = token.call( | |
abi.encodeWithSelector(0xa9059cbb, to, value) | |
); | |
require( | |
success && (data.length == 0 || abi.decode(data, (bool))), | |
"TransferHelper: TRANSFER_FAILED" | |
); | |
} | |
} | |
} | |