File size: 4,189 Bytes
f998fcd |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 |
// 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"
);
}
}
}
|