Проверенный контрактТочноеMIT
UMADVoucherRedeemer
Адрес0x9dfecc75d2b9df1ae2cd4e76b5d4a4bdb4365e0d
- Баланс
0 ETH
- Транзакций
3
- Компилятор
solc 0.7.6
- Тот же код
Только этот контракт
Подробности
- Компилятор
- solc 0.7.6+commit.7338295f
- Язык
- solidity
- Оптимизация
- Нет
- Лицензия
- MIT
- Совпадение
- Точноекод создания: да, код в сети: да
- Проверен на Sourcify
- Создан в блоке
- 14 899 080
- Транзакция создания
- 0x202df09747…f850d9d5a3
13 файлов
- UMADVoucherRedeemer.solглавный5,29 КБ
- @animoca/[email protected]/contracts/token/ERC1155/ERC1155TokenReceiver.sol1,13 КБ
- @animoca/[email protected]/contracts/token/ERC1155/IERC1155InventoryBurnable.sol1,98 КБ
- @animoca/[email protected]/contracts/token/ERC1155/IERC1155TokenReceiver.sol2,87 КБ
- @animoca/ethereum-contracts-core/contracts/introspection/IERC165.sol625 Б
- @animoca/[email protected]/contracts/access/IERC173.sol968 Б
- @animoca/[email protected]/contracts/access/Ownable.sol1,91 КБ
- @animoca/[email protected]/contracts/lifecycle/Pausable.sol1,3 КБ
- @animoca/[email protected]/contracts/metatx/ManagedIdentity.sol565 Б
- @animoca/[email protected]/contracts/utils/ERC20Wrapper.sol2,33 КБ
- @animoca/[email protected]/contracts/utils/Recoverable.sol3,15 КБ
- @animoca/[email protected]/contracts/utils/types/AddressIsContract.sol1,29 КБ
- @openzeppelin/[email protected]/math/SafeMath.sol7,23 КБ
UMADVoucherRedeemer.sol
// SPDX-License-Identifier: MIT
pragma solidity >=0.7.6 <0.8.0;
import "@animoca/[email protected]/contracts/token/ERC1155/ERC1155TokenReceiver.sol";
import "@animoca/[email protected]/contracts/token/ERC1155/IERC1155InventoryBurnable.sol";
import "@animoca/[email protected]/contracts/utils/Recoverable.sol";
import "@animoca/[email protected]/contracts/lifecycle/Pausable.sol";
import "@openzeppelin/[email protected]/math/SafeMath.sol";
contract UMADVoucherRedeemer is Recoverable, Pausable, ERC1155TokenReceiver{
using ERC20Wrapper for IWrappedERC20;
using SafeMath for uint256;
IERC1155InventoryBurnable public immutable vouchersContract;
IWrappedERC20 public immutable tokenContract;
address public tokenHolder;
mapping (uint256 => uint256) private _voucherTokenAmount;
/**
* Constructor.
* @param _vouchersContract the address of the vouchers contract.
* @param _tokenContract the address of the ERC20 token contract.
* @param _tokenHolder the address of the ERC20 token holder.
*/
constructor(
IERC1155InventoryBurnable _vouchersContract,
IWrappedERC20 _tokenContract,
address _tokenHolder
) Ownable(msg.sender) Pausable(true){
vouchersContract = _vouchersContract;
tokenContract = _tokenContract;
tokenHolder = _tokenHolder;
}
/**
* Sets the ERC20 token value for voucher.
* @dev Reverts if the sender is not the contract owner.
* @param tokenIds the id of the voucher.
* @param amounts value of the voucher in ERC20 token.
*/
function setVoucherValues(uint256[] memory tokenIds, uint256[] memory amounts) external virtual{
_requireOwnership(_msgSender());
require(tokenIds.length == amounts.length, "UMADVoucherRedeemer: invalid length of array");
for(uint256 i; i < tokenIds.length; ++i){
uint256 amount = amounts[i];
require(amount > 0, "UMADVoucherRedeemer: invalid amount");
_voucherTokenAmount[tokenIds[i]] = amount;
}
}
/**
* Gets the ERC20 token value for voucher.
* @param tokenId the id of the voucher.
*/
function getVoucherValue(uint256 tokenId) external view virtual returns (uint256){
return _voucherTokenAmount[tokenId];
}
/**
* Validates the validity of the voucher and returns its value.
* @dev Reverts if the voucher is not a valid voucher.
* @param tokenId the id of the voucher.
* @return the value of the voucher in ERC20 token.
*/
function _voucherValue(uint256 tokenId) internal view virtual returns (uint256) {
uint256 tokenValue = _voucherTokenAmount[tokenId];
require(tokenValue > 0, "UMADVoucherRedeemer: invalid voucher");
return tokenValue;
}
/**
* Sets the token holder address.
* @dev Reverts if the sender is not the contract owner.
* @param _tokenHolder the new address for the token holder.
*/
function setTokenHolder(address _tokenHolder) external virtual {
_requireOwnership(_msgSender());
tokenHolder = _tokenHolder;
}
/**
* Pause the redeem function.
* @dev Reverts if the sender is not the contract owner.
*/
function pause() public{
_requireOwnership(_msgSender());
_pause();
}
/**
* Unpause the redeem function.
* @dev Reverts if the sender is not the contract owner.
*/
function unpause() public{
_requireOwnership(_msgSender());
_unpause();
}
/**
* Handle the receipt of a single ERC1155 token type.
* @dev See {IERC1155TokenReceiver-onERC1155Received(address,address,uint256,uint256,bytes)}.
*/
function onERC1155Received(
address, /*operator*/
address from,
uint256 id,
uint256 value,
bytes calldata /*data*/
) external virtual override returns (bytes4) {
_requireNotPaused();
require(msg.sender == address(vouchersContract), "UMADVoucherRedeemer: wrong sender");
vouchersContract.burnFrom(address(this), id, value);
uint256 tokenAmount = _voucherValue(id).mul(value);
tokenContract.wrappedTransferFrom(tokenHolder, from, tokenAmount);
return _ERC1155_RECEIVED;
}
/**
* Handle the receipt of multiple ERC1155 token types.
* @dev See {IERC1155TokenReceiver-onERC1155BatchReceived(address,address,uint256[],uint256[],bytes)}.
*/
function onERC1155BatchReceived(
address, /*operator*/
address from,
uint256[] calldata ids,
uint256[] calldata values,
bytes calldata /*data*/
) external virtual override returns (bytes4) {
_requireNotPaused();
require(msg.sender == address(vouchersContract), "UMADVoucherRedeemer: wrong sender");
vouchersContract.batchBurnFrom(address(this), ids, values);
uint256 tokenAmount;
for (uint256 i; i != ids.length; ++i) {
uint256 id = ids[i];
tokenAmount = tokenAmount.add(_voucherValue(id).mul(values[i]));
}
tokenContract.wrappedTransferFrom(tokenHolder, from, tokenAmount);
return _ERC1155_BATCH_RECEIVED;
}
}@animoca/[email protected]/contracts/token/ERC1155/ERC1155TokenReceiver.sol
// SPDX-License-Identifier: MIT
pragma solidity >=0.7.6 <0.8.0;
import {IERC1155TokenReceiver} from "./IERC1155TokenReceiver.sol";
import {IERC165} from "@animoca/ethereum-contracts-core/contracts/introspection/IERC165.sol";
abstract contract ERC1155TokenReceiver is IERC1155TokenReceiver, IERC165 {
bytes4 private constant _ERC165_INTERFACE_ID = type(IERC165).interfaceId;
bytes4 private constant _ERC1155_TOKEN_RECEIVER_INTERFACE_ID = type(IERC1155TokenReceiver).interfaceId;
// bytes4(keccak256("onERC1155Received(address,address,uint256,uint256,bytes)"))
bytes4 internal constant _ERC1155_RECEIVED = 0xf23a6e61;
// bytes4(keccak256("onERC1155BatchReceived(address,address,uint256[],uint256[],bytes)"))
bytes4 internal constant _ERC1155_BATCH_RECEIVED = 0xbc197c81;
bytes4 internal constant _ERC1155_REJECTED = 0xffffffff;
/**
* @dev See {IERC165-supportsInterface}.
*/
function supportsInterface(bytes4 interfaceId) public view virtual override returns (bool) {
return interfaceId == _ERC165_INTERFACE_ID || interfaceId == _ERC1155_TOKEN_RECEIVER_INTERFACE_ID;
}
}
@animoca/[email protected]/contracts/token/ERC1155/IERC1155InventoryBurnable.sol
// SPDX-License-Identifier: MIT
pragma solidity >=0.7.6 <0.8.0;
/**
* @title ERC-1155 Inventory additional burning interface
* @dev See https://eips.ethereum.org/EIPS/eip-1155
*/
interface IERC1155InventoryBurnable {
/**
* Burns some token.
* @dev Reverts if the sender is not approved.
* @dev Reverts if `id` does not represent a token.
* @dev Reverts if `id` represents a fungible token and `value` is 0.
* @dev Reverts if `id` represents a fungible token and `value` is higher than `from`'s balance.
* @dev Reverts if `id` represents a non-fungible token and `value` is not 1.
* @dev Reverts if `id` represents a non-fungible token which is not owned by `from`.
* @dev Emits an {IERC1155-TransferSingle} event.
* @param from Address of the current token owner.
* @param id Identifier of the token to burn.
* @param value Amount of token to burn.
*/
function burnFrom(
address from,
uint256 id,
uint256 value
) external;
/**
* Burns multiple tokens.
* @dev Reverts if `ids` and `values` have different lengths.
* @dev Reverts if the sender is not approved.
* @dev Reverts if one of `ids` does not represent a token.
* @dev Reverts if one of `ids` represents a fungible token and `value` is 0.
* @dev Reverts if one of `ids` represents a fungible token and `value` is higher than `from`'s balance.
* @dev Reverts if one of `ids` represents a non-fungible token and `value` is not 1.
* @dev Reverts if one of `ids` represents a non-fungible token which is not owned by `from`.
* @dev Emits an {IERC1155-TransferBatch} event.
* @param from Address of the current tokens owner.
* @param ids Identifiers of the tokens to burn.
* @param values Amounts of tokens to burn.
*/
function batchBurnFrom(
address from,
uint256[] calldata ids,
uint256[] calldata values
) external;
}
@animoca/[email protected]/contracts/token/ERC1155/IERC1155TokenReceiver.sol
// SPDX-License-Identifier: MIT
pragma solidity >=0.7.6 <0.8.0;
/**
* @title ERC-1155 Multi Token Standard, token receiver
* @dev See https://eips.ethereum.org/EIPS/eip-1155
* Interface for any contract that wants to support transfers from ERC1155 asset contracts.
* Note: The ERC-165 identifier for this interface is 0x4e2312e0.
*/
interface IERC1155TokenReceiver {
/**
* @notice Handle the receipt of a single ERC1155 token type.
* An ERC1155 contract MUST call this function on a recipient contract, at the end of a `safeTransferFrom` after the balance update.
* This function MUST return `bytes4(keccak256("onERC1155Received(address,address,uint256,uint256,bytes)"))`
* (i.e. 0xf23a6e61) to accept the transfer.
* Return of any other value than the prescribed keccak256 generated value MUST result in the transaction being reverted by the caller.
* @param operator The address which initiated the transfer (i.e. msg.sender)
* @param from The address which previously owned the token
* @param id The ID of the token being transferred
* @param value The amount of tokens being transferred
* @param data Additional data with no specified format
* @return bytes4 `bytes4(keccak256("onERC1155Received(address,address,uint256,uint256,bytes)"))`
*/
function onERC1155Received(
address operator,
address from,
uint256 id,
uint256 value,
bytes calldata data
) external returns (bytes4);
/**
* @notice Handle the receipt of multiple ERC1155 token types.
* An ERC1155 contract MUST call this function on a recipient contract, at the end of a `safeBatchTransferFrom` after the balance updates.
* This function MUST return `bytes4(keccak256("onERC1155BatchReceived(address,address,uint256[],uint256[],bytes)"))`
* (i.e. 0xbc197c81) if to accept the transfer(s).
* Return of any other value than the prescribed keccak256 generated value MUST result in the transaction being reverted by the caller.
* @param operator The address which initiated the batch transfer (i.e. msg.sender)
* @param from The address which previously owned the token
* @param ids An array containing ids of each token being transferred (order and length must match _values array)
* @param values An array containing amounts of each token being transferred (order and length must match _ids array)
* @param data Additional data with no specified format
* @return `bytes4(keccak256("onERC1155BatchReceived(address,address,uint256[],uint256[],bytes)"))`
*/
function onERC1155BatchReceived(
address operator,
address from,
uint256[] calldata ids,
uint256[] calldata values,
bytes calldata data
) external returns (bytes4);
}
@animoca/ethereum-contracts-core/contracts/introspection/IERC165.sol
// SPDX-License-Identifier: MIT
pragma solidity >=0.7.6 <0.8.0;
/**
* @dev Interface of the ERC165 standard, as defined in the
* https://eips.ethereum.org/EIPS/eip-165.
*/
interface IERC165 {
/**
* @dev Returns true if this contract implements the interface defined by
* `interfaceId`. See the corresponding
* https://eips.ethereum.org/EIPS/eip-165#how-interfaces-are-identified[EIP section]
* to learn more about how these ids are created.
*
* This function call must use less than 30 000 gas.
*/
function supportsInterface(bytes4 interfaceId) external view returns (bool);
}
@animoca/[email protected]/contracts/access/IERC173.sol
// SPDX-License-Identifier: MIT
pragma solidity >=0.7.6 <0.8.0;
/**
* @title ERC-173 Contract Ownership Standard
* Note: the ERC-165 identifier for this interface is 0x7f5828d0
*/
interface IERC173 {
/**
* Event emited when ownership of a contract changes.
* @param previousOwner the previous owner.
* @param newOwner the new owner.
*/
event OwnershipTransferred(address indexed previousOwner, address indexed newOwner);
/**
* Get the address of the owner
* @return The address of the owner.
*/
function owner() external view returns (address);
/**
* Set the address of the new owner of the contract
* Set newOwner to address(0) to renounce any ownership.
* @dev Emits an {OwnershipTransferred} event.
* @param newOwner The address of the new owner of the contract. Using the zero address means renouncing ownership.
*/
function transferOwnership(address newOwner) external;
}
@animoca/[email protected]/contracts/access/Ownable.sol
// SPDX-License-Identifier: MIT
pragma solidity >=0.7.6 <0.8.0;
import {ManagedIdentity} from "../metatx/ManagedIdentity.sol";
import {IERC173} from "./IERC173.sol";
/**
* @dev Contract module which provides a basic access control mechanism, where
* there is an account (an owner) that can be granted exclusive access to
* specific functions.
*
* By default, the owner account will be the one that deploys the contract. This
* can later be changed with {transferOwnership}.
*
* This module is used through inheritance. It will make available the modifier
* `onlyOwner`, which can be applied to your functions to restrict their use to
* the owner.
*/
abstract contract Ownable is ManagedIdentity, IERC173 {
address internal _owner;
/**
* Initializes the contract, setting the deployer as the initial owner.
* @dev Emits an {IERC173-OwnershipTransferred(address,address)} event.
*/
constructor(address owner_) {
_owner = owner_;
emit OwnershipTransferred(address(0), owner_);
}
/**
* Gets the address of the current contract owner.
*/
function owner() public view virtual override returns (address) {
return _owner;
}
/**
* See {IERC173-transferOwnership(address)}
* @dev Reverts if the sender is not the current contract owner.
* @param newOwner the address of the new owner. Use the zero address to renounce the ownership.
*/
function transferOwnership(address newOwner) public virtual override {
_requireOwnership(_msgSender());
_owner = newOwner;
emit OwnershipTransferred(_owner, newOwner);
}
/**
* @dev Reverts if `account` is not the contract owner.
* @param account the account to test.
*/
function _requireOwnership(address account) internal virtual {
require(account == this.owner(), "Ownable: not the owner");
}
}
@animoca/[email protected]/contracts/lifecycle/Pausable.sol
// SPDX-License-Identifier: MIT
pragma solidity >=0.7.6 <0.8.0;
import {ManagedIdentity} from "../metatx/ManagedIdentity.sol";
/**
* @dev Contract which allows children to implement pausability.
*/
abstract contract Pausable is ManagedIdentity {
/**
* @dev Emitted when the pause is triggered by `account`.
*/
event Paused(address account);
/**
* @dev Emitted when the pause is lifted by `account`.
*/
event Unpaused(address account);
bool public paused;
constructor(bool paused_) {
paused = paused_;
}
function _requireNotPaused() internal view {
require(!paused, "Pausable: paused");
}
function _requirePaused() internal view {
require(paused, "Pausable: not paused");
}
/**
* @dev Triggers stopped state.
*
* Requirements:
*
* - The contract must not be paused.
*/
function _pause() internal virtual {
_requireNotPaused();
paused = true;
emit Paused(_msgSender());
}
/**
* @dev Returns to normal state.
*
* Requirements:
*
* - The contract must be paused.
*/
function _unpause() internal virtual {
_requirePaused();
paused = false;
emit Unpaused(_msgSender());
}
}
@animoca/[email protected]/contracts/metatx/ManagedIdentity.sol
// SPDX-License-Identifier: MIT
pragma solidity >=0.7.6 <0.8.0;
/*
* Provides information about the current execution context, including the
* sender of the transaction and its data. While these are generally available
* via msg.sender and msg.data, they should not be accessed in such a direct
* manner.
*/
abstract contract ManagedIdentity {
function _msgSender() internal view virtual returns (address payable) {
return msg.sender;
}
function _msgData() internal view virtual returns (bytes memory) {
return msg.data;
}
}
@animoca/[email protected]/contracts/utils/ERC20Wrapper.sol
// SPDX-License-Identifier: MIT
pragma solidity >=0.7.6 <0.8.0;
import {AddressIsContract} from "./types/AddressIsContract.sol";
/**
* @title ERC20Wrapper
* Wraps ERC20 functions to support non-standard implementations which do not return a bool value.
* Calls to the wrapped functions revert only if they throw or if they return false.
*/
library ERC20Wrapper {
using AddressIsContract for address;
function wrappedTransfer(
IWrappedERC20 token,
address to,
uint256 value
) internal {
_callWithOptionalReturnData(token, abi.encodeWithSelector(token.transfer.selector, to, value));
}
function wrappedTransferFrom(
IWrappedERC20 token,
address from,
address to,
uint256 value
) internal {
_callWithOptionalReturnData(token, abi.encodeWithSelector(token.transferFrom.selector, from, to, value));
}
function wrappedApprove(
IWrappedERC20 token,
address spender,
uint256 value
) internal {
_callWithOptionalReturnData(token, abi.encodeWithSelector(token.approve.selector, spender, value));
}
function _callWithOptionalReturnData(IWrappedERC20 token, bytes memory callData) internal {
address target = address(token);
require(target.isContract(), "ERC20Wrapper: non-contract");
// solhint-disable-next-line avoid-low-level-calls
(bool success, bytes memory data) = target.call(callData);
if (success) {
if (data.length != 0) {
require(abi.decode(data, (bool)), "ERC20Wrapper: operation failed");
}
} else {
// revert using a standard revert message
if (data.length == 0) {
revert("ERC20Wrapper: operation failed");
}
// revert using the revert message coming from the call
assembly {
let size := mload(data)
revert(add(32, data), size)
}
}
}
}
interface IWrappedERC20 {
function transfer(address to, uint256 value) external returns (bool);
function transferFrom(
address from,
address to,
uint256 value
) external returns (bool);
function approve(address spender, uint256 value) external returns (bool);
}
@animoca/[email protected]/contracts/utils/Recoverable.sol
// SPDX-License-Identifier: MIT
pragma solidity >=0.7.6 <0.8.0;
import {ManagedIdentity} from "../metatx/ManagedIdentity.sol";
import {Ownable} from "../access/Ownable.sol";
import {IWrappedERC20, ERC20Wrapper} from "./ERC20Wrapper.sol";
abstract contract Recoverable is ManagedIdentity, Ownable {
using ERC20Wrapper for IWrappedERC20;
/**
* Extract ERC20 tokens which were accidentally sent to the contract to a list of accounts.
* Warning: this function should be overriden for contracts which are supposed to hold ERC20 tokens
* so that the extraction is limited to only amounts sent accidentally.
* @dev Reverts if the sender is not the contract owner.
* @dev Reverts if `accounts`, `tokens` and `amounts` do not have the same length.
* @dev Reverts if one of `tokens` is does not implement the ERC20 transfer function.
* @dev Reverts if one of the ERC20 transfers fail for any reason.
* @param accounts the list of accounts to transfer the tokens to.
* @param tokens the list of ERC20 token addresses.
* @param amounts the list of token amounts to transfer.
*/
function recoverERC20s(
address[] calldata accounts,
address[] calldata tokens,
uint256[] calldata amounts
) external virtual {
_requireOwnership(_msgSender());
uint256 length = accounts.length;
require(length == tokens.length && length == amounts.length, "Recov: inconsistent arrays");
for (uint256 i = 0; i != length; ++i) {
IWrappedERC20(tokens[i]).wrappedTransfer(accounts[i], amounts[i]);
}
}
/**
* Extract ERC721 tokens which were accidentally sent to the contract to a list of accounts.
* Warning: this function should be overriden for contracts which are supposed to hold ERC721 tokens
* so that the extraction is limited to only tokens sent accidentally.
* @dev Reverts if the sender is not the contract owner.
* @dev Reverts if `accounts`, `contracts` and `amounts` do not have the same length.
* @dev Reverts if one of `contracts` is does not implement the ERC721 transferFrom function.
* @dev Reverts if one of the ERC721 transfers fail for any reason.
* @param accounts the list of accounts to transfer the tokens to.
* @param contracts the list of ERC721 contract addresses.
* @param tokenIds the list of token ids to transfer.
*/
function recoverERC721s(
address[] calldata accounts,
address[] calldata contracts,
uint256[] calldata tokenIds
) external virtual {
_requireOwnership(_msgSender());
uint256 length = accounts.length;
require(length == contracts.length && length == tokenIds.length, "Recov: inconsistent arrays");
for (uint256 i = 0; i != length; ++i) {
IRecoverableERC721(contracts[i]).transferFrom(address(this), accounts[i], tokenIds[i]);
}
}
}
interface IRecoverableERC721 {
/// See {IERC721-transferFrom(address,address,uint256)}
function transferFrom(
address from,
address to,
uint256 tokenId
) external;
}
@animoca/[email protected]/contracts/utils/types/AddressIsContract.sol
// SPDX-License-Identifier: MIT
// Partially derived from OpenZeppelin:
// https://github.com/OpenZeppelin/openzeppelin-contracts/blob/406c83649bd6169fc1b578e08506d78f0873b276/contracts/utils/Address.sol
pragma solidity >=0.7.6 <0.8.0;
/**
* @dev Upgrades the address type to check if it is a contract.
*/
library AddressIsContract {
/**
* @dev Returns true if `account` is a contract.
*
* [IMPORTANT]
* ====
* It is unsafe to assume that an address for which this function returns
* false is an externally-owned account (EOA) and not a contract.
*
* Among others, `isContract` will return false for the following
* types of addresses:
*
* - an externally-owned account
* - a contract in construction
* - an address where a contract will be created
* - an address where a contract lived, but was destroyed
* ====
*/
function isContract(address account) internal view returns (bool) {
// This method relies on extcodesize, which returns 0 for contracts in
// construction, since the code is only stored at the end of the
// constructor execution.
uint256 size;
assembly {
size := extcodesize(account)
}
return size > 0;
}
}
@openzeppelin/[email protected]/math/SafeMath.sol
// SPDX-License-Identifier: MIT
pragma solidity >=0.6.0 <0.8.0;
/**
* @dev Wrappers over Solidity's arithmetic operations with added overflow
* checks.
*
* Arithmetic operations in Solidity wrap on overflow. This can easily result
* in bugs, because programmers usually assume that an overflow raises an
* error, which is the standard behavior in high level programming languages.
* `SafeMath` restores this intuition by reverting the transaction when an
* operation overflows.
*
* Using this library instead of the unchecked operations eliminates an entire
* class of bugs, so it's recommended to use it always.
*/
library SafeMath {
/**
* @dev Returns the addition of two unsigned integers, with an overflow flag.
*
* _Available since v3.4._
*/
function tryAdd(uint256 a, uint256 b) internal pure returns (bool, uint256) {
uint256 c = a + b;
if (c < a) return (false, 0);
return (true, c);
}
/**
* @dev Returns the substraction of two unsigned integers, with an overflow flag.
*
* _Available since v3.4._
*/
function trySub(uint256 a, uint256 b) internal pure returns (bool, uint256) {
if (b > a) return (false, 0);
return (true, a - b);
}
/**
* @dev Returns the multiplication of two unsigned integers, with an overflow flag.
*
* _Available since v3.4._
*/
function tryMul(uint256 a, uint256 b) internal pure returns (bool, uint256) {
// Gas optimization: this is cheaper than requiring 'a' not being zero, but the
// benefit is lost if 'b' is also tested.
// See: https://github.com/OpenZeppelin/openzeppelin-contracts/pull/522
if (a == 0) return (true, 0);
uint256 c = a * b;
if (c / a != b) return (false, 0);
return (true, c);
}
/**
* @dev Returns the division of two unsigned integers, with a division by zero flag.
*
* _Available since v3.4._
*/
function tryDiv(uint256 a, uint256 b) internal pure returns (bool, uint256) {
if (b == 0) return (false, 0);
return (true, a / b);
}
/**
* @dev Returns the remainder of dividing two unsigned integers, with a division by zero flag.
*
* _Available since v3.4._
*/
function tryMod(uint256 a, uint256 b) internal pure returns (bool, uint256) {
if (b == 0) return (false, 0);
return (true, a % b);
}
/**
* @dev Returns the addition of two unsigned integers, reverting on
* overflow.
*
* Counterpart to Solidity's `+` operator.
*
* Requirements:
*
* - Addition cannot overflow.
*/
function add(uint256 a, uint256 b) internal pure returns (uint256) {
uint256 c = a + b;
require(c >= a, "SafeMath: addition overflow");
return c;
}
/**
* @dev Returns the subtraction of two unsigned integers, reverting on
* overflow (when the result is negative).
*
* Counterpart to Solidity's `-` operator.
*
* Requirements:
*
* - Subtraction cannot overflow.
*/
function sub(uint256 a, uint256 b) internal pure returns (uint256) {
require(b <= a, "SafeMath: subtraction overflow");
return a - b;
}
/**
* @dev Returns the multiplication of two unsigned integers, reverting on
* overflow.
*
* Counterpart to Solidity's `*` operator.
*
* Requirements:
*
* - Multiplication cannot overflow.
*/
function mul(uint256 a, uint256 b) internal pure returns (uint256) {
if (a == 0) return 0;
uint256 c = a * b;
require(c / a == b, "SafeMath: multiplication overflow");
return c;
}
/**
* @dev Returns the integer division of two unsigned integers, reverting on
* division by zero. The result is rounded towards zero.
*
* Counterpart to Solidity's `/` operator. Note: this function uses a
* `revert` opcode (which leaves remaining gas untouched) while Solidity
* uses an invalid opcode to revert (consuming all remaining gas).
*
* Requirements:
*
* - The divisor cannot be zero.
*/
function div(uint256 a, uint256 b) internal pure returns (uint256) {
require(b > 0, "SafeMath: division by zero");
return a / b;
}
/**
* @dev Returns the remainder of dividing two unsigned integers. (unsigned integer modulo),
* reverting when dividing by zero.
*
* Counterpart to Solidity's `%` operator. This function uses a `revert`
* opcode (which leaves remaining gas untouched) while Solidity uses an
* invalid opcode to revert (consuming all remaining gas).
*
* Requirements:
*
* - The divisor cannot be zero.
*/
function mod(uint256 a, uint256 b) internal pure returns (uint256) {
require(b > 0, "SafeMath: modulo by zero");
return a % b;
}
/**
* @dev Returns the subtraction of two unsigned integers, reverting with custom message on
* overflow (when the result is negative).
*
* CAUTION: This function is deprecated because it requires allocating memory for the error
* message unnecessarily. For custom revert reasons use {trySub}.
*
* Counterpart to Solidity's `-` operator.
*
* Requirements:
*
* - Subtraction cannot overflow.
*/
function sub(uint256 a, uint256 b, string memory errorMessage) internal pure returns (uint256) {
require(b <= a, errorMessage);
return a - b;
}
/**
* @dev Returns the integer division of two unsigned integers, reverting with custom message on
* division by zero. The result is rounded towards zero.
*
* CAUTION: This function is deprecated because it requires allocating memory for the error
* message unnecessarily. For custom revert reasons use {tryDiv}.
*
* Counterpart to Solidity's `/` operator. Note: this function uses a
* `revert` opcode (which leaves remaining gas untouched) while Solidity
* uses an invalid opcode to revert (consuming all remaining gas).
*
* Requirements:
*
* - The divisor cannot be zero.
*/
function div(uint256 a, uint256 b, string memory errorMessage) internal pure returns (uint256) {
require(b > 0, errorMessage);
return a / b;
}
/**
* @dev Returns the remainder of dividing two unsigned integers. (unsigned integer modulo),
* reverting with custom message when dividing by zero.
*
* CAUTION: This function is deprecated because it requires allocating memory for the error
* message unnecessarily. For custom revert reasons use {tryMod}.
*
* Counterpart to Solidity's `%` operator. This function uses a `revert`
* opcode (which leaves remaining gas untouched) while Solidity uses an
* invalid opcode to revert (consuming all remaining gas).
*
* Requirements:
*
* - The divisor cannot be zero.
*/
function mod(uint256 a, uint256 b, string memory errorMessage) internal pure returns (uint256) {
require(b > 0, errorMessage);
return a % b;
}
}
Настройки компилятораПоказать
{
"optimizer": {
"runs": 200,
"enabled": false
}
}Исходный код и метаданные: Sourcify.
Исходный код принадлежит авторам контракта.